Răsfoiți Sursa

Fix xHCI EP0 max packet size so the VL805 sees the boot stick

On the 970A-DS3P's VIA VL805 (1106:3483) a high-speed USB stick never
enumerated: Address Device kept the USB core's 8-byte first-read guess
for EP0, and the 18-byte device descriptor read failed with Babble
Detected, so the booted stick was never usb0 and install had no source.

- Address Device uses the speed's fixed EP0 size (8 low, 64 high, 512
  SuperSpeed); a full-speed EP0 is corrected with Evaluate Context once
  the core knows bMaxPacketSize0.
- Do not PED-disable a SuperSpeed port before its reset (that leaves the
  link in SS.Disabled).
- Set the Setup TRB transfer type only on xHCI >= 1.0.
- Always log the controller id/version and every failed control
  transfer (completion code, PORTSC, slot/EP0 state) to COM1/netmon.

Verified on the board: the SanDisk stick enumerates first try and
mounts as usb0; QEMU xHCI USB3 boot + HID unchanged.
Johannes Zellner 5 zile în urmă
părinte
comite
9eb38faa14
3 a modificat fișierele cu 140 adăugiri și 21 ștergeri
  1. 14 4
      AGENTS.md
  2. 1 1
      docs/drivers.html
  3. 125 16
      kernel/xhci.c

+ 14 - 4
AGENTS.md

@@ -2089,10 +2089,20 @@ briefly reporting disconnected.
   bounded wait for the BIOS-owned semaphore to clear, and only programs
   `CONFIG`/`DCBAAP`/scratchpads/`CRCR`/`ERST` once the post-reset controller
   is halted.  Port reset clears stale change latches, waits for `PRC` then
-  `PED`, and adds the 10 ms USB2 reset-recovery delay.  The BSR Address
-  Device uses the port-speed EP0 max packet size (64 for high/full speed,
-  8 for low speed); using 8 for a high-speed slot leaves EP0 unusable on
-  real xHCs.  Enumeration uses the traditional scheme: **SET_ADDRESS before
+  `PED`, and adds the 10 ms USB2 reset-recovery delay.  Only a USB2 port
+  is disabled (PED write) before the reset: on a SuperSpeed port that puts
+  the link into SS.Disabled, which a hot reset does not leave.  Address
+  Device programs the speed's fixed EP0 max packet size (8 low, 64 high,
+  512 SuperSpeed) whatever the core passes; only a full-speed EP0 takes the
+  core's 8-byte first guess and is then corrected with an **Evaluate
+  Context** once the core reads the real `bMaxPacketSize0`.  Without that
+  the VIA VL805 (`1106:3483`) on the 970A-DS3P kept an 8-byte EP0 for a
+  high-speed stick, and the 18-byte descriptor read failed with Babble
+  Detected, so a USB-booted stick never became `usb0` for `install`.  The
+  Setup TRB's Transfer Type field is only set on xHCI >= 1.0 (reserved in
+  0.96).  The init line (`xhci: <pci id> hci <ver> ...`) and every failed
+  control transfer (request, completion code, PORTSC, slot/EP0 state) are
+  always logged to COM1/netmon.  Enumeration uses the traditional scheme: **SET_ADDRESS before
   the first descriptor read**.  The xHCI SET_ADDRESS intercept creates the
   slot in the Enabled state and issues Address Device with **BSR=0**
   directly (Enabled -> Addressed), never leaving the slot in the Default

+ 1 - 1
docs/drivers.html

@@ -286,7 +286,7 @@
 					<tr><td>UHCI</td><td>PCI I/O ports</td><td>1</td><td>Frame list at <code>0x3000</code>; control QH plus one QH per interrupt slot.</td></tr>
 					<tr><td>OHCI</td><td>PCI MMIO</td><td>up to 4</td><td>HCCA from the page allocator, static ED/TD sets in the uncacheable <code>ohci_dma_page</code>, TDs start at <code>CC = NotAccessed</code>. Before reset it requests SMM ownership (<code>HcCommandStatus.OCR</code>, clear <code>HcControl.IR</code>) and disables the AMD southbridge USB SMI on <code>1002:4385</code>.</td></tr>
 					<tr><td>EHCI</td><td>PCI MMIO</td><td>up to 4</td><td>Async QH/qTD schedule, page-allocated descriptors, polling only, no periodic list.</td></tr>
