Skip to contentSkip to conversation

mctp: Add MCTP-over-USB hardware transport binding

PATCH Superseded Jeremy Kerr 3 participants 9 messages Feb 12, 2025 – Feb 17, 2025

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jeremy Kerr

author maintainer
#0 lore raw

v2 cover mctp: Add MCTP-over-USB hardware transport binding

Add an implementation of the DMTF standard DSP0283, providing an MCTP channel over high-speed USB.

This is a fairly trivial first implementation, in that we only submit one tx and one rx URB at a time. We do accept multi-packet transfers, but do not yet generate them on transmit.

Of course, questions and comments are most welcome, particularly on the USB interfaces.

---
Changes in v2:
- greg k-h claims that it is 2025; update copyright year
- Add spec references
- Clean up dbg/warn output
- Changes from Oliver Neukum: drop usbdev ref, avoid a GFP_ATOMIC alloc
- Changes from Simon Horman: do rx stats before netif_rx
- Add module metadata
- specify phys binding type
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jeremy Kerr (2):
usb: Add base USB MCTP definitions
net: mctp: Add MCTP USB transport driver

 MAINTAINERS                  |   1 +
 drivers/net/mctp/Kconfig     |  10 ++
 drivers/net/mctp/Makefile    |   1 +
 drivers/net/mctp/mctp-usb.c  | 368 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/usb/mctp-usb.h |  30 ++++
 include/uapi/linux/usb/ch9.h |   1 +
 6 files changed, 411 insertions(+)

---
base-commit: be1d2a1b151deb195cd9749988163aa26ad6f616
change-id: 20250206-dev-mctp-usb-c59dea025395

Best regards,

Signature · 1 line
Jeremy Kerr <[email protected]>
author maintainer
#1 lore raw

PATCH v2 1/2 usb: Add base USB MCTP definitions

Upcoming changes will add a USB host (and later gadget) driver for the MCTP-over-USB protocol. Add a header that provides common definitions for protocol support: the packet header format and a few framing definitions. Add a define for the MCTP class code, as per
https://usb.org/defined-class-codes.

---
v2:
- add reference & URL to DSP0283
- update copyright year

---
 MAINTAINERS                  |  1 +
 include/linux/usb/mctp-usb.h | 30 ++++++++++++++++++++++++++++++
 include/uapi/linux/usb/ch9.h |  1 +
 3 files changed, 32 insertions(+)
MAINTAINERS +1 -0
index 67665d9dd536873e94afffc00393c2fe2e8c2797..e7b326dba9a9e6f50c3beeb172d93641841f6242 100644
@@ -13903,6 +13903,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/networking/mctp.rst
 F:	drivers/net/mctp/
+F:	include/linux/usb/mctp-usb.h
 F:	include/net/mctp.h
 F:	include/net/mctpdevice.h
 F:	include/net/netns/mctp.h
