blob: 6e599932152b465f3218c5cf5b606a7ba64c865f [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"
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080019
20/* Internal data structures */
21
22struct mctp_bus {
23 mctp_eid_t eid;
24 struct mctp_binding *binding;
Jeremy Kerr1cd31182019-02-27 18:01:00 +080025 bool tx_enabled;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080026
Jeremy Kerrcc2458d2019-03-01 08:23:33 +080027 struct mctp_pktbuf *tx_queue_head;
28 struct mctp_pktbuf *tx_queue_tail;
29
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080030 /* todo: routing */
31};
32
Jeremy Kerr24db71f2019-02-07 21:37:35 +080033struct mctp_msg_ctx {
34 uint8_t src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080035 uint8_t dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080036 uint8_t tag;
37 uint8_t last_seq;
38 void *buf;
39 size_t buf_size;
40 size_t buf_alloc_size;
41};
42
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080043struct mctp {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080044 int n_busses;
45 struct mctp_bus *busses;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080046
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080047 /* Message RX callback */
48 mctp_rx_fn message_rx;
49 void *message_rx_data;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080050
51 /* Message reassembly.
52 * @todo: flexible context count
53 */
54 struct mctp_msg_ctx msg_ctxs[16];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080055
56 enum {
57 ROUTE_ENDPOINT,
58 ROUTE_BRIDGE,
59 } route_policy;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080060};
61
62#ifndef BUILD_ASSERT
63#define BUILD_ASSERT(x) \
64 do { (void)sizeof(char[0-(!(x))]); } while (0)
65#endif
66
Jeremy Kerr24db71f2019-02-07 21:37:35 +080067#ifndef ARRAY_SIZE
68#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
69#endif
70
Andrew Jefferyb93b6112020-06-05 14:13:44 +093071static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
72 mctp_eid_t dest, void *msg, size_t msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080073
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080074struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080075{
76 struct mctp_pktbuf *buf;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080077 size_t size;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080078
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080079 size = binding->pkt_size + binding->pkt_pad;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080080
81 /* todo: pools */
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080082 buf = __mctp_alloc(sizeof(*buf) + size);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080083
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080084 buf->size = size;
85 buf->start = binding->pkt_pad;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080086 buf->end = buf->start + len;
87 buf->mctp_hdr_off = buf->start;
Jeremy Kerrdd109f12019-04-04 11:46:49 +080088 buf->next = NULL;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080089
90 return buf;
91}
92
93void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
94{
95 __mctp_free(pkt);
96}
97
98struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
99{
100 return (void *)pkt->data + pkt->mctp_hdr_off;
101}
102
103void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
104{
105 return (void *)pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
106}
107
108uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt)
109{
110 return pkt->end - pkt->start;
111}
112
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800113void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800114{
115 assert(size <= pkt->start);
116 pkt->start -= size;
117 return pkt->data + pkt->start;
118}
119
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800120void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800121{
122 void *buf;
123
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800124 assert(size < (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800125 buf = pkt->data + pkt->end;
126 pkt->end += size;
127 return buf;
128}
129
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800130int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800131{
132 void *p;
133
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800134 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800135 return -1;
136
137 p = pkt->data + pkt->end;
138
139 pkt->end += len;
140 memcpy(p, data, len);
141
142 return 0;
143}
144
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800145/* Message reassembly */
146static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800147 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800148{
149 unsigned int i;
150
151 /* @todo: better lookup, if we add support for more outstanding
152 * message contexts */
153 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
154 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800155 if (ctx->src == src && ctx->dest == dest && ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800156 return ctx;
157 }
158
159 return NULL;
160}
161
162static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800163 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800164{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800165 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800166 unsigned int i;
167
168 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
169 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
170 if (!tmp->src) {
171 ctx = tmp;
172 break;
173 }
174 }
175
176 if (!ctx)
177 return NULL;
178
179 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800180 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800181 ctx->tag = tag;
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800182 ctx->buf_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800183
184 return ctx;
185}
186
187static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx)
188{
189 ctx->src = 0;
190}
191
192static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
193{
194 ctx->buf_size = 0;
195}
196
197static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
198 struct mctp_pktbuf *pkt)
199{
200 size_t len;
201
202 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
203
204 if (ctx->buf_size + len > ctx->buf_alloc_size) {
205 size_t new_alloc_size;
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030206 void *lbuf;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800207
208 /* @todo: finer-grained allocation, size limits */
209 if (!ctx->buf_alloc_size) {
210 new_alloc_size = 4096;
211 } else {
212 new_alloc_size = ctx->buf_alloc_size * 2;
213 }
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030214
215 lbuf = __mctp_realloc(ctx->buf, new_alloc_size);
216 if (lbuf) {
217 ctx->buf = lbuf;
218 ctx->buf_alloc_size = new_alloc_size;
219 } else {
220 __mctp_free(ctx->buf);
221 return -1;
222 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800223 }
224
225 memcpy(ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
226 ctx->buf_size += len;
227
228 return 0;
229}
230
231/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800232struct mctp *mctp_init(void)
233{
234 struct mctp *mctp;
235
236 mctp = __mctp_alloc(sizeof(*mctp));
237 memset(mctp, 0, sizeof(*mctp));
238
239 return mctp;
240}
241
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030242void mctp_destroy(struct mctp *mctp)
243{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930244 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030245
246 /* Cleanup message assembly contexts */
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930247 BUILD_ASSERT(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030248 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
249 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
250 if (tmp->buf)
251 __mctp_free(tmp->buf);
252 }
253
254 __mctp_free(mctp->busses);
255 __mctp_free(mctp);
256}
257
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800258int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
259{
260 mctp->message_rx = fn;
261 mctp->message_rx_data = data;
262 return 0;
263}
264
265static struct mctp_bus *find_bus_for_eid(struct mctp *mctp,
266 mctp_eid_t dest __attribute__((unused)))
267{
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800268 /* for now, just use the first bus. For full routing support,
269 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800270 return &mctp->busses[0];
271}
272
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800273int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800274 struct mctp_binding *binding,
275 mctp_eid_t eid)
276{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930277 int rc = 0;
278
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800279 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800280 assert(mctp->n_busses == 0);
281 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930282
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800283 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930284 if (!mctp->busses)
285 return -ENOMEM;
286
James Feist62d72362019-12-13 13:43:32 -0800287 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800288 mctp->busses[0].binding = binding;
289 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800290 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800291 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800292 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800293
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930294 if (binding->start) {
295 rc = binding->start(binding);
296 if (rc < 0) {
297 mctp_prerr("Failed to start binding: %d", rc);
298 __mctp_free(mctp->busses);
299 mctp->busses = NULL;
300 }
301 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800302
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930303 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800304}
305
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800306int mctp_bridge_busses(struct mctp *mctp,
307 struct mctp_binding *b1, struct mctp_binding *b2)
308{
309 assert(mctp->n_busses == 0);
310 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
James Feist62d72362019-12-13 13:43:32 -0800311 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800312 mctp->n_busses = 2;
313 mctp->busses[0].binding = b1;
314 b1->bus = &mctp->busses[0];
315 b1->mctp = mctp;
316 mctp->busses[1].binding = b2;
317 b2->bus = &mctp->busses[1];
318 b2->mctp = mctp;
319
320 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800321
322 if (b1->start)
323 b1->start(b1);
324
325 if (b2->start)
326 b2->start(b2);
327
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800328 return 0;
329}
330
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100331static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800332{
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100333 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
334 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
335}
336
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930337static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
338 void *buffer, size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100339{
340 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
341
342 /*
343 * Control message is received. If a transport control message handler
344 * is provided, it will called. If there is no dedicated handler, this
345 * function returns false and data can be handled by the generic
346 * message handler. The transport control message handler will be
347 * provided with messages in the command range 0xF0 - 0xFF.
348 */
349 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
350 if (bus->binding->control_rx != NULL) {
351 /* MCTP bus binding handler */
352 bus->binding->control_rx(src,
353 bus->binding->control_rx_data,
354 buffer, length);
355 return true;
356 }
357 }
358
359 /*
360 * Command was not handled, due to lack of specific callback.
361 * It will be passed to regular message_rx handler.
362 */
363 return false;
364}
365
366static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
367{
368 return dest == bus->eid || dest == MCTP_EID_NULL ||
369 dest == MCTP_EID_BROADCAST;
370}
371
372static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
373{
374 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
375 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
376}
377
378/*
379 * Receive the complete MCTP message and route it.
380 * Asserts:
381 * 'buf' is not NULL.
382 */
383static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
384 mctp_eid_t dest, void *buf, size_t len)
385{
386 assert(buf != NULL);
387
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800388 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100389 mctp_rx_dest_is_local(bus, dest)) {
390 /* Handle MCTP Control Messages: */
391 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
392 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
393
394 /*
395 * Identify if this is a control request message.
396 * See DSP0236 v1.3.0 sec. 11.5.
397 */
398 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
399 bool handled;
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930400 handled = mctp_ctrl_handle_msg(bus, src, buf,
401 len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100402 if (handled)
403 return;
404 }
405 }
406 if (mctp->message_rx)
407 mctp->message_rx(src, mctp->message_rx_data, buf, len);
408 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800409
410 if (mctp->route_policy == ROUTE_BRIDGE) {
411 int i;
412
413 for (i = 0; i < mctp->n_busses; i++) {
414 struct mctp_bus *dest_bus = &mctp->busses[i];
415 if (dest_bus == bus)
416 continue;
417
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930418 mctp_message_tx_on_bus(dest_bus, src, dest, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800419 }
420
421 }
422}
423
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800424void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800425{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800426 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800427 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800428 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800429 struct mctp_msg_ctx *ctx;
430 struct mctp_hdr *hdr;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800431 size_t len;
432 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800433 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800434
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800435 assert(bus);
436
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800437 hdr = mctp_pktbuf_hdr(pkt);
438
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800439 /* small optimisation: don't bother reassembly if we're going to
440 * drop the packet in mctp_rx anyway */
441 if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800442 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800443
444 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
445 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
446 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
447
448 switch (flags) {
449 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
450 /* single-packet message - send straight up to rx function,
451 * no need to create a message context */
452 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
453 p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr),
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800454 mctp_rx(mctp, bus, hdr->src, hdr->dest, p, len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800455 break;
456
457 case MCTP_HDR_FLAG_SOM:
458 /* start of a new message - start the new context for
459 * future message reception. If an existing context is
460 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800461 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800462 if (ctx) {
463 mctp_msg_ctx_reset(ctx);
464 } else {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800465 ctx = mctp_msg_ctx_create(mctp,
466 hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800467 }
468
469 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
470 if (rc) {
471 mctp_msg_ctx_drop(ctx);
472 } else {
473 ctx->last_seq = seq;
474 }
475
476 break;
477
478 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800479 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800480 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800481 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800482
Ed Tanousc2def9f2019-02-21 08:33:08 -0800483 exp_seq = (ctx->last_seq + 1) % 4;
484
485 if (exp_seq != seq) {
486 mctp_prdebug(
487 "Sequence number %d does not match expected %d",
488 seq, exp_seq);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800489 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800490 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800491 }
492
493 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800494 if (!rc)
495 mctp_rx(mctp, bus, ctx->src, ctx->dest,
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800496 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800497
498 mctp_msg_ctx_drop(ctx);
499 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800500
501 case 0:
502 /* Neither SOM nor EOM */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800503 ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800504 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800505 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800506
507 exp_seq = (ctx->last_seq + 1) % 4;
508 if (exp_seq != seq) {
509 mctp_prdebug(
510 "Sequence number %d does not match expected %d",
511 seq, exp_seq);
512 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800513 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800514 }
515
516 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
517 if (rc) {
518 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800519 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800520 }
521 ctx->last_seq = seq;
522
523 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800524 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800525out:
526 mctp_pktbuf_free(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800527}
528
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800529static int mctp_packet_tx(struct mctp_bus *bus,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800530 struct mctp_pktbuf *pkt)
531{
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800532 if (!bus->tx_enabled)
533 return -1;
534
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800535 return bus->binding->tx(bus->binding, pkt);
536}
537
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800538static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800539{
540 struct mctp_pktbuf *pkt;
541
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800542 while ((pkt = bus->tx_queue_head)) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800543 int rc;
544
545 rc = mctp_packet_tx(bus, pkt);
546 if (rc)
547 break;
548
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800549 bus->tx_queue_head = pkt->next;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800550 mctp_pktbuf_free(pkt);
551 }
552
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800553 if (!bus->tx_queue_head)
554 bus->tx_queue_tail = NULL;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800555
556}
557
558void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
559{
560 struct mctp_bus *bus = binding->bus;
561 bus->tx_enabled = enable;
562 if (enable)
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800563 mctp_send_tx_queue(bus);
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800564}
565
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930566static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
567 mctp_eid_t dest, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800568{
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800569 size_t max_payload_len, payload_len, p;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800570 struct mctp_pktbuf *pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800571 struct mctp_hdr *hdr;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800572 int i;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800573
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800574 max_payload_len = bus->binding->pkt_size - sizeof(*hdr);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800575
Andrew Jeffery298865f2020-02-06 11:51:29 +1030576 mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
577 __func__, msg_len, src, dest);
578
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800579 /* queue up packets, each of max MCTP_MTU size */
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800580 for (p = 0, i = 0; p < msg_len; i++) {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800581 payload_len = msg_len - p;
582 if (payload_len > max_payload_len)
583 payload_len = max_payload_len;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800584
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800585 pkt = mctp_pktbuf_alloc(bus->binding,
586 payload_len + sizeof(*hdr));
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800587 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800588
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800589 /* todo: tags */
590 hdr->ver = bus->binding->version & 0xf;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800591 hdr->dest = dest;
592 hdr->src = src;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800593 hdr->flags_seq_tag = MCTP_HDR_FLAG_TO |
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800594 (0 << MCTP_HDR_TAG_SHIFT);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800595
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800596 if (i == 0)
597 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800598 if (p + payload_len >= msg_len)
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800599 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
600 hdr->flags_seq_tag |=
601 (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT;
602
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800603 memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800604
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800605 /* add to tx queue */
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800606 if (bus->tx_queue_tail)
607 bus->tx_queue_tail->next = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800608 else
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800609 bus->tx_queue_head = pkt;
610 bus->tx_queue_tail = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800611
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800612 p += payload_len;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800613 }
614
Andrew Jeffery298865f2020-02-06 11:51:29 +1030615 mctp_prdebug("%s: Enqueued %d packets", __func__, i);
616
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800617 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800618
619 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800620}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800621
622int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
623 void *msg, size_t msg_len)
624{
625 struct mctp_bus *bus;
626
627 bus = find_bus_for_eid(mctp, eid);
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930628 return mctp_message_tx_on_bus(bus, bus->eid, eid, msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800629}