Procházet zdrojové kódy

ehci: hand every non-high-speed device to the companion

After a port reset an unenabled port means the device is not high-speed
(EHCI 4.2.2), whatever the line state reads.  The driver only handed the
port to the companion on a K state; the ION's MCP79 reads SE0 there for
a full-speed keyboard/mouse receiver, so the port was treated as a stuck
reset and the whole controller was reset.  That re-asserted CONFIGFLAG,
taking every root port back from the OHCI companions, and busy-waited
~2.5 s, which the hotplug watcher repeated forever: no USB input, and
netmon drained one line per retry.

Now a low-speed device (K before the reset) goes to the companion at
once, an unenabled port after the reset always does, handed-off ports
are remembered in cc_ports so a later scan does not take them back, and
the controller-reset retry is only used on controllers without
companions (HCSPARAMS N_CC 0, the Intel PCH case it was written for).

QEMU's EHCI accepts full-speed devices directly, so the handoff is not
reproducible there; the USB regression tests pass and the ION's
keyboard and mouse work again.
Johannes Zellner před 2 dny
rodič
revize
a218af1968
3 změnil soubory, kde provedl 50 přidání a 8 odebrání
  1. 7 1
      AGENTS.md
  2. 3 1
      docs/drivers.html
  3. 40 6
      kernel/ehci.c

+ 7 - 1
AGENTS.md

@@ -2306,7 +2306,13 @@ briefly reporting disconnected.
   not used.  Polling only (no IRQs).  Ports are claimed from the companions
   with CONFIGFLAG; `port_reset` holds PORT_RESET for 50 ms, then clears it
   (QEMU and hardware complete the reset on that write), waits for PED and
-  sets PORT_OWNER for a non-high-speed device.  `ehci.c` supports up to four
+  sets PORT_OWNER for a non-high-speed device: a K line state before the
+  reset (low speed), or no PED after it whatever the line state (EHCI 4.2.2;
+  the ION's MCP79 reads SE0 there for a full-speed receiver, which used to
+  loop through controller resets that also stole the companions' ports).
+  Handed-off ports are remembered (`cc_ports`) so a later scan does not take
+  them back; the stuck-reset controller-reset retry is only for controllers
+  without companions (HCSPARAMS N_CC 0).  `ehci.c` supports up to four
   PCI instances.  The async schedule is enabled only while a control/bulk
   transfer or an armed interrupt endpoint needs it (`ehci_sched_get/put`);
   with nothing attached the controller stops fetching QHs and qTDs, which

+ 3 - 1
docs/drivers.html

@@ -275,7 +275,9 @@
 			<figcaption>
 				<strong>Companions are not optional.</strong> EHCI is scanned first
 				so its port resets can hand non-high-speed devices back to a
-				companion. There is no transaction translator: a low/full-speed
+				companion: a K line state before the reset, or no port-enable
+				after it (whatever the line state reads), means the device is
+				not high-speed. There is no transaction translator: a low/full-speed
 				device behind a high-speed hub is invisible to EHCI.
 			</figcaption>
 		</figure>

+ 40 - 6
kernel/ehci.c

@@ -172,6 +172,7 @@ struct ehci_ctrl {
 	unsigned int n_ports;
 	int ppc;
 	int last_port;
+	unsigned int cc_ports;  /* ports handed to a companion (bitmask) */
 	unsigned int hcs;
 	unsigned int hcc;
 
@@ -857,6 +858,27 @@ static int ehci_port_take_change(unsigned int ctrl, unsigned int port)
 	return 1;
 }
 
+/* HCSPARAMS N_CC: the number of companion controllers. */
+static unsigned int n_companions(const struct ehci_ctrl *c)
+{
+	return (c->hcs >> 12) & 0x0Fu;
+}
+
+/* Give a non-high-speed device to the companion controller. */
+static void port_to_companion(struct ehci_ctrl *c, unsigned int port,
+			      unsigned int ps)
+{
+	wr32(port_reg(c, port),
+	     (ps & ~(PORT_RWC | PORT_PE | PORT_RESET)) | PORT_OWNER |
+	     PORT_CSC | PORT_PEC | PORT_OCC);
+	if (!(c->cc_ports & (1u << port))) {
+		c->cc_ports |= 1u << port;
+		serial_puts(SERIAL_COM1, "ehci: port ");
+		serial_putdec(SERIAL_COM1, port + 1);
+		serial_puts(SERIAL_COM1, " not high-speed, handed to companion\n");
+	}
+}
+
 static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 {
 	struct ehci_ctrl *c;
@@ -895,8 +917,11 @@ static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 	   low/full-speed device belongs there; a high-speed one can be taken
 	   over by clearing PORT_OWNER (the controller may need more than one
 	   write while the port is enabled). */
+	/* The hardware returns a port to the EHCI when its device goes away. */
+	if (!(ps & PORT_OWNER))
+		c->cc_ports &= ~(1u << port);
 	if (ps & PORT_OWNER) {
-		if (PORT_USB11(ps))
+		if (PORT_USB11(ps) || (c->cc_ports & (1u << port)))
 			return -1;
 		for (try = 0; try < 8 && (ps & PORT_OWNER); try++) {
 			wr32(port_reg(c, port),
@@ -910,6 +935,13 @@ static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 			return -1;
 	}
 
+	/* A low-speed device (K state on an unenabled port) is never reset by
+	   the EHCI: hand it straight to the companion (EHCI 4.2.2). */
+	if (!(ps & PORT_PE) && PORT_USB11(ps) && n_companions(c)) {
+		port_to_companion(c, port, ps);
+		return -1;
+	}
+
 	port_endpoints_reset(c, port);
 	c->last_port = (int)port;
 
@@ -968,11 +1000,13 @@ static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
 	if (!(ps & PORT_PE)) {
 		if (ps & PORT_OWNER)
 			return -1;
-		if (PORT_USB11(ps)) {
-			/* Low/full speed: make sure the companion owns it. */
-			wr32(port_reg(c, port),
-			     (ps & ~(PORT_RWC | PORT_PE)) | PORT_OWNER |
-			     PORT_CSC | PORT_PEC | PORT_OCC);
+		/* Not enabled after the reset: the device is not high-speed
+		   (EHCI 4.2.2), whatever the line state reads -- a full-speed
+		   receiver on the MCP79 reports SE0 here.  It belongs to the
+		   companion.  Only a controller without companions (Intel PCH
+		   rate-matching hubs) can have a genuinely stuck HS reset. */
+		if (n_companions(c) || PORT_USB11(ps)) {
+			port_to_companion(c, port, ps);
 			return -1;
 		}
 		serial_puts(SERIAL_COM1, "ehci: reset no PED ");