Răsfoiți Sursa

Start the UI before storage and network probing

Storage bring-up used to run synchronously before the WM and probe hardware that was not there: every empty AHCI port waited out a COMRESET timeout, an absent floppy went through the ten-retry recovery loop, and floating legacy IDE ports waited a second per target. The first frame could therefore be several seconds late.

Bring AHCI ports up lazily in ahci_scan(), skip DET 0/4 ports and keep a port_ready bitmap so a later scan still sees a newly attached disk.  Probe floppies with one short read and cache a missing controller; keep per-drive motor state instead of resetting the FDC on every drive switch.  Fail a floating IDE channel before the BSY wait.

Start the WM from /ramfs first, then run the storage scan, spawn the first terminal and do the NIC probe/DHCP.  vfs_mount_all() keeps ramfs/share mounted and publishes vfs_scan_busy so file syscalls wait for the mount rebuild.  r8168 no longer blocks boot waiting for link.

Add boot-stage, per-device, per-port and first-frame timings to COM1 (and therefore the netlog mirror).
Johannes Zellner 2 săptămâni în urmă
părinte
comite
7166634f8c
13 a modificat fișierele cu 389 adăugiri și 107 ștergeri
  1. 45 10
      ahci.c
  2. 4 2
      ahci.h
  3. 19 3
      ata.c
  4. 43 6
      blkdev.c
  5. 1 1
      blkdev.h
  6. 134 38
      disk.c
  7. 7 1
      disk.h
  8. 56 11
      kernel.c
  9. 4 24
      r8168.c
  10. 10 0
      sys_fs.c
  11. 49 8
      vfs.c
  12. 8 1
      vfs.h
  13. 9 2
      wm.c

+ 45 - 10
ahci.c

