Procházet zdrojové kódy

Add USB3 xHCI storage and make the installer robust

The xHCI driver now includes both USB2 and USB3 supported-protocol port
ranges and uses a 512-byte EP0 for SuperSpeed slots; the USB core's
initial 64-byte SET_ADDRESS guess is overridden from the port speed.
Direct USB3 root-port devices are enumerated (USB3 hubs are still not
decoded), which makes the HP Chromebook 11 G5's USB 3.0 ports usable.

The USB mass-storage driver accumulates short bulk-IN chunks within a
BOT data stage instead of treating them as an error, fetches REQUEST
SENSE after a failed READ(10), retries three times and falls back to
one-sector reads for a range a stick refuses as one large command.

Change install to read the entire source image into RAM before touching
the target.  The unused LBA 1..62 gap is synthesized as zeros, so a bad
block there cannot abort the install.  The target is then written from
RAM (MBR last) and verified; a source-read failure leaves the target
untouched.  The source read keeps the BKL so a USB3 stick stays active
across chunks, while target writes release it per chunk so the WM and
terminals remain responsive on the slow eMMC.

Update AGENTS.md and the architecture pages.  The QMP installer test
uses the default qemu-xhci SuperSpeed port.  Validated on the HP
Chromebook 11 G5: usb0 appears, install mmc0 completes and the eMMC
boots NebOS; make test-install and its --xhci variant pass in QEMU.
Johannes Zellner před 1 týdnem
rodič
revize
cb115fa872
9 změnil soubory, kde provedl 271 přidání a 98 odebrání
  1. 39 27
      AGENTS.md
  2. 2 2
      Makefile
  3. 8 6
      docs/boot.html
  4. 13 10
      docs/drivers.html
  5. 4 3
      docs/userland.html
  6. 101 19
      install.c
  7. 4 3
      tools/qmp_install_test.py
  8. 73 9
      usbmsc.c
  9. 27 19
      xhci.c

+ 39 - 27
AGENTS.md

@@ -473,12 +473,17 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   command clones the live NebOS image.  It detects a source by the MBR plus
   the LBA-63 boot-sector signature (`NEBOS` OEM string, `kernel_lba` 64,
   `pack_lba` 1088), computes the used length from the highest partition end,
-  copies sectors 1..end in chunks, writes the source MBR **last**, flushes and
-  verifies by checksum before remounting the target.  The source can be
-  `usb0` (USB mass storage), IDE, SATA or eMMC; the target is any non-floppy
-  block device with enough sectors.  It reuses `format_busy`; the copy holds
-  the BKL so a USB source cannot be re-enumerated mid-install.  It installs
-  the flashed image, not runtime `cmds-dev` pack updates.
+  allocates a page buffer for the whole image and reads the source into RAM
+  first (LBA 1..62 are synthesized as zeros instead of read).  It then writes
+  the target from RAM in chunks with the source no longer active, writes the
+  MBR **last**, flushes and verifies by checksum before remounting the
+  target.  A source-read failure therefore leaves the target untouched.  The
+  source read keeps the BKL (a USB3 stick must stay active across chunks);
+  the target writes release it per chunk so the WM/terminal stay responsive.
+  It reuses `format_busy`.  The source can be `usb0` (USB mass storage), IDE,
+  SATA or eMMC; the target is any non-floppy block device with enough
+  sectors.  It installs the flashed image, not runtime `cmds-dev` pack
+  updates.
 - **`/share`**: `share.c/h` — writable, page-backed in-memory filesystem for
   network uploads (the netbooted board may have no writable disk).  Entries
   are full relative paths (like the pack ramfs) plus explicit directory
@@ -603,7 +608,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   frame list at `0x3000`, one instance), `ohci.c` (MMIO OHCI, up to 4
   instances, HCCA + ED/TD lists, polling), `ehci.c` (MMIO EHCI, QH/qTD
   asynchronous schedule, up to 4 instances) and `xhci.c` (MMIO xHCI, USB2
-  root ports; halt/HCH reset sequence and BIOS/OS handoff) implement it.
+  and USB3 root ports; halt/HCH reset sequence and BIOS/OS handoff)
+  implement it.
   `lsusb.c` — the `lsusb` command (lists every controller and its tree).
   `usbmsc.c/h` — USB mass-storage class driver (BOT/SCSI) exposed as
   `usb0..`; its device probe is registered next to ASIX and `blk_scan()`
@@ -826,8 +832,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `z-index`, `transform: translate`/`scale`/`rotate` with
   `transform-origin` (paint-time box and inline-image transforms), flex rows
   with `justify-content` (including `space-evenly`)/`align-items`, two-column