include/linux/usb/mctp-usb.h new file +30 -0
new file mode 100644
index 0000000000000000000000000000000000000000..b19392aa29310eda504f65bd098c849bd02dc0a1
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * 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
+ *
+ * These are protocol-level definitions, that may be shared between host
+ * and gadget drivers.
+ *
+ * Copyright (C) 2024-2025 Code Construct Pty Ltd
+ */
+
+#ifndef __LINUX_USB_MCTP_USB_H
+#define __LINUX_USB_MCTP_USB_H
+
+#include <linux/types.h>
+
+struct mctp_usb_hdr {
+	__be16	id;
+	__u8	rsvd;
+	__u8	len;
+} __packed;
+
+#define MCTP_USB_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_DMTF_ID	0x1ab4
+
+#endif /*  __LINUX_USB_MCTP_USB_H */
include/uapi/linux/usb/ch9.h +1 -0
index 91f0f7e214a5a57c8bee3f44c4dbf7b175843d8c..052290652046591fba46f1f0cb5cf77fd965f555 100644
@@ -330,6 +330,7 @@ struct usb_device_descriptor {
 #define USB_CLASS_AUDIO_VIDEO		0x10
 #define USB_CLASS_BILLBOARD		0x11
 #define USB_CLASS_USB_TYPE_C_BRIDGE	0x12
+#define USB_CLASS_MCTP			0x14
 #define USB_CLASS_MISC			0xef
 #define USB_CLASS_APP_SPEC		0xfe
 #define USB_SUBCLASS_DFU			0x01
Signature · 1 line
2.39.5
author maintainer
#6 lore raw

PATCH v2 2/2 net: mctp: Add MCTP USB transport driver

In reply to Jeremy Kerr

Add an implementation for DMTF DSP0283, which defines a MCTP-over-USB transport. As per that spec, we're restricted to full speed mode, requiring 512-byte transfers.

Each MCTP-over-USB interface is a peer-to-peer link to a single MCTP endpoint, so no physical addressing is required (of course, that MCTP endpoint may then bridge to further MCTP endpoints). Consequently, interfaces will report with no lladdr data:

    # mctp link
    dev lo index 1 address 00:00:00:00:00:00 net 1 mtu 65536 up
    dev mctpusb0 index 6 address none net 1 mtu 68 up

This is a simple initial implementation, with single rx & tx urbs, and no multi-packet tx transfers - although we do accept multi-packet rx from the device.

Includes suggested fixes from Santosh Puranik <[email protected]>.

Cc Santosh Puranik <[email protected]>

---
v2:
- update copyright year to 2025
- improve dbg messages: use netdev, demote from _err, remove __func__
- pass gfp_t to mctp_usb_rx_queue, allocate first as GFP_KERNEL
- release ->usbdev on disconnect
- include MODULE_{DESCRIPTION,AUTHOR,DEVICE_TABLE}
- more sensible driver->name
- do rx stats update before netif_rx()
- specify phys binding type via mctp_register_netdev()

---
 drivers/net/mctp/Kconfig    |  10 ++
 drivers/net/mctp/Makefile   |   1 +
 drivers/net/mctp/mctp-usb.c | 368 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 379 insertions(+)
drivers/net/mctp/Kconfig +10 -0
index 15860d6ac39fef62847d7186f1f0d81c1d3cd619..cf325ab0b1ef555e21983ace1b838e10c7f34570 100644
@@ -47,6 +47,16 @@ config MCTP_TRANSPORT_I3C
 	  A MCTP protocol network device is created for each I3C bus
 	  having a "mctp-controller" devicetree property.
 
+config MCTP_TRANSPORT_USB
+	tristate "MCTP USB transport"
+	depends on USB
+	help
+	  Provides a driver to access MCTP devices over USB transport,
+	  defined by DMTF specification DSP0283.
+
+	  MCTP-over-USB interfaces are peer-to-peer, so each interface
+	  represents a physical connection to one remote MCTP endpoint.
+
 endmenu
 
 endif
drivers/net/mctp/Makefile +1 -0
index e1cb99ced54ac136db0347a9ee0435a5ed938955..c36006849a1e7d04f2cafafb8931329fc0992b63 100644
@@ -1,3 +1,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
drivers/net/mctp/mctp-usb.c new file +368 -0

368 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer Acked-by on v2 1/2
#2 lore raw

Re: [PATCH net-next v2 1/2] usb: Add base USB MCTP definitions

In reply to Jeremy Kerr
Quoted 36 lines · Jeremy Kerr

Upcoming changes will add a USB host (and later gadget) driver for the MCTP-over-USB protocol. Add a header that provides common definitions for protocol support: the packet header format and a few framing definitions. Add a define for the MCTP class code, as per
https://usb.org/defined-class-codes.

---
v2:
- add reference & URL to DSP0283
- update copyright year

---
 MAINTAINERS                  |  1 +
 include/linux/usb/mctp-usb.h | 30 ++++++++++++++++++++++++++++++
 include/uapi/linux/usb/ch9.h |  1 +
 3 files changed, 32 insertions(+)
MAINTAINERS +1 -0
index 67665d9dd536873e94afffc00393c2fe2e8c2797..e7b326dba9a9e6f50c3beeb172d93641841f6242 100644
@@ -13903,6 +13903,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/networking/mctp.rst
 F:	drivers/net/mctp/
+F:	include/linux/usb/mctp-usb.h
 F:	include/net/mctp.h
 F:	include/net/mctpdevice.h
 F:	include/net/netns/mctp.h
include/linux/usb/mctp-usb.h new file +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..b19392aa29310eda504f65bd098c849bd02dc0a1

include/linux/usb/mctp-usb.h line 1

Quoted 1 line
include/linux/usb/mctp-usb.h new file +1 -0
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ */

I missed this the last time, sorry, but I have to ask, do you really mean v2 or later? If so, that's fine, just want to make sure.

Whichever you pick is fine with me, so:

maintainer
#5 lore raw

Re: [PATCH net-next v2 1/2] usb: Add base USB MCTP definitions

In reply to Jeremy Kerr

include/linux/usb/mctp-usb.h line 21

Quoted 2 lines · On Wed, 12 Feb 2025 10:46:50 +0800 Jeremy Kerr wrote:
include/linux/usb/mctp-usb.h new file +2 -0
+	__u8	rsvd;
+	__u8	len;

Since this is not a uAPI header u8 is preferred to __u8

maintainer
#7 lore raw

Re: [PATCH net-next v2 2/2] net: mctp: Add MCTP USB transport driver

In reply to Jeremy Kerr

drivers/net/mctp/mctp-usb.c line 29

Quoted 2 lines · On Wed, 12 Feb 2025 10:46:51 +0800 Jeremy Kerr wrote:
drivers/net/mctp/mctp-usb.c new file +2 -0
+	__u8 ep_in;
+	__u8 ep_out;

same nit about u8 as on the header

drivers/net/mctp/mctp-usb.c line 42

Quoted 12 lines
drivers/net/mctp/mctp-usb.c new file +12 -0
+	struct urb *tx_urb;
+	struct urb *rx_urb;
+};
+
+static void mctp_usb_stat_tx_dropped(struct net_device *dev)
+{
+	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
+
+	u64_stats_update_begin(&dstats->syncp);
+	u64_stats_inc(&dstats->tx_drops);
+	u64_stats_update_end(&dstats->syncp);
+}

