nakato / nixos-sbc

Nix Flake to make managing Single Board Computers easy and repeatable.
MIT License
26 stars 7 forks source link

ubootBananaPiR4: add persistent MAC #13

Closed newAM closed 2 months ago

newAM commented 3 months ago

Untested. Will mark as ready for review after testing.

newAM commented 3 months ago

Does not work yet, this chunk of code is detecting an invalid MAC: https://github.com/frank-w/BPI-Router-Linux/blob/a5f209f3f88daf5bacaab883ca30d16d88ff0506/drivers/net/ethernet/mediatek/mtk_eth_soc.c#L4915-L4933

I didn't get the "Generated ethaddrX was invalid" print from u-boot though.

nakato commented 3 months ago

Does not work yet, this chunk of code is detecting an invalid MAC: https://github.com/frank-w/BPI-Router-Linux/blob/a5f209f3f88daf5bacaab883ca30d16d88ff0506/drivers/net/ethernet/mediatek/mtk_eth_soc.c#L4915-L4933

I didn't get the "Generated ethaddrX was invalid" print from u-boot though.

I had another look at what that code in mtk_eth_soc.c does. The validation it does makes sure it's not all-zero, and not multicast. So with the bit manipulation that shouldn't be happening. It also falls through to random if it can't find an address in the DT.

U-boot doesn't get upgraded on nixos-switch, so you have to manually dd them into place, was that done?

You can do so without needing to remember the part id mapping by using the partlabel's.

dd if=result/fip.bin of=/dev/disk/by-partlabel/fip
dd if=result/bl2.img of=/dev/disk/by-partlabel/bl2

71ec0b2d487812ea4430f56703fb92606deefc7f, added today, adds the hash contained in $out to the u-boot version to make it easier to reason about what boot-code is in use and could be used for some scripting eventually.

newAM commented 2 months ago

Lets figure out why the address is invalid before we make changes that would change the generated MAC addr.

I think the handoff between u-boot and the kernel isn't working. I'm looking into this more since I want to know how this mechanism works anyway.

There's no Ethernet under /sys/firmware/devicetree/base/aliases.

This ethernet device does exist /sys/firmware/devicetree/base/soc/ethernet@15100000/mac@1, but there is no local-mac-address file.

In u-boot I only see one Ethernet device, which doesn't seem correct, but the MAC matches the environment variable.

MT7988> net list
eth0 : ethernet@15100000 b2:23:f5:38:e8:96 active
MT7988> env print
eth1addr=b2:23:f5:38:e8:97
ethaddr=b2:23:f5:38:e8:96

U-boot doesn't get upgraded on nixos-switch, so you have to manually dd them into place, was that done?

Yeah, I've been updating u-boot each time. I break networking lots while I'm tinkering so I'm using an SD card mux and flashing a new image each time.

image

nakato commented 2 months ago

Lets figure out why the address is invalid before we make changes that would change the generated MAC addr.

I think the handoff between u-boot and the kernel isn't working. I'm looking into this more since I want to know how this mechanism works anyway.

Take a look at fdt_fixup_ethernet()

There's no Ethernet under /sys/firmware/devicetree/base/aliases.

This ethernet device does exist /sys/firmware/devicetree/base/soc/ethernet@15100000/mac@1, but there is no local-mac-address file.

Without the Ethernet aliases, u-boot doesn't know where to put it.

Try this:

add-ethernet-alias.patch

diff --git a/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4-v0.dts b/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4-v0.dts
index d75ccf722532..608bb8319338 100644
--- a/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4-v0.dts
+++ b/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4-v0.dts
@@ -22,6 +22,9 @@ aliases {
                led-failsafe = &led_green;
                led-running = &led_green;
                led-upgrade = &led_green;
+               ethernet0 = &gmac0;
+               ethernet1 = &gmac1;
+               ethernet2 = &gmac2;
        };

        chosen {
diff --git a/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4.dts b/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4.dts
index 06eade0561e4..f8982feeedf8 100644
--- a/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4.dts
+++ b/arch/arm64/boot/dts/mediatek/mt7988a-bananapi-bpi-r4.dts
@@ -20,6 +20,9 @@ aliases {
                led-failsafe = &led_green;
                led-running = &led_green;
                led-upgrade = &led_green;
+               ethernet0 = &gmac0;
+               ethernet1 = &gmac1;
+               ethernet2 = &gmac2;
        };

        chosen {

And if you want to avoid a kernel recompilation.

hardware.deviceTree.kernelPackage = linuxPackages_frankw_6_10_bananaPiR4.kernel.overrideAttrs (oldAttrs: { 
  pname = "linux-bpir4-dtbs";
  buildFlags = ["dtbs"];
  installTargets = ["dtbs_install"];
  installFlags = ["INSTALL_DTBS_PATH=$(out)/dtbs"];
  patches = oldAttrs.patches ++ [ ./add-ethernet-alias.patch ];
});

In u-boot I only see one Ethernet device, which doesn't seem correct, but the MAC matches the environment variable.

MT7988> net list
eth0 : ethernet@15100000 b2:23:f5:38:e8:96 active

I'm more surprised that one shows up than some being missing as I don't see ethernet described for the R4 in the device-tree used by u-boot. I suspect it probably doesn't work in u-boot as that's probably the switch CPU port rather than the physical "front" ports, which means it doesn't know it needs to DSA tagging to work.

U-boot doesn't get upgraded on nixos-switch, so you have to manually dd them into place, was that done?

Yeah, I've been updating u-boot each time. I break networking lots while I'm tinkering so I'm using an SD card mux and flashing a new image each time.

That's awesome!

nakato commented 2 months ago

I sent the ethernet alias changes to frank-w for inclusion, and they're now in 6.10-main.

I've bumped the kernel version in use in this repo and tested this patchset, with the third interface and 16 bytes against a R4 and confirmed it works. I'm going to pull this into the main branch.

nakato commented 2 months ago

Thanks! Merged as 9291af61273b7c10ee8aadf4431df22a6a459807

newAM commented 2 months ago

Thanks! Merged as 9291af6

Thank you, the aliases did the trick, everything works now!

Sorry for the slow replies on my part, work has been keeping me busy lately.