-  grids, table colspan and
-  percentage columns, and basic `float: left/right` columns.
+  grids, table colspan and percentage columns, and basic `float: left/right`
+  columns.
   `@media (min-width/max-width)` blocks are evaluated against the browser
   canvas width when the page is parsed; `@import`/`@keyframes` are skipped.
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
@@ -1212,8 +1218,10 @@ so addresses do not collide.
   layouts put the USB function at 0:07.2 / 0:01.2) and ignores controllers
   whose BARs the firmware left unassigned. One instance only.
 - **xHCI**: MMIO BAR0 (PCI class `0c03:30`), one controller, polled.  The
-  Supported Protocol extended capabilities decide which root ports are USB2;
-  USB3 SuperSpeed ports are left alone.  `xhci_init()` waits for `CNR=0`,
+  Supported Protocol extended capabilities include both USB2 and USB3
+  protocol entries; direct SuperSpeed root-port devices are enumerated with
+  a 512-byte EP0 and the port speed in the slot context.  USB3 hub
+  port-status decoding is not implemented.  `xhci_init()` waits for `CNR=0`,
   clears `RS` and waits for `USBSTS.HCH` **before** `HCRST` (the xHC may
   ignore the reset while running), waits 1-2 ms after the HCRST write before
   touching any other register (Intel CherryView/Braswell requires this, or
@@ -1254,14 +1262,16 @@ so addresses do not collide.
   with SCSI INQUIRY + READ CAPACITY.  Commands use BOT CBW/CSW with LUN 0,
   512-byte blocks and at most 62 sectors per data stage; READ(10), WRITE(10),
   SYNCHRONIZE CACHE, a BOT reset and endpoint-halt clear are implemented.
+  Short bulk-IN chunks are accumulated within a BOT data stage; a failed
+  READ(10) gets REQUEST SENSE, three retries and a 1-sector fallback.
   `blk_scan()` registers present devices as `usb0..` (`BLK_USB`), so
   `storage`, the VFS mount path, `format` and `install` can use them.  Bulk
   transfers exist only on EHCI and xHCI; a storage device on a UHCI/OHCI
   companion is left unclaimed.  The xHCI bulk-IN path arms one transfer per
   call and does not auto re-arm, so a consumer can alternate CBW, data and
-  CSW buffers.  The installer reads the live image from `usb0`; USB3
-  SuperSpeed ports (not driven by `xhci.c`) and UAS/4Kn sticks are not
-  usable.
+  CSW buffers.  The installer reads the live image from `usb0`; direct
+  USB3 root-port sticks work, while UHCI/OHCI bulk devices, USB3 hubs and
+  UAS/4Kn sticks are not usable.
 - **Devices** (`usbhid.c`): every HID (class 3) interface with an
   interrupt-IN endpoint gets its own interrupt slot (USB_HCD_INT_SLOTS = 6).
   Boot interfaces (subclass 1, protocol 2/1) are used as-is in boot
@@ -1487,9 +1497,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 11. The BKL serializes all kernel execution: a long FAT/disk operation, a
     non-network driver wait or a big shadow-buffer operation can stall syscalls
     on the other CPUs for its duration.  `bkl_pause()`/`bkl_resume()` is used
-    for serial/network waits and the format raw-sector loop; the installer
-    holds the BKL for the copy so a USB source cannot be re-enumerated
-    mid-install.
+    for serial/network waits, the format raw-sector loop and the installer's
+    target writes; the installer keeps the BKL only for its source-read burst
+    so a USB source cannot be re-enumerated mid-read.
 12. Kernel threads (kworker, kexec/payload, kernel commands) are pinned to
     the BSP.  Only ring-3 processes use the APs, so a single CPU-bound kernel
     driver still limits throughput.
@@ -1621,10 +1631,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     is unsigned, so Secure Boot must be off.  The loader needs a GOP mode with
     BGRX or RGBX 32bpp, width ≤ 1280, `pitch * height` ≤ 8 MB and the
     framebuffer below 4 GB; otherwise the kernel starts headless (serial only).
-    xHCI drives USB2 root ports only (USB3 SuperSpeed devices are not
-    handled), and EHCI only high-speed root ports (no transaction translator
-    for low/full-speed devices behind a high-speed hub), so a USB3-only
-    keyboard/mouse can still leave the UI without input (PS/2 may still work).
+    xHCI drives USB2 and USB3 root ports (USB3 hubs are not decoded), and
+    EHCI only high-speed root ports (no transaction translator for
+    low/full-speed devices behind a high-speed hub), so a full-speed device
+    behind a USB2 hub can still leave the UI without input (PS/2 may still
+    work).
     There is no IOAPIC setup: OVMF delivers the ISA IRQs through the 8259
     path, but a platform that routes them through the IOAPIC may need a
     mask-all step after ACPI init.  UEFI runtime services are not used, so
@@ -1716,9 +1727,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     for `usb-efi.img`, ~8.5 MB for `usb.img`), so the rest of the target stays
     unpartitioned and runtime `cmds-dev` pack updates are not part of the
     clone.  The source must be visible as a `blkdev`: USB mass storage is