@@ -73,6 +73,7 @@ static struct {
 	unsigned int abar;
 	unsigned int abar;
 	unsigned int nports;
 	unsigned int nports;
 	unsigned int pi;
 	unsigned int pi;
+	unsigned int port_ready;      /* bitmask: port brought up and usable */
 	unsigned int cl;              /* command list (1 page) */
 	unsigned int cl;              /* command list (1 page) */
 	unsigned int fis;             /* FIS receive area (1 page) */
 	unsigned int fis;             /* FIS receive area (1 page) */
 	unsigned int ct;              /* command table (1 page) */
 	unsigned int ct;              /* command table (1 page) */
@@ -172,7 +173,15 @@ static int ahci_find(unsigned int *out_bus, unsigned int *out_dev, unsigned int
 
 
 static void ahci_port_init(unsigned int port)
 static void ahci_port_init(unsigned int port)
 {
 {
-	unsigned int t0, c;
+	unsigned int det, t0, c;
+
+	/* An implemented port with no device (DET=0) or a powered-down PHY
+	 * (DET=4) has nothing to bring up.  Issuing COMRESET and waiting for
+	 * the link there is what made boot spend ~1 s on every empty port;
+	 * the port is initialised lazily by ahci_scan() if a device appears. */
+	det = prd(port, P_SSTS) & 0x0Fu;
+	if (det == 0 || det == 4)
+		return;
 
 
 	/* Stop the port and its FIS receive engine. */
 	/* Stop the port and its FIS receive engine. */
 	c = prd(port, P_CMD);
 	c = prd(port, P_CMD);
@@ -220,7 +229,7 @@ static void ahci_port_init(unsigned int port)
 
 
 int ahci_init(void)
 int ahci_init(void)
 {
 {
-	unsigned int bus, dev, func, bar, cmd, cap, i;
+	unsigned int bus, dev, func, bar, cmd, cap;
 
 
 	if (ahci.inited)
 	if (ahci.inited)
 		return ahci.ok ? 0 : -1;
 		return ahci.ok ? 0 : -1;
@@ -291,11 +300,8 @@ int ahci_init(void)
 	serial_puthex(SERIAL_COM1, ahci.pi);
 	serial_puthex(SERIAL_COM1, ahci.pi);
 	serial_putc(SERIAL_COM1, '\n');
 	serial_putc(SERIAL_COM1, '\n');
 
 
-	for (i = 0; i < ahci.nports; i++) {
-		if (ahci.pi & (1u << i))
-			ahci_port_init(i);
-	}
-
+	/* Ports are brought up on demand by ahci_scan(): empty ports are
+	 * skipped, and a scan after a disk was plugged in still works. */
 	ahci.ok = 1;
 	ahci.ok = 1;
 	return 0;
 	return 0;
 }
 }
@@ -485,14 +491,43 @@ unsigned int ahci_scan(struct ahci_disk *out, unsigned int max)
 		return 0;
 		return 0;
 
 
 	for (port = 0; port < ahci.nports && n < max; port++) {
 	for (port = 0; port < ahci.nports && n < max; port++) {
-		if (!(ahci.pi & (1u << port)))
+		unsigned int ssts, bit = 1u << port;
+
+		if (!(ahci.pi & bit))
 			continue;
 			continue;
-		if ((prd(port, P_SSTS) & 0x0Fu) != 3)
+
+		ssts = prd(port, P_SSTS) & 0x0Fu;
+		if (ssts == 0 || ssts == 4) {
+			/* No device (or PHY offline).  Drop the ready bit so a
+			 * later scan re-initialises the port if a disk appears. */
+			ahci.port_ready &= ~bit;
 			continue;
 			continue;
+		}
+		if (!(ahci.port_ready & bit)) {
+			unsigned int t0 = get_ticks_ms();
+
+			ahci_port_init(port);
+			serial_puts(SERIAL_COM1, "ahci: port ");
+			serial_putdec(SERIAL_COM1, port);
+			serial_puts(SERIAL_COM1, " init ");
+			serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
+			serial_puts(SERIAL_COM1, " ms\n");
+			if ((prd(port, P_SSTS) & 0x0Fu) != 3)
+				continue;
+			ahci.port_ready |= bit;
+		}
+		if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
+			ahci.port_ready &= ~bit;
+			continue;
+		}
 		if (prd(port, P_SIG) != SIG_ATA)
 		if (prd(port, P_SIG) != SIG_ATA)
 			continue;              /* ATAPI / port multiplier / SEMB */
 			continue;              /* ATAPI / port multiplier / SEMB */
-		if (ahci_identify_port(port, &out[n].sectors, out[n].model) != 0)
+		if (ahci_identify_port(port, &out[n].sectors, out[n].model) != 0) {
+			/* A slow disk may not answer yet; leave the port for a
+			 * later scan instead of reporting a half-initialised one. */
+			ahci.port_ready &= ~bit;
 			continue;
 			continue;
+		}
 		out[n].port = port;
 		out[n].port = port;
 		serial_puts(SERIAL_COM1, "ahci: port ");
 		serial_puts(SERIAL_COM1, "ahci: port ");
 		serial_putdec(SERIAL_COM1, port);
 		serial_putdec(SERIAL_COM1, port);

+ 4 - 2
ahci.h

@@ -13,8 +13,10 @@ struct ahci_disk {
 	char model[41];
 	char model[41];
 };
 };
 
 
-/* Initialise the first AHCI controller. Idempotent; returns 0 if a controller
- * was found and brought up, -1 otherwise. */
+/* Initialise the first AHCI controller (register window + DMA areas).  Ports
+ * are brought up lazily by ahci_scan(), so empty ports cost nothing at boot.
+ * Idempotent; returns 0 if a controller was found and brought up, -1
+ * otherwise. */
 int ahci_init(void);
 int ahci_init(void);
 
 
 /* 1 if an AHCI controller was found and initialised. */
 /* 1 if an AHCI controller was found and initialised. */

+ 19 - 3
ata.c

@@ -170,6 +170,16 @@ static void ata_400ns(unsigned short base)
 	(void)port_inb(base + REG_CMD);
 	(void)port_inb(base + REG_CMD);
 }
 }
 
 
+/* A channel with no device floats to 0xFF.  Treat that as "absent" before
+ * any reset/wait: otherwise an AHCI-only machine spends a full second in
+ * ata_reset_channel() for each of the four legacy targets.  (0x00 is left
+ * alone: a drive that has not finished spinning up can report it, and the
+ * IDENTIFY path already fails fast when it stays 0x00.) */
+static int ata_bus_floating(unsigned short base)
+{
+	return port_inb(base + REG_CMD) == 0xFF;
+}
+
 static int ata_wait_not_busy(unsigned short base, unsigned int timeout_ms)
 static int ata_wait_not_busy(unsigned short base, unsigned int timeout_ms)
 {
 {
 	unsigned int t0 = get_ticks_ms();
 	unsigned int t0 = get_ticks_ms();
@@ -196,9 +206,12 @@ static void ata_reset_channel(unsigned short base, unsigned short ctrl)
 	 * instead of leaving a masked level-triggered interrupt asserted. */
 	 * instead of leaving a masked level-triggered interrupt asserted. */
 	port_outb(ctrl, 0x02);
 	port_outb(ctrl, 0x02);
 	t0 = get_ticks_ms();
 	t0 = get_ticks_ms();
-	while ((port_inb(base + REG_CMD) & SR_BSY) &&
-	       get_ticks_ms() - t0 < 1000)
-		;
+	while (get_ticks_ms() - t0 < 1000) {
+		unsigned char st = port_inb(base + REG_CMD);
+
+		if (!(st & SR_BSY) || st == 0x00 || st == 0xFF)
+			break;
+	}
 	ata_400ns(base);
 	ata_400ns(base);
 }
 }
 
 
@@ -241,6 +254,9 @@ static int ata_identify(int bus, int unit, unsigned short *id)
 	unsigned int t0;
 	unsigned int t0;
 	unsigned int i;
 	unsigned int i;
 
 
+	if (ata_bus_floating(base))
+		return -1;             /* floating bus: no device on this channel */
+
 	/* A previous failed transfer can leave the drive waiting for PIO
 	/* A previous failed transfer can leave the drive waiting for PIO
 	 * data, in which case it ignores IDENTIFY and returns zeros. */
 	 * data, in which case it ignores IDENTIFY and returns zeros. */
 	if (ata_wait_idle(base, 1000) != 0)
 	if (ata_wait_idle(base, 1000) != 0)

+ 43 - 6
blkdev.c

@@ -4,6 +4,8 @@
 #include "ahci.h"
 #include "ahci.h"
 #include "ata.h"
 #include "ata.h"
 #include "disk.h"
 #include "disk.h"
+#include "serial.h"
+#include "timer.h"
 
 
 blk_dev_t blk_devs[BLKDEV_MAX];
 blk_dev_t blk_devs[BLKDEV_MAX];
 unsigned int blk_dev_count;
 unsigned int blk_dev_count;
@@ -41,17 +43,40 @@ static blk_dev_t *add(const char *name, blk_kind_t kind,
 	return d;
 	return d;
 }
 }
 
 
+static void blk_trace(const char *name, int present, unsigned int ms)
+{
+	serial_puts(SERIAL_COM1, "blk: ");
+	serial_puts(SERIAL_COM1, name);
+	serial_puts(SERIAL_COM1, present ? " present " : " absent ");
+	serial_putdec(SERIAL_COM1, ms);
+	serial_puts(SERIAL_COM1, " ms\n");
+}
+
 void blk_scan(void)
 void blk_scan(void)
 {
 {
 	blk_dev_t *d;
 	blk_dev_t *d;
+	unsigned int t0;
 
 
 	blk_dev_count = 0;
 	blk_dev_count = 0;
 
 
-	/* Floppy drives 0 and 1. */
+	/* Floppy drives 0 and 1.  Probe for media instead of assuming both
+	 * drives exist: an empty drive would otherwise go through the full
+	 * ten-retry recovery loop at every mount. */
+	t0 = get_ticks_ms();
 	d = add("floppy0", BLK_FLOPPY, 0, 0);
 	d = add("floppy0", BLK_FLOPPY, 0, 0);
-	if (d) { d->present = 1; d->total_sectors = 2880; }
+	if (d) {
+		d->present = disk_probe(0);
+		d->total_sectors = 2880;
+	}
+	blk_trace("floppy0", d ? d->present : 0, get_ticks_ms() - t0);
+
+	t0 = get_ticks_ms();
 	d = add("floppy1", BLK_FLOPPY, 1, 0);
 	d = add("floppy1", BLK_FLOPPY, 1, 0);
-	if (d) { d->present = 1; d->total_sectors = 2880; }
+	if (d) {
+		d->present = disk_probe(1);
+		d->total_sectors = 2880;
+	}
+	blk_trace("floppy1", d ? d->present : 0, get_ticks_ms() - t0);
 
 
 	/* IDE: primary + secondary, master + slave.  The channel port bases
 	/* IDE: primary + secondary, master + slave.  The channel port bases
 	 * come from the PCI IDE interface (native mode) or the legacy default. */
 	 * come from the PCI IDE interface (native mode) or the legacy default. */
@@ -62,10 +87,15 @@ void blk_scan(void)
 		for (i = 0; i < 4; i++) {
 		for (i = 0; i < 4; i++) {
 			unsigned int sectors = 0;
 			unsigned int sectors = 0;
 			char model[41];
 			char model[41];
+			int ok;
+
 			d = add(names[i], BLK_IDE, i / 2, i % 2);
 			d = add(names[i], BLK_IDE, i / 2, i % 2);
 			if (!d)
 			if (!d)
 				continue;
 				continue;
-			if (ata_probe(i / 2, i % 2, &sectors, model) == 0) {
+			t0 = get_ticks_ms();
+			ok = ata_probe(i / 2, i % 2, &sectors, model) == 0;
+			blk_trace(names[i], ok, get_ticks_ms() - t0);
+			if (ok) {
 				unsigned int j;
 				unsigned int j;
 				d->present = 1;
 				d->present = 1;
 				d->total_sectors = sectors;
 				d->total_sectors = sectors;
@@ -79,8 +109,15 @@ void blk_scan(void)
 	/* SATA/AHCI: only present disks are registered, named by port index. */
 	/* SATA/AHCI: only present disks are registered, named by port index. */
 	{
 	{
 		struct ahci_disk disks[AHCI_MAX_DISKS];
 		struct ahci_disk disks[AHCI_MAX_DISKS];
-		unsigned int n = ahci_scan(disks, AHCI_MAX_DISKS);
-		unsigned int i;
+		unsigned int n, i;
+
+		t0 = get_ticks_ms();
+		n = ahci_scan(disks, AHCI_MAX_DISKS);
+		serial_puts(SERIAL_COM1, "blk: ahci ");
+		serial_putdec(SERIAL_COM1, n);
+		serial_puts(SERIAL_COM1, " disk(s) ");
+		serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
+		serial_puts(SERIAL_COM1, " ms\n");
 		for (i = 0; i < n; i++) {
 		for (i = 0; i < n; i++) {
 			unsigned int j;
 			unsigned int j;
 			d = add(sata_name(i, disks[i].port), BLK_SATA, 0, disks[i].port);
 			d = add(sata_name(i, disks[i].port), BLK_SATA, 0, disks[i].port);

+ 1 - 1
blkdev.h

@@ -11,7 +11,7 @@ typedef struct {
 	unsigned int bus;          /* floppy: drive #; ide: 0=primary,1=secondary;
 	unsigned int bus;          /* floppy: drive #; ide: 0=primary,1=secondary;
 				    * sata: controller index (0) */
 				    * sata: controller index (0) */
 	unsigned int unit;         /* ide: 0=master,1=slave; sata: AHCI port */
 	unsigned int unit;         /* ide: 0=master,1=slave; sata: AHCI port */
-	int present;               /* set by IDENTIFY; floppy: always listed */
+	int present;               /* set by IDENTIFY/probe; floppy: media present */
 	unsigned int total_sectors;/* 0 if unknown (floppy) */
 	unsigned int total_sectors;/* 0 if unknown (floppy) */
 	char model[41];            /* IDE/SATA model string */
 	char model[41];            /* IDE/SATA model string */
 } blk_dev_t;
 } blk_dev_t;

+ 134 - 38
disk.c

@@ -38,6 +38,7 @@
 
 
 #define MOTOR_DELAY_MS   300
 #define MOTOR_DELAY_MS   300
 #define IRQ_TIMEOUT_MS   2000
 #define IRQ_TIMEOUT_MS   2000
+#define PROBE_TIMEOUT_MS 500   /* boot-scan probe: shorter than normal I/O */
 #define MAX_RETRIES      10
 #define MAX_RETRIES      10
 #define SEEK_SETTLE_MS   25   /* head settle after seek */
 #define SEEK_SETTLE_MS   25   /* head settle after seek */
 
 
@@ -65,9 +66,12 @@ void fdc_irq(void)
 }
 }
 
 
 static int fdc_initialized;
 static int fdc_initialized;
-static int motor_on;
+static int fdc_absent;          /* controller did not answer at all */
+static unsigned int motor_mask; /* bits 0/1: motor spinning for drive N */
 static unsigned int cur_cylinder;
 static unsigned int cur_cylinder;
 static unsigned int current_drive;
 static unsigned int current_drive;
+static unsigned int irq_timeout_ms = IRQ_TIMEOUT_MS;
+static int quiet_errors;        /* presence probe: don't print read errors */
 
 
 /* ---- low-level FDC helpers ---- */
 /* ---- low-level FDC helpers ---- */
 
 
@@ -105,13 +109,30 @@ static int fdc_wait_irq(void)
 {
 {
 	unsigned int t0 = get_ticks_ms();
 	unsigned int t0 = get_ticks_ms();
 	while (!fdc_irq_fired) {
 	while (!fdc_irq_fired) {
-		if ((get_ticks_ms() - t0) >= IRQ_TIMEOUT_MS)
+		if ((get_ticks_ms() - t0) >= irq_timeout_ms)
 			return -1;
 			return -1;
 		__asm__ volatile ("hlt");
 		__asm__ volatile ("hlt");
 	}
 	}
 	return 0;
 	return 0;
 }
 }
 
 
+/* A controller that reads back 0x00/0xFF on the MSR is not there (or the
+ * floppy function is disabled).  Checking this before waiting for IRQ 6 keeps
+ * a machine with no floppy controller from sitting in a 2 s timeout. */
+static int fdc_controller_alive(void)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	for (;;) {
+		unsigned char msr = inb(FDC_MSR);
+
+		if (msr != 0x00 && msr != 0xFF && (msr & MSR_RQM))
+			return 1;
+		if ((get_ticks_ms() - t0) >= 200)
+			return 0;
+	}
+}
+
 static int fdc_sense_interrupt(unsigned char *st0, unsigned char *cyl)
 static int fdc_sense_interrupt(unsigned char *st0, unsigned char *cyl)
 {
 {
 	if (fdc_send(CMD_SENSE_INT) != 0)
 	if (fdc_send(CMD_SENSE_INT) != 0)
@@ -125,17 +146,36 @@ static int fdc_sense_interrupt(unsigned char *st0, unsigned char *cyl)
 
 
 /* ---- motor control ---- */
 /* ---- motor control ---- */
 
 
+/* Program DOR: controller on, DMA+IRQ enabled, current drive selected, and
+ * every motor that has been spun up left running.  Keeping the other drive's
+ * motor bit set avoids a second 300 ms spin-up when the scan switches from
+ * floppy0 to floppy1. */
+static void fdc_select_drive(void)
+{
+	unsigned int dor = 0x0Cu | current_drive;
+
+	if (motor_mask & 1u)
+		dor |= 1u << 4;
+	if (motor_mask & 2u)
+		dor |= 1u << 5;
+	outb(FDC_DOR, (unsigned char)dor);
+}
+
 static void fdc_motor_on(void)
 static void fdc_motor_on(void)
 {
 {
-	if (motor_on)
+	unsigned int bit = 1u << current_drive;
+
+	if (motor_mask & bit) {
+		fdc_select_drive();     /* switch select bits, no delay */
 		return;
 		return;
-	outb(FDC_DOR, (unsigned char)((1 << (4 + current_drive)) | 0x0C | current_drive));
+	}
+	motor_mask |= bit;
+	fdc_select_drive();
 	{
 	{
 		unsigned int t0 = get_ticks_ms();
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < MOTOR_DELAY_MS)
 		while ((get_ticks_ms() - t0) < MOTOR_DELAY_MS)
 			__asm__ volatile ("hlt");
 			__asm__ volatile ("hlt");
 	}
 	}
-	motor_on = 1;
 }
 }
 
 
 /* ---- reset ---- */
 /* ---- reset ---- */
@@ -145,9 +185,12 @@ static int fdc_reset(void)
 	unsigned char st0, cyl;
 	unsigned char st0, cyl;
 	int i;
 	int i;
 
 
+	if (fdc_absent)
+		return -1;
+
 	/* Pull reset line: DOR = 0 turns off controller + motor */
 	/* Pull reset line: DOR = 0 turns off controller + motor */
 	outb(FDC_DOR, 0x00);
 	outb(FDC_DOR, 0x00);
-	motor_on = 0;
+	motor_mask = 0;
 	{
 	{
 		unsigned int t0 = get_ticks_ms();
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < 10)
 		while ((get_ticks_ms() - t0) < 10)
@@ -157,7 +200,7 @@ static int fdc_reset(void)
 	/* Release reset: controller on, DMA/IRQ enabled, no motor.
 	/* Release reset: controller on, DMA/IRQ enabled, no motor.
 	 * The FDC fires IRQ 6 once the reset completes. */
 	 * The FDC fires IRQ 6 once the reset completes. */
 	fdc_irq_fired = 0;
 	fdc_irq_fired = 0;
-	outb(FDC_DOR, (unsigned char)(0x0C | current_drive));
+	fdc_select_drive();
 
 
 	if (fdc_wait_irq() != 0)
 	if (fdc_wait_irq() != 0)
 		return -1;
 		return -1;
@@ -304,19 +347,21 @@ static int fdc_do_read(unsigned int cyl, unsigned int head, unsigned int sec)
 	}
 	}
 
 
 	if (results[0] & 0xC0) {
 	if (results[0] & 0xC0) {
-		io_putstr("fdc: CHS=");
-		io_puthex(cyl);
-		io_putchar('/');
-		io_puthex(head);
-		io_putchar('/');
-		io_puthex(sec);
-		io_putstr(" ST0=");
-		io_puthex(results[0]);
-		io_putstr(" ST1=");
-		io_puthex(results[1]);
-		io_putstr(" ST2=");
-		io_puthex(results[2]);
-		io_putchar('\n');
+		if (!quiet_errors) {
+			io_putstr("fdc: CHS=");
+			io_puthex(cyl);
+			io_putchar('/');
+			io_puthex(head);
+			io_putchar('/');
+			io_puthex(sec);
+			io_putstr(" ST0=");
+			io_puthex(results[0]);
+			io_putstr(" ST1=");
+			io_puthex(results[1]);
+			io_putstr(" ST2=");
+			io_puthex(results[2]);
+			io_putchar('\n');
+		}
 		return -1;
 		return -1;
 	}
 	}
 
 
@@ -351,19 +396,21 @@ static int fdc_do_write(unsigned int cyl, unsigned int head, unsigned int sec)
 	}
 	}
 
 
 	if (results[0] & 0xC0) {
 	if (results[0] & 0xC0) {
-		io_putstr("fdc write: CHS=");
-		io_puthex(cyl);
-		io_putchar('/');
-		io_puthex(head);
-		io_putchar('/');
-		io_puthex(sec);
-		io_putstr(" ST0=");
-		io_puthex(results[0]);
-		io_putstr(" ST1=");
-		io_puthex(results[1]);
-		io_putstr(" ST2=");
-		io_puthex(results[2]);
-		io_putchar('\n');
+		if (!quiet_errors) {
+			io_putstr("fdc write: CHS=");
+			io_puthex(cyl);
+			io_putchar('/');
+			io_puthex(head);
+			io_putchar('/');
+			io_puthex(sec);
+			io_putstr(" ST0=");
+			io_puthex(results[0]);
+			io_putstr(" ST1=");
+			io_puthex(results[1]);
+			io_putstr(" ST2=");
+			io_puthex(results[2]);
+			io_putchar('\n');
+		}
 		return -1;
 		return -1;
 	}
 	}
 
 
@@ -431,13 +478,16 @@ static int fdc_write_one_retry(unsigned int lba)
 
 
 static int fdc_init_once(void)
 static int fdc_init_once(void)
 {
 {
+	if (fdc_absent)
+		return -1;
+
 	/* Mask IRQ 6 before reset so the completion interrupt doesn't
 	/* Mask IRQ 6 before reset so the completion interrupt doesn't
 	 * fire before we're ready (IRQ 6 may already be unmasked from
 	 * fire before we're ready (IRQ 6 may already be unmasked from
 	 * a previous init).  Then clear the flag and release the reset. */
 	 * a previous init).  Then clear the flag and release the reset. */
 	outb(PIC1_DATA, inb(PIC1_DATA) | 0x40);
 	outb(PIC1_DATA, inb(PIC1_DATA) | 0x40);
 
 
 	outb(FDC_DOR, 0x00);
 	outb(FDC_DOR, 0x00);
-	motor_on = 0;
+	motor_mask = 0;
 	{
 	{
 		unsigned int t0 = get_ticks_ms();
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < 10)
 		while ((get_ticks_ms() - t0) < 10)
@@ -445,11 +495,18 @@ static int fdc_init_once(void)
 	}
 	}
 
 
 	fdc_irq_fired = 0;
 	fdc_irq_fired = 0;
-	outb(FDC_DOR, (unsigned char)(0x0C | current_drive));
+	fdc_select_drive();
 
 
 	/* Unmask IRQ 6 — the latched reset-completion interrupt fires now. */
 	/* Unmask IRQ 6 — the latched reset-completion interrupt fires now. */
 	outb(PIC1_DATA, inb(PIC1_DATA) & ~0x40);
 	outb(PIC1_DATA, inb(PIC1_DATA) & ~0x40);
 
 
+	/* No FDC at all: fail now instead of waiting out the IRQ timeout. */
+	if (!fdc_controller_alive()) {
+		outb(PIC1_DATA, inb(PIC1_DATA) | 0x40);
+		fdc_absent = 1;
+		return -1;
+	}
+
 	if (fdc_wait_irq() != 0)
 	if (fdc_wait_irq() != 0)
 		return -1;
 		return -1;
 
 
@@ -481,11 +538,50 @@ void disk_set_drive(unsigned int drive)
 {
 {
 	if (drive > 1)
 	if (drive > 1)
 		return;
 		return;
-	if (drive == current_drive && fdc_initialized)
+	if (drive == current_drive)
 		return;
 		return;
 	current_drive = drive;
 	current_drive = drive;
-	motor_on = 0;
-	fdc_initialized = 0;
+	if (fdc_initialized) {
+		/* Same controller, different drive: its motor may still be off
+		 * and its head position is unknown.  Keep the controller
+		 * initialised instead of paying a full reset. */
+		cur_cylinder = 0xFFFFFFFFu;
+		fdc_motor_on();
+		fdc_recalibrate();
+	}
+}
+
+/* Quick presence/media probe used by the boot scan.  One command with a short
+ * timeout, never the ten-retry recovery loop: an empty drive must not hold the
+ * UI back.  Normal I/O after a successful mount still gets full retries. */
+int disk_probe(unsigned int drive)
+{
+	int rc;
+
+	if (drive > 1 || fdc_absent)
+		return 0;
+
+	irq_timeout_ms = PROBE_TIMEOUT_MS;
+	disk_set_drive(drive);
+	if (!fdc_initialized && fdc_init_once() != 0) {
+		irq_timeout_ms = IRQ_TIMEOUT_MS;
+		return 0;
+	}
+
+	quiet_errors = 1;
+	rc = fdc_read_one(0);
+	quiet_errors = 0;
+	irq_timeout_ms = IRQ_TIMEOUT_MS;
+	if (rc != 0) {
+		/* A timed-out command may still be sitting in the FDC: force a
+		 * clean re-init next time.  A normal error response (empty
+		 * drive / no media) leaves the controller usable, so keep it
+		 * initialized for the mount that follows. */
+		if (rc == -2)
+			fdc_initialized = 0;
+		return 0;
+	}
+	return 1;
 }
 }
 
 
 unsigned int disk_get_drive(void)
 unsigned int disk_get_drive(void)

+ 7 - 1
disk.h

@@ -4,9 +4,15 @@
 #define DISK_H
 #define DISK_H
 
 
 /* Select the floppy drive (0 or 1) used by subsequent disk_read_sectors calls.
 /* Select the floppy drive (0 or 1) used by subsequent disk_read_sectors calls.
- * Resets FDC state so the next read re-initialises for the new drive. */
+ * Keeps the controller initialised; the new drive is spun up and recalibrated
+ * only when needed. */
 void disk_set_drive(unsigned int drive);
 void disk_set_drive(unsigned int drive);
 
 
+/* Quick boot-scan presence/media probe: one read with a short timeout, no
+ * recovery retries.  Returns 1 if a readable disk is in the drive, 0
+ * otherwise.  Normal I/O after a successful probe still gets full retries. */
+int disk_probe(unsigned int drive);
+
 /* Return currently selected drive number. */
 /* Return currently selected drive number. */
 unsigned int disk_get_drive(void);
 unsigned int disk_get_drive(void);
 
 

+ 56 - 11
kernel.c

@@ -264,6 +264,19 @@ static void kernel_idle_loop(void)
 	}
 	}
 }
 }
 
 
+/* Boot-stage timing: written to COM1, which the netlog mirror forwards to
+ * `make netmon` on boards without a serial port. */
+static unsigned int boot_t0;
+
+static void boot_trace(const char *what)
+{
+	serial_puts(SERIAL_COM1, "[boot] ");
+	serial_puts(SERIAL_COM1, what);
+	serial_puts(SERIAL_COM1, " +");
+	serial_putdec(SERIAL_COM1, get_ticks_ms() - boot_t0);
+	serial_puts(SERIAL_COM1, " ms\n");
+}
+
 void kernel_main(void);
 void kernel_main(void);
 void kernel_main(void)
 void kernel_main(void)
 {
 {
@@ -303,18 +316,25 @@ void kernel_main(void)
 	 * the current process, its CR3 and the TSS, so those must be valid. */
 	 * the current process, its CR3 and the TSS, so those must be valid. */
 	process_init();
 	process_init();
 	timer_init();
 	timer_init();
+	boot_t0 = get_ticks_ms();
+	boot_trace("timer");
 	/* The PIT (and its sti) is live, so the AP bring-up can use it as a
 	/* The PIT (and its sti) is live, so the AP bring-up can use it as a
 	 * timeout clock.  APs idle until smp_enable_sched() after boot. */
 	 * timeout clock.  APs idle until smp_enable_sched() after boot. */
 	smp_init();
 	smp_init();
+	boot_trace("smp");
 	mouse_init();
 	mouse_init();
 	/* USB HID backend (mouse + keyboard): controller bring-up + first
 	/* USB HID backend (mouse + keyboard): controller bring-up + first
 	 * scan; devices plugged in later are picked up by the hotplug
 	 * scan; devices plugged in later are picked up by the hotplug
 	 * watcher in the WM loop. */
 	 * watcher in the WM loop. */
 	usbhid_init();
 	usbhid_init();
+	boot_trace("usb");
 	vga_shadow_init();
 	vga_shadow_init();
 	vga_fb_flush();
 	vga_fb_flush();
-	vfs_mount_all();
-	/* The command pack: read-only, mounted at /ramfs (see ramfs.c). */
+	boot_trace("vga");
+
+	/* The command pack: read-only, mounted at /ramfs (see ramfs.c).  This
+	 * happens before the block-device scan because the WM and terminal are
+	 * loaded from /ramfs, not from a disk. */
 	{
 	{
 		extern unsigned char _binary_cmds_pack_gz_bin_start[];
 		extern unsigned char _binary_cmds_pack_gz_bin_start[];
 		extern unsigned char _binary_cmds_pack_gz_bin_end[];
 		extern unsigned char _binary_cmds_pack_gz_bin_end[];
@@ -334,17 +354,21 @@ void kernel_main(void)
 			vfs_mount_ramfs();
 			vfs_mount_ramfs();
 		}
 		}
 	}
 	}
-	/* Bring the NIC up before the WM loop starts, so an idle board can pick
-	 * up a network kexec (`make net-dev`) without any typing. */
-	netdev_active();
-	/* With a NIC present, try DHCP; on failure the static default stays. */
-	if (netdev_active())
-		dhcp_run(3000);
-	/* The idle process is the kernel_main context; the ring-3 WM is spawned
-	 * from /ramfs/bin/wm.  Housekeeping runs in its own process so it cannot
-	 * be starved by a busy system. */
+	boot_trace("ramfs");
+
+	/* Start the WM before touching disks: the first frame can be painted
+	 * while storage and the network are still being probed. */
 	wm_server_init();
 	wm_server_init();
 	wm_server_start();
 	wm_server_start();
+	boot_trace("wm");
+
+	/* Probe the NIC (link negotiation is asynchronous now) before the
+	 * kworker starts, so its netlog flushes have a device to use. */
+	netdev_active();
+	boot_trace("net");
+
+	/* The idle process is the kernel_main context; housekeeping runs in
+	 * its own process so it cannot be starved by a busy system. */
 	kworker_pid = process_create("kworker", kworker_main);
 	kworker_pid = process_create("kworker", kworker_main);
 	if (kworker_pid < 0) {
 	if (kworker_pid < 0) {
 		serial_puts(SERIAL_COM1, "kworker: cannot create, using idle fallback\n");
 		serial_puts(SERIAL_COM1, "kworker: cannot create, using idle fallback\n");
@@ -353,8 +377,29 @@ void kernel_main(void)
 		process_set_weight((unsigned int)kworker_pid, 1);
 		process_set_weight((unsigned int)kworker_pid, 1);
 		process_set_affinity((unsigned int)kworker_pid, 1);
 		process_set_affinity((unsigned int)kworker_pid, 1);
 	}
 	}
+	boot_trace("kworker");
+
+	/* Slow storage bring-up happens behind the usable desktop now. */
+	boot_trace("storage begin");
+	vfs_mount_all();
+	boot_trace("storage");
+
+	/* The first terminal is spawned only now: while the boot scan was
+	 * rebuilding the mount table, no user command could touch the disks.
+	 * The desktop itself was already painted from /ramfs. */
+	wm_spawn_terminal();
+	boot_trace("terminal");
+
+	/* With a NIC present, try DHCP; on failure the static default stays.
+	 * Deliberately after the WM: a missing cable or DHCP server must not
+	 * delay the first frame. */
+	if (netdev_active())
+		dhcp_run(3000);
+	boot_trace("dhcp");
+
 	/* Boot-time device/FS setup is complete; APs may now take user work.
 	/* Boot-time device/FS setup is complete; APs may now take user work.
 	 * Kernel threads stay pinned to the BSP and serialize on the BKL. */
 	 * Kernel threads stay pinned to the BSP and serialize on the BKL. */
 	smp_enable_sched();
 	smp_enable_sched();
+	boot_trace("sched");
 	kernel_idle_loop();
 	kernel_idle_loop();
 }
 }

