Просмотр исходного кода

Prefer a wired link over WiFi at boot

Expose the physical link state from e1000e, r8168 and ASIX through the optional netdev link_up() hook, and skip the boot DHCP when the active NIC has no link.  wifi autoconnect now checks that link instead of a DHCP lease, so a connected Ethernet cable wins and WiFi is only probed and joined when the cable is down.

Keep an r8168 link that survived the probe reset, with a bounded wait only when the reset drops a link that was already up.

Update AGENTS.md and the network page.
Johannes Zellner 1 неделя назад
Родитель
Сommit
3b2607df02
8 измененных файлов с 112 добавлено и 47 удалено
  1. 9 5
      AGENTS.md
  2. 10 2
      asix.c
  3. 15 9
      docs/network.html
  4. 10 1
      e1000e.c
  5. 17 9
      kernel.c
  6. 3 3
      netdev.h
  7. 39 6
      r8168.c
  8. 9 12
      ucmd_wifi.c

+ 9 - 5
AGENTS.md

@@ -595,8 +595,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   BSSIDs that share an SSID/security collapse to the strongest entry (the
   BSSIDs that share an SSID/security collapse to the strongest entry (the
   active BSSID wins while connected).  `wifi.c` (the Intel
   active BSSID wins while connected).  `wifi.c` (the Intel
   7265D/3165 netdev, see the WiFi bullet) is
   7265D/3165 netdev, see the WiFi bullet) is
-  last in the table; `struct netdev` has an optional `link_up()` hook, and
-  the boot DHCP is skipped while it returns 0.  `net.c` — ARP + ICMP echo, IPv4/UDP
+  last in the table; `struct netdev` has an optional `link_up()` hook
+  (e1000e `STATUS.LU`, r8168 `PHYstatus`, ASIX probe-time `BMSR`, WiFi
+  association state), the boot DHCP is skipped while it returns 0, and a
+  physically linked wired NIC keeps `wifi autoconnect` from overriding it.
+  `net.c` — ARP + ICMP echo, IPv4/UDP
   send + parse helpers,
   send + parse helpers,
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
   decode, `net_checksum()`/`net_checksum_tcp()`.  ARP entries expire after five
   decode, `net_checksum()`/`net_checksum_tcp()`.  ARP entries expire after five
@@ -680,9 +683,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   A successful menu/CLI connect also writes the SSID and passphrase to
   A successful menu/CLI connect also writes the SSID and passphrase to
   `/data/WIFI.CFG` (8.3, plaintext, BSSID zeroed so a mesh/repeater AP works);
   `/data/WIFI.CFG` (8.3, plaintext, BSSID zeroed so a mesh/repeater AP works);
   after the boot DHCP attempt `kernel_main` spawns
   after the boot DHCP attempt `kernel_main` spawns
-  `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect skips a
-  wired link that got a DHCP lease and leaves an already-linked WiFi card
-  alone; `/data` is the persistent FAT volume from the installer.
+  `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect leaves a
+  wired link that is physically up alone and only tries WiFi when no wired
+  link is present; an already-linked WiFi card is also left alone.  `/data`
+  is the persistent FAT volume from the installer.
   `wifi forget` removes the saved record.  A FAT read may write a final
   `wifi forget` removes the saved record.  A FAT read may write a final
   partial sector past the requested length, so the credential reader keeps
   partial sector past the requested length, so the credential reader keeps
   512 bytes of slack on the stack.
   512 bytes of slack on the stack.

+ 10 - 2
asix.c

@@ -334,12 +334,20 @@ static void net_print_info(void)
 	put_mac();
 	put_mac();
 	io_putstr("\n");
 	io_putstr("\n");
 	st = dev.phy ? mdio_read(MII_BMSR) : -1;
 	st = dev.phy ? mdio_read(MII_BMSR) : -1;