-    BOT/SCSI on EHCI/xHCI USB2 bulk only (no UHCI/OHCI bulk path, no USB3
-    SuperSpeed ports, no UAS or 4Kn), so a USB3-only port or an unsupported
-    stick leaves no source.  The copy holds the BKL and can freeze the UI for
-    its duration; a failed copy leaves an incomplete target.  IDE, SATA and
-    eMMC sources work without USB, but a target smaller than the image is
-    refused.
+    BOT/SCSI on EHCI and xHCI bulk, including direct USB3 root-port devices
+    (no UHCI/OHCI bulk path, no USB3 hubs, no UAS or 4Kn), so an unsupported
+    stick or controller leaves no source.  The installer needs ~17 MB of
+    free RAM for the source image (less for `usb.img`); a source-read failure
+    leaves the target untouched, while a target-write failure can leave it
+    incomplete (the MBR is written last).  IDE, SATA and eMMC sources work
+    without USB, but a target smaller than the image is refused.

+ 2 - 2
Makefile

@@ -286,10 +286,10 @@ fatformat.o: fatformat.c fatformat.h blkdev.h io.h mm.h sched.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
-install.o: install.c blkdev.h fatformat.h io.h process.h serial.h vga.h vfs.h
+install.o: install.c blkdev.h fatformat.h io.h mm.h process.h sched.h serial.h vga.h vfs.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
-usbmsc.o: usbmsc.c usbmsc.h blkdev.h serial.h timer.h usb.h
+usbmsc.o: usbmsc.c usbmsc.h blkdev.h io.h serial.h timer.h usb.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
 mouse.o: mouse.c mouse.h bootinfo.h cirrus.h cursor_shape.h radeon.h timer.h touchpad.h usbhid.h vga.h

+ 8 - 6
docs/boot.html

@@ -353,12 +353,14 @@
 			command clones the image's used sectors (MBR through the end of the
 			last partition) onto a target disk. It finds the source by the MBR
 			plus the LBA-63 boot-sector signature and prefers <code>usb0</code>,
-			the USB mass-storage view of the live stick. A clone of
-			<code>usb-efi.img</code> keeps the legacy MBR chain and the ESP/UEFI
-			chain; a clone of <code>usb.img</code> is BIOS-only. The copy writes
-			the source MBR last, flushes and verifies the target. The rest of the
-			target stays unpartitioned, and runtime <code>cmds-dev</code> pack
-			updates are not part of the cloned image.
+			the USB mass-storage view of the live stick. It reads the whole
+			source image into RAM first (the unused LBA 1..62 gap is synthesized
+			as zeros), then writes the target from RAM, writes the MBR last,
+			flushes and verifies it. A source-read failure leaves the target
+			untouched. A clone of <code>usb-efi.img</code> keeps the legacy MBR
+			chain and the ESP/UEFI chain; a clone of <code>usb.img</code> is
+			BIOS-only. The rest of the target stays unpartitioned, and runtime
+			<code>cmds-dev</code> pack updates are not part of the cloned image.
 		</p>
 	</section>
 

+ 13 - 10
docs/drivers.html

@@ -231,7 +231,7 @@
 		<figure class="figure">
 			<svg viewBox="0 0 920 300" role="img" aria-labelledby="usb-title usb-desc">
 				<title id="usb-title">USB controller topology</title>
-				<desc id="usb-desc">EHCI handles high-speed root ports, UHCI and OHCI handle low and full speed devices, xHCI handles USB2 root ports. All feed the same USB core, which drives HID input and USB mass storage.</desc>
+				<desc id="usb-desc">EHCI handles high-speed root ports, UHCI and OHCI handle low and full speed devices, xHCI handles USB2 and USB3 root ports. All feed the same USB core, which drives HID input and USB mass storage.</desc>
 
 				<rect class="svg-box accent" x="30" y="24" width="200" height="64" rx="9"/>
 				<text class="svg-text" x="50" y="50">EHCI</text>
@@ -247,7 +247,7 @@
 
 				<rect class="svg-box green" x="696" y="24" width="194" height="64" rx="9"/>
 				<text class="svg-text" x="716" y="50">xHCI</text>