Letter for letter dev_dstats_tx_dropped() ?

drivers/net/mctp/mctp-usb.c line 52

Quoted 9 lines
drivers/net/mctp/mctp-usb.c new file +9 -0
+static void mctp_usb_stat_tx_done(struct net_device *dev, unsigned int len)
+{
+	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
+
+	u64_stats_update_begin(&dstats->syncp);
+	u64_stats_inc(&dstats->tx_packets);
+	u64_stats_add(&dstats->tx_bytes, len);
+	u64_stats_update_end(&dstats->syncp);
+}

And this dev_dstats_tx_add() ?

Quoted 5 lines
drivers/net/mctp/mctp-usb.c new file +5 -0
+static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb,
+				       struct net_device *dev)
+{
+	struct mctp_usb *mctp_usb = netdev_priv(dev);
+	struct mctp_usb_hdr *hdr;

drivers/net/mctp/mctp-usb.c line 96

Quoted 10 lines
drivers/net/mctp/mctp-usb.c new file +10 -0
+	unsigned int plen;
+	struct urb *urb;
+	int rc;
+
+	plen = skb->len;
+
+	if (plen + sizeof(*hdr) > MCTP_USB_XFER_SIZE)
+		goto err_drop;
+
+	hdr = skb_push(skb, sizeof(*hdr));

Hm, I guess MCTP may have its own rules but technically you should call skb_cow_head() before you start writing to the header buffer.

Quoted 12 lines
drivers/net/mctp/mctp-usb.c new file +11 -0
+	if (!hdr)
+		goto err_drop;
+
+	hdr->id = cpu_to_be16(MCTP_USB_DMTF_ID);
+	hdr->rsvd = 0;
+	hdr->len = plen + sizeof(*hdr);
 
+static void mctp_usb_in_complete(struct urb *urb)
+{
+	struct sk_buff *skb = urb->context;
+	struct net_device *netdev = skb->dev;
+	struct pcpu_dstats *dstats = this_cpu_ptr(netdev->dstats);

drivers/net/mctp/mctp-usb.c

Quoted 10 lines
drivers/net/mctp/mctp-usb.c new file +9 -0
+	struct mctp_usb *mctp_usb = netdev_priv(netdev);
+	struct mctp_skb_cb *cb;
+	unsigned int len;
+	int status;
+
 
+		u64_stats_update_begin(&dstats->syncp);
+		u64_stats_inc(&dstats->rx_packets);
+		u64_stats_add(&dstats->rx_bytes, skb->len);
+		u64_stats_update_end(&dstats->syncp);

dev_dstats_rx_add()

drivers/net/mctp/mctp-usb.c line 242

Quoted 13 lines
drivers/net/mctp/mctp-usb.c new file +13 -0
+		skb->protocol = htons(ETH_P_MCTP);
+		skb_reset_network_header(skb);
+		cb = __mctp_cb(skb);
+		cb->halen = 0;
+		netif_rx(skb);
+
+		skb = skb2;
+	}
+
+	if (skb)
+		kfree_skb(skb);
+
+	mctp_usb_rx_queue(mctp_usb, GFP_ATOMIC);

What if we fail to allocate an skb ?
Admittedly the buffers are relatively small but if the allocation fails we'd get stuck, no more packets will ever be received, right? May be safer to allocate the skb first, and if it fails reuse the skb that just completed (effectively discarding the incoming packets until a replacement buffer can be allocated).

drivers/net/mctp/mctp-usb.c line 282

Quoted 8 lines
drivers/net/mctp/mctp-usb.c new file +7 -0
+}
 
+	dev->hard_header_len = sizeof(struct mctp_usb_hdr);
+	dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
+	dev->addr_len = 0;
+	dev->flags = IFF_NOARP;
+	dev->netdev_ops = &mctp_usb_netdev_ops;
+	dev->needs_free_netdev = false;

Is there a reason to set this to false?
dev memory is guaranteed to be zero'ed out.

Quoted 2 lines
(quoted diff) +2 -0
+	dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
+}
Signature · 1 line
pw-bot: cr
author maintainer
#8 lore raw

Re: [PATCH net-next v2 2/2] net: mctp: Add MCTP USB transport driver