-	if (st >= 0 && (st & BMSR_LSTATUS))
+	dev.link = st >= 0 && (st & BMSR_LSTATUS);
+	if (dev.link)
 		io_putstr("  link up, 100 Mbit capable\n");
 		io_putstr("  link up, 100 Mbit capable\n");
 	else
 	else
 		io_putstr("  link down\n");
 		io_putstr("  link down\n");
 }
 }
 
 
+/* Probe-time link state (chip_init waits for auto-negotiation): the boot
+ * network policy prefers this link over the saved WiFi network. */
+static int net_link_up(void)
+{
+	return dev.present && dev.chip_ready && dev.link;
+}
+
 static void net_reset(void)
 static void net_reset(void)
 {
 {
 	rx_pos = 0;
 	rx_pos = 0;
@@ -369,7 +377,7 @@ const struct netdev asix_netdev = {
 	asix_default_mask,
 	asix_default_mask,
 	0,
 	0,
 	0,
 	0,
-	0,
+	net_link_up,
 };
 };
 
 
 /* Read and parse the configuration descriptor.  The AX88772 has one
 /* Read and parse the configuration descriptor.  The AX88772 has one

+ 15 - 9
docs/network.html

@@ -172,9 +172,11 @@
 			<span class="file">wifi.c</span> is the built-in WiFi of the
 			<span class="file">wifi.c</span> is the built-in WiFi of the
 			Chromebook (Intel 7265D/3165, see the
 			Chromebook (Intel 7265D/3165, see the
 			<a href="drivers.html#wifi">driver page</a>). It is last in the
 			<a href="drivers.html#wifi">driver page</a>). It is last in the
-			registry, so it only becomes active when nothing wired probes, and
-			it reports <code>link_up() = 0</code> until it is connected: the boot
-			DHCP checks that optional hook and skips the 4 s attempt. The
+			registry, so the boot probe only makes it active when nothing
+			wired is present (a later <code>wifi connect</code> can still select
+			it over a wired NIC whose cable is unplugged), and it reports
+			<code>link_up() = 0</code> until it is connected: the boot DHCP
+			checks that optional hook and skips the 4 s attempt. The
 			<code>wifi</code> command scans, associates, runs the WPA2-PSK
 			<code>wifi</code> command scans, associates, runs the WPA2-PSK
 			handshake in ring 3 and then calls <code>SYS_WIFI_AUTHORIZE</code>,
 			handshake in ring 3 and then calls <code>SYS_WIFI_AUTHORIZE</code>,
 			which opens the data port and selects the <code>wifi</code> netdev.
 			which opens the data port and selects the <code>wifi</code> netdev.
@@ -193,9 +195,11 @@
 			the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
 			the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
 			persistent <code>NEBOSDATA</code> partition. After the boot DHCP
 			persistent <code>NEBOSDATA</code> partition. After the boot DHCP
 			attempt <code>kernel_main</code> spawns
 			attempt <code>kernel_main</code> spawns
-			<code>wifi autoconnect</code> when that file exists; it skips a
-			wired link that already has a DHCP lease and leaves an existing
-			WiFi link alone. <code>wifi forget</code> removes the saved record.
+			<code>wifi autoconnect</code> when that file exists. It samples the
+			active NIC's <code>link_up()</code> state: a wired cable that is
+			physically up keeps the Ethernet path, while no wired link lets
+			WiFi associate and run DHCP. An existing WiFi link is left alone.
+			<code>wifi forget</code> removes the saved record.
 			The file is a plaintext 8.3 FAT file with the BSSID zeroed so the
 			The file is a plaintext 8.3 FAT file with the BSSID zeroed so the
 			strongest AP advertising that SSID is used.
 			strongest AP advertising that SSID is used.
 		</p>
 		</p>
@@ -235,9 +239,11 @@
 		</ul>
 		</ul>
 		<p>
 		<p>
 			The static default address is <code>192.168.178.240</code>. At boot,
 			The static default address is <code>192.168.178.240</code>. At boot,
-			with a NIC present, a DHCP client runs with a 4-second budget
-			(requesting the current address); if no server answers, the static
-			address stays. The idle WM loop answers ARP/ICMP for the board's IP
+			when the active NIC reports a physical link, a DHCP client runs with
+			a 4-second budget (requesting the current address); if no server
+			answers, the static address stays, and a cable that is not plugged
+			in skips the wait so the saved WiFi network can be tried. The idle
+			WM loop answers ARP/ICMP for the board's IP
 			even when no command is running, so the board is reachable and
 			even when no command is running, so the board is reachable and
 			MAC-resolvable.
 			MAC-resolvable.
 		</p>
 		</p>

+ 10 - 1
e1000e.c

@@ -1743,6 +1743,15 @@ static void e1000e_get_mac(unsigned char out[6])
 		out[i] = mac[i];
 		out[i] = mac[i];
 }
 }
 
 
+/* Physical link state: kernel_main and `wifi autoconnect` prefer a wired
+ * link over the saved WiFi network. */
+static int e1000e_link_up(void)
+{
+	if (!present || !mmio)
+		return 0;
+	return (r32(REG_STATUS) & STATUS_LU) ? 1 : 0;
+}
+
 /* Give a descriptor back to the hardware.  The buffer address is rewritten
 /* Give a descriptor back to the hardware.  The buffer address is rewritten
  * every time: the extended descriptor write-back overwrites the first 8
  * every time: the extended descriptor write-back overwrites the first 8
  * bytes with RSS/checksum data, so restoring it makes the ring survive even
  * bytes with RSS/checksum data, so restoring it makes the ring survive even
@@ -2293,5 +2302,5 @@ const struct netdev e1000e_netdev = {
 	e1000e_dump_regs, 0, e1000e_reset, e1000e_set_speed, e1000e_set_mac,
 	e1000e_dump_regs, 0, e1000e_reset, e1000e_set_speed, e1000e_set_mac,
 	e1000e_default_ip, e1000e_default_mask, e1000e_dump_rx,
 	e1000e_default_ip, e1000e_default_mask, e1000e_dump_rx,
 	e1000e_stop,
 	e1000e_stop,
-	0,
+	e1000e_link_up,
 };
 };

+ 17 - 9
kernel.c

@@ -568,17 +568,25 @@ void kernel_main(void)
 	boot_trace("terminal off");
 	boot_trace("terminal off");
 #endif
 #endif
 
 
-	/* 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() && !net_kept &&
-	    (!netdev_active()->link_up || netdev_active()->link_up()))
-		dhcp_run(4000);
-	boot_trace(net_kept ? "dhcp skipped" : "dhcp");
+	/* With a linked NIC present, try DHCP; on failure the static default
+	 * stays.  A wired NIC without a cable is skipped so the WiFi fallback
+	 * below does not have to wait out the 4 s timeout first.  Deliberately
+	 * after the WM: a missing cable or DHCP server must not delay the first
+	 * frame. */
+	{
+		const struct netdev *nd = netdev_active();
+		int linked = nd && netdev_link(nd);
+
+		if (linked && !net_kept)
+			dhcp_run(4000);
+		boot_trace(net_kept ? "dhcp skipped" :
+			   (linked ? "dhcp" : "dhcp no link"));
+	}
 
 
 	/* If the writable /data volume has saved WiFi credentials, let the
 	/* If the writable /data volume has saved WiFi credentials, let the
-	 * normal supplicant reconnect in the background.  It skips a working
-	 * wired link, and a missing /data or wifi helper is harmless. */
+	 * normal supplicant reconnect in the background.  It leaves a wired
+	 * link that is physically up alone, and a missing /data or wifi
+	 * helper is harmless. */
 	{
 	{
 		unsigned int sz = 0;
 		unsigned int sz = 0;
 		int is_dir = 0;
 		int is_dir = 0;

+ 3 - 3
netdev.h

@@ -29,9 +29,9 @@ struct netdev {
 	void (*dump_brief)(void);
 	void (*dump_brief)(void);
 	/* Stop DMA before a kexec jump; may be NULL. */
 	/* Stop DMA before a kexec jump; may be NULL. */
 	void (*stop)(void);
 	void (*stop)(void);
-	/* Link state for drivers whose link can be down for long (WiFi before
-	 * association): 0 = no link, so the boot DHCP is skipped.  NULL means
-	 * "always up" (the wired drivers). */
+	/* Physical link state: 0 = no link, so the boot DHCP is skipped and
+	 * the saved WiFi network is tried instead.  Drivers without a reliable
+	 * link bit (ne2000, RNDIS) leave this NULL and count as always up. */
 	int (*link_up)(void);
 	int (*link_up)(void);
 };
 };
 
 

+ 39 - 6
r8168.c

@@ -545,6 +545,20 @@ static void set_rx_mode(void)
 	w32(REG_RxConfig, cfg);
 	w32(REG_RxConfig, cfg);
 }
 }
 
 