+ 4 - 24
r8168.c

@@ -561,19 +561,6 @@ static void phy_autoneg(void)
 	mdio_write(MII_BMCR, BMCR_AUTONEG | BMCR_RESTART_AN);
 	mdio_write(MII_BMCR, BMCR_AUTONEG | BMCR_RESTART_AN);
 }
 }
 
 
-static void wait_link(unsigned int ms)
-{
-	unsigned int t0 = get_ticks_ms();
-
-	while (get_ticks_ms() - t0 <= ms) {
-		int bmsr = mdio_read(MII_BMSR);
-
-		if ((bmsr >= 0 && (bmsr & BMSR_LINK)) ||
-		    (r8(REG_PHYstatus) & PHYST_LINK))
-			return;
-	}
-}
-
 /* ---- netdev interface ---- */
 /* ---- netdev interface ---- */
 
 
 /* Rings + configuration registers + PHY + RX/TX enable.  Used by both the
 /* Rings + configuration registers + PHY + RX/TX enable.  Used by both the
@@ -598,17 +585,10 @@ static void hw_start(void)
 
 
 	w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
 	w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
 
 
-	/* Give autoneg a moment, then wait for link (bounded). */
-	{
-		unsigned int t0 = get_ticks_ms();
-
-		while (get_ticks_ms() - t0 < 3000) {
-			if (r8(REG_PHYstatus) & PHYST_LINK)
-				break;
-		}
-	}
-	wait_link(2000);
-	dbg(r8(REG_PHYstatus) & PHYST_LINK ? "link up" : "no link");
+	/* Auto-negotiation runs in the background.  Do not wait for link here:
+	 * that used to add up to 5 s to boot (and to every netreset), while
+	 * DHCP already retries and netinfo reports the live link state. */
+	dbg(r8(REG_PHYstatus) & PHYST_LINK ? "link up" : "link started");
 }
 }
 
 
 int r8168_init(void)
 int r8168_init(void)