-				<text class="svg-sub" x="716" y="70">USB2 root ports only</text>
+				<text class="svg-sub" x="716" y="70">USB2 + USB3 root ports</text>
 
 				<path class="svg-line accent" d="M130 88v40h330v28"/>
 				<path class="svg-line blue" d="M352 88v68"/>
@@ -282,7 +282,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, TDs start at <code>CC = NotAccessed</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 root ports only; USB3 SuperSpeed devices are not handled. 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 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>
 				</tbody>
 			</table>
 		</div>
@@ -305,18 +305,21 @@
 			identifies the disk with SCSI INQUIRY + READ CAPACITY. Commands use
 			Bulk-Only Transport with LUN 0 and 512-byte blocks: READ(10),
 			WRITE(10), SYNCHRONIZE CACHE and a BOT reset/endpoint-halt recovery.
-			Present devices are registered as <code>usb0..</code>
+			Short bulk-IN chunks are accumulated within a BOT data stage; a
+			failed READ(10) gets REQUEST SENSE, retries and a 1-sector
+			fallback. Present devices are registered as <code>usb0..</code>
 			(<code>BLK_USB</code>) by <code>blk_scan()</code>, so
 			<code>storage</code>, the VFS, <code>format</code> and
 			<code>install</code> can use them.
 		</p>
 		<p>
 			Bulk transfers exist on EHCI and xHCI only; a mass-storage device on
-			a UHCI/OHCI companion is left unclaimed, and the xHCI driver does not
-			drive USB3 SuperSpeed ports. This is what makes the installer's
-			<code>usb0</code> source possible on the supported hardware: it reads
-			the live image from the stick and clones its used sectors to the
-			target.
+			a UHCI/OHCI companion is left unclaimed. The xHCI driver drives
+			direct USB3 root-port devices (USB3 hubs are not decoded), so a
+			SuperSpeed stick on the Chromebook works. This is what makes the
+			installer's <code>usb0</code> source possible: it reads the whole
+			live image into RAM first and then writes the target, so slow eMMC
+			writes cannot leave the USB source idle between chunks.
 		</p>
 	</section>
 
@@ -463,7 +466,7 @@
 		<ul>
 			<li><strong>Polling.</strong> USB, storage and network drivers consume CPU time while waiting; there is no IRQ-driven data path outside PS/2, serial and the PIT.</li>
 			<li><strong>EHCI.</strong> High-speed root ports only; no split transactions for low/full-speed devices behind a high-speed hub.</li>
-			<li><strong>xHCI.</strong> USB2 root ports only; USB3 SuperSpeed devices are not handled.</li>
+			<li><strong>xHCI.</strong> USB2 and USB3 root ports; direct SuperSpeed devices work, but USB3 hub port-status decoding is not implemented.</li>
 			<li><strong>HID.</strong> Report IDs are not handled, absolute pointers/tablets are ignored, and there is no wheel support.</li>
 			<li><strong>AHCI.</strong> First controller only, no hotplug after boot, disks above 2 TB are clamped to 32-bit LBA.</li>
 			<li><strong>eMMC.</strong> Polled 1-bit/25 MHz PIO; no DMA, IRQ, CMD6 bus-width/high-speed/DDR switch or hotplug. Only the eMMC function is driven; the SDIO/SD functions and SD card reader are not.</li>

+ 4 - 3
docs/userland.html

@@ -283,9 +283,10 @@
 			<code>install &lt;target&gt; [source]</code> clones the live NebOS
 			image to a target disk: it auto-detects a source whose MBR and LBA-63
 			boot sector carry the NebOS signature (preferring <code>usb0</code>),
-			copies the image's used sectors, writes the MBR last and verifies the
-			result. It installs the flashed image, so both the BIOS and UEFI boot
-			paths are cloned as-is.
+			reads the source image into RAM, then writes the target from RAM,
+			writes the MBR last and verifies the result. A source-read failure
+			leaves the target untouched. It installs the flashed image, so both
+			the BIOS and UEFI boot paths are cloned as-is.
 		</p>
 	</section>
 

+ 101 - 19
install.c

@@ -14,7 +14,9 @@
 #include "blkdev.h"
 #include "fatformat.h"
 #include "io.h"
+#include "mm.h"
 #include "process.h"
+#include "sched.h"
 #include "serial.h"
 #include "vga.h"
 #include "vfs.h"
@@ -35,6 +37,42 @@
 static unsigned char sector0[512];
 static unsigned char copy_buf[INSTALL_CHUNK * 512];
 