-					<tr><td>xHCI</td><td>PCI MMIO</td><td>—</td><td>USB2 and USB3 root ports; direct SuperSpeed devices use a 512-byte EP0 and the port speed in the slot context (USB3 hubs are not decoded). Halts and waits for <code>HCH</code> around <code>HCRST</code>; EP0 max packet comes from the port speed; EP0 stages are encoded as one TD per stage (EDK2 style) because chained control TDs stall on Braswell.</td></tr>
+					<tr><td>xHCI</td><td>PCI MMIO</td><td>—</td><td>USB2 and USB3 root ports; direct SuperSpeed devices use a 512-byte EP0 and the port speed in the slot context (USB3 hubs are not decoded). Halts and waits for <code>HCH</code> around <code>HCRST</code>; EP0 max packet is the speed's fixed value (8/64/512), and a full-speed EP0 is corrected with Evaluate Context after the first descriptor read (the VIA VL805 on the 970A-DS3P babbled on a high-speed stick otherwise); a SuperSpeed port is not PED-disabled before its reset; failed control transfers are always logged with their completion code; EP0 stages are encoded as one TD per stage (EDK2 style) because chained control TDs stall on Braswell.</td></tr>
 				</tbody>
 			</table>
 		</div>

+ 125 - 16
kernel/xhci.c

@@ -104,6 +104,7 @@ static unsigned int portsc_neutral(unsigned int ps)
 #define TRB_DISABLE_SLOT	10
 #define TRB_ADDR_DEV		11
 #define TRB_CONFIG_EP		12
+#define TRB_EVAL_CTX		13
 #define TRB_TRANSFER		32
 #define TRB_COMPLETION		33
 
@@ -148,6 +149,7 @@ struct xhci_slot {
 	unsigned int ep0;	/* EP0 transfer ring page */
 	unsigned int ep0_enq;
 	unsigned int ep0_cycle;
+	unsigned int ep0_mps;	/* max packet size programmed into EP0 */
 	unsigned int ep_mask;	/* configured endpoint flags */
 };
 
@@ -194,6 +196,8 @@ static unsigned int ctx_size;
 static unsigned int port_mask;
 static unsigned int hcc_params;
 static unsigned int pci_bus, pci_dev, pci_func;
+static unsigned int pci_id;
+static unsigned int hci_version;
 static int found;
 static int last_port = -1;
 
@@ -265,6 +269,14 @@ static void dbg_hex(const char *s, unsigned int v) { (void)s; (void)v; }
 static void dbg_dec(const char *s, unsigned int v) { (void)s; (void)v; }
 #endif
 
+/* Always-on one-line diagnostics (COM1, mirrored to netmon).  Only used on
+ * the init line and on failures, so a working machine stays quiet. */
+static void xlog_hex(const char *s, unsigned int v)
+{
+	serial_puts(SERIAL_COM1, s);
+	serial_puthex(SERIAL_COM1, v);
+}
+
 static void delay_ms(unsigned int ms)
 {
 	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
@@ -601,6 +613,47 @@ static unsigned int ep0_mps_for_speed(unsigned int speed)
 	return 64;		/* full speed (guess) and high speed */
 }
 