+/* Bounded poll of PHYstatus.LINK.  Only used to recover a link that was
+ * established before the probe reset; a missing cable must not delay boot. */
+static int wait_link(unsigned int timeout_ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	while (!(r8(REG_PHYstatus) & PHYST_LINK)) {
+		if (timed_out(t0, timeout_ms))
+			return 0;
+		__asm__ volatile ("pause");
+	}
+	return 1;
+}
+
 static void phy_autoneg(void)
 static void phy_autoneg(void)
 {
 {
 	int id1 = mdio_read(MII_PHYSID1);
 	int id1 = mdio_read(MII_PHYSID1);
@@ -564,8 +578,8 @@ static void phy_autoneg(void)
 /* ---- 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
- * first probe and `netreset`. */
-static void hw_start(void)
+ * first probe and `netreset`; the latter forces a fresh auto-negotiation. */
+static void hw_start(int restart_phy)
 {
 {
 	w8(REG_Cfg9346, Cfg9346_Unlock);
 	w8(REG_Cfg9346, Cfg9346_Unlock);
 	setup_rings();
 	setup_rings();
@@ -580,7 +594,11 @@ static void hw_start(void)
 	w8(REG_Cfg9346, 0x00);
 	w8(REG_Cfg9346, 0x00);
 	dbg("regs ok");
 	dbg("regs ok");
 
 
-	phy_autoneg();
+	/* Keep a link the firmware already established: the boot network
+	 * policy samples PHYstatus to choose between wired and WiFi, and
+	 * restarting auto-negotiation would make a plugged cable look down. */
+	if (restart_phy || !(r8(REG_PHYstatus) & PHYST_LINK))
+		phy_autoneg();
 	dbg("phy up");
 	dbg("phy up");
 
 
 	w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
 	w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
@@ -595,6 +613,7 @@ int r8168_init(void)
 {
 {
 	char buf[40];
 	char buf[40];
 	unsigned int i;
 	unsigned int i;
+	int had_link;
 
 
 	if (inited)
 	if (inited)
 		return present ? 0 : -1;
 		return present ? 0 : -1;
@@ -632,6 +651,9 @@ int r8168_init(void)
 	}
 	}
 
 
 	xid = (r32(REG_TxConfig) >> 20) & 0xfcf;
 	xid = (r32(REG_TxConfig) >> 20) & 0xfcf;
+	/* Sample before the reset: a cable that was already linked keeps
+	 * the boot DHCP on the wired path instead of falling back to WiFi. */
+	had_link = (r8(REG_PHYstatus) & PHYST_LINK) != 0;
 
 
 	if (hw_reset() != 0) {
 	if (hw_reset() != 0) {
 		dbg("reset timeout");
 		dbg("reset timeout");
@@ -654,7 +676,9 @@ int r8168_init(void)
 	}
 	}
 	dbg("ring memory ok");
 	dbg("ring memory ok");
 
 
-	hw_start();
+	hw_start(0);
+	if (had_link && !(r8(REG_PHYstatus) & PHYST_LINK))
+		wait_link(3000);
 
 
 	/* "<chip> XID <xxx> <mac>" */
 	/* "<chip> XID <xxx> <mac>" */
 	i = 0;
 	i = 0;
@@ -688,7 +712,7 @@ void r8168_reset(void)
 		return;
 		return;
 	}
 	}
 	read_mac();
 	read_mac();
-	hw_start();
+	hw_start(1);
 	dbg("netreset done");
 	dbg("netreset done");
 }
 }
 
 