+/* The copy and verify release the BKL around each chunk, like the format
+ * raw-sector loop, so the WM and terminals keep running.  format_busy keeps
+ * disk-backed filesystem syscalls out; the device descriptors are copied
+ * before the loop so a mount rescan cannot invalidate them. */
+static int read_chunk(blk_dev_t *d, unsigned int lba, void *buf,
+		      unsigned int n)
+{
+	int r;
+
+	bkl_pause();
+	r = blk_read(d, lba, buf, n);
+	bkl_resume();
+	return r;
+}
+
+static int write_chunk(blk_dev_t *d, unsigned int lba, const void *buf,
+		       unsigned int n)
+{
+	int r;
+
+	bkl_pause();
+	r = blk_write(d, lba, buf, n);
+	bkl_resume();
+	return r;
+}
+
+static void free_pages(unsigned char *p, unsigned int pages)
+{
+	unsigned int i;
+
+	if (!p)
+		return;
+	for (i = 0; i < pages; i++)
+		mm_free_page((unsigned int)p + i * 4096u);
+}
+
 static unsigned int get16(const unsigned char *p)
 {
 	return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
@@ -230,6 +268,10 @@ void program_install_main(const char *arg)
 	char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX];
 	char source_dev[INSTALL_NAME_MAX];
 	blk_dev_t *target, *source;
+	blk_dev_t src_copy, dst_copy;
+	unsigned char *image = 0;
+	unsigned int image_pages = 0;
+	int have_copies = 0;
 	unsigned int used, i, j, lba, sum, vsum;
 	int has_esp = 0, explicit_source = 0;
 	int k;
@@ -341,9 +383,9 @@ void program_install_main(const char *arg)
 	io_putstr("? (y/n) ");
 	vga_fb_flush();
 
-	/* The copy runs with the BKL held: a USB source transfer must not be
-	 * interrupted by a bus rescan.  The confirmation loop yields so the
-	 * kworker can poll the USB HID keyboard. */
+	/* The confirmation loop yields so the kworker can poll the USB HID
+	 * keyboard.  The copy then releases the BKL per chunk; do not run
+	 * lsusb (a bus rescan) while an install is in progress. */
 	io_set_raw_keyboard(1);
 	for (;;) {
 		k = io_poll_key_with_hw();
@@ -376,42 +418,74 @@ void program_install_main(const char *arg)
 		io_putstr("install: device set changed; aborted\n");
 		goto fail;
 	}
