1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * mctp-usblib.c - MCTP-over-USB (DMTF DSP0283) transport helper library 4 * 5 * DSP0283 is available at: 6 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.1.0.pdf 7 * 8 * Copyright (C) 2024-2026 Code Construct Pty Ltd 9 */ 10 11 #include <linux/module.h> 12 #include <linux/netdevice.h> 13 #include <linux/skbuff.h> 14 #include <linux/usb/ch9.h> 15 #include <linux/usb/mctp-usb.h> 16 #include <net/mctp.h> 17 18 int mctp_usblib_rx_init(struct mctp_usblib_rx *rx, u16 ep_pktlen, bool span) 19 { 20 if (!ep_pktlen) 21 return -EINVAL; 22 23 if (ep_pktlen & ~USB_ENDPOINT_MAXP_MASK) 24 return -EINVAL; 25 26 memset(rx, 0, sizeof(*rx)); 27 rx->span = span; 28 rx->ep_pktlen = ep_pktlen; 29 30 return 0; 31 } 32 EXPORT_SYMBOL_GPL(mctp_usblib_rx_init); 33 34 void mctp_usblib_rx_fini(struct mctp_usblib_rx *rx) 35 { 36 kfree_skb(rx->skb); 37 } 38 EXPORT_SYMBOL_GPL(mctp_usblib_rx_fini); 39 40 /* 41 * Prepare a transfer buffer for future completion; *bufp and *lenp will 42 * be populated on success. 43 */ 44 int mctp_usblib_rx_prepare(struct net_device *netdev, 45 struct mctp_usblib_rx *rx, 46 void **bufp, size_t *lenp, gfp_t gfp) 47 { 48 struct sk_buff *skb = rx->skb; 49 unsigned int len = 0; 50 51 if (skb && skb->len >= MCTP_USB_1_1_PKTLEN_MAX) { 52 /* something must have gone terribly wrong. clear and restart */ 53 mctp_usblib_rx_cancel(rx); 54 skb = NULL; 55 } 56 57 len = rx->span ? roundup(MCTP_USB_1_1_PKTLEN_MAX, rx->ep_pktlen) 58 : MCTP_USB_1_0_XFER_SIZE; 59 60 if (!skb) { 61 skb = __netdev_alloc_skb(netdev, len, gfp); 62 if (!skb) 63 return -ENOMEM; 64 65 } else if (skb->cloned || skb_tailroom(skb) < rx->ep_pktlen) { 66 /* We always need to realloc if ->cloned, as we cannot 67 * resubmit the (now-shared) skb buffer for possible DMA. 68 * 69 * Otherwise (if we have an un-cloned SKB): just ensure we 70 * have sufficient space to prevent babble. Since we allocated 71 * for max size in the last prepare (and have not consumed any 72 * of that space for a prior MCTP packet, because !cloned), we 73 * have sufficient data to finish the current MCTP packet. 74 */ 75 struct sk_buff *skb2; 76 77 skb2 = skb_copy_expand(skb, 0, len, gfp); 78 if (!skb2) 79 return -ENOMEM; 80 dev_kfree_skb_any(skb); 81 skb = skb2; 82 } 83 84 rx->skb = skb; 85 86 /* Spanning mode allows ZLPs, so we don't require exactly one 87 * transfer packet. If we have extra tailroom, may as well use it, 88 * and we have ensured that the tailroom >= ep_pktlen. 89 */ 90 if (rx->span) 91 len = rounddown(skb_tailroom(skb), rx->ep_pktlen); 92 93 *bufp = skb_tail_pointer(skb); 94 *lenp = len; 95 96 return 0; 97 } 98 EXPORT_SYMBOL_GPL(mctp_usblib_rx_prepare); 99 100 static void mctp_usblib_rx(struct net_device *netdev, struct sk_buff *skb) 101 { 102 struct pcpu_dstats *dstats = this_cpu_ptr(netdev->dstats); 103 struct mctp_skb_cb *cb; 104 unsigned long flags; 105 106 skb_reset_mac_header(skb); 107 skb_pull(skb, sizeof(struct mctp_usb_hdr)); 108 109 /* we're called from an URB completion handler, and cannot assume local 110 * irqs are always disabled 111 */ 112 flags = u64_stats_update_begin_irqsave(&dstats->syncp); 113 u64_stats_inc(&dstats->rx_packets); 114 u64_stats_add(&dstats->rx_bytes, skb->len); 115 u64_stats_update_end_irqrestore(&dstats->syncp, flags); 116 117 skb->protocol = htons(ETH_P_MCTP); 118 skb_reset_network_header(skb); 119 cb = __mctp_cb(skb); 120 cb->halen = 0; 121 netif_rx(skb); 122 } 123 124 static void mctp_usblib_rx_stats_single_drop(struct net_device *dev) 125 { 126 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); 127 unsigned long flags; 128 129 flags = u64_stats_update_begin_irqsave(&dstats->syncp); 130 u64_stats_inc(&dstats->rx_drops); 131 u64_stats_update_end_irqrestore(&dstats->syncp, flags); 132 } 133 134 /* 135 * Receive a USB completion of @len bytes of incoming data. We will then split 136 * this into packets and netif_rx() each. Intended to be called in atomic 137 * contexts - ie., URB completion. 138 * 139 * Assumes @netdev uses dstats. 140 */ 141 int mctp_usblib_rx_complete(struct net_device *netdev, 142 struct mctp_usblib_rx *rx, size_t len) 143 { 144 struct sk_buff *skb = rx->skb; 145 int rc = 0; 146 147 __skb_put(skb, len); 148 149 for (;;) { 150 struct mctp_usb_hdr *hdr; 151 struct sk_buff *skb2; 152 /* length of MCTP packet, including USB header */ 153 u16 pkt_len; 154 155 /* no header yet, resubmit for the rest of the packet */ 156 if (skb->len < sizeof(*hdr)) { 157 if (!rx->span) { 158 netdev_dbg(netdev, 159 "rx: tiny xfer (%d) in non-span mode", 160 skb->len); 161 rc = -ENOMSG; 162 goto err_reset; 163 } 164 break; 165 } 166 167 hdr = (struct mctp_usb_hdr *)skb->data; 168 169 if (be16_to_cpu(hdr->id) != MCTP_USB_DMTF_ID) { 170 /* By resetting here, will start the next IN transfer 171 * at the beginning of the new skb. This will mean 172 * we re-sync when we next see a spanned packet aligned 173 * with the start of a transfer. 174 * 175 * In non-spanning mode, this just means we'll drop 176 * the current transfer only 177 */ 178 netdev_dbg(netdev, "rx: invalid id %04x\n", 179 be16_to_cpu(hdr->id)); 180 rc = -EPROTO; 181 goto err_reset; 182 } 183 184 pkt_len = be16_to_cpu(hdr->len); 185 /* v1.1, with span enabled, has a 13-bit length */ 186 pkt_len &= rx->span ? 187 MCTP_USB_1_1_PKTLEN_MAX : MCTP_USB_1_0_PKTLEN_MAX; 188 if (pkt_len < sizeof(*hdr) + sizeof(struct mctp_hdr)) { 189 netdev_dbg(netdev, "rx: invalid len %d\n", pkt_len); 190 rc = -EPROTO; 191 goto err_reset; 192 } 193 194 /* span continues to the next transfer, resubmit */ 195 if (pkt_len > skb->len) { 196 if (!rx->span) { 197 netdev_dbg(netdev, 198 "rx: short xfer (%d vs %d) in non-span mode", 199 pkt_len, skb->len); 200 rc = -EPROTO; 201 goto err_reset; 202 } 203 break; 204 } 205 206 /* we have (exactly) a complete packet, RX it directly */ 207 if (pkt_len == skb->len) { 208 mctp_usblib_rx(netdev, skb); 209 rx->skb = NULL; 210 break; 211 } 212 213 /* more packets follow - RX a clone so that we can continue 214 * processing the current SKB, which may be the start of a 215 * span. 216 */ 217 skb2 = skb_clone(skb, GFP_ATOMIC); 218 if (skb2) { 219 skb_trim(skb2, pkt_len); 220 mctp_usblib_rx(netdev, skb2); 221 } else { 222 mctp_usblib_rx_stats_single_drop(netdev); 223 } 224 skb_pull(skb, pkt_len); 225 } 226 227 return 0; 228 229 err_reset: 230 dev_kfree_skb_any(rx->skb); 231 rx->skb = NULL; 232 return rc; 233 } 234 EXPORT_SYMBOL_GPL(mctp_usblib_rx_complete); 235 236 /* 237 * Cancel a rx context; subsequent prepare/complete calls will not be a 238 * continuation of any data already received. 239 */ 240 void mctp_usblib_rx_cancel(struct mctp_usblib_rx *rx) 241 { 242 dev_kfree_skb_any(rx->skb); 243 rx->skb = NULL; 244 } 245 EXPORT_SYMBOL_GPL(mctp_usblib_rx_cancel); 246 247 /* transmit context: encapsulates one transfer */ 248 struct mctp_usblib_tx_ctx { 249 struct mctp_usblib_tx *tx; 250 struct sk_buff_head skbs; 251 unsigned int buf_len, len; 252 enum mctp_usblib_tx_buf_type { 253 TX_SINGLE, 254 TX_FLAT, 255 } buf_type; 256 u8 buf[] ____cacheline_aligned; 257 }; 258 259 void mctp_usblib_tx_init(struct mctp_usblib_tx *tx, 260 const struct mctp_usblib_tx_ops *ops, 261 void *priv, bool span) 262 { 263 memset(tx, 0, sizeof(*tx)); 264 tx->ops = *ops; 265 tx->priv = priv; 266 tx->span = span; 267 spin_lock_init(&tx->lock); 268 } 269 EXPORT_SYMBOL_GPL(mctp_usblib_tx_init); 270 271 static int mctp_usblib_tx_avail(struct mctp_usblib_tx_ctx *ctx) 272 { 273 return ctx->buf_type == TX_SINGLE ? 0 : ctx->buf_len - ctx->len; 274 } 275 276 static bool mctp_usblib_tx_should_send(struct mctp_usblib_tx_ctx *ctx) 277 { 278 /* Use the baseline length (ie, BTU) as an approximate 279 * "reasonably-sized" packet we could expect. If there is 280 * insufficient capacity for that, then send. 281 */ 282 const size_t pkt_len = MCTP_USB_BTU + sizeof(struct mctp_usb_hdr); 283 284 return mctp_usblib_tx_avail(ctx) < pkt_len; 285 } 286 287 /* 288 * Returns zero on success, non-zero on failure - indicating that the new skb 289 * could not be appended. So, errors reported here to the TX path will result 290 * in the TX being transmitted. 291 */ 292 static int mctp_usblib_tx_append(struct mctp_usblib_tx_ctx *ctx, 293 struct sk_buff *skb) 294 { 295 if (ctx->buf_type == TX_SINGLE) 296 return -EINVAL; 297 298 if (mctp_usblib_tx_avail(ctx) < skb->len) 299 return -ENOBUFS; 300 301 __skb_queue_tail(&ctx->skbs, skb); 302 303 ctx->len += skb->len; 304 305 return 0; 306 } 307 308 static int mctp_usblib_tx_send(struct mctp_usblib_tx_ctx *ctx) 309 { 310 void *buf; 311 312 /* If we have a qlen of 1, we only ended up packing a single skb, 313 * despite allocating for multiple. Skip the copy and send directly 314 * from the skb data. 315 */ 316 if (ctx->buf_type == TX_SINGLE || ctx->skbs.qlen == 1) { 317 buf = ctx->skbs.next->data; 318 319 } else if (ctx->buf_type == TX_FLAT) { 320 struct sk_buff *skb; 321 size_t pos = 0; 322 323 skb_queue_walk(&ctx->skbs, skb) { 324 skb_copy_bits(skb, 0, ctx->buf + pos, skb->len); 325 pos += skb->len; 326 } 327 328 buf = ctx->buf; 329 } else { 330 return -EINVAL; 331 } 332 333 return ctx->tx->ops.send(ctx, buf, ctx->len); 334 } 335 336 static void mctp_usblib_tx_ctx_free(struct mctp_usblib_tx_ctx *ctx, 337 enum skb_drop_reason reason) 338 { 339 struct sk_buff *skb; 340 341 if (!ctx) 342 return; 343 344 while ((skb = __skb_dequeue(&ctx->skbs)) != NULL) 345 dev_kfree_skb_any_reason(skb, reason); 346 kfree(ctx); 347 } 348 349 void *mctp_usblib_tx_ctx_priv(struct mctp_usblib_tx_ctx *tx_ctx) 350 { 351 return tx_ctx->tx->priv; 352 } 353 EXPORT_SYMBOL_GPL(mctp_usblib_tx_ctx_priv); 354 355 /* caller must ensure the tx & completion path is quiesced */ 356 void mctp_usblib_tx_fini(struct mctp_usblib_tx *tx) 357 { 358 mctp_usblib_tx_ctx_free(tx->cur_ctx, SKB_DROP_REASON_NOT_SPECIFIED); 359 } 360 EXPORT_SYMBOL_GPL(mctp_usblib_tx_fini); 361 362 /* Max size of a spanned TX. Since we allocate a separate span buffer, limit 363 * the tx-time allocations to 4k. Larger packets will be sent as single 364 * transfers. 365 */ 366 static const unsigned int TX_SPAN_MAX = 4096 - sizeof(struct mctp_usblib_tx_ctx); 367 368 static struct mctp_usblib_tx_ctx * 369 mctp_usblib_tx_ctx_create(struct mctp_usblib_tx *tx, struct sk_buff *skb, 370 bool single) 371 { 372 enum mctp_usblib_tx_buf_type type; 373 struct mctp_usblib_tx_ctx *ctx; 374 size_t sz = 0; 375 376 if (single || skb->len > TX_SPAN_MAX) { 377 type = TX_SINGLE; 378 } else { 379 type = TX_FLAT; 380 sz = tx->span ? TX_SPAN_MAX : MCTP_USB_1_0_XFER_SIZE; 381 } 382 383 ctx = kzalloc_flex(*ctx, buf, sz, GFP_ATOMIC); 384 if (!ctx) 385 return NULL; 386 387 ctx->tx = tx; 388 ctx->buf_type = type; 389 ctx->buf_len = sz; 390 ctx->len = skb->len; 391 skb_queue_head_init(&ctx->skbs); 392 __skb_queue_tail(&ctx->skbs, skb); 393 394 return ctx; 395 } 396 397 static void mctp_usblib_tx_stats_update(struct mctp_usblib_tx_ctx *ctx, 398 struct net_device *dev, 399 bool ok) 400 { 401 struct pcpu_dstats *dstats = get_cpu_ptr(dev->dstats); 402 unsigned long flags; 403 404 flags = u64_stats_update_begin_irqsave(&dstats->syncp); 405 if (ok) { 406 /* Only include the network-layer data in tx stats; we know 407 * that there is a 4-byte header pushed to all skbs in 408 * tx_skb_prepare() 409 */ 410 u64 n = ctx->skbs.qlen; 411 s64 len = ctx->len - (n * sizeof(struct mctp_usb_hdr)); 412 413 u64_stats_add(&dstats->tx_packets, n); 414 u64_stats_add(&dstats->tx_bytes, len); 415 } else { 416 u64_stats_add(&dstats->tx_drops, ctx->skbs.qlen); 417 } 418 u64_stats_update_end_irqrestore(&dstats->syncp, flags); 419 put_cpu_ptr(dev->dstats); 420 } 421 422 static void mctp_usblib_tx_stats_single_drop(struct net_device *dev) 423 { 424 struct pcpu_dstats *dstats = get_cpu_ptr(dev->dstats); 425 unsigned long flags; 426 427 flags = u64_stats_update_begin_irqsave(&dstats->syncp); 428 u64_stats_inc(&dstats->tx_drops); 429 u64_stats_update_end_irqrestore(&dstats->syncp, flags); 430 put_cpu_ptr(dev->dstats); 431 } 432 433 /* 434 * Completion for the ->send() op. This will update netdev stats and 435 * free the tx context. 436 * 437 * Likely called from (atomic) URB completion context. 438 */ 439 void mctp_usblib_tx_send_complete(struct mctp_usblib_tx_ctx *tx_ctx, 440 struct net_device *dev, bool ok) 441 { 442 enum skb_drop_reason reason = 443 ok ? SKB_CONSUMED : SKB_DROP_REASON_NOT_SPECIFIED; 444 445 mctp_usblib_tx_stats_update(tx_ctx, dev, ok); 446 mctp_usblib_tx_ctx_free(tx_ctx, reason); 447 } 448 EXPORT_SYMBOL_GPL(mctp_usblib_tx_send_complete); 449 450 /* Prepare a skb for push() 451 * 452 * On error, populates @reason. 453 */ 454 static int mctp_usblib_tx_skb_prepare(struct sk_buff *skb, bool span, 455 enum skb_drop_reason *reason) 456 { 457 unsigned long plen, max_len; 458 struct mctp_usb_hdr *hdr; 459 int rc; 460 461 max_len = span ? MCTP_USB_1_1_PKTLEN_MAX : MCTP_USB_1_0_PKTLEN_MAX; 462 463 plen = skb->len; 464 if (plen + sizeof(*hdr) > max_len) { 465 *reason = SKB_DROP_REASON_PKT_TOO_BIG; 466 return -EMSGSIZE; 467 } 468 469 rc = skb_cow_head(skb, sizeof(*hdr)); 470 if (rc) { 471 *reason = SKB_DROP_REASON_NOMEM; 472 return rc; 473 } 474 475 hdr = skb_push(skb, sizeof(*hdr)); 476 if (!hdr) { 477 *reason = SKB_DROP_REASON_NOMEM; 478 return -ENOMEM; 479 } 480 481 hdr->id = cpu_to_be16(MCTP_USB_DMTF_ID); 482 hdr->len = cpu_to_be16(plen + sizeof(*hdr)); 483 484 return 0; 485 } 486 487 /* 488 * Push a new skb to the transfer. May result in zero or more calls to 489 * ops->send(). 490 * 491 * Takes ownership of @skb, including on error. 492 */ 493 int mctp_usblib_tx_push(struct net_device *dev, 494 struct mctp_usblib_tx *tx, 495 struct sk_buff *skb, bool more) 496 { 497 struct mctp_usblib_tx_ctx *ctx, *send_ctx = NULL; 498 enum skb_drop_reason reason; 499 const int max_tries = 3; 500 unsigned long flags; 501 int try = 1, rc; 502 503 rc = mctp_usblib_tx_skb_prepare(skb, tx->span, &reason); 504 if (rc) { 505 mctp_usblib_tx_stats_single_drop(dev); 506 kfree_skb_reason(skb, reason); 507 /* we may still need to proceed, in case an existing ctx 508 * is now sendable (ie.: !more). 509 */ 510 skb = NULL; 511 } 512 513 reason = SKB_DROP_REASON_NOT_SPECIFIED; 514 retry: 515 /* Try and queue to the current context. We exit this critical section 516 * with a few bits of state: 517 * - send_ctx: indicating a prior context that needs to be sent 518 * - skb: indicating that a skb still needs to be queued/sent 519 */ 520 spin_lock_irqsave(&tx->lock, flags); 521 ctx = tx->cur_ctx; 522 if (ctx) { 523 if (skb) { 524 rc = mctp_usblib_tx_append(ctx, skb); 525 if (rc) { 526 /* can't append to the pending tx - detach for 527 * sending, and we'll create a new tx below. 528 */ 529 swap(tx->cur_ctx, send_ctx); 530 } else { 531 /* we have queued */ 532 skb = NULL; 533 if (!more || mctp_usblib_tx_should_send(ctx)) 534 swap(tx->cur_ctx, send_ctx); 535 } 536 } else if (!more) { 537 swap(tx->cur_ctx, send_ctx); 538 } 539 } 540 spin_unlock_irqrestore(&tx->lock, flags); 541 542 if (send_ctx) { 543 rc = mctp_usblib_tx_send(send_ctx); 544 if (rc) { 545 mctp_usblib_tx_stats_update(send_ctx, dev, false); 546 mctp_usblib_tx_ctx_free(send_ctx, reason); 547 } 548 send_ctx = NULL; 549 } 550 551 /* we have either queued, or the prepare failed; nothing more to do */ 552 if (!skb) 553 return 0; 554 555 ctx = mctp_usblib_tx_ctx_create(tx, skb, !more); 556 if (!ctx) { 557 netdev_dbg(dev, "TX context create failed\n"); 558 mctp_usblib_tx_stats_single_drop(dev); 559 kfree_skb(skb); 560 return -ENOMEM; 561 } 562 563 /* if we're ready to send now, no need to enqueue */ 564 if (!more || mctp_usblib_tx_should_send(ctx)) { 565 rc = mctp_usblib_tx_send(ctx); 566 if (rc) { 567 mctp_usblib_tx_stats_update(ctx, dev, false); 568 mctp_usblib_tx_ctx_free(ctx, reason); 569 } 570 return 0; 571 } 572 573 spin_lock_irqsave(&tx->lock, flags); 574 if (!tx->cur_ctx) { 575 tx->cur_ctx = ctx; 576 ctx = NULL; 577 } 578 spin_unlock_irqrestore(&tx->lock, flags); 579 580 /* we may have lost the race with a concurrent tx; shouldn't happen, as 581 * ndo_start_xmit should be serialised over one queue, but try again 582 * from the top, as we may be able to queue the skb to that context. 583 */ 584 if (ctx) { 585 /* unlink the new (sole) skb, we don't want it freed with ctx */ 586 __skb_queue_head_init(&ctx->skbs); 587 mctp_usblib_tx_ctx_free(ctx, reason); 588 if (++try > max_tries) { 589 kfree_skb(skb); 590 mctp_usblib_tx_stats_single_drop(dev); 591 return -EBUSY; 592 } 593 goto retry; 594 } 595 596 return 0; 597 } 598 EXPORT_SYMBOL_GPL(mctp_usblib_tx_push); 599 600 /* Cancel a tx: any un-sent context is released. */ 601 void mctp_usblib_tx_cancel(struct mctp_usblib_tx *tx, struct net_device *dev, 602 enum skb_drop_reason reason) 603 { 604 struct mctp_usblib_tx_ctx *ctx = NULL; 605 unsigned long flags; 606 607 spin_lock_irqsave(&tx->lock, flags); 608 swap(tx->cur_ctx, ctx); 609 spin_unlock_irqrestore(&tx->lock, flags); 610 611 if (!ctx) 612 return; 613 614 mctp_usblib_tx_stats_update(ctx, dev, false); 615 mctp_usblib_tx_ctx_free(ctx, reason); 616 } 617 EXPORT_SYMBOL_GPL(mctp_usblib_tx_cancel); 618 619 MODULE_LICENSE("GPL"); 620 MODULE_AUTHOR("Jeremy Kerr <jk@codeconstruct.com.au>"); 621 MODULE_DESCRIPTION("MCTP USB transport library"); 622 623 #if IS_ENABLED(CONFIG_MCTP_TRANSPORT_USBLIB_TEST) 624 #include "mctp-usblib-test.c" 625 #endif 626