Hi Jakub,

Thanks for the review. Comments inline.

Quoted 4 lines
Quoted 2 lines

+       __u8 ep_in;
+       __u8 ep_out;

same nit about u8 as on the header

Ack, have changed, as well as on the header.

Quoted 1 line

Letter for letter dev_dstats_tx_dropped() ?

[...]

Quoted 1 line

And this dev_dstats_tx_add() ?

[...]

Quoted 1 line

dev_dstats_rx_add()

Neat, thanks!

Quoted 10 lines
Quoted 10 lines
drivers/net/mctp/mctp-usb.c new file +10 -0
+static netdev_tx_t mctp_usb_start_xmit(struct sk_buff *skb,
+                                      struct net_device *dev)
+{
+       struct mctp_usb *mctp_usb = netdev_priv(dev);
+       struct mctp_usb_hdr *hdr;
+       unsigned int plen;
+       struct urb *urb;
+       int rc;
+
+       plen = skb->len;
Quoted 8 lines

drivers/net/mctp/mctp-usb.c line 96

Quoted 5 lines
drivers/net/mctp/mctp-usb.c new file +5 -0
+
+       if (plen + sizeof(*hdr) > MCTP_USB_XFER_SIZE)
+               goto err_drop;
+
+       hdr = skb_push(skb, sizeof(*hdr));

Hm, I guess MCTP may have its own rules but technically you should call skb_cow_head() before you start writing to the header buffer.

We currently have ensured that we have headroom when the skb had been allocated. However, things will get a bit more complex when we introduce proper forwarding, so I will add the skb_cow_head() for v3 (and introduce it on the other drivers as we go...)

Quoted 11 lines
Quoted 4 lines

+       if (skb)
+               kfree_skb(skb);
+
+       mctp_usb_rx_queue(mctp_usb, GFP_ATOMIC);

What if we fail to allocate an skb ?
Admittedly the buffers are relatively small but if the allocation fails we'd get stuck, no more packets will ever be received, right? May be safer to allocate the skb first, and if it fails reuse the skb that just completed (effectively discarding the incoming packets until a replacement buffer can be allocated).

I think we can do a little better, and defer retries to a non-atomic context instead. This means we have a chance of flow control over the IN transfers from the device too, rather than dropping everything.

I'll implement that for v3.

Quoted 9 lines
Quoted 6 lines

+       dev->hard_header_len = sizeof(struct mctp_usb_hdr); +       dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; +       dev->addr_len = 0;
+       dev->flags = IFF_NOARP;
+       dev->netdev_ops = &mctp_usb_netdev_ops;
+       dev->needs_free_netdev = false;

Is there a reason to set this to false?
dev memory is guaranteed to be zero'ed out.

.. only because I had previously had it as true before the usb disconnect was implemented. With that change, I had decided to not remove it with the justification that it's a little more clear that we need to do our own free after unregister.

Happy to make this more conventional though, so will remove (as well as the addr_len assignment) in v3.

Cheers,

Jeremy

author maintainer
#3 lore raw

Re: [PATCH net-next v2 1/2] usb: Add base USB MCTP definitions

In reply to Greg Kroah-Hartman

Hi Greg,

Quoted 7 lines

include/linux/usb/mctp-usb.h line 1

Quoted 4 lines
include/linux/usb/mctp-usb.h new file +1 -0
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ */

I missed this the last time, sorry, but I have to ask, do you really mean v2 or later?  If so, that's fine, just want to make sure.

I'm fine with 2.0+, but I figure the preference is consistency here. So, since I'm doing a v3, I will send that out with GPL-2.0.

Quoted 3 lines

Whichever you pick is fine with me, so:

Thanks! v3 will have the __u8s changed to u8, as Jakub has requested. Would you like me to keep the Ack on that?

Cheers,

Jeremy

#4 lore raw

Re: [PATCH net-next v2 1/2] usb: Add base USB MCTP definitions

Quoted 13 lines · Jeremy Kerr

Hi Greg,

Quoted 7 lines

include/linux/usb/mctp-usb.h line 1

Quoted 4 lines
include/linux/usb/mctp-usb.h new file +1 -0
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0+ */

I missed this the last time, sorry, but I have to ask, do you really mean v2 or later?  If so, that's fine, just want to make sure.

I'm fine with 2.0+, but I figure the preference is consistency here. So, since I'm doing a v3, I will send that out with GPL-2.0.

Quoted 6 lines
Quoted 3 lines

Whichever you pick is fine with me, so:

Thanks! v3 will have the __u8s changed to u8, as Jakub has requested. Would you like me to keep the Ack on that?

Yes, that's fine. But to be fair, "__u8" is correct here as that's what the endpoint variable is as well (it is coming directly from hardware), but I'm not going to complain about that as it really doesn't matter :)

thanks,

greg k-h