+ 10 - 0
sys_fs.c

@@ -29,6 +29,16 @@ void sys_fs_enter(void)
 		}
 		}
 		process_sleep_ms(10);
 		process_sleep_ms(10);
 	}
 	}
+	/* A boot-time `storage` scan (or a concurrent storage command) is
+	 * rebuilding the mount table and reading through the FAT static
+	 * buffers.  Wait for it instead of racing. */
+	while (vfs_scan_busy && current_proc()->pid != vfs_scan_owner_pid) {
+		if (vfs_scan_owner_pid && !process_is_alive(vfs_scan_owner_pid)) {
+			vfs_scan_busy = 0;
+			break;
+		}
+		process_sleep_ms(10);
+	}
 	preempt_disable();
 	preempt_disable();
 	__asm__ volatile ("sti");   /* the disk may need its IRQ */
 	__asm__ volatile ("sti");   /* the disk may need its IRQ */
 }
 }

+ 49 - 8
vfs.c

@@ -6,12 +6,22 @@
 #include "fatdetect.h"
 #include "fatdetect.h"
 #include "io.h"
 #include "io.h"
 #include "kmalloc.h"
 #include "kmalloc.h"
+#include "process.h"
 #include "ramfs.h"
 #include "ramfs.h"
+#include "serial.h"
 #include "share.h"
 #include "share.h"