-	if (used > target->total_sectors) {
+	src_copy = *source;
+	dst_copy = *target;
+	have_copies = 1;
+	if (used > dst_copy.total_sectors) {
 		io_putstr("install: source no longer fits; aborted\n");
 		goto fail;
 	}
-	if (!check_nebos_image(source, &used, &has_esp, sector0)) {
+	if (!check_nebos_image(&src_copy, &used, &has_esp, sector0)) {
 		io_putstr("install: source changed; aborted\n");
 		goto fail;
 	}
-	if (used > target->total_sectors) {
+	if (used > dst_copy.total_sectors) {
 		io_putstr("install: source no longer fits; aborted\n");
 		goto fail;
 	}
 
 	serial_puts(SERIAL_COM1, "install: ");
-	serial_puts(SERIAL_COM1, source->name);
+	serial_puts(SERIAL_COM1, src_copy.name);
 	serial_puts(SERIAL_COM1, " -> ");
-	serial_puts(SERIAL_COM1, target->name);
+	serial_puts(SERIAL_COM1, dst_copy.name);
 	serial_puts(SERIAL_COM1, ", ");
 	serial_putdec(SERIAL_COM1, used);
 	serial_puts(SERIAL_COM1, " sectors\n");
 
-	io_putstr("install: copying");
+	/* Read the whole image into RAM first.  Interleaving USB source reads
+	 * with the slow eMMC target writes lets the USB3 link go idle between
+	 * chunks; some sticks then fail the next READ(10) (the eMMC target is
+	 * still written from RAM afterwards). */
+	image_pages = (used * 512u + 4095u) / 4096u;
+	image = (unsigned char *)mm_alloc_pages(image_pages);
+	if (!image) {
+		io_putstr("install: out of memory for the source image\n");
+		goto fail;
+	}
+	for (i = 0; i < sizeof(sector0); i++)
+		image[i] = sector0[i];
+	/* LBA 1..62 are the unused zero gap (partition 1 starts at 63). */
+	for (i = sizeof(sector0); i < BOOT_LBA * 512u; i++)
+		image[i] = 0;
+	sum = sum_bytes(0, sector0, sizeof(sector0));
+
+	io_putstr("install: reading source");
 	vga_fb_flush();
 
-	sum = sum_bytes(0, sector0, sizeof(sector0));
-	for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
+	/* Keep the BKL here: the USB source must stay active across all
+	 * chunks.  The target writes below release it per chunk. */
+	for (lba = BOOT_LBA; lba < used; lba += INSTALL_CHUNK) {
 		unsigned int n = used - lba;
 
 		if (n > INSTALL_CHUNK)
 			n = INSTALL_CHUNK;
-		if (blk_read(source, lba, copy_buf, n) != 0) {
+		if (blk_read(&src_copy, lba, image + lba * 512u, n) != 0) {
 			install_fail("source read failed", lba);
 			goto fail;
 		}
-		sum = sum_bytes(sum, copy_buf, n * 512u);
-		if (blk_write(target, lba, copy_buf, n) != 0) {
+		sum = sum_bytes(sum, image + lba * 512u, n * 512u);
+		if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
+			io_putchar('.');
+	}
+	io_putchar('\n');
+
+	io_putstr("install: writing target");
+	for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
+		unsigned int n = used - lba;
+
+		if (n > INSTALL_CHUNK)
+			n = INSTALL_CHUNK;
+		if (write_chunk(&dst_copy, lba, image + lba * 512u, n) != 0) {
 			install_fail("target write failed", lba);
 			goto fail;
 		}
@@ -420,15 +494,18 @@ void program_install_main(const char *arg)
 	}
 
 	/* MBR last: an interrupted copy is not bootable. */
-	if (blk_write(target, 0, sector0, 1) != 0) {
+	if (write_chunk(&dst_copy, 0, image, 1) != 0) {
 		install_fail("target MBR write failed", 0);
 		goto fail;
 	}
 	io_putchar('\n');
 
 	{
-		int fr = blk_flush(target);
+		int fr;
 
+		bkl_pause();
+		fr = blk_flush(&dst_copy);
+		bkl_resume();
 		if (fr != 0)
 			io_putstr("install: cache flush failed; verifying anyway\n");
 	}
@@ -440,7 +517,7 @@ void program_install_main(const char *arg)
 
 		if (n > INSTALL_CHUNK)
 			n = INSTALL_CHUNK;
-		if (blk_read(target, lba, copy_buf, n) != 0) {
+		if (read_chunk(&dst_copy, lba, copy_buf, n) != 0) {
 			install_fail("target read-back failed", lba);
 			goto fail;
 		}
@@ -454,14 +531,19 @@ void program_install_main(const char *arg)
 	format_putdec(used);
 	io_putstr(" sectors\n");
 
-	vfs_remount_device(target);
+	free_pages(image, image_pages);
+	image = 0;
+	vfs_remount_device(&dst_copy);
 	format_busy = 0;
 	io_putstr("install: done; remove the install medium and reboot.\n");
 	serial_puts(SERIAL_COM1, "install: done\n");
 	return;
 
 fail:
-	vfs_remount_device(target);
+	if (image)
+		free_pages(image, image_pages);
+	if (have_copies)
+		vfs_remount_device(&dst_copy);
 	format_busy = 0;
 	io_putstr("install: target is incomplete; do not boot it\n");
 	serial_puts(SERIAL_COM1, "install: failed\n");

+ 4 - 3
tools/qmp_install_test.py

@@ -168,9 +168,10 @@ def main():
         f.truncate(32 * 1024 * 1024)
 
     if xhci:
-        # p3=0 keeps the whole xHCI bus at USB2 speeds; the kernel's xHCI
-        # driver deliberately does not drive USB3 SuperSpeed ports.
-        usb = ["-device", "qemu-xhci,p2=4,p3=0",
+        # qemu-xhci's first port is SuperSpeed: this exercises the USB3
+        # path used by the Chromebook's USB 3.0 ports.  The HID devices
+        # land on the USB2 ports of the same controller.
+        usb = ["-device", "qemu-xhci",
                "-device", "usb-storage,drive=src",
                "-device", "usb-mouse", "-device", "usb-kbd"]
     else:

+ 73 - 9
usbmsc.c

@@ -12,6 +12,7 @@
 
 #include "usbmsc.h"
 #include "blkdev.h"
+#include "io.h"
 #include "serial.h"
 #include "timer.h"
 #include "usb.h"
@@ -34,6 +35,8 @@ struct usb_msc {
 	unsigned int iface;
 	unsigned int sectors;
 	unsigned int tag;
+	unsigned int last_status, last_residue;
+	unsigned int sense_key, sense_asc, sense_ascq;
 	char model[41];
 };
 
@@ -100,12 +103,12 @@ static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
 		if (n < 0)
 			return -1;
 		if (n > 0) {
+			/* A short packet is normal inside a BOT data stage:
+			 * the device may split it at any USB transfer
+			 * boundary.  Keep reading until the Stage's known
+			 * byte count is reached. */
 			got += (unsigned int)n;
 			t0 = get_ticks_ms();
-			/* A short packet before the requested end means the
-			 * device ended the data stage early. */
-			if (got < len && (unsigned int)n < want)
-				return -1;
 		} else if (get_ticks_ms() - t0 > MSC_TIMEOUT_MS) {
 			return -1;
 		}
@@ -164,12 +167,31 @@ static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
 	}
 	if (msc_bulk_in(m, csw, sizeof(csw)) != 0)
 		return -1;
-	if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag)
+	if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag) {
+		m->last_status = 0xFF;
 		return -1;
+	}
+	m->last_status = csw[12];
+	m->last_residue = rd32le(csw + 8);
 	if (csw[12] != 0)
 		return -1;
-	if (rd32le(csw + 8) != 0)	/* residue */
+	if (m->last_residue != 0)
+		return -1;
+	return 0;
+}
+
+/* Fetch the sense data after a failed command so the next CBW does not keep
+ * returning the same unit attention/medium error. */
+static int msc_sense(struct usb_msc *m)
+{
+	unsigned char cb[6] = { 0x03, 0, 0, 0, 18, 0 };
+	unsigned char data[18];
+
+	if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
 		return -1;
+	m->sense_key = data[2] & 0x0F;
+	m->sense_asc = data[12];
+	m->sense_ascq = data[13];
 	return 0;
 }
 
@@ -213,6 +235,21 @@ static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
 	return bot_cmd(m, cb, sizeof(cb), buf, count * 512u, 1);
 }
 
+static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
+			    unsigned int count, void *buf)
+{
+	unsigned int i;
+
+	for (i = 0; i < 3; i++) {
+		if (msc_read10(m, lba, count, buf) == 0)
+			return 0;
+		msc_sense(m);
+		msc_reset(m);
+		msc_delay(10);
+	}
+	return -1;
+}
+
 static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count,
 		       const void *buf)
 {
@@ -468,10 +505,37 @@ int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
 	while (count) {
 		unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
 
-		if (msc_read10(m, lba, n, p) != 0) {
-			msc_reset(m);
-			if (msc_read10(m, lba, n, p) != 0)
+		if (msc_read10_retry(m, lba, n, p) != 0) {
+			/* Some bridges/Sticks fail a large READ(10) but
+			 * accept the same range one sector at a time.  This
+			 * also narrows a real medium error to one LBA. */
+			if (n > 1) {
+				unsigned int i;
+
+				io_putstr("usbmsc: lba ");
+				io_puthex(lba);
+				io_putstr(" retrying 1-sector reads\n");
+				for (i = 0; i < n; i++)
+					if (msc_read10_retry(m, lba + i, 1,
+							     p + i * 512u) != 0) {
+						io_putstr("usbmsc: sector ");
+						io_puthex(lba + i);
+						io_putstr(" failed, sense ");
+						io_puthex((m->sense_key << 16) |
+							  (m->sense_asc << 8) |
+							  m->sense_ascq);
+						io_putchar('\n');
+						return -1;
+					}
+			} else {
+				io_putstr("usbmsc: lba ");
+				io_puthex(lba);
+				io_putstr(" failed, sense ");
+				io_puthex((m->sense_key << 16) |
+					  (m->sense_asc << 8) | m->sense_ascq);
+				io_putchar('\n');
 				return -1;
+			}
 		}
 		lba += n;
 		p += n * 512u;

+ 27 - 19
xhci.c

@@ -1,10 +1,11 @@
 /* xHCI (USB 3.0) host controller driver.
  *
- * Scope: one controller, USB2 root ports and devices plugged directly into
- * them.  The USB3 ports are left alone (devices there need SuperSpeed
- * resource/companion-descriptor handling that the HID stack does not need
- * yet).  Control transfers and one interrupt-IN endpoint per HID interface
- * are implemented, which is exactly what usbhid.c uses.
+ * Scope: one controller, USB2 and USB3 root ports and devices plugged
+ * directly into them.  Control transfers, one interrupt-IN endpoint per HID
+ * interface and the bulk endpoints used by ASIX and usbmsc are implemented.
+ * SuperSpeed devices use a 512-byte EP0 and their endpoint descriptors may
+ * carry a companion descriptor, which is ignored because only the bulk/HID
+ * endpoint itself is needed.
  *
  * The schedule is polled, like the rest of the kernel.  Rings and contexts
  * are page-allocated so the driver does not grow the kernel BSS. */
@@ -189,7 +190,7 @@ static unsigned int db_off;
 static unsigned int max_slots;
 static unsigned int max_ports;
 static unsigned int ctx_size;
-static unsigned int usb2_mask;
+static unsigned int port_mask;
 static unsigned int hcc_params;
 static unsigned int pci_bus, pci_dev, pci_func;
 static int found;
@@ -635,7 +636,7 @@ static int xhci_config_ep(struct xhci_slot *s, unsigned int ep_index,
  * enumeration scheme).  bsr=0: the command carries target_addr and moves
  * Enabled -> Addressed directly (traditional scheme), which is what Linux
  * does when the port is addressed before the first descriptor read.
- * mps == 0 means "derive it from the port speed". */
+ * mps == 0 (or a SuperSpeed port) derives it from the port speed. */
 static struct xhci_slot *new_slot_common(unsigned int target_addr,
 					 unsigned int mps, int bsr)
 {
@@ -680,7 +681,7 @@ 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)
+	if (!mps || s->speed >= 4)
 		mps = ep0_mps_for_speed(s->speed);
 
 	ram_wr64(dcbaa + slot_id * 8, dc);
@@ -777,6 +778,11 @@ 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);
+
 	spin_lock_irqsave(&xhci_lock, &flags);
 	zero_page(input_ctx, 4096);
 	*(unsigned int *)input_ctx = 0;
@@ -1239,7 +1245,7 @@ static int port_reset(unsigned int ctrl, unsigned int port)
 
 	(void)ctrl;
 	if (!found || port >= max_ports ||
-	    !(usb2_mask & (1u << port)))
+	    !(port_mask & (1u << port)))
 		return -1;
 
 	if (port_slot[port])
@@ -1298,9 +1304,8 @@ static int port_reset(unsigned int ctrl, unsigned int port)
 		return -1;
 	}
 
