Procházet zdrojové kódy

usb: arm EHCI bulk-IN qTDs with the exact transfer length

A bulk-IN qTD was always armed with the 2048-byte bounce buffer, even when usbmsc asked for one 512-byte sector.  A device that ends the data stage without a zero-length packet (the ION's SanDisk) left the qTD ACTIVE forever, so install could not read its USB source.

Arm each qTD with exactly the length of the call that starts it and do not pre-arm the next transfer: a BOT data stage's final chunk can be shorter than the previous one.  This also stops a completed larger chunk from being clipped to a later, smaller length.
Johannes Zellner před 1 dnem
rodič
revize
f268094347
4 změnil soubory, kde provedl 32 přidání a 13 odebrání
  1. 6 1
      AGENTS.md
  2. 1 1
      docs/drivers.html
  3. 17 7
      kernel/ehci.c
  4. 8 4
      kernel/usb_hcd.h

+ 6 - 1
AGENTS.md

@@ -2536,7 +2536,12 @@ briefly reporting disconnected.
   Disabling clears ASE and acknowledges `STS_IAA`; leaving the IAA doorbell
   status set stalls the next enable.  Bulk `in`/`out` use one qTD per call
   (up to five 4 KB buffer pages), which is enough for the ASIX AX88772 RX/TX
-  path.  The HCD vtable's optional `bulk_reset` disarms the endpoint's qTD,
+  path.  A bulk-IN qTD is armed with exactly the length of the call that
+  starts it and is not pre-armed for a later, possibly shorter chunk: a
+  device that ends a transfer on a max-packet boundary without a zero-length
+  packet (a USB stick's 512-byte READ(10) data stage) would otherwise leave a
+  larger qTD ACTIVE forever.  The HCD vtable's optional `bulk_reset` disarms
+  the endpoint's qTD,
   clears its data toggle and drops the bulk state, so a timed-out bulk-IN
   transfer cannot leave the endpoint permanently armed; xHCI stops the
   endpoint before dropping its ring.  After an async-transfer gap of 100 ms

+ 1 - 1
docs/drivers.html

@@ -290,7 +290,7 @@
 				<tbody>
 					<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. The async-advance doorbell (<code>IAAD</code>) is rung after an idle gap and before cancelling a stuck transfer; the MCP79 can otherwise keep a halted QH's old qTD cached and never fetch a USB disk's first read after enumeration.</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. The async-advance doorbell (<code>IAAD</code>) is rung after an idle gap and before cancelling a stuck transfer; the MCP79 can otherwise keep a halted QH's old qTD cached and never fetch a USB disk's first read after enumeration. A bulk-IN qTD is armed with exactly the starting call's length and is not pre-armed for a later chunk, so a device that ends a 512-byte sector transfer without a zero-length packet still completes.</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>

+ 17 - 7
kernel/ehci.c

@@ -146,7 +146,8 @@ struct ehci_bulk {
 	unsigned int addr;
 	unsigned int ep;
 	unsigned int mps;
-	unsigned int buf_len;
+	unsigned int buf_len;       /* capacity of the HCD's IN bounce buffer */
+	unsigned int armed_len;     /* length the current IN qTD was armed with */
 	unsigned int toggle;
 	unsigned int arm_ms;
 	int armed;
@@ -450,6 +451,7 @@ static void bulk_reset(struct ehci_bulk *b)
 	b->ep = 0;
 	b->mps = 0;
 	b->buf_len = 0;
+	b->armed_len = 0;
 	b->toggle = 0;
 }
 
@@ -464,6 +466,7 @@ static void bulk_configure(struct ehci_bulk *b, unsigned int addr,
 	b->ep = ep;
 	b->mps = mps;
 	b->buf_len = buf_len;
+	b->armed_len = 0;
 	b->toggle = 0;                  /* first packet is DATA0 */
 	qh_init(b->qh);                 /* keeps the link pointer out of it */
 	qh_configure(b->qh, addr, ep, mps, 0);
@@ -596,7 +599,7 @@ static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
 {
 	struct ehci_ctrl *c;
 	struct ehci_bulk *b;
-	unsigned int token, raw, packets, actual, i;
+	unsigned int token, raw, packets, actual, i, alen;
 
 	if (ctrl >= n_ctrls || !buf || !len)
 		return -1;
@@ -612,7 +615,16 @@ static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
 	bulk_configure(b, addr, ep, mps, EHCI_RX_MAX);
 
 	if (!b->armed) {
-		qtd_fill(b->qtd, c->rx_buf, b->buf_len, QTD_PID_IN, 0);
+		/* Arm exactly the length this call is waiting for.  A device
+		 * that ends a transfer which is an exact multiple of the max
+		 * packet size without a zero-length packet leaves a larger
+		 * qTD active forever (the ION's SanDisk does this for every
+		 * 512-byte sector read).  Do not pre-arm the next transfer:
+		 * the next call's length can be smaller, because a BOT data
+		 * stage's final chunk need not fill the whole bounce buffer. */
+		alen = len > EHCI_RX_MAX ? EHCI_RX_MAX : len;
+		b->armed_len = alen;
+		qtd_fill(b->qtd, c->rx_buf, alen, QTD_PID_IN, 0);
 		bulk_arm(b);
 		return 0;
 	}
@@ -630,8 +642,8 @@ static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
 		return -1;
 	}
 
-	raw = QTD_LEN(token) <= b->buf_len ?
-		b->buf_len - QTD_LEN(token) : 0;
+	raw = QTD_LEN(token) <= b->armed_len ?
+		b->armed_len - QTD_LEN(token) : 0;
 	actual = raw;
 	if (actual > len)
 		actual = len;
@@ -644,8 +656,6 @@ static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
 	b->toggle ^= packets & 1u;
 
 	qh_disarm(b->qh);
-	qtd_fill(b->qtd, c->rx_buf, b->buf_len, QTD_PID_IN, 0);
-	bulk_arm(b);
 	return (int)actual;
 }
 

+ 8 - 4
kernel/usb_hcd.h

@@ -79,10 +79,14 @@ struct usb_hcd_ops {
 	void (*int_in_stop)(unsigned int slot);
 
 	/* Bulk transfers (USB Ethernet / mass storage).  bulk_in is
-	 * start-or-poll: the first call arms the transfer and returns 0,
-	 * later calls return the frame length, 0 while NAKed, or -1.  A
-	 * delivered chunk stays in the caller's buffer until the next call,
-	 * which re-arms the transfer.  May be NULL. */
+	 * start-or-poll: the first call arms the transfer and returns 0;
+	 * later calls return the completed length, 0 while it is still in
+	 * progress, or -1.  The transfer is armed with exactly that call's
+	 * length, so a device that ends an exact multiple of the max packet
+	 * size without a zero-length packet still completes; the next call
+	 * after a completion arms a new transfer with its own length.  The
+	 * delivered chunk is copied into the caller's buffer before the
+	 * return.  May be NULL. */
 	int (*bulk_out)(unsigned int ctrl, unsigned int addr,
 			unsigned int ep, unsigned int mps,
 			const unsigned char *buf, unsigned int len);