+#include "timer.h"
 
 
 vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 unsigned int vfs_mount_count;
 unsigned int vfs_mount_count;
 
 
+/* Set while vfs_mount_all() rebuilds the mount table.  File syscalls wait on
+ * this (sys_fs.c) so they never resolve a path in a half-rebuilt table or use
+ * the FAT sector buffer while the scan is reading disks.  The owner (a
+ * `storage` syscall) is exempt. */
+volatile int vfs_scan_busy;
+volatile unsigned int vfs_scan_owner_pid;
+
 static void put_dec(unsigned int n)
 static void put_dec(unsigned int n)
 {
 {
 	char buf[12];
 	char buf[12];
@@ -544,11 +554,17 @@ static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 {
 {
 	fat_fs_t fs;
 	fat_fs_t fs;
 	vfs_mount_t *m;
 	vfs_mount_t *m;
+	int r;
 
 
 	fat_cache_clear();
 	fat_cache_clear();
 
 
-	if (fat_mount(dev, part_lba, &fs) != 0)
+	r = fat_mount(dev, part_lba, &fs);
+	if (r != 0) {
+		serial_puts(SERIAL_COM1, "vfs: ");
+		serial_puts(SERIAL_COM1, name);
+		serial_puts(SERIAL_COM1, " no FAT\n");
 		return;
 		return;
+	}
 	m = mount_slot(name);
 	m = mount_slot(name);
 	if (!m)
 	if (!m)
 		return;
 		return;
@@ -559,6 +575,11 @@ static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 	copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
 	copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
 	copy_bounded(m->label, fs.label, sizeof(m->label));
 	copy_bounded(m->label, fs.label, sizeof(m->label));
 	vfs_mount_count++;
 	vfs_mount_count++;
+	serial_puts(SERIAL_COM1, "vfs: ");
+	serial_puts(SERIAL_COM1, name);
+	serial_puts(SERIAL_COM1, " ");
+	serial_puts(SERIAL_COM1, fat_name(fs.type));
+	serial_puts(SERIAL_COM1, "\n");
 }
 }
 
 
 void vfs_mount_ramfs(void)
 void vfs_mount_ramfs(void)
@@ -623,7 +644,7 @@ static void mount_device(blk_dev_t *d, unsigned char *buf)
 	int n, j;
 	int n, j;
 	int any_fat = 0;
 	int any_fat = 0;
 
 
-	if (d->kind != BLK_FLOPPY && !d->present)
+	if (!d->present)
 		return;
 		return;
 	if (d->kind == BLK_FLOPPY) {
 	if (d->kind == BLK_FLOPPY) {
 		mount_one(d, 0, d->name);
 		mount_one(d, 0, d->name);
@@ -679,17 +700,37 @@ void vfs_mount_all(void)
 	static unsigned char buf[512];
 	static unsigned char buf[512];
 	unsigned int i;
 	unsigned int i;
 
 
+	/* Publish the scan before touching the table: a ring-3 file syscall
+	 * on another CPU waits for it instead of resolving a path halfway
+	 * through the rebuild.  At boot current_proc() is idle (pid 0). */
+	vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0;
+	vfs_scan_busy = 1;
+
 	vfs_mount_count = 0;
 	vfs_mount_count = 0;
+
+	/* Non-block filesystems first.  The WM and terminal live on /ramfs,
+	 * so they stay usable while the (possibly slow) disk scan runs. */
+	vfs_mount_ramfs();
+	vfs_mount_share();
+
 	blk_scan();
 	blk_scan();
 
 
-	for (i = 0; i < blk_dev_count; i++)
+	for (i = 0; i < blk_dev_count; i++) {
+		unsigned int t0 = get_ticks_ms();
+
 		mount_device(&blk_devs[i], buf);
 		mount_device(&blk_devs[i], buf);
+		serial_puts(SERIAL_COM1, "vfs: ");
+		serial_puts(SERIAL_COM1, blk_devs[i].name);
+		serial_puts(SERIAL_COM1, " mount ");
+		serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
+		serial_puts(SERIAL_COM1, " ms\n");
+	}
 
 
-	/* The ramfs is not a block device; keep it mounted across a refresh
-	 * (the `storage` command calls vfs_mount_all()).  /share is likewise
-	 * independent of the block devices. */
-	vfs_mount_ramfs();
-	vfs_mount_share();
+	serial_puts(SERIAL_COM1, "vfs: ");
+	serial_putdec(SERIAL_COM1, vfs_mount_count);
+	serial_puts(SERIAL_COM1, " mount(s)\n");
+
+	vfs_scan_busy = 0;
 }
 }
 
 
 static int device_is_mounted(const char *dev)
 static int device_is_mounted(const char *dev)

+ 8 - 1
vfs.h

@@ -74,11 +74,18 @@ typedef struct vfs_mount {
 extern vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 extern vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 extern unsigned int vfs_mount_count;
 extern unsigned int vfs_mount_count;
 
 
-/* Scan devices and mount all detected FAT filesystems. */
+/* Scan devices and mount all detected FAT filesystems.  The ramfs/share
+ * mounts are kept available throughout the scan; file syscalls that run on
+ * another CPU wait until it completes (see vfs_scan_busy). */
 void vfs_mount_all(void);
 void vfs_mount_all(void);
 /* Re-mount only one block device after it was formatted. */
 /* Re-mount only one block device after it was formatted. */
 void vfs_remount_device(blk_dev_t *d);
 void vfs_remount_device(blk_dev_t *d);
 
 
+/* Set while vfs_mount_all() owns the mount table and the FAT sector buffer.
+ * sys_fs.c waits for it; the owner (the calling `storage` syscall) is exempt. */
+extern volatile int vfs_scan_busy;
+extern volatile unsigned int vfs_scan_owner_pid;
+
 /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
 /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
 void vfs_mount_ramfs(void);
 void vfs_mount_ramfs(void);
 
 

+ 9 - 2
wm.c

@@ -79,6 +79,7 @@ static unsigned int wm_pid;
 static unsigned int wm_cr3;
 static unsigned int wm_cr3;
 static int wm_attached;
 static int wm_attached;
 static int wm_frame_active;
 static int wm_frame_active;
+static int wm_first_frame;
 static int wm_kernel_fallback;
 static int wm_kernel_fallback;
 static unsigned int wm_last_start;
 static unsigned int wm_last_start;
 static unsigned int wm_last_seen;
 static unsigned int wm_last_seen;
@@ -1006,6 +1007,10 @@ int wm_server_present(const struct wm_present_info *in)
 	else
 	else
 		vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
 		vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
 	sys_gfx_collect();
 	sys_gfx_collect();
+	if (!wm_first_frame) {
+		wm_first_frame = 1;
+		serial_puts(SERIAL_COM1, "wm: first frame\n");
+	}
 	return 0;
 	return 0;
 }
 }
 
 
@@ -1154,9 +1159,11 @@ void wm_server_init(void)
 	wm_cr3 = 0;
 	wm_cr3 = 0;
 	wm_attached = 0;
 	wm_attached = 0;
 	wm_frame_active = 0;
 	wm_frame_active = 0;
+	wm_first_frame = 0;
 	wm_kernel_fallback = 0;
 	wm_kernel_fallback = 0;
-	if (have_terms)
-		wm_spawn_terminal();
+	/* The first terminal is spawned by kernel_main after the boot storage
+	 * scan: no user command can touch the disks while the mount table is
+	 * being rebuilt.  The desktop itself does not need a window. */
 }
 }
 
 
 static int wm_point_in_rect(unsigned int px, unsigned int py,
 static int wm_point_in_rect(unsigned int px, unsigned int py,