+/* EP0 max packet size for Address Device.  Low, high and SuperSpeed have
+ * one legal value, so the USB core's 8-byte first-read guess is replaced
+ * by it; only full speed (8/16/32/64) takes the caller's value and is
+ * corrected later by ep0_set_mps(). */
+static unsigned int ep0_mps_initial(unsigned int speed, unsigned int mps)
+{
+	if (!mps || speed != 1)
+		return ep0_mps_for_speed(speed);
+	return mps;
+}
+
+/* Evaluate Context: tell the xHC the device's real bMaxPacketSize0 once
+ * the first descriptor read has returned it.  Without this the VL805 on
+ * the 970A-DS3P kept the 8-byte EP0 that SET_ADDRESS created, and the
+ * 18-byte device descriptor read of a high-speed stick failed with
+ * Babble Detected (the device answers in one 18-byte packet). */
+static int ep0_set_mps(struct xhci_slot *s, unsigned int mps)
+{
+	unsigned int flags;
+	int rc;
+
+	spin_lock_irqsave(&xhci_lock, &flags);
+	zero_page(input_ctx, 4096);
+	*(unsigned int *)input_ctx = 0;
+	*(unsigned int *)(input_ctx + 4) = EP0_FLAG;
+	fill_ep0_ctx(mps, s->ep0, s->ep0_enq, s->ep0_cycle);
+	spin_unlock_irqrestore(&xhci_lock, flags);
+
+	rc = xhci_cmd(input_ctx, 0,
+		      TRB_TYPE(TRB_EVAL_CTX) | (s->slot_id << 24), 0);
+	if (rc == 0) {
+		s->ep0_mps = mps;
+	} else {
+		xlog_hex("xhci: evaluate context failed slot ", s->slot_id);
+		xlog_hex(" mps ", mps);
+		xlog_hex(" cc ", cmd_status);
+		serial_putc(SERIAL_COM1, '\n');
+	}
+	return rc;
+}
+
 static int xhci_config_ep(struct xhci_slot *s, unsigned int ep_index,
 			  unsigned int type, unsigned int mps,
 			  unsigned int interval, unsigned int ring)
