Skip to contentSkip to conversation

usb: core: Pair ports of different host controllers through their connector

PATCH Superseded Arun Iyengar 2 participants 5 messages Nov 12 USB

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover usb: core: Pair ports of different host controllers through their connector

The USB core pairs the USB 2.0 and SuperSpeed halves of a USB 3 port only within one xHCI (shared_hcd, same port number) or through ACPI location data. Some SoCs route the two halves of a connector to different host controllers: on the Rockchip RK3588, USB 3.0 host 2 is SuperSpeed-only (its own USB 2.0 root hub port is not wired to a PHY) and the connector's USB 2.0 lines go to a separate EHCI/OHCI pair. Today the SuperSpeed port is paired with the dead USB 2.0 port of its own xHCI, and the EHCI port that really shares the connector has no peer.

That is more than a cosmetic problem on boards where VBUS of such a port is always on. On the Orange Pi 5 the Type-A USB 3 port has no VBUS switch at all (no enable GPIO in the vendor or mainline device tree; confirmed on the board), so a device that is plugged in across power-up or a reboot is powered long before the kernel runs. It looks for a SuperSpeed link while the xHCI is still down, gives up, and connects at high speed to the EHCI, which starts ~10 ms before the xHCI. It then stays at 480 Mb/s until it is unplugged: USB 3 devices only look for a SuperSpeed link again after a VBUS cycle or a USB 2.0 bus reset.

The series lets the USB core use a description of the connector in the device tree:

1. usb: core: when a port's graph endpoint leads to a usb-{a,b,c} connector whose other half ends on a different hub or host controller, pair the two ports through the connector and keep them out of the default pairing. Connectors whose halves end on the same node (Type-C on a dwc3) are unchanged.
2. usb: hub: when a SuperSpeed root hub whose ports are paired through a connector with USB 2.0 root hub ports of another host controller starts, reset a SuperSpeed-capable device (not a hub) that sits at high speed on the USB 2.0 peer, once, so that it can train its SuperSpeed link now that the host is up. Resume and reset of the root hub don't do this. A device that still does not train stays at high speed; there is no loop.

The Orange Pi 5/5B device tree that describes its Type-A USB 3 connector this way was patch 4 of v1; it now goes separately to the Rockchip tree as "arm64: dts: rockchip: Describe the USB3 Type-A connector on Orange Pi 5/5B". Other RK3588 boards with the same split would need the same node.

That DT patch links an EHCI root hub port to the connector through a "ports" node on the EHCI. v1 added that node to generic-ehci.yaml in its patch 1; v2 relies instead on Chen-Yu Tsai's "dt-bindings:
usb: hcd: Add downstream USB ports", which adds "ports" to usb-hcd.yaml for all host controllers [1]. It is not merged yet. The two patches here don't need it to build or run; only the separate DT patch needs it to pass dtbs_check.

Port numbers: patch 1 finds a root hub port in the graph by its root hub port number, as usb_of_get_connect_type() does today, and not by the xHCI port number. On an EHCI the two are the same. On the SuperSpeed-only xHCI of the RK3588, a dwc3, the SuperSpeed root hub port 1 is graph port@1, which is also where snps,dwc3-common.yaml puts the SuperSpeed port today. Chen-Yu has said that the graph port of a root hub port should be found by the xHCI port number (usb_hcd_find_raw_port_number()) [2], and patch 2 of his series renumbers the dwc3 ports to match. If that is the way to go, patch 1 has to translate the numbers on both sides of the pairing. I would like to hear which numbering is wanted before I change it.

Honest limits: patch 2 only helps devices that retry SuperSpeed after a USB 2.0 bus reset. A SanDisk Extreme does; a SanDisk Ultra Fit does not and stays at high speed after the single reset. The hardware fix (a switchable VBUS, cycled once the SuperSpeed host is up) is not available on this board. The pairing itself (patch 1) also makes the existing peer runtime-PM rules apply to such connectors.

v1 was tested on an Orange Pi 5 (RK3588S): with the series the EHCI root hub port 1 and the xHCI SuperSpeed port 1 show each other as "peer" in sysfs. A SanDisk Extreme present at boot enumerates at high speed on the EHCI at ~3.2 s, "SuperSpeed peer port available, resetting" is logged once, and it re-enumerates at 5 Gb/s on the xHCI at ~3.8 s, on every boot; without the series (two different kernels) it came up at 480 Mb/s on the EHCI on every boot and stayed there until replugged. A SanDisk Ultra Fit that ignores the USB 2.0 reset stays at high speed after the one reset. Hot-plug after boot (5 Gb/s), the Type-C port (both halves on the same dwc3 node) and USB 2.0-only devices in the port behave as before.

