net: mctp: usb: Add support for MCTP-over-USB v1.1
Jeremy Kerr
maintainerv5 cover net: mctp: usb: Add support for MCTP-over-USB v1.1
Version 1.1.0 of DSP0283 (MCTP over USB transport binding) has been released, this patch series updates our current v1.0.1 support for the changes in v1.1.x.
The major change in v1.1 is the introduction of "packet spanning" mode, where a single MCTP packet may be split over multiple USB packets (themselves forming a single USB bulk transfer). This relaxes the requirement for USB high-speed mode, as we can now send MCTP packets contained over multiple 64-byte full-speed USB bulk transfers, and gives us an increase in the maximum MCTP packet size - we now have 13 bits of packet length (previously 8) in the transport header.
Handling packet spanning introduces some complexity in the transmit and receive paths, as we lose some constraints on where packet boundaries may correspond to USB transfer boundaries, and may need to retain state across separate transfers. To contain this complexity, we introduce a new library for the transfer packing- and unpacking implementations, "mctp-usblib". The host driver is a consumer of this library, and a future gadget driver can use the same implementations. We can now also implement tests on the API boundary of the library.
The series implements an incremental shift to mctp-usblib, then implements packet spanning mode in the new library. We have a few changes to prepare for this, in altering a few constants and behaviours as v1.0-specific. Once packet spanning is implemented in mctp-usblib, we enable it in the host-side driver.
To: Matt Johnston <[email protected]>
To: Andrew Lunn <[email protected]>
To: "David S. Miller" <[email protected]>
To: Eric Dumazet <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Paolo Abeni <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
Changes in v5:
- cancel pending rx on ndo_stop, to be consistent with urb-completion
shutdown
- allow for non-power-of-two endpoint wMaxPacketSize
- Link to v4: https://patch.msgid.link/[email protected]
Changes in v4:
- fix missing rx cancel on IN urb EPIPE
- add retries for usb_clear_halt
- validate wMaxPacketSize before using for alignment
- don't KUNIT_ASSERT in test cleanup
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- split rx urb status handling to a separate change, handle stalls
- merge tx anchor usage into initial usblib implementation
- add skb drop reasons
- prevent unnecessary skb reallocation
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- address sashiko reviews:
- rx mac_header correction
- rx/tx stats fixes
- limit non-spanned packets (where we have no ZLP) to max size
- handle transient rx urb error status
- set skb_drop_reasons
- kunit early-exit cleanups, take rntl lock where needed
- rework tx_qmem locking and anchor handling (we don't need to unanchor
on completion)
- Link to v1: https://patch.msgid.link/[email protected]
---
Jeremy Kerr (12):
net: mctp: usb: Include version indicator in max packet size defines
net: mctp: usb: Use packet-length max for maximum packet-size check
net: mctp: usblib: Move RX transfer processing to a new mctp-usblib
net: mctp: usb: Improve IN endpoint status handling
net: mctp: usblib: Move TX transfer processing to mctp-usblib
net: mctp: usblib: Add support for multi-packet transmit
net: mctp: usb: Accommodate DSP0283 v1.1 header format
net: mctp: usblib: Implement receive-side packet spanning
net: mctp: usblib: Implement transmit-side packet spanning
net: mctp: usblib: Add initial kunit tests
net: mctp: usb: enable v1.1 packet spanning
net: mctp: usb: Allow multiple urbs in flight
drivers/net/mctp/Kconfig | 16 + drivers/net/mctp/Makefile | 1 + drivers/net/mctp/mctp-usb.c | 348 +++++++++++--------- drivers/net/mctp/mctp-usblib-test.c | 412 ++++++++++++++++++++++++ drivers/net/mctp/mctp-usblib.c | 625 ++++++++++++++++++++++++++++++++++++ include/linux/usb/mctp-usb.h | 88 ++++- 6 files changed, 1336 insertions(+), 154 deletions(-)
---
base-commit: b85966adbf5de0668a815c6e3527f87e0c387fb4
change-id: 20260604-dev-mctp-usb-1-1-6fd854ad13e8
Best regards,
Signature · 1 line
Jeremy Kerr <[email protected]>
Jeremy Kerr
maintainerPATCH v5 1/12 net: mctp: usb: Include version indicator in max packet size defines
DSP0283 v1.1.0 will introduce larger maximum packet sizes. In preparation, indicate that the current maxima are specific to v1.0.x.
--- drivers/net/mctp/mctp-usb.c | 8 ++++---- include/linux/usb/mctp-usb.h | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-)
drivers/net/mctp/mctp-usb.c +4 -4
@@ -76,7 +76,7 @@ static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb, plen = skb->len; -if (plen +sizeof (*hdr) >MCTP_USB_XFER_SIZE ) +if (plen +sizeof (*hdr) >MCTP_USB_1_0_XFER_SIZE ) goto err_drop; rc =skb_cow_head (skb,sizeof (*hdr));
@@ -128,7 +128,7 @@ static int mctp_usb_rx_queue(struct mctp_usb *mctp_usb, gfp_t gfp)struct sk_buff *skb;int rc; - skb =__netdev_alloc_skb (mctp_usb->netdev,MCTP_USB_XFER_SIZE , gfp); + skb =__netdev_alloc_skb (mctp_usb->netdev,MCTP_USB_1_0_XFER_SIZE , gfp);if (!skb) { rc = -ENOMEM ; goto err_retry;
@@ -136,7 +136,7 @@ static int mctp_usb_rx_queue(struct mctp_usb *mctp_usb, gfp_t gfp)usb_fill_bulk_urb (mctp_usb->rx_urb, mctp_usb->usbdev,usb_rcvbulkpipe (mctp_usb->usbdev, mctp_usb->ep_in), - skb->data,MCTP_USB_XFER_SIZE , + skb->data,MCTP_USB_1_0_XFER_SIZE , mctp_usb_in_complete, skb); rc =usb_submit_urb (mctp_usb->rx_urb, gfp);
@@ -301,7 +301,7 @@ static void mctp_usb_netdev_setup(struct net_device *dev) dev->mtu =MCTP_USB_MTU_MIN ; dev->min_mtu =MCTP_USB_MTU_MIN ; - dev->max_mtu =MCTP_USB_MTU_MAX ; + dev->max_mtu =MCTP_USB_1_0_MTU_MAX ; dev->hard_header_len =sizeof (struct mctp_usb_hdr ); dev->tx_queue_len =DEFAULT_TX_QUEUE_LEN ;
include/linux/usb/mctp-usb.h +3 -2
@@ -21,10 +21,11 @@ struct mctp_usb_hdr { u8 len; } __packed; -#define MCTP_USB_XFER_SIZE 512 +/* max transfer size for DSP0283 v1.0 */ +#define MCTP_USB_1_0_XFER_SIZE 512#define MCTP_USB_BTU 68#define MCTP_USB_MTU_MIN MCTP_USB_BTU -#define MCTP_USB_MTU_MAX (U8_MAX - sizeof(struct mctp_usb_hdr)) +#define MCTP_USB_1_0_MTU_MAX (U8_MAX - sizeof(struct mctp_usb_hdr))#define MCTP_USB_DMTF_ID 0x1ab4#endif /* __LINUX_USB_MCTP_USB_H */
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 2/12 net: mctp: usb: Use packet-length max for maximum packet-size check
In reply to Jeremy KerrThe max packet size is smaller than the max transfer size, as we only have a u8 length field in the transport header.
Add a define for the maximum representable length, and use that for our check. Use this for the MTU maximum calculation too.
--- drivers/net/mctp/mctp-usb.c | 2 +- include/linux/usb/mctp-usb.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/net/mctp/mctp-usb.c +1 -1
@@ -76,7 +76,7 @@ static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb, plen = skb->len; -if (plen +sizeof (*hdr) >MCTP_USB_1_0_XFER_SIZE ) +if (plen +sizeof (*hdr) >MCTP_USB_1_0_PKTLEN_MAX ) goto err_drop; rc =skb_cow_head (skb,sizeof (*hdr));
include/linux/usb/mctp-usb.h +2 -1
@@ -25,7 +25,8 @@ struct mctp_usb_hdr {#define MCTP_USB_1_0_XFER_SIZE 512#define MCTP_USB_BTU 68#define MCTP_USB_MTU_MIN MCTP_USB_BTU -#define MCTP_USB_1_0_MTU_MAX (U8_MAX - sizeof(struct mctp_usb_hdr)) +#define MCTP_USB_1_0_PKTLEN_MAX U8_MAX +#define MCTP_USB_1_0_MTU_MAX (MCTP_USB_1_0_PKTLEN_MAX - sizeof(struct mctp_usb_hdr))#define MCTP_USB_DMTF_ID 0x1ab4#endif /* __LINUX_USB_MCTP_USB_H */
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 3/12 net: mctp: usblib: Move RX transfer processing to a new mctp-usblib
In reply to Jeremy KerrThe processing of USB receive transfers is common to both sides of a MCTP over USB transport. In order to support a future gadget driver, move the current host-side driver into a new common file, mctp-usblib.
This currently handles the submit-complete-packetise process of the receive path of the USB transport. We'll add transmit handling in an upcoming change.
---
v5:
- cancel pending RX on ndo_stop; we may be waiting in a deferred re-submit, and so have no completion on kill_urb().
v3:
- split improved urb status handling to a separate change
- add module.h include
- account for rx drops due to failed skb_clone()
v2:
- drop unneeded skb_reset_mac_header
- don't count transport header in rx bytes stats
- disallow >512 bytes on RX URBs; the non-spanning protocol does not specify ZLPs, so we cannot allow an over-length transfer
- add requeue on transient urb errors
--- drivers/net/mctp/Kconfig | 11 +++ drivers/net/mctp/Makefile | 1 + drivers/net/mctp/mctp-usb.c | 104 ++++++------------------ drivers/net/mctp/mctp-usblib.c | 179 +++++++++++++++++++++++++++++++++++++++++ include/linux/usb/mctp-usb.h | 26 ++++++ 5 files changed, 240 insertions(+), 81 deletions(-)
drivers/net/mctp/Kconfig +11 -0
@@ -47,9 +47,20 @@ config MCTP_TRANSPORT_I3C A MCTP protocol network device is created for each I3C bus having a "mctp-controller" devicetree property. +config MCTP_TRANSPORT_USBLIB + tristate "MCTP over USB common library" + depends on USB + help + Common protocol handling functions for MCTP-over-USB transport + implementations, suitable for use in either host- or gadget-side + transport driver + + This will be automatically enabled by the transport driver. + config MCTP_TRANSPORT_USB tristate "MCTP USB transport" depends on USB + select MCTP_TRANSPORT_USBLIB help Provides a driver to access MCTP devices over USB transport, defined by DMTF specification DSP0283.
drivers/net/mctp/Makefile +1 -0
@@ -2,3 +2,4 @@ obj-$(CONFIG_MCTP_SERIAL) += mctp-serial.o obj-$(CONFIG_MCTP_TRANSPORT_I2C) += mctp-i2c.o obj-$(CONFIG_MCTP_TRANSPORT_I3C) += mctp-i3c.o obj-$(CONFIG_MCTP_TRANSPORT_USB) += mctp-usb.o +obj-$(CONFIG_MCTP_TRANSPORT_USBLIB) += mctp-usblib.o
drivers/net/mctp/mctp-usb.c +23 -81
170 lines not on this page · raw message
drivers/net/mctp/mctp-usblib.c new file +179 -0
179 lines not on this page · raw message
include/linux/usb/mctp-usb.h +26 -0
@@ -13,6 +13,8 @@#ifndef __LINUX_USB_MCTP_USB_H#define __LINUX_USB_MCTP_USB_H +#include <linux/netdevice.h> +#include <linux/skbuff.h> #include <linux/types.h> struct mctp_usb_hdr {
@@ -29,4 +31,28 @@ struct mctp_usb_hdr {#define MCTP_USB_1_0_MTU_MAX (MCTP_USB_1_0_PKTLEN_MAX - sizeof(struct mctp_usb_hdr))#define MCTP_USB_DMTF_ID 0x1ab4 +/* mctp-usblib */ + +/* +* RX handle: drivers will typically create one on init, which persists for +* the life of the driver. The same handle is used for progressive +* prepare -> complete operations (for each incoming USB transfer), which +* result in netif_rx()-ing the MCTP packets received +*/ +struct mctp_usblib_rx { +struct sk_buff *skb; +}; + +void mctp_usblib_rx_init (struct mctp_usblib_rx *rx); +void mctp_usblib_rx_fini (struct mctp_usblib_rx *rx); + +int mctp_usblib_rx_prepare (struct net_device *netdev, +struct mctp_usblib_rx *rx, +void **bufp,size_t *lenp,gfp_t gfp); + +int mctp_usblib_rx_complete (struct net_device *netdev, +struct mctp_usblib_rx *rx,size_t len); + +void mctp_usblib_rx_cancel (struct mctp_usblib_rx *rx); +#endif /* __LINUX_USB_MCTP_USB_H */
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 4/12 net: mctp: usb: Improve IN endpoint status handling
In reply to Jeremy KerrCurrently, we give-up on all non-zero status values on our IN/rx urb, and do not re-queue the urb. This will stall the driver, and prevent any further receive.
Instead, attempt a re-queue on transient errors, with a max of ten successive failures. Handle EPIPE specially, by scheduling a usb_clear_halt() in non-atomic context.
---
v4:
- move urb error common code to mctp_usb_in_urb_err, fixing a missing rx_cancel
- allow retries of usb_clear_halt
v3:
- improved IN urb status handling was introduced in v2, but now split to a separate change, via sashiko feedback
- now with usb_clear_halt on EPIPE.
--- drivers/net/mctp/mctp-usb.c | 91 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 7 deletions(-)
drivers/net/mctp/mctp-usb.c +84 -7
@@ -32,6 +32,9 @@ struct mctp_usb {struct urb *tx_urb;struct urb *rx_urb; +int in_err_count; +int in_err_orig; +bool clear_halt;/* enforces atomic access to rx_stopped and requeuing the retry work */ spinlock_t rx_lock;
@@ -158,27 +161,75 @@ static int mctp_usb_rx_queue(struct mctp_usb *mctp_usb, gfp_t gfp)return 0 ; } +static const unsigned int rx_err_max =10 ; + +/* Returns -1 if we have hit excessive errors, zero otherwise. */ +static int mctp_usb_in_urb_err (struct mctp_usb *mctp_usb,int status, +bool stalled) +{ +mctp_usblib_rx_cancel (&mctp_usb->rx); + +if (!mctp_usb->in_err_count++) + mctp_usb->in_err_orig = status; + +if (mctp_usb->in_err_count >= rx_err_max) { +netdev_err (mctp_usb->netdev, +"excessive errors from%s IN EP, first: %d\n" , + stalled ?" (stalled)" :"" , + mctp_usb->in_err_orig); +return -1 ; + } + +return 0 ; +} +static void mctp_usb_in_complete (struct urb *urb) {struct mctp_usb *mctp_usb = urb->context;struct net_device *netdev = mctp_usb->netdev; -int status; +unsigned long flags; +int rc, status; status = urb->status;switch (status) { -default : -netdev_dbg (netdev,"unexpected rx urb status: %d\n" , status); - fallthrough;case -ENOENT :case -ECONNRESET :case -ESHUTDOWN : -case -EPROTO : +/* device shutdown, don't resubmit */ mctp_usblib_rx_cancel (&mctp_usb->rx);return ; + +case -EPIPE : +/* endpoint stall: clear halt, which will cause a resubmit */ + rc =mctp_usb_in_urb_err (mctp_usb, status, true); +if (rc) +return ; + + mctp_usb->clear_halt = true; +spin_lock_irqsave (&mctp_usb->rx_lock, flags); +if (!mctp_usb->rx_stopped) +schedule_delayed_work (&mctp_usb->rx_retry_work, +RX_RETRY_DELAY ); +spin_unlock_irqrestore (&mctp_usb->rx_lock, flags); +return ; + +default : +netdev_dbg (netdev,"unexpected rx urb status: %d\n" , status); + fallthrough; +case -ETIME : +case -EPROTO : +case -EILSEQ : +case -EOVERFLOW : +/* possibly transient; record first failure, resubmit */ + rc =mctp_usb_in_urb_err (mctp_usb, status, false); +if (rc) +return ; +break ; +case 0 : -mctp_usblib_rx_complete (netdev, &mctp_usb->rx, - urb->actual_length); +mctp_usblib_rx_complete (netdev, &mctp_usb->rx, urb->actual_length); + mctp_usb->in_err_count =0 ;break ; }
@@ -189,6 +240,30 @@ static void mctp_usb_rx_retry_work(struct work_struct *work) { struct mctp_usb *mctp_usb = container_of(work, struct mctp_usb, rx_retry_work.work); +unsigned long flags; +int rc; + +/* We are only called when rx completions are suspended */ +if (mctp_usb->clear_halt) { +int pipe =usb_rcvbulkpipe (mctp_usb->usbdev, mctp_usb->ep_in); + + rc =usb_clear_halt (mctp_usb->usbdev, pipe); +if (rc) { +netdev_err (mctp_usb->netdev, +"can't clear IN EP halt: %d\n" , rc); + +if (++mctp_usb->in_err_count >= rx_err_max) +return ; + +spin_lock_irqsave (&mctp_usb->rx_lock, flags); +if (!mctp_usb->rx_stopped) +schedule_delayed_work (&mctp_usb->rx_retry_work, +RX_RETRY_DELAY ); +spin_unlock_irqrestore (&mctp_usb->rx_lock, flags); +return ; + } + mctp_usb->clear_halt = false; + }mctp_usb_rx_queue (mctp_usb,GFP_KERNEL ); }
@@ -198,6 +273,8 @@ static int mctp_usb_open(struct net_device *dev)struct mctp_usb *mctp_usb =netdev_priv (dev);WRITE_ONCE (mctp_usb->rx_stopped, false); + mctp_usb->clear_halt = false; + mctp_usb->in_err_count =0 ;netif_start_queue (dev);
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 5/12 net: mctp: usblib: Move TX transfer processing to mctp-usblib
In reply to Jeremy KerrWith the RX processing in mctp-usblib, add TX processing alongside.
To accommodate packed transfers in DSP0283, where a transfer may contain multiple MCTP packets, we move to a split process for the transmit API:
* push: create a new transmit context, and add a skb to it.
* send: callback to the driver implementation to send the (possibly multi-packet) USB transfer
* complete: update skb accounting and release the tx context
The actual multi-packet transfer implementation will be added in the next change; no tx context persists beyond the single send at present. However, we use an anchor in the host driver implementation to track the submitted TX urb when necessary.
While we're here, fix an inconsistency between tx and rx stats: both should not include the transport header.
---
v3:
- consolidate with tx_anchor introduction in the host-side driver; the single-urb approach (with racy free) was being introduced then immediately removed. Incorporating the anchor-based approach resolves this.
v2:
- make tx stats consistent with rx stats
- reinstate missing tx_stats_update in tx_send_complete
- don't usb_kill_urb() with the tx lock held
- implement skb_drop_reasons
- [squashed] adjust tx_anchor handling; the urb is unanchored before completion, so we don't need to unanchor explicitly. We can now rely on the anchor's own lock for serialisation
--- drivers/net/mctp/mctp-usb.c | 114 ++++++++++------------- drivers/net/mctp/mctp-usblib.c | 203 +++++++++++++++++++++++++++++++++++++++++ include/linux/usb/mctp-usb.h | 39 ++++++++ 3 files changed, 292 insertions(+), 64 deletions(-)
drivers/net/mctp/mctp-usb.c +50 -64
@@ -29,8 +29,6 @@ struct mctp_usb {u8 ep_out;struct mctp_usblib_rx rx; - -struct urb *tx_urb;struct urb *rx_urb;int in_err_count;int in_err_orig;
@@ -40,82 +38,66 @@ struct mctp_usb {spinlock_t rx_lock;bool rx_stopped;struct delayed_work rx_retry_work; + +struct mctp_usblib_tx tx; +struct usb_anchor tx_anchor; };static void mctp_usb_out_complete (struct urb *urb) { -struct sk_buff *skb = urb->context; -struct net_device *netdev = skb->dev; -int status; +struct mctp_usblib_tx_ctx *tx_ctx = urb->context; +struct mctp_usb *mctp_usb =mctp_usblib_tx_ctx_priv (tx_ctx); +struct net_device *netdev = mctp_usb->netdev; - status = urb->status; +mctp_usblib_tx_send_complete (tx_ctx, netdev, urb->status ==0 ); -switch (status) { -case -ENOENT : -case -ECONNRESET : -case -ESHUTDOWN : -case -EPROTO : -dev_dstats_tx_dropped (netdev); -break ; -case 0 : -dev_dstats_tx_add (netdev, skb->len); -netif_wake_queue (netdev); -consume_skb (skb); -return ; -default : -netdev_dbg (netdev,"unexpected tx urb status: %d\n" , status); -dev_dstats_tx_dropped (netdev); - } +usb_free_urb (urb); -kfree_skb (skb); +netif_wake_queue (netdev); } -static netdev_tx_t mctp_usb_start_xmit (struct sk_buff *skb, -struct net_device *dev) +static int mctp_usb_tx_send (struct mctp_usblib_tx_ctx *tx_ctx, +void *data,size_t len) { -struct mctp_usb *mctp_usb =netdev_priv (dev); -struct mctp_usb_hdr *hdr; -unsigned int plen; +struct mctp_usb *mctp_usb =mctp_usblib_tx_ctx_priv (tx_ctx);struct urb *urb;int rc; - plen = skb->len; - -if (plen +sizeof (*hdr) >MCTP_USB_1_0_PKTLEN_MAX ) - goto err_drop; - - rc =skb_cow_head (skb,sizeof (*hdr)); -if (rc) - goto err_drop; - - hdr =skb_push (skb,sizeof (*hdr)); -if (!hdr) - goto err_drop; - - hdr->id =cpu_to_be16 (MCTP_USB_DMTF_ID ); - hdr->rsvd =0 ; - hdr->len = plen +sizeof (*hdr); - - urb = mctp_usb->tx_urb; + urb =usb_alloc_urb (0 ,GFP_ATOMIC ); +if (!urb) +return -ENOMEM ;usb_fill_bulk_urb (urb, mctp_usb->usbdev,usb_sndbulkpipe (mctp_usb->usbdev, mctp_usb->ep_out), - skb->data, skb->len, - mctp_usb_out_complete, skb); + data, len, mctp_usb_out_complete, tx_ctx); + +netif_stop_queue (mctp_usb->netdev); + +usb_anchor_urb (urb, &mctp_usb->tx_anchor); -/* Stops TX queue first to prevent race condition with URB complete */ -netif_stop_queue (dev); rc =usb_submit_urb (urb,GFP_ATOMIC );if (rc) { -netif_wake_queue (dev); - goto err_drop; +netdev_dbg (mctp_usb->netdev,"TX urb submit failed, %d\n" , rc); +usb_unanchor_urb (urb); +usb_free_urb (urb); +netif_start_queue (mctp_usb->netdev); } -return NETDEV_TX_OK ; +return rc; +} + +static const struct mctp_usblib_tx_ops tx_ops = { + .send = mctp_usb_tx_send, +}; + +static netdev_tx_t mctp_usb_start_xmit (struct sk_buff *skb, +struct net_device *dev) +{ +struct mctp_usb *mctp_usb =netdev_priv (dev); +bool more =netdev_xmit_more (); + +mctp_usblib_tx_push (dev, &mctp_usb->tx, skb, more); -err_drop: -dev_dstats_tx_dropped (dev); -kfree_skb (skb);return NETDEV_TX_OK ; }
@@ -297,8 +279,10 @@ static int mctp_usb_stop(struct net_device *dev)flush_delayed_work (&mctp_usb->rx_retry_work);usb_kill_urb (mctp_usb->rx_urb); -usb_kill_urb (mctp_usb->tx_urb); +usb_kill_anchored_urbs (&mctp_usb->tx_anchor); + +mctp_usblib_tx_cancel (&mctp_usb->tx, dev,SKB_DROP_REASON_DEV_READY );mctp_usblib_rx_cancel (&mctp_usb->rx);return 0 ;
@@ -357,28 +341,30 @@ static int mctp_usb_probe(struct usb_interface *intf,usb_set_intfdata (intf, dev);mctp_usblib_rx_init (&dev->rx); +mctp_usblib_tx_init (&dev->tx, &tx_ops, dev); +init_usb_anchor (&dev->tx_anchor); dev->ep_in = ep_in->bEndpointAddress; dev->ep_out = ep_out->bEndpointAddress; - dev->tx_urb =usb_alloc_urb (0 ,GFP_KERNEL ); dev->rx_urb =usb_alloc_urb (0 ,GFP_KERNEL ); -if (!dev->tx_urb || !dev->rx_urb) { +if (!dev->rx_urb) { rc = -ENOMEM ; - goto err_free_urbs; + goto err_fini_rxtx; }INIT_DELAYED_WORK (&dev->rx_retry_work, mctp_usb_rx_retry_work); rc =mctp_register_netdev (netdev,NULL ,MCTP_PHYS_BINDING_USB );if (rc) - goto err_free_urbs; + goto err_free_urb;return 0 ; -err_free_urbs: -usb_free_urb (dev->tx_urb); +err_free_urb:usb_free_urb (dev->rx_urb); +err_fini_rxtx: +mctp_usblib_tx_fini (&dev->tx);mctp_usblib_rx_fini (&dev->rx);free_netdev (netdev);return rc;
@@ -390,7 +376,7 @@ static void mctp_usb_disconnect(struct usb_interface *intf)mctp_unregister_netdev (dev->netdev);mctp_usblib_rx_fini (&dev->rx); -usb_free_urb (dev->tx_urb); +mctp_usblib_tx_fini (&dev->tx);usb_free_urb (dev->rx_urb);free_netdev (dev->netdev); }
drivers/net/mctp/mctp-usblib.c +203 -0
209 lines not on this page · raw message
include/linux/usb/mctp-usb.h +39 -0
@@ -55,4 +55,43 @@ int mctp_usblib_rx_complete(struct net_device *netdev,void mctp_usblib_rx_cancel (struct mctp_usblib_rx *rx); +/* +* TX handle: created by mctp_usblib_tx_push() during the tx path, and +* may persist across multiple packet transmits. +* +* Currently though, there is a 1:1 mapping between packets and transfers, so +* the tx context will be cleared over each transmit. This will change in +* future. +*/ +struct mctp_usblib_tx_ctx ; + +struct mctp_usblib_tx_ops { +/* Start a USB TX for @data. On returning success, the implementation +* must arrange for mctp_usblib_tx_send_complete() to be called at some +* later point (eg., on urb completion). +*/ +int (*send)(struct mctp_usblib_tx_ctx *tx_ctx,void *data,size_t len); +}; + +struct mctp_usblib_tx { +struct mctp_usblib_tx_ops ops; +void *priv; +}; + +void mctp_usblib_tx_init (struct mctp_usblib_tx *tx, +const struct mctp_usblib_tx_ops *ops,void *priv); +void mctp_usblib_tx_fini (struct mctp_usblib_tx *tx); + +void *mctp_usblib_tx_ctx_priv (struct mctp_usblib_tx_ctx *tx_ctx); + +int mctp_usblib_tx_push (struct net_device *dev, +struct mctp_usblib_tx *tx, +struct sk_buff *skb,bool more); + +void mctp_usblib_tx_send_complete (struct mctp_usblib_tx_ctx *tx_ctx, +struct net_device *dev,bool ok); + +void mctp_usblib_tx_cancel (struct mctp_usblib_tx *tx,struct net_device *dev, +enum skb_drop_reason reason); +#endif /* __LINUX_USB_MCTP_USB_H */
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 6/12 net: mctp: usblib: Add support for multi-packet transmit
In reply to Jeremy KerrThe MCTP over USB spec allows us to pack multiple packets in one transfer. Given the packet max length is 255, and the transfer max length is 512, we can typically include two full-size packets per urb submission.
To do this, we allow a struct mctp_usb_tx to persist a tx_ctx, representing the ongoing context for a transmit. If possible, a TX skb will be queued to the context and the send deferred until the context is full, or the device queue reports no more packets.
This typically requires a linear buffer for the 512-byte TX, which we allocate along with the TX context.
---
v3:
- ensure we are setting drop reasons correctly on _prepare() failure
v2:
- expand on tx_should_send logic, rather than the hardcoded 68 value.
- don't increment ctx->len (from zero) on ctx_create(), set explicitly
- add skb_drop_reasons
--- drivers/net/mctp/mctp-usblib.c | 252 +++++++++++++++++++++++++++++++++-------- include/linux/usb/mctp-usb.h | 8 +- 2 files changed, 210 insertions(+), 50 deletions(-)
drivers/net/mctp/mctp-usblib.c +206 -46
@@ -177,11 +177,13 @@ EXPORT_SYMBOL_GPL(mctp_usblib_rx_cancel);/* transmit context: encapsulates one transfer */ struct mctp_usblib_tx_ctx {struct mctp_usblib_tx *tx; -struct sk_buff *skb; +struct sk_buff_head skbs;unsigned int len;enum mctp_usblib_tx_buf_type {TX_SINGLE , +TX_FLAT , } buf_type; +u8 buf[]____cacheline_aligned ; }; void mctp_usblib_tx_init(struct mctp_usblib_tx *tx,
@@ -191,53 +193,129 @@ void mctp_usblib_tx_init(struct mctp_usblib_tx *tx,memset (tx,0 ,sizeof (*tx)); tx->ops = *ops; tx->priv = priv; +spin_lock_init (&tx->lock); }EXPORT_SYMBOL_GPL (mctp_usblib_tx_init); -void mctp_usblib_tx_fini (struct mctp_usblib_tx *tx) +static int mctp_usblib_tx_avail (struct mctp_usblib_tx_ctx *ctx) { +return ctx->buf_type ==TX_SINGLE ?0 :MCTP_USB_1_0_XFER_SIZE - ctx->len; } -EXPORT_SYMBOL_GPL (mctp_usblib_tx_fini); -void *mctp_usblib_tx_ctx_priv (struct mctp_usblib_tx_ctx *tx_ctx) +static bool mctp_usblib_tx_should_send (struct mctp_usblib_tx_ctx *ctx) { -return tx_ctx->tx->priv; +/* Use the baseline length (ie, BTU) as an approximate +* "reasonably-sized" packet we could expect. If there is +* insufficient capacity for that, then send. +*/ +const size_t pkt_len =MCTP_USB_BTU +sizeof (struct mctp_usb_hdr ); + +return mctp_usblib_tx_avail (ctx) < pkt_len; } -EXPORT_SYMBOL_GPL (mctp_usblib_tx_ctx_priv); -static struct mctp_usblib_tx_ctx * -mctp_usblib_tx_ctx_create (struct mctp_usblib_tx *tx,struct sk_buff *skb) +/* +* Returns zero on success, non-zero on failure - indicating that the new skb +* could not be appended. So, errors reported here to the TX path will result +* in the TX being transmitted. +*/ +static int mctp_usblib_tx_append (struct mctp_usblib_tx_ctx *ctx, +struct sk_buff *skb) { -struct mctp_usblib_tx_ctx *ctx; +if (ctx->buf_type ==TX_SINGLE ) +return -EINVAL ; - ctx =kzalloc_obj (*ctx,GFP_ATOMIC ); -if (!ctx) -return NULL ; +if (mctp_usblib_tx_avail (ctx) < skb->len) +return -ENOBUFS ; + +__skb_queue_tail (&ctx->skbs, skb); - ctx->tx = tx; - ctx->buf_type =TX_SINGLE ; - ctx->skb = skb; ctx->len += skb->len; -return ctx; +return 0 ; }static int mctp_usblib_tx_send (struct mctp_usblib_tx_ctx *ctx) { -struct mctp_usblib_tx *tx = ctx->tx; -void *buf = ctx->skb->data; +void *buf; + +/* If we have a qlen of 1, we only ended up packing a single skb, +* despite allocating for multiple. Skip the copy and send directly +* from the skb data. +*/ +if (ctx->buf_type ==TX_SINGLE || ctx->skbs.qlen ==1 ) { + buf = ctx->skbs.next->data; + + }else if (ctx->buf_type ==TX_FLAT ) { +struct sk_buff *skb; +size_t pos =0 ; + + skb_queue_walk(&ctx->skbs, skb) { +skb_copy_bits (skb,0 , ctx->buf + pos, skb->len); + pos += skb->len; + } -return tx->ops.send (ctx, buf, ctx->len); + buf = ctx->buf; + }else { +return -EINVAL ; + } + +return ctx->tx->ops.send (ctx, buf, ctx->len); }static void mctp_usblib_tx_ctx_free (struct mctp_usblib_tx_ctx *ctx,enum skb_drop_reason reason) { -if (ctx) -dev_kfree_skb_any_reason (ctx->skb, reason); +struct sk_buff *skb; + +if (!ctx) +return ; + +while ((skb =__skb_dequeue (&ctx->skbs)) !=NULL ) +dev_kfree_skb_any_reason (skb, reason);kfree (ctx); } +void *mctp_usblib_tx_ctx_priv (struct mctp_usblib_tx_ctx *tx_ctx) +{ +return tx_ctx->tx->priv; +} +EXPORT_SYMBOL_GPL (mctp_usblib_tx_ctx_priv); + +/* caller must ensure the tx & completion path is quiesced */ +void mctp_usblib_tx_fini (struct mctp_usblib_tx *tx) +{ +mctp_usblib_tx_ctx_free (tx->cur_ctx,SKB_DROP_REASON_NOT_SPECIFIED ); +} +EXPORT_SYMBOL_GPL (mctp_usblib_tx_fini); + +static struct mctp_usblib_tx_ctx * +mctp_usblib_tx_ctx_create (struct mctp_usblib_tx *tx,struct sk_buff *skb, +bool single) +{ +enum mctp_usblib_tx_buf_type type; +struct mctp_usblib_tx_ctx *ctx; +size_t sz =0 ; + +if (single) { + type =TX_SINGLE ; + }else { + type =TX_FLAT ; + sz =MCTP_USB_1_0_XFER_SIZE ; + } + + ctx =kzalloc_flex (*ctx, buf, sz,GFP_ATOMIC ); +if (!ctx) +return NULL ; + + ctx->tx = tx; + ctx->buf_type = type; + ctx->len = skb->len; +skb_queue_head_init (&ctx->skbs); +__skb_queue_tail (&ctx->skbs, skb); + +return ctx; +} +static void mctp_usblib_tx_stats_update (struct mctp_usblib_tx_ctx *ctx,struct net_device *dev,bool ok)
@@ -251,12 +329,13 @@ static void mctp_usblib_tx_stats_update(struct mctp_usblib_tx_ctx *ctx,* that there is a 4-byte header pushed to all skbs in * tx_skb_prepare() */ -s64 len = ctx->len -sizeof (struct mctp_usb_hdr ); +u64 n = ctx->skbs.qlen; +s64 len = ctx->len - (n *sizeof (struct mctp_usb_hdr )); -u64_stats_inc (&dstats->tx_packets); +u64_stats_add (&dstats->tx_packets, n);u64_stats_add (&dstats->tx_bytes, len); }else { -u64_stats_inc (&dstats->tx_drops); +u64_stats_add (&dstats->tx_drops, ctx->skbs.qlen); }u64_stats_update_end_irqrestore (&dstats->syncp, flags);put_cpu_ptr (dev->dstats);
@@ -327,8 +406,8 @@ static int mctp_usblib_tx_skb_prepare(struct sk_buff *skb, }/* -* Push a new skb to the transfer. At present, no send must be in progress, -* as we only handle single-packet USB transfers. +* Push a new skb to the transfer. May result in zero or more calls to +* ops->send(). * * Takes ownership of @skb, including on error. */
@@ -336,36 +415,106 @@ int mctp_usblib_tx_push(struct net_device *dev,struct mctp_usblib_tx *tx, struct sk_buff *skb, bool more) { - struct mctp_usblib_tx_ctx *ctx; + struct mctp_usblib_tx_ctx *ctx, *send_ctx = NULL;enum skb_drop_reason reason; -int rc; +const int max_tries =3 ; +unsigned long flags; +int try =1 , rc; + + rc =mctp_usblib_tx_skb_prepare (skb, &reason); +if (rc) { +mctp_usblib_tx_stats_single_drop (dev); +kfree_skb_reason (skb, reason); +/* we may still need to proceed, in case an existing ctx +* is now sendable (ie.: !more). +*/ + skb =NULL ; + } + + reason =SKB_DROP_REASON_NOT_SPECIFIED ; +retry: +/* Try and queue to the current context. We exit this critical section +* with a few bits of state: +* - send_ctx: indicating a prior context that needs to be sent +* - skb: indicating that a skb still needs to be queued/sent +*/ +spin_lock_irqsave (&tx->lock, flags); + ctx = tx->cur_ctx; +if (ctx) { +if (skb) { + rc =mctp_usblib_tx_append (ctx, skb); +if (rc) { +/* can't append to the pending tx - detach for +* sending, and we'll create a new tx below. +*/ +swap (tx->cur_ctx, send_ctx); + }else { +/* we have queued */ + skb =NULL ; +if (!more ||mctp_usblib_tx_should_send (ctx)) +swap (tx->cur_ctx, send_ctx); + } + }else if (!more) { +swap (tx->cur_ctx, send_ctx); + } + } +spin_unlock_irqrestore (&tx->lock, flags); + +if (send_ctx) { + rc =mctp_usblib_tx_send (send_ctx); +if (rc) { +mctp_usblib_tx_stats_update (send_ctx, dev, false); +mctp_usblib_tx_ctx_free (send_ctx, reason); + } + send_ctx =NULL ; + } +/* we have either queued, or the prepare failed; nothing more to do */ if (!skb)return 0 ; - rc =mctp_usblib_tx_skb_prepare (skb, &reason); -if (rc) - goto err_drop_single; - - ctx =mctp_usblib_tx_ctx_create (tx, skb); + ctx =mctp_usblib_tx_ctx_create (tx, skb, !more);if (!ctx) { - rc = -ENOMEM ; - reason =SKB_DROP_REASON_NOMEM ; - goto err_drop_single; +netdev_dbg (dev,"TX context create failed\n" ); +mctp_usblib_tx_stats_single_drop (dev); +kfree_skb (skb); +return -ENOMEM ; } - rc =mctp_usblib_tx_send (ctx); -if (rc) { -mctp_usblib_tx_stats_update (ctx, dev, false); -mctp_usblib_tx_ctx_free (ctx,SKB_DROP_REASON_NOT_SPECIFIED ); +/* if we're ready to send now, no need to enqueue */ +if (!more ||mctp_usblib_tx_should_send (ctx)) { + rc =mctp_usblib_tx_send (ctx); +if (rc) { +mctp_usblib_tx_stats_update (ctx, dev, false); +mctp_usblib_tx_ctx_free (ctx, reason); + } +return 0 ; } -return rc; +spin_lock_irqsave (&tx->lock, flags); +if (!tx->cur_ctx) { + tx->cur_ctx = ctx; + ctx =NULL ; + } +spin_unlock_irqrestore (&tx->lock, flags); -err_drop_single: -mctp_usblib_tx_stats_single_drop (dev); -kfree_skb_reason (skb, reason); -return rc; +/* we may have lost the race with a concurrent tx; shouldn't happen, as +* ndo_start_xmit should be serialised over one queue, but try again +* from the top, as we may be able to queue the skb to that context. +*/ +if (ctx) { +/* unlink the new (sole) skb, we don't want it freed with ctx */ +__skb_queue_head_init (&ctx->skbs); +mctp_usblib_tx_ctx_free (ctx, reason); +if (++try > max_tries) { +kfree_skb (skb); +mctp_usblib_tx_stats_single_drop (dev); +return -EBUSY ; + } + goto retry; + } + +return 0 ; }EXPORT_SYMBOL_GPL (mctp_usblib_tx_push);
@@ -373,7 +522,18 @@ EXPORT_SYMBOL_GPL(mctp_usblib_tx_push);void mctp_usblib_tx_cancel (struct mctp_usblib_tx *tx,struct net_device *dev,enum skb_drop_reason reason) { -/* nothing to do at present, no ctx is persistent */ +struct mctp_usblib_tx_ctx *ctx =NULL ; +unsigned long flags; + +spin_lock_irqsave (&tx->lock, flags); +swap (tx->cur_ctx, ctx); +spin_unlock_irqrestore (&tx->lock, flags); + +if (!ctx) +return ; + +mctp_usblib_tx_stats_update (ctx, dev, false); +mctp_usblib_tx_ctx_free (ctx, reason); }EXPORT_SYMBOL_GPL (mctp_usblib_tx_cancel);
include/linux/usb/mctp-usb.h +4 -4
@@ -58,10 +58,6 @@ void mctp_usblib_rx_cancel(struct mctp_usblib_rx *rx);/* * TX handle: created by mctp_usblib_tx_push() during the tx path, and * may persist across multiple packet transmits. -* -* Currently though, there is a 1:1 mapping between packets and transfers, so -* the tx context will be cleared over each transmit. This will change in -* future. */ struct mctp_usblib_tx_ctx ;
@@ -76,6 +72,10 @@ struct mctp_usblib_tx_ops {struct mctp_usblib_tx {struct mctp_usblib_tx_ops ops;void *priv; +/* protects access to cur_ctx */ +spinlock_t lock; +/* context to which we are adding packets, cleared on send */ +struct mctp_usblib_tx_ctx *cur_ctx; }; void mctp_usblib_tx_init(struct mctp_usblib_tx *tx,
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 7/12 net: mctp: usb: Accommodate DSP0283 v1.1 header format
In reply to Jeremy KerrIn the v1.1 update to DSP0283, we have a larger header field, of 13 bits rather than 8.
In order to accommodate this, in preparation for proper v1.1 support, expand our struct mctp_usb_hdr's len field to a u16, and endian-convert when necessary. Because we don't yet support spanning mode, we will never receive or transmit with the top 5 bits set, so we always mask out anyway.
This allows for a future change where we allow spanning mode with
Quoted 1 line
512-byte transfers.
---
v2:
- fix short packet (hdr) debug message: use endian-converted value
--- drivers/net/mctp/mctp-usblib.c | 14 ++++++++------ include/linux/usb/mctp-usb.h | 11 ++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-)
drivers/net/mctp/mctp-usblib.c +8 -6
@@ -99,6 +99,7 @@ int mctp_usblib_rx_complete(struct net_device *netdev,while (skb) {struct sk_buff *skb2 =NULL ;struct mctp_usb_hdr *hdr; +u16 hdr_len;/* length of MCTP packet, no USB header */ u8 pkt_len;
@@ -116,21 +117,23 @@ int mctp_usblib_rx_complete(struct net_device *netdev,break ; } -if (hdr->len < + hdr_len =be16_to_cpu (hdr->len) &MCTP_USB_1_0_PKTLEN_MAX ; + +if (hdr_len <sizeof (struct mctp_hdr ) +sizeof (struct mctp_usb_hdr )) {netdev_dbg (netdev,"rx: short packet (hdr) %d\n" , - hdr->len); + hdr_len); rc = -EPROTO ;break ; }/* we know we have at least sizeof(struct mctp_usb_hdr) here */ - pkt_len = hdr->len -sizeof (struct mctp_usb_hdr ); + pkt_len = hdr_len -sizeof (struct mctp_usb_hdr );if (pkt_len > skb->len) { rc = -EPROTO ;netdev_dbg (netdev,"rx: short packet (xfer) %d, actual %d\n" , - hdr->len, skb->len); + hdr_len, skb->len);break ; }
@@ -399,8 +402,7 @@ static int mctp_usblib_tx_skb_prepare(struct sk_buff *skb, } hdr->id =cpu_to_be16 (MCTP_USB_DMTF_ID ); - hdr->rsvd =0 ; - hdr->len = plen +sizeof (*hdr); + hdr->len =cpu_to_be16 (plen +sizeof (*hdr));return 0 ; }
include/linux/usb/mctp-usb.h +8 -3
@@ -2,7 +2,7 @@/* * mctp-usb.h - MCTP USB transport binding: common definitions, * based on DMTF0283 specification: -* https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.0.1.pdf +* https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.1.0.pdf * * These are protocol-level definitions, that may be shared between host * and gadget drivers.
@@ -17,10 +17,15 @@#include <linux/skbuff.h> #include <linux/types.h> +/* +* MCTP-over-USB transport header. DSP0283 v1.0 has an 8-bit length field +* (preceded by 8 reserved bits), v1.1 has a 13-bit length field (preceded by +* 3 reserved bits). We use a be16 for our length to handle the larger v1.1 +* representation, and mask as appropriate. +*/ struct mctp_usb_hdr {__be16 id; -u8 rsvd; -u8 len; +__be16 len; } __packed;/* max transfer size for DSP0283 v1.0 */
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 8/12 net: mctp: usblib: Implement receive-side packet spanning
In reply to Jeremy KerrUsing the existing prepare/complete API, we can persist the rx skb across receives to implement v1.1 packet spanning.
Alter the packet-extraction loop to allow truncated packets, returning early with the skb persisted for the next IN urb completion. When we see we have a complete packet, netif_rx() that. If the packet boundary aligns with the urb completion, we can netif_rx() the whole thing.
Those intermediate packets are cloned from the original (large-transfer-data) skb. Unlike existing behaviour, if the clone fails, we drop just that clone, instead of the existing transfer skb. This allows us to process the rest of the skb data, and any continuation of the span into the next transfer.
One subtle change: the mctp_usblib_rx() helper now handles skbs with the full transport header, so we shift the skb_pull() for the header data to the helper, before doing the rx_bytes stats update.
We still need to handle non-spanning mode, so error out on truncated-packet cases there.
---
v5:
- allow for non-power-of-two max-endpoint sizes; use round rather than ALIGN.
v4:
- describe clone failure behaviour
- keep EPROTO for header-size mismatches
- validate wMaxPacketSize on init
v3:
- only expand skb if cloned or under min xfer size, preventing excessive reallocation
- align rx len to ep pktlen.
v2:
- note change in semantics for mctp_usblib_rx
- reject rx packets too short for a MCTP header, rather than deferring to the MCTP core do do so
--- drivers/net/mctp/mctp-usb.c | 6 +- drivers/net/mctp/mctp-usblib.c | 167 +++++++++++++++++++++++++++++------------ include/linux/usb/mctp-usb.h | 6 +- 3 files changed, 127 insertions(+), 52 deletions(-)
drivers/net/mctp/mctp-usb.c +5 -1
@@ -340,7 +340,10 @@ static int mctp_usb_probe(struct usb_interface *intf,spin_lock_init (&dev->rx_lock);usb_set_intfdata (intf, dev); -mctp_usblib_rx_init (&dev->rx); + rc =mctp_usblib_rx_init (&dev->rx,le16_to_cpu (ep_in->wMaxPacketSize), + false); +if (rc) + goto err_free_netdev;mctp_usblib_tx_init (&dev->tx, &tx_ops, dev);init_usb_anchor (&dev->tx_anchor);
@@ -366,6 +369,7 @@ static int mctp_usb_probe(struct usb_interface *intf, err_fini_rxtx:mctp_usblib_tx_fini (&dev->tx);mctp_usblib_rx_fini (&dev->rx); +err_free_netdev:free_netdev (netdev);return rc; }
drivers/net/mctp/mctp-usblib.c +117 -50
229 lines not on this page · raw message
include/linux/usb/mctp-usb.h +5 -1
@@ -34,6 +34,8 @@ struct mctp_usb_hdr {#define MCTP_USB_MTU_MIN MCTP_USB_BTU#define MCTP_USB_1_0_PKTLEN_MAX U8_MAX#define MCTP_USB_1_0_MTU_MAX (MCTP_USB_1_0_PKTLEN_MAX - sizeof(struct mctp_usb_hdr)) +#define MCTP_USB_1_1_PKTLEN_MAX GENMASK(12, 0) +#define MCTP_USB_1_1_MTU_MAX (MCTP_USB_1_1_PKTLEN_MAX - sizeof(struct mctp_usb_hdr))#define MCTP_USB_DMTF_ID 0x1ab4/* mctp-usblib */
@@ -46,9 +48,11 @@ struct mctp_usb_hdr {*/ struct mctp_usblib_rx {struct sk_buff *skb; +u16 ep_pktlen; +bool span; }; -void mctp_usblib_rx_init (struct mctp_usblib_rx *rx); +int mctp_usblib_rx_init (struct mctp_usblib_rx *rx,u16 ep_pktlen,bool span);void mctp_usblib_rx_fini (struct mctp_usblib_rx *rx); int mctp_usblib_rx_prepare(struct net_device *netdev,
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 9/12 net: mctp: usblib: Implement transmit-side packet spanning
In reply to Jeremy KerrAdd support for packet spanning as defined in DSP0283 v1.1.
With the existing v1.0 implementation of multi-packet transfers, all we need here is to adjust the buffer sizes to suit v1.1.
--- drivers/net/mctp/mctp-usb.c | 2 +- drivers/net/mctp/mctp-usblib.c | 28 +++++++++++++++++++--------- include/linux/usb/mctp-usb.h | 4 +++- 3 files changed, 23 insertions(+), 11 deletions(-)
drivers/net/mctp/mctp-usb.c +1 -1
@@ -344,7 +344,7 @@ static int mctp_usb_probe(struct usb_interface *intf, false);if (rc) goto err_free_netdev; -mctp_usblib_tx_init (&dev->tx, &tx_ops, dev); +mctp_usblib_tx_init (&dev->tx, &tx_ops, dev, false);init_usb_anchor (&dev->tx_anchor); dev->ep_in = ep_in->bEndpointAddress;
drivers/net/mctp/mctp-usblib.c +19 -9
@@ -248,7 +248,7 @@ EXPORT_SYMBOL_GPL(mctp_usblib_rx_cancel);struct mctp_usblib_tx_ctx {struct mctp_usblib_tx *tx;struct sk_buff_head skbs; -unsigned int len; +unsigned int buf_len, len;enum mctp_usblib_tx_buf_type {TX_SINGLE ,TX_FLAT ,
@@ -258,18 +258,19 @@ struct mctp_usblib_tx_ctx {void mctp_usblib_tx_init (struct mctp_usblib_tx *tx,const struct mctp_usblib_tx_ops *ops, -void *priv) +void *priv,bool span) {memset (tx,0 ,sizeof (*tx)); tx->ops = *ops; tx->priv = priv; + tx->span = span;spin_lock_init (&tx->lock); }EXPORT_SYMBOL_GPL (mctp_usblib_tx_init);static int mctp_usblib_tx_avail (struct mctp_usblib_tx_ctx *ctx) { -return ctx->buf_type ==TX_SINGLE ?0 :MCTP_USB_1_0_XFER_SIZE - ctx->len; +return ctx->buf_type ==TX_SINGLE ?0 : ctx->buf_len - ctx->len; }static bool mctp_usblib_tx_should_send (struct mctp_usblib_tx_ctx *ctx)
@@ -358,6 +359,12 @@ void mctp_usblib_tx_fini(struct mctp_usblib_tx *tx) }EXPORT_SYMBOL_GPL (mctp_usblib_tx_fini); +/* Max size of a spanned TX. Since we allocate a separate span buffer, limit +* the tx-time allocations to 4k. Larger packets will be sent as single +* transfers. +*/ +static const unsigned int TX_SPAN_MAX =4096 -sizeof (struct mctp_usblib_tx_ctx ); +static struct mctp_usblib_tx_ctx *mctp_usblib_tx_ctx_create (struct mctp_usblib_tx *tx,struct sk_buff *skb,bool single)
@@ -366,11 +373,11 @@ mctp_usblib_tx_ctx_create(struct mctp_usblib_tx *tx, struct sk_buff *skb,struct mctp_usblib_tx_ctx *ctx;size_t sz =0 ; -if (single) { +if (single || skb->len >TX_SPAN_MAX ) { type =TX_SINGLE ; }else { type =TX_FLAT ; - sz =MCTP_USB_1_0_XFER_SIZE ; + sz = tx->span ?TX_SPAN_MAX :MCTP_USB_1_0_XFER_SIZE ; } ctx =kzalloc_flex (*ctx, buf, sz,GFP_ATOMIC );
@@ -379,6 +386,7 @@ mctp_usblib_tx_ctx_create(struct mctp_usblib_tx *tx, struct sk_buff *skb, ctx->tx = tx; ctx->buf_type = type; + ctx->buf_len = sz; ctx->len = skb->len;skb_queue_head_init (&ctx->skbs);__skb_queue_tail (&ctx->skbs, skb);
@@ -443,15 +451,17 @@ EXPORT_SYMBOL_GPL(mctp_usblib_tx_send_complete);* * On error, populates @reason. */ -static int mctp_usblib_tx_skb_prepare (struct sk_buff *skb, +static int mctp_usblib_tx_skb_prepare (struct sk_buff *skb,bool span,enum skb_drop_reason *reason) { +unsigned long plen, max_len;struct mctp_usb_hdr *hdr; -unsigned long plen;int rc; + max_len = span ?MCTP_USB_1_1_PKTLEN_MAX :MCTP_USB_1_0_PKTLEN_MAX ; + plen = skb->len; -if (plen +sizeof (*hdr) >MCTP_USB_1_0_PKTLEN_MAX ) { +if (plen +sizeof (*hdr) > max_len) { *reason =SKB_DROP_REASON_PKT_TOO_BIG ;return -EMSGSIZE ; }
@@ -490,7 +500,7 @@ int mctp_usblib_tx_push(struct net_device *dev,unsigned long flags;int try =1 , rc; - rc =mctp_usblib_tx_skb_prepare (skb, &reason); + rc =mctp_usblib_tx_skb_prepare (skb, tx->span, &reason);if (rc) {mctp_usblib_tx_stats_single_drop (dev);kfree_skb_reason (skb, reason);
include/linux/usb/mctp-usb.h +3 -1
@@ -81,6 +81,7 @@ struct mctp_usblib_tx_ops {struct mctp_usblib_tx {struct mctp_usblib_tx_ops ops;void *priv; +bool span;/* protects access to cur_ctx */ spinlock_t lock;/* context to which we are adding packets, cleared on send */
@@ -88,7 +89,8 @@ struct mctp_usblib_tx { };void mctp_usblib_tx_init (struct mctp_usblib_tx *tx, -const struct mctp_usblib_tx_ops *ops,void *priv); +const struct mctp_usblib_tx_ops *ops,void *priv, +bool span);void mctp_usblib_tx_fini (struct mctp_usblib_tx *tx);void *mctp_usblib_tx_ctx_priv (struct mctp_usblib_tx_ctx *tx_ctx);
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 10/12 net: mctp: usblib: Add initial kunit tests
In reply to Jeremy KerrAdd some initial tests for the usblib receive path, where we're extracting MCTP packets from incoming USB transfer data.
---
v4:
- use %u for unsigned formatting
- don't KUNIT_ASSERT in the test_fini cleanup action
v2:
- account for KUNIT_ASSERT-based exits; do cleanup through kunit_action facilities.
- fix off-by-one in packet count duing skb length checks, in case we ended up with more skbs than expected
- perform route updates under rntl lock
--- drivers/net/mctp/Kconfig | 5 + drivers/net/mctp/mctp-usblib-test.c | 412 ++++++++++++++++++++++++++++++++++++ drivers/net/mctp/mctp-usblib.c | 4 + 3 files changed, 421 insertions(+)
drivers/net/mctp/Kconfig +5 -0
@@ -57,6 +57,11 @@ config MCTP_TRANSPORT_USBLIB This will be automatically enabled by the transport driver. +config MCTP_TRANSPORT_USBLIB_TEST + bool "MCTP usblib tests" if !KUNIT_ALL_TESTS + depends on MCTP_TRANSPORT_USBLIB=y && KUNIT=y + default KUNIT_ALL_TESTS + config MCTP_TRANSPORT_USB tristate "MCTP USB transport" depends on USB
drivers/net/mctp/mctp-usblib-test.c new file +412 -0
412 lines not on this page · raw message
drivers/net/mctp/mctp-usblib.c +4 -0
@@ -619,3 +619,7 @@ EXPORT_SYMBOL_GPL(mctp_usblib_tx_cancel);MODULE_LICENSE ("GPL" );MODULE_AUTHOR ("Jeremy Kerr <[email protected]>" );MODULE_DESCRIPTION ("MCTP USB transport library" ); + +#if IS_ENABLED (CONFIG_MCTP_TRANSPORT_USBLIB_TEST ) +#include "mctp-usblib-test.c" +#endif
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 11/12 net: mctp: usb: enable v1.1 packet spanning
In reply to Jeremy KerrNow that mctp-usblib supports DSP0283 v1.1 packet spanning, enable it in our host-side transport driver.
Add a match for the new device subclass (0x02), and indicating spanning mode to the usblib rx/tx implementation.
--- drivers/net/mctp/mctp-usb.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
drivers/net/mctp/mctp-usb.c +21 -5
@@ -3,9 +3,9 @@* mctp-usb.c - MCTP-over-USB (DMTF DSP0283) transport binding driver. * * DSP0283 is available at: -* https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.0.1.pdf +* https://www.dmtf.org/sites/default/files/standards/documents/DSP0283_1.1.0.pdf * -* Copyright (C) 2024-2025 Code Construct Pty Ltd +* Copyright (C) 2024-2026 Code Construct Pty Ltd */ #include <linux/module.h>
@@ -22,6 +22,7 @@struct mctp_usb {struct usb_device *usbdev;struct usb_interface *intf; +bool span;struct net_device *netdev;
@@ -43,6 +44,11 @@ struct mctp_usb {struct usb_anchor tx_anchor; }; +enum { +MCTP_USB_SUBCLASS_BASE =0x00 , +MCTP_USB_SUBCLASS_SPAN =0x02 , +}; +static void mctp_usb_out_complete (struct urb *urb) {struct mctp_usblib_tx_ctx *tx_ctx = urb->context;
@@ -71,6 +77,9 @@ static int mctp_usb_tx_send(struct mctp_usblib_tx_ctx *tx_ctx,usb_sndbulkpipe (mctp_usb->usbdev, mctp_usb->ep_out), data, len, mctp_usb_out_complete, tx_ctx); +if (mctp_usb->span) + urb->transfer_flags |=URB_ZERO_PACKET ; +netif_stop_queue (mctp_usb->netdev);usb_anchor_urb (urb, &mctp_usb->tx_anchor);
@@ -316,6 +325,7 @@ static int mctp_usb_probe(struct usb_interface *intf,struct usb_host_interface *iface_desc;struct net_device *netdev;struct mctp_usb *dev; +bool span;int rc;/* only one alternate */
@@ -327,6 +337,8 @@ static int mctp_usb_probe(struct usb_interface *intf,return rc; } + span = iface_desc->desc.bInterfaceSubClass ==MCTP_USB_SUBCLASS_SPAN ; + netdev =alloc_netdev (sizeof (*dev),"mctpusb%d" ,NET_NAME_ENUM , mctp_usb_netdev_setup);if (!netdev)
@@ -334,17 +346,20 @@ static int mctp_usb_probe(struct usb_interface *intf,SET_NETDEV_DEV (netdev, &intf->dev); dev =netdev_priv (netdev); + dev->span = span; dev->netdev = netdev; dev->usbdev =interface_to_usbdev (intf); dev->intf = intf;spin_lock_init (&dev->rx_lock); +if (dev->span) + netdev->max_mtu =MCTP_USB_1_1_MTU_MAX ;usb_set_intfdata (intf, dev); rc =mctp_usblib_rx_init (&dev->rx,le16_to_cpu (ep_in->wMaxPacketSize), - false); + dev->span);if (rc) goto err_free_netdev; -mctp_usblib_tx_init (&dev->tx, &tx_ops, dev, false); +mctp_usblib_tx_init (&dev->tx, &tx_ops, dev, dev->span);init_usb_anchor (&dev->tx_anchor); dev->ep_in = ep_in->bEndpointAddress;
@@ -386,7 +401,8 @@ static void mctp_usb_disconnect(struct usb_interface *intf) }static const struct usb_device_id mctp_usb_devices[] = { - {USB_INTERFACE_INFO (USB_CLASS_MCTP ,0x0 ,0x1 ) }, + {USB_INTERFACE_INFO (USB_CLASS_MCTP ,MCTP_USB_SUBCLASS_BASE ,0x1 ) }, + {USB_INTERFACE_INFO (USB_CLASS_MCTP ,MCTP_USB_SUBCLASS_SPAN ,0x1 ) }, {0 }, };
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerPATCH v5 12/12 net: mctp: usb: Allow multiple urbs in flight
In reply to Jeremy KerrCurrently, we stop tx queues when we have one urb submitted. This means we will immediately hit dev_hard_start_xmit's tx-queues-off -> NETDEV_TX_BUSY case, and revert to the requeue -> gso_skb single-dequeue path, and no longer be able to pack skbs without an xmit_more indication.
Instead, allow a few urbs to be in-flight, with a limit of 16kB of data outstanding (after which we will disable queues). With this, the tx path will cause fewer requeues (and therefore non-packed transfers) under normal loads.
---
v2:
- rework tx_qmem locking, prevent race between completion and submission for stopping/waking the tx queues; perform the wake while locked
- only wake if netif_running()
--- drivers/net/mctp/mctp-usb.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
drivers/net/mctp/mctp-usb.c +29 -4
@@ -42,6 +42,9 @@ struct mctp_usb {struct mctp_usblib_tx tx;struct usb_anchor tx_anchor; +/* serialises tx_qmem updates to netdev queue states */ +spinlock_t tx_qmem_lock; +int tx_qmem; };enum {
@@ -49,23 +52,41 @@ enum { MCTP_USB_SUBCLASS_SPAN = 0x02, }; +/* We use a total-size limit for outstanding URBs, as the transfer counts +* may vary a lot between spanning- and non-spanning modes. In spanning mode, +* this will allow for a couple of max-sized transfers to be in flight. In +* non-spanning mode, 32. +* +* We want to avoid disabling the tx queue if possible; doing so will end up +* requeueing to gso_skb, and we only dequeue from that one skb at a time, +* so can no longer perform transfer packing. +*/ +static const unsigned int TX_QMEM_MAX =16384 ; +static void mctp_usb_out_complete (struct urb *urb) {struct mctp_usblib_tx_ctx *tx_ctx = urb->context;struct mctp_usb *mctp_usb =mctp_usblib_tx_ctx_priv (tx_ctx); +unsigned int len = urb->transfer_buffer_length;struct net_device *netdev = mctp_usb->netdev; +unsigned long flags;mctp_usblib_tx_send_complete (tx_ctx, netdev, urb->status ==0 );usb_free_urb (urb); -netif_wake_queue (netdev); +spin_lock_irqsave (&mctp_usb->tx_qmem_lock, flags); + mctp_usb->tx_qmem -= len; +if (mctp_usb->tx_qmem <TX_QMEM_MAX &&netif_running (netdev)) +netif_wake_queue (netdev); +spin_unlock_irqrestore (&mctp_usb->tx_qmem_lock, flags); }static int mctp_usb_tx_send (struct mctp_usblib_tx_ctx *tx_ctx,void *data,size_t len) {struct mctp_usb *mctp_usb =mctp_usblib_tx_ctx_priv (tx_ctx); +unsigned long flags;struct urb *urb;int rc;
@@ -80,8 +101,6 @@ static int mctp_usb_tx_send(struct mctp_usblib_tx_ctx *tx_ctx,if (mctp_usb->span) urb->transfer_flags |=URB_ZERO_PACKET ; -netif_stop_queue (mctp_usb->netdev); -usb_anchor_urb (urb, &mctp_usb->tx_anchor); rc =usb_submit_urb (urb,GFP_ATOMIC );
@@ -89,7 +108,12 @@ static int mctp_usb_tx_send(struct mctp_usblib_tx_ctx *tx_ctx,netdev_dbg (mctp_usb->netdev,"TX urb submit failed, %d\n" , rc);usb_unanchor_urb (urb);usb_free_urb (urb); -netif_start_queue (mctp_usb->netdev); + }else { +spin_lock_irqsave (&mctp_usb->tx_qmem_lock, flags); + mctp_usb->tx_qmem += len; +if (mctp_usb->tx_qmem >=TX_QMEM_MAX ) +netif_stop_queue (mctp_usb->netdev); +spin_unlock_irqrestore (&mctp_usb->tx_qmem_lock, flags); }return rc;
@@ -353,6 +377,7 @@ static int mctp_usb_probe(struct usb_interface *intf,spin_lock_init (&dev->rx_lock);if (dev->span) netdev->max_mtu =MCTP_USB_1_1_MTU_MAX ; +spin_lock_init (&dev->tx_qmem_lock);usb_set_intfdata (intf, dev); rc = mctp_usblib_rx_init(&dev->rx, le16_to_cpu(ep_in->wMaxPacketSize),
Signature · 1 line
2.47.3
Jeremy Kerr
maintainerHi all,
Some review feedback from sashiko, along with responses:
Quoted 18 lines
--- Patch [2]: [PATCH net-next v5 02/12] net: mctp: usb: Use packet-length max for maximum packet-size check --- commit
df3ce01edadf3abe9a5e04b1500317e960abd98f
Author: Jeremy Kerr <[email protected]>net: mctp: usb: Use packet-length max for maximum packet-size check
This commit updates the maximum packet size check in mctp-usb to use a new define based on the u8 transport header length field. It also applies this new maximum to the MTU calculation.
Quoted 8 lines
drivers/net/mctp/mctp-usb.c +0
-0@@ -76,7 +76,7 @@ static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb, plen = skb->len;
Quoted 8 lines
drivers/net/mctp/mctp-usb.c static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb, · line 82
Quoted 5 lines
drivers/net/mctp/mctp-usb.c +1
-1-if (plen +sizeof (*hdr) >MCTP_USB_1_0_XFER_SIZE ) +if (plen +sizeof (*hdr) >MCTP_USB_1_0_PKTLEN_MAX ) goto err_drop; rc =skb_cow_head (skb,sizeof (*hdr));This isn't a bug introduced by this patch, but could a transient USB error during TX permanently stall the network device's transmit queue?
This is a shortcoming in the existing tx path, and is addressed by patch 05/12.
[...]
Quoted 2 lines
This is also a pre-existing issue, but does the RX polling loop stop permanently on transient USB errors?
... and this by patch 04/12.
Quoted 35 lines
--- Patch [5]: [PATCH net-next v5 05/12] net: mctp: usblib: Move TX transfer processing to mctp-usblib --- commit
ee53d6679c1c34404c4b886475dd086d680df184
Author: Jeremy Kerr <[email protected]>net: mctp: usblib: Move TX transfer processing to mctp-usblib
This commit moves the TX processing alongside RX processing in mctp-usblib to accommodate packed transfers where a transfer may contain multiple MCTP packets. It splits the transmit API into push, send, and complete stages and fixes an inconsistency between tx and rx stats.
Quoted 3 lines
drivers/net/mctp/mctp-usblib.c +0
-0[ ... ]
Quoted 6 lines
drivers/net/mctp/mctp-usblib.c +6
-0+static int mctp_usblib_tx_skb_prepare (struct sk_buff *skb, +enum skb_drop_reason *reason) +{ +struct mctp_usb_hdr *hdr; +unsigned long plen; +int rc;[ ... ]
drivers/net/mctp/mctp-usblib.c
Quoted 9 lines
drivers/net/mctp/mctp-usblib.c +9
-0+ hdr =skb_push (skb,sizeof (*hdr)); +if (!hdr) { + *reason =SKB_DROP_REASON_NOMEM ; +return -ENOMEM ; + } + + hdr->id =cpu_to_be16 (MCTP_USB_DMTF_ID ); + hdr->rsvd =0 ; + hdr->len = plen +sizeof (*hdr);This isn't a bug introduced by this patch, but does this assignment miss a
cpu_to_be16()conversion?
Quoted 2 lines
The len field in struct
mctp_usb_hdris defined as__be16, but it appears to be assigned a native integer value here.
At this point in the series, hdr->len is a u8, not __be16.
We change this to a __be16 two patches later in 07/12, and at the same time add the necessary endian conversion to all uses of hdr->len.
Quoted 16 lines
--- Patch [10]: [PATCH net-next v5 10/12] net: mctp: usblib: Add initial kunit tests --- commit
6e78640ea8bf2b13bb32c513ae1e30a04bebf125
Author: Jeremy Kerr <[email protected]>net: mctp: usblib: Add initial kunit tests
This commit adds initial KUnit tests for the MCTP USB library receive path to verify extraction of MCTP packets from incoming USB transfer data.
Quoted 4 lines
drivers/net/mctp/mctp-usblib-test.c new file +0
-0[ ... ]
Quoted 11 lines
drivers/net/mctp/mctp-usblib-test.c
Quoted 9 lines
drivers/net/mctp/mctp-usblib-test.c new file +9
-0+static int mctp_usblib_test_dst_output (struct mctp_dst *dst, +struct sk_buff *skb) +{ +struct mctp_usblib_test_dev *dev =netdev_priv (skb->dev); + +skb_queue_tail (&dev->rx_pkts, skb); + +return 0 ; +}Could this lead to memory corruption if an external packet is routed here?
This is testcase code, so intentionally simplified, and typically running within the kunit harness. We should not be receiving or routing external packets at this point.
Cheers,
Jeremy
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 12 lines · On Fri, 24 Jul 2026 13:15:21 +0800 you wrote:
Version 1.1.0 of DSP0283 (MCTP over USB transport binding) has been released, this patch series updates our current v1.0.1 support for the changes in v1.1.x.
The major change in v1.1 is the introduction of "packet spanning" mode, where a single MCTP packet may be split over multiple USB packets (themselves forming a single USB bulk transfer). This relaxes the requirement for USB high-speed mode, as we can now send MCTP packets contained over multiple 64-byte full-speed USB bulk transfers, and gives us an increase in the maximum MCTP packet size - we now have 13 bits of packet length (previously 8) in the transport header.
Quoted 1 line
[...]
Here is the summary with links:
- [net-next,v5,01/12] net: mctp: usb: Include version indicator in max packet size defines
https://git.kernel.org/netdev/net-next/c/2f2e974f8fbe - [net-next,v5,02/12] net: mctp: usb: Use packet-length max for maximum packet-size check https://git.kernel.org/netdev/net-next/c/05b1a3a5eeaa - [net-next,v5,03/12] net: mctp: usblib: Move RX transfer processing to a new mctp-usblib https://git.kernel.org/netdev/net-next/c/b8564b19c0ff - [net-next,v5,04/12] net: mctp: usb: Improve IN endpoint status handling https://git.kernel.org/netdev/net-next/c/9f66be014070 - [net-next,v5,05/12] net: mctp: usblib: Move TX transfer processing to mctp-usblib https://git.kernel.org/netdev/net-next/c/aadf5ed03e85 - [net-next,v5,06/12] net: mctp: usblib: Add support for multi-packet transmit https://git.kernel.org/netdev/net-next/c/76a58ffd7417 - [net-next,v5,07/12] net: mctp: usb: Accommodate DSP0283 v1.1 header format https://git.kernel.org/netdev/net-next/c/f5bf226f3c7a - [net-next,v5,08/12] net: mctp: usblib: Implement receive-side packet spanning https://git.kernel.org/netdev/net-next/c/d52fc7f09a18 - [net-next,v5,09/12] net: mctp: usblib: Implement transmit-side packet spanning https://git.kernel.org/netdev/net-next/c/081ac93ca99c - [net-next,v5,10/12] net: mctp: usblib: Add initial kunit tests https://git.kernel.org/netdev/net-next/c/e84c0edbc41a - [net-next,v5,11/12] net: mctp: usb: enable v1.1 packet spanning https://git.kernel.org/netdev/net-next/c/b4a27a474b51 - [net-next,v5,12/12] net: mctp: usb: Allow multiple urbs in flight https://git.kernel.org/netdev/net-next/c/88dbfe0d9581
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html