@@ -680,8 +733,8 @@ static struct xhci_slot *new_slot_common(unsigned int target_addr,
 	s->ep_mask = 0;
 	s->dev_addr = 0;
 	ring_init(ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle);
-	if (!mps || s->speed >= 4)
-		mps = ep0_mps_for_speed(s->speed);
+	mps = ep0_mps_initial(s->speed, mps);
+	s->ep0_mps = mps;
 
 	ram_wr64(dcbaa + slot_id * 8, dc);
 
@@ -697,8 +750,13 @@ static struct xhci_slot *new_slot_common(unsigned int target_addr,
 		      TRB_TYPE(TRB_ADDR_DEV) | (slot_id << 24) |
 		      (bsr ? TRB_BSR : 0), 0);
 	if (rc != 0) {
-		dbg(bsr ? "xhci: address default failed\n" :
-		    "xhci: address device failed\n");
+		xlog_hex(bsr ? "xhci: address default failed port " :
+			 "xhci: address device failed port ", s->port);
+		xlog_hex(" spd ", s->speed);
+		xlog_hex(" cc ", cmd_status);
+		xlog_hex(" portsc ",
+			 mmio_rd(op(XHCI_OP_PORTSC) + s->port * 0x10));
+		serial_putc(SERIAL_COM1, '\n');
 		ram_wr64(dcbaa + slot_id * 8, 0);
 		xhci_cmd(0, 0,
 			 TRB_TYPE(TRB_DISABLE_SLOT) | (slot_id << 24), 0);
@@ -777,10 +835,7 @@ static int set_address(unsigned int new_addr, unsigned int mps)
 	if (!s || new_addr == 0 || new_addr > 127)
 		return -1;
 
-	/* The USB core's first SET_ADDRESS uses its high/full-speed 64-byte
-	 * guess; a SuperSpeed slot's EP0 is always 512 bytes. */
-	if (s->speed >= 4)
-		mps = ep0_mps_for_speed(s->speed);
+	mps = ep0_mps_initial(s->speed, mps);
 
 	spin_lock_irqsave(&xhci_lock, &flags);
 	zero_page(input_ctx, 4096);
@@ -799,6 +854,7 @@ static int set_address(unsigned int new_addr, unsigned int mps)
 
 	spin_lock_irqsave(&xhci_lock, &flags);
 	s->dev_addr = ((unsigned int *)s->dc)[3] & 0xFF;
+	s->ep0_mps = mps;
 	addr_slot[0] = 0;
 	s->addr = new_addr;
 	addr_slot[new_addr] = s->slot_id;
@@ -807,6 +863,32 @@ static int set_address(unsigned int new_addr, unsigned int mps)
 }
 
 /* ---- control transfers ------------------------------------------------- */
+/* One line per failed control transfer: the request, the completion code
+ * (0 = no event within the timeout), the root port's PORTSC, and the slot /
+ * EP0 states from the output device context (slot state 2 = Addressed,
+ * EP state 2 = Halted, 4 = Error). */
+static void ctl_fail_log(const struct xhci_slot *s, const unsigned char *setup,
+			 unsigned int cc, unsigned int data_seen)
+{
+	const unsigned int *dc = (const unsigned int *)s->dc;
+	const unsigned int *ep0 = (const unsigned int *)(s->dc + ctx_size);
+
+	serial_puts(SERIAL_COM1, cc ? "xhci: ctl err" : "xhci: ctl timeout");
+	xlog_hex(" req ", (unsigned int)setup[0] | ((unsigned int)setup[1] << 8) |
+		 ((unsigned int)setup[2] << 16) | ((unsigned int)setup[3] << 24));
+	xlog_hex(" cc ", cc);
+	xlog_hex(" data ", data_seen);
+	xlog_hex(" slot ", s->slot_id);
+	xlog_hex(" spd ", s->speed);
+	xlog_hex(" portsc ", mmio_rd(op(XHCI_OP_PORTSC) + s->port * 0x10));
+	xlog_hex(" sstate ", dc[3] >> 27);
+	xlog_hex(" dev ", dc[3] & 0xFF);
+	xlog_hex(" ep0 ", ep0[0] & 7);
+	xlog_hex(" mps ", ep0[1] >> 16);
+	xlog_hex(" sts ", mmio_rd(op(XHCI_OP_USBSTS)));
+	serial_putc(SERIAL_COM1, '\n');
+}
+
 static int control_transfer(unsigned int ctrl, unsigned int addr,
 			    unsigned int mps, int low_speed,
 			    const unsigned char *setup, unsigned char *data,
@@ -854,6 +936,14 @@ static int control_transfer(unsigned int ctrl, unsigned int addr,
 			return -1;
 	}
 
+	/* The core learned the real bMaxPacketSize0 from the first 8 bytes
+	 * of the device descriptor; reprogram EP0 before using it.  Only a
+	 * full-speed EP0 can legally differ from the speed's fixed value. */
+	if (s->speed == 1 && (mps == 8 || mps == 16 || mps == 32 ||
+			      mps == 64) && mps != s->ep0_mps &&
+	    ep0_set_mps(s, mps) != 0)
+		return -1;
+
 	spin_lock_irqsave(&xhci_lock, &flags);
 	if (ctl_busy) {
 		spin_unlock_irqrestore(&xhci_lock, flags);
@@ -875,7 +965,10 @@ static int control_transfer(unsigned int ctrl, unsigned int addr,
 	 * transfer event is ever posted, while the per-stage encoding
 	 * completes. */
 	setup_c = TRB_TYPE(TRB_SETUP) | TRB_IDT | TRB_IOC;
-	if (len)
+	/* Transfer Type (TRT) only exists from xHCI 1.0 on; in a 0.96
+	 * controller (Etron EJ168, early VIA/Renesas parts) bits 17:16 are
+	 * reserved.  Linux sets it only for hci_version >= 0x100 too. */
+	if (len && hci_version >= 0x100)
 		setup_c |= (dir_in ? 3u : 2u) << 16;
 	ctl_setup_trb = s->ep0 + s->ep0_enq * 16;
 	ring_queue(s->ep0, XHCI_EP0_TRBS, &s->ep0_enq, &s->ep0_cycle,
@@ -931,7 +1024,7 @@ static int control_transfer(unsigned int ctrl, unsigned int addr,
 	if (!ctl_done) {
 		ctl_busy = 0;
 		spin_unlock_irqrestore(&xhci_lock, flags);
-		dbg("xhci: control timeout\n");
+		ctl_fail_log(s, setup, 0, ctl_data_seen);
 		return -1;
 	}
 	rc = ctl_actual;
@@ -940,7 +1033,7 @@ static int control_transfer(unsigned int ctrl, unsigned int addr,
 	spin_unlock_irqrestore(&xhci_lock, flags);
 
 	if (st != CC_SUCCESS && st != CC_SHORT_PACKET) {
-		dbg_hex("xhci: control status ", st);
+		ctl_fail_log(s, setup, st, ctl_data_seen);
 		return -1;
 	}
 	return (int)rc;
@@ -1258,8 +1351,12 @@ static int port_reset(unsigned int ctrl, unsigned int port)
 
 	/* Force the port into the Disabled state first: some xHCs ignore
 	 * PR while the port is already enabled, leaving the device at its
-	 * firmware-assigned address instead of address 0. */
-	if (ps & PORTSC_PED) {
+	 * firmware-assigned address instead of address 0.  USB2 ports only:
+	 * on a SuperSpeed port PED=1 puts the link into SS.Disabled, which a
+	 * hot reset (PR) does not leave -- software would have to write
+	 * PLS=RxDetect -- so the BIOS-booted stick stayed unreachable. */
+	if ((ps & PORTSC_PED) &&
+	    ((ps >> PORTSC_SPEED_SHIFT) & PORTSC_SPEED_MASK) < 4) {
 		mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
 			portsc_neutral(ps) | PORTSC_PED);
 		t0 = get_ticks_ms();
@@ -1290,7 +1387,9 @@ static int port_reset(unsigned int ctrl, unsigned int port)
 		usb_poll_relax(t0);
 	}
 	if (!(ps & PORTSC_PRC)) {
-		dbg_hex("xhci: port reset timeout ", port);
+		xlog_hex("xhci: port reset timeout port ", port);
+		xlog_hex(" portsc ", ps);
+		serial_putc(SERIAL_COM1, '\n');
 		return -1;
 	}
 	mmio_wr(op(XHCI_OP_PORTSC) + port * 0x10,
@@ -1304,7 +1403,9 @@ static int port_reset(unsigned int ctrl, unsigned int port)
 		usb_poll_relax(t0);
 	}
 	if (!(ps & PORTSC_PED)) {
-		dbg_hex("xhci: port not enabled ", port);
+		xlog_hex("xhci: port not enabled port ", port);
+		xlog_hex(" portsc ", ps);
+		serial_putc(SERIAL_COM1, '\n');
 		return -1;
 	}
 
@@ -1485,7 +1586,9 @@ found_xhci:
 	mmio_base = bar;
 	paging_map_mmio(mmio_base, 0x10000);
 
+	pci_id = pci_cfg_read32(pci_bus, pci_dev, pci_func, 0x00);
 	cap_len = mmio_rd(0) & 0xFF;
+	hci_version = mmio_rd(0) >> 16;
 	hcs1 = mmio_rd(XHCI_CAP_HCSPARAMS1);
 	hcs2 = mmio_rd(XHCI_CAP_HCSPARAMS2);
 	hcc_params = mmio_rd(XHCI_CAP_HCCPARAMS1);
@@ -1656,7 +1759,13 @@ found_xhci:
 	 * while commands complete normally. */
 
 	scan_protocols();
-	dbg_hex("xhci: ports ", port_mask);
+	xlog_hex("xhci: ", pci_id);
+	xlog_hex(" hci ", hci_version);
+	xlog_hex(" mmio ", mmio_base);
+	xlog_hex(" ports ", port_mask);
+	xlog_hex(" csz ", ctx_size);
+	xlog_hex(" scratch ", n);
+	serial_putc(SERIAL_COM1, '\n');
 
 	/* Power every root port.  VBus is supposed to come up after HCRST,
 	 * but some firmware/PPC combinations leave PP clear; the USB3