v2 was tested on the same Orange Pi 5 with the SanDisk Extreme in the
port: on 4 of 4 boots it ended at 5 Gb/s on the xHCI with the EHCI port paired (3 of them logged the one peer reset), no WARN or BUG. Root hubs runtime-suspend as without the series. 5 s2idle cycles with the bus kept and 3 in which the root hubs lost power: no peer reset on resume, no device lost. A logical replug, 2 xHCI unbind/bind cycles and 10 unbind/bind races of the xHCI and of the hub driver at different delays: no hang, no WARN, the device back at 5 Gb/s each time. Not tested: an external USB 3 hub in the port (none at hand). With Chen-Yu's binding patch applied, CHECK_DTBS for the Orange Pi 5 and 5B with the separate DT patch shows no new warnings.

All board tests ran on a v7.3 based kernel with these patches and the DT patch applied. On v7.4-rc1 the series is build-tested only.

This series was developed with Claude (Anthropic) as a coding assistant:
it wrote the code and the commit messages, and I reviewed and tested every change as described above.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CAGXv+5GdBUafk6niG-UMDx=z6LJQ6iHAWhDpm1Ly2ce6CQDaxg@mail.gmail.com/

---
Changes in v2:
- Dropped v1 patch 1 "dt-bindings: usb: generic-ehci: Allow describing
the root hub ports": Chen-Yu Tsai's "ports" binding in usb-hcd.yaml
covers all host controllers, EHCI included, and Rob Herring's comment
on it about the port numbers applied to ours too (Chen-Yu Tsai, Rob Herring). v1 patches 2 and 3 are now patches 1 and 2.
- Patch 2: reset the USB 2.0 peer only when the SuperSpeed root hub
starts, not on its reset-resume or after a reset: a device that is suspended with the system can't be reset (Sashiko)
- Patch 2: never reset a hub, and only look at USB 2.0 root hub ports
of another host controller: the USB 2.0 half of a USB 3 hub stays at
high speed, and resetting it would disconnect its children (Sashiko)
- Patch 2: cancel the work in hub_quiesce() (Sashiko) and hold a
runtime PM reference on the hub while it is queued (Sashiko); run it
on the freezable hub_wq
- Patch 1 unchanged
- v1 patch 4 (Orange Pi 5/5B DTS) split out of the series; it goes to
the Rockchip tree on its own (Krzysztof Kozlowski)
- Link to v1: https://patch.msgid.link/[email protected]