-	/* USB2 reset recovery: the device needs TRSTRCY (10 ms) before it
-	 * answers the first control request.  QEMU replies immediately,
-	 * real devices do not. */
+	/* Reset recovery: USB2 devices need TRSTRCY (10 ms) before the first
+	 * control request; the delay is harmless for SuperSpeed. */
 	delay_ms(10);
 
 	last_port = (int)port;
@@ -1313,7 +1318,7 @@ static int port_connected(unsigned int ctrl, unsigned int port)
 
 	(void)ctrl;
 	if (!found || port >= max_ports ||
-	    !(usb2_mask & (1u << port)))
+	    !(port_mask & (1u << port)))
 		return 0;
 	ps = mmio_rd(op(XHCI_OP_PORTSC) + port * 0x10);
 	return (ps & PORTSC_CCS) ? 1 : 0;
@@ -1398,7 +1403,7 @@ static void scan_protocols(void)
 {
 	unsigned int off, guard = 0;
 
-	usb2_mask = 0;
+	port_mask = 0;
 	off = ((hcc_params >> 16) & 0xFFFF) << 2;
 
 	while (off && guard++ < 32) {
@@ -1413,9 +1418,12 @@ static void scan_protocols(void)
 			unsigned int pc = (pi >> 8) & 0xFF;
 			unsigned int i;
 
-			if (major <= 2 && po >= 1) {
+			/* The controller may expose separate USB2 and USB3
+			 * protocol entries for the same physical connectors;
+			 * include both so a SuperSpeed device is not skipped. */
+			if ((major == 2 || major == 3) && po >= 1) {
 				for (i = 0; i < pc && po + i <= 32; i++)
-					usb2_mask |= 1u << (po - 1 + i);
+					port_mask |= 1u << (po - 1 + i);
 			}
 		}
 		if (!next)
@@ -1423,8 +1431,8 @@ static void scan_protocols(void)
 		off += next << 2;
 	}
 
-	if (!usb2_mask)
-		usb2_mask = (max_ports >= 32) ? 0xFFFFFFFFu :
+	if (!port_mask)
+		port_mask = (max_ports >= 32) ? 0xFFFFFFFFu :
 			    ((1u << max_ports) - 1);
 }
 
@@ -1644,7 +1652,7 @@ found_xhci:
 	 * while commands complete normally. */
 
 	scan_protocols();
-	dbg_hex("xhci: usb2 ports ", usb2_mask);
+	dbg_hex("xhci: ports ", port_mask);
 
 	/* Power every root port.  VBus is supposed to come up after HCRST,
 	 * but some firmware/PPC combinations leave PP clear; the USB3