@@ -907,6 +931,15 @@ void r8168_print_info(void)
 	io_putchar('\n');
 	io_putchar('\n');
 }
 }
 
 
+/* Physical link state: kernel_main and `wifi autoconnect` prefer a wired
+ * link over the saved WiFi network. */
+static int r8168_link_up(void)
+{
+	if (!present || !mmio)
+		return 0;
+	return (r8(REG_PHYstatus) & PHYST_LINK) ? 1 : 0;
+}
+
 void r8168_dump_regs(void)
 void r8168_dump_regs(void)
 {
 {
 	static const struct {
 	static const struct {
@@ -986,5 +1019,5 @@ const struct netdev r8168_netdev = {
 	r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
 	r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
 	r8168_dump_regs, r8168_set_loopback, r8168_reset,
 	r8168_dump_regs, r8168_set_loopback, r8168_reset,
 	0, 0, 0, 0, 0, 0,
 	0, 0, 0, 0, 0, 0,
-	0,
+	r8168_link_up,
 };
 };

+ 9 - 12
ucmd_wifi.c

@@ -625,7 +625,7 @@ static int cmd_connect_file(const char *args)
 }
 }
 
 
 /* Boot-time helper: reconnect to the network saved in /data/WIFI.CFG.  A
 /* Boot-time helper: reconnect to the network saved in /data/WIFI.CFG.  A
- * wired link that already has an address wins; an existing WiFi link is left
+ * wired link that is physically up wins; an existing WiFi link is left
  * alone. */
  * alone. */
 static int cmd_autoconnect(void)
 static int cmd_autoconnect(void)
 {
 {
@@ -638,6 +638,14 @@ static int cmd_autoconnect(void)
 
 
 	if (load_wifi_cred(&cfg) != 0)
 	if (load_wifi_cred(&cfg) != 0)
 		return 0;
 		return 0;
+	/* A wired link that is physically up wins: do not even probe the WiFi
+	 * card in that case.  Only fall back to WiFi when no cable is
+	 * connected. */
+	if (u_net_info(&ni) == 1 && ni.present && ni.link &&
+	    !name_eq(ni.name, "wifi")) {
+		wipe_cfg(&cfg);
+		return 0;
+	}
 	if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) != 0 || !ws.present) {
 	if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) != 0 || !ws.present) {
 		wipe_cfg(&cfg);
 		wipe_cfg(&cfg);
 		return 0;
 		return 0;
@@ -646,17 +654,6 @@ static int cmd_autoconnect(void)
 		wipe_cfg(&cfg);
 		wipe_cfg(&cfg);
 		return 0;
 		return 0;
 	}
 	}
-	if (u_net_info(&ni) == 1 && ni.present && !name_eq(ni.name, "wifi")) {
-		unsigned char srv[4];
-		unsigned int lease = 0;
-
-		/* Only a real wired DHCP lease keeps the boot-time wired link;
-		 * the static fallback address does not mean the cable is up. */
-		if (u_syscall2(SYS_DHCP_INFO, (int)srv, (int)&lease) == 1) {
-			wipe_cfg(&cfg);
-			return 0;
-		}
-	}
 	for (i = 0; i < cfg.ssid_len; i++)
 	for (i = 0; i < cfg.ssid_len; i++)
 		ssid[i] = (char)cfg.ssid[i];
 		ssid[i] = (char)cfg.ssid[i];
 	ssid[cfg.ssid_len] = '\0';
 	ssid[cfg.ssid_len] = '\0';