---
Arun Iyengar (2):
usb: core: Pair ports of different hubs through their connector
usb: hub: Retry SuperSpeed for a device stuck on its USB 2.0 peer

 drivers/usb/core/hub.c  | 113 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/hub.h  |   3 ++
 drivers/usb/core/of.c   | 101 +++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/port.c |  54 +++++++++++++++++++++--
 include/linux/usb/of.h  |   9 ++++
 5 files changed, 276 insertions(+), 4 deletions(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261110-usb-port-pairing-af0f66f0ab2e

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw

PATCH v2 1/2 usb: core: Pair ports of different hubs through their connector

A USB 3 port is two ports to the USB core, a USB 2.0 one and a SuperSpeed one, and the core pairs them as peers. Root hub ports are paired with the same port number of the other root hub of the same xHCI, unless ACPI gives their locations.

Some SoCs route the two halves of a connector to different host controllers. On the Rockchip RK3588, USB 3.0 host 2 is SuperSpeed-only:
its own USB 2.0 root hub port is not wired to a PHY, and the connector's USB 2.0 lines go to a separate EHCI instead. Default pairing then joins the SuperSpeed port to a dead USB 2.0 port, and nothing knows about the EHCI port that really shares the connector.

A usb-connector node describes its USB 2.0 data lines as graph port 0 and its SuperSpeed lanes as graph port 1. When a hub's or host controller's graph links one of its ports to a connector whose other half leads to a different hub or host controller, pair the port with that one instead, and keep it out of the default pairing. Connectors whose two halves end on the same node, like a Type-C connector on a DWC3, keep the default pairing.

This lets the runtime PM rules for peers (keep the SuperSpeed port powered while its USB 2.0 peer is) apply to such connectors too.

---
 drivers/usb/core/hub.h  |   2 +
 drivers/usb/core/of.c   | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/port.c |  54 ++++++++++++++++++++++++--
 include/linux/usb/of.h  |   9 +++++
 4 files changed, 162 insertions(+), 4 deletions(-)
drivers/usb/core/hub.h +2 -0
index 9ebc5ef54a32..78382839be48 100644
@@ -97,6 +97,7 @@ struct usb_hub {
  * @usb3_lpm_u2_permit: whether USB3 U2 LPM is permitted.
  * @early_stop: whether port initialization will be stopped earlier.
  * @ignore_event: whether events of the port are ignored.
+ * @connector_peer: @peer is on another hub, paired through a connector.
  */
 struct usb_port {
 	struct usb_device *child;
@@ -118,6 +119,7 @@ struct usb_port {
 	unsigned int is_superspeed:1;
 	unsigned int usb3_lpm_u1_permit:1;
 	unsigned int usb3_lpm_u2_permit:1;
+	unsigned int connector_peer:1;
 };
 
 #define to_usb_port(_dev) \
drivers/usb/core/of.c +101 -0

107 lines not on this page · raw message

drivers/usb/core/port.c +50 -4

80 lines not on this page · raw message

include/linux/usb/of.h +9 -0
index de42f14bd280..cfcfa26e7c69 100644
@@ -20,6 +20,9 @@ int of_usb_update_otg_caps(struct device_node *np,
 			struct usb_otg_caps *otg_caps);
 enum usb_port_connect_type usb_of_get_connect_type(struct usb_device *hub, int port1);
 struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1);
+struct device_node *usb_of_get_connector_peer(struct usb_device *hub,
+					      int port1, bool superspeed,
+					      u32 *peer_port1);
 bool usb_of_has_combined_node(struct usb_device *udev);
 struct device_node *usb_of_get_interface_node(struct usb_device *udev,
 		u8 config, u8 ifnum);
@@ -49,6 +52,12 @@ usb_of_get_device_node(struct usb_device *hub, int port1)
 {
 	return NULL;
 }
+static inline struct device_node *
+usb_of_get_connector_peer(struct usb_device *hub, int port1, bool superspeed,
+			  u32 *peer_port1)
+{
+	return NULL;
+}
 static inline bool usb_of_has_combined_node(struct usb_device *udev)
 {
 	return false;
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/2] usb: core: Pair ports of different hubs through their connector

Sashiko has reviewed this patch and found no issues.
Quoted 22 lines

From: Arun Iyengar <[email protected]>

A USB 3 port is two ports to the USB core, a USB 2.0 one and a SuperSpeed one, and the core pairs them as peers. Root hub ports are paired with the same port number of the other root hub of the same xHCI, unless ACPI gives their locations.

Some SoCs route the two halves of a connector to different host controllers. On the Rockchip RK3588, USB 3.0 host 2 is SuperSpeed-only:
its own USB 2.0 root hub port is not wired to a PHY, and the connector's USB 2.0 lines go to a separate EHCI instead. Default pairing then joins the SuperSpeed port to a dead USB 2.0 port, and nothing knows about the EHCI port that really shares the connector.

A usb-connector node describes its USB 2.0 data lines as graph port 0 and its SuperSpeed lanes as graph port 1. When a hub's or host controller's graph links one of its ports to a connector whose other half leads to a different hub or host controller, pair the port with that one instead, and keep it out of the default pairing. Connectors whose two halves end on the same node, like a Type-C connector on a DWC3, keep the default pairing.

Quoted 4 lines

This lets the runtime PM rules for peers (keep the SuperSpeed port powered while its USB 2.0 peer is) apply to such connectors too.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/2 usb: hub: Retry SuperSpeed for a device stuck on its USB 2.0 peer

In reply to Arun Iyengar

A SuperSpeed device looks for a SuperSpeed link first when it sees VBUS, and connects at high speed when the link doesn't come up. It looks again only when VBUS is cycled or after a USB 2.0 bus reset.

When a connector's two halves belong to different host controllers, the USB 2.0 host can start first. On the Orange Pi 5 (RK3588S), whose USB 3 Type-A port has VBUS on from power-up, a USB 3 flash drive plugged in at boot was reset by the EHCI about 10 ms before the SuperSpeed-only xHCI registered, so it came up at 480 Mb/s on the EHCI in every one of 6 boots, while plugging it in after boot gave 5 Gb/s. It stays at high speed until it is unplugged.

When a SuperSpeed root hub with ports paired through a connector to a root hub port of another host controller has started, check those USB 2.0 peers. If a SuperSpeed-capable device that is not a hub is connected at high speed there, reset it once, so that it can connect at SuperSpeed now that the link can train. Lock the peer root hub first, so an enumeration that is under way completes before the check. A device that still can't train its SuperSpeed link stays connected at high speed after that one reset.

Only the start of the root hub does this, not a resume or a reset of
it: a device that is suspended with the system can't be reset, and the problem is the start-up order of the two controllers. Hubs are left alone, because the USB 2.0 half of a USB 3 hub stays at high speed next to its SuperSpeed half. The work runs on the freezable hub_wq, holds a runtime PM reference on the SuperSpeed hub and is cancelled when the hub is quiesced.

---
 drivers/usb/core/hub.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/hub.h |   1 +
 2 files changed, 114 insertions(+)
drivers/usb/core/hub.c +113 -0
index 199d3f535a89..eaa2a5b6aba0 100644
@@ -1076,6 +1076,109 @@ int usb_remove_device(struct usb_device *udev)
 	return 0;
 }
 
+/*
+ * A SuperSpeed root hub port can be paired through a connector with a USB 2.0
+ * root hub port of another host controller. With VBUS always on, a device
+ * present at boot looks for a SuperSpeed link while the SuperSpeed host is
+ * still down, gives up and connects at high speed to the USB 2.0 host. It
+ * only tries SuperSpeed again after VBUS is cycled or after a USB 2.0 bus
+ * reset, so reset it once when the SuperSpeed root hub has started.
+ */
+static void hub_reset_peer_child(struct usb_device *peer_hdev, int peer_port1)
+{
+	struct usb_hub *peer_hub;
+	struct usb_device *udev;
+
+	/* Wait for an enumeration in progress on the peer port to finish */
+	usb_lock_device(peer_hdev);
+	peer_hub = usb_hub_to_struct_hub(peer_hdev);
+	if (!peer_hub || peer_port1 > peer_hdev->maxchild)
+		goto out;
+
+	udev = peer_hub->ports[peer_port1 - 1]->child;
+	if (!udev || udev->speed != USB_SPEED_HIGH)
+		goto out;
+
+	/*
+	 * The USB 2.0 half of a USB 3 hub stays at high speed next to its
+	 * SuperSpeed half, and resetting it would disconnect its children.
+	 */
+	if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
+		goto out;
+
+	usb_lock_device(udev);
+	if (udev->state != USB_STATE_NOTATTACHED &&
+	    udev->bos && udev->bos->ss_cap) {
+		dev_info(&udev->dev,
+			 "SuperSpeed peer port available, resetting\n");
+		usb_reset_device(udev);
+	}
+	usb_unlock_device(udev);
+out:
+	usb_unlock_device(peer_hdev);
+}
+
+static void hub_peer_reset_work(struct work_struct *ws)
+{
+	struct usb_hub *hub = container_of(ws, struct usb_hub, peer_reset_work);
+	struct usb_device *hdev = hub->hdev;
+	int port1;
+
+	for (port1 = 1; ; port1++) {
+		struct usb_device *peer_hdev = NULL;
+		struct usb_port *port_dev;
+		int peer_port1 = 0;
+
+		mutex_lock(&usb_port_peer_mutex);
+		if (port1 > hdev->maxchild) {
+			mutex_unlock(&usb_port_peer_mutex);
+			break;
+		}
+		port_dev = hub->ports[port1 - 1];
+		if (port_dev->connector_peer && port_dev->peer) {
+			peer_hdev = to_usb_device(port_dev->peer->dev.parent->parent);
+			peer_port1 = port_dev->peer->portnum;
+			/* Only root hub ports of another host controller */
+			if (peer_hdev->parent ||
+			    peer_hdev->bus->controller == hdev->bus->controller)
+				peer_hdev = NULL;
+			else
+				usb_get_dev(peer_hdev);
+		}
+		mutex_unlock(&usb_port_peer_mutex);
+
+		if (peer_hdev) {
+			hub_reset_peer_child(peer_hdev, peer_port1);
+			usb_put_dev(peer_hdev);
+		}
+	}
+
+	usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
+	hub_put(hub);
+}
+
+/* Called when a SuperSpeed root hub has started */
+static void hub_queue_peer_reset(struct usb_hub *hub)
+{
+	struct usb_interface *intf = to_usb_interface(hub->intfdev);
+	int port1;
+
+	for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) {
+		if (hub->ports[port1 - 1]->connector_peer)
+			break;
+	}
+	if (port1 > hub->hdev->maxchild)
+		return;
+
+	/* Keep the hub active until the work is done; hub_quiesce() cancels it */
+	usb_autopm_get_interface_no_resume(intf);
+	hub_get(hub);
+	if (!queue_work(hub_wq, &hub->peer_reset_work)) {
+		usb_autopm_put_interface_async(intf);
+		hub_put(hub);
+	}
+}
+
 enum hub_activation_type {
 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
@@ -1346,6 +1449,11 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
 
 	/* Scan all ports that need attention */
 	kick_hub_wq(hub);
+
+	/* Let devices stuck at high speed on a connector peer move over */
+	if (!hdev->parent && hub_is_superspeed(hdev) &&
+	    (type == HUB_INIT2 || type == HUB_INIT3))
+		hub_queue_peer_reset(hub);
  abort:
 	if (type == HUB_INIT2 || type == HUB_INIT3) {
 		/* Allow autosuspend if it was suppressed */
@@ -1419,6 +1527,10 @@ static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
 	/* Stop hub_wq and related activity */
 	timer_delete_sync(&hub->irq_urb_retry);
 	flush_delayed_work(&hub->post_resume_work);
+	if (cancel_work_sync(&hub->peer_reset_work)) {
+		usb_autopm_put_interface_no_suspend(to_usb_interface(hub->intfdev));
+		hub_put(hub);
+	}
 	usb_kill_urb(hub->urb);
 	if (hub->has_indicators)
 		cancel_delayed_work_sync(&hub->leds);
@@ -1978,6 +2090,7 @@ static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
 	INIT_DELAYED_WORK(&hub->leds, led_work);
 	INIT_DELAYED_WORK(&hub->init_work, NULL);
 	INIT_DELAYED_WORK(&hub->post_resume_work, hub_post_resume);
+	INIT_WORK(&hub->peer_reset_work, hub_peer_reset_work);
 	INIT_WORK(&hub->events, hub_event);
 	INIT_LIST_HEAD(&hub->onboard_devs);
 	spin_lock_init(&hub->irq_urb_lock);
drivers/usb/core/hub.h +1 -0
index 78382839be48..edce19d7bf13 100644
@@ -71,6 +71,7 @@ struct usb_hub {
 	struct delayed_work	leds;
 	struct delayed_work	init_work;
 	struct delayed_work	post_resume_work;
+	struct work_struct	peer_reset_work;
 	struct work_struct      events;
 	spinlock_t		irq_urb_lock;
 	struct timer_list	irq_urb_retry;
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH v2 2/2] usb: hub: Retry SuperSpeed for a device stuck on its USB 2.0 peer

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

From: Arun Iyengar <[email protected]>

A SuperSpeed device looks for a SuperSpeed link first when it sees VBUS, and connects at high speed when the link doesn't come up. It looks again only when VBUS is cycled or after a USB 2.0 bus reset.

When a connector's two halves belong to different host controllers, the USB 2.0 host can start first. On the Orange Pi 5 (RK3588S), whose USB 3 Type-A port has VBUS on from power-up, a USB 3 flash drive plugged in at boot was reset by the EHCI about 10 ms before the SuperSpeed-only xHCI registered, so it came up at 480 Mb/s on the EHCI in every one of 6 boots, while plugging it in after boot gave 5 Gb/s. It stays at high speed until it is unplugged.

Quoted 7 lines

When a SuperSpeed root hub with ports paired through a connector to a [ ... ]
to its SuperSpeed half. The work runs on the freezable hub_wq, holds a runtime PM reference on the SuperSpeed hub and is cancelled when the hub is quiesced.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2