drivers/net/r8168: Fix ethernet_mac[0-9] format for vpd

The format for VPD has changed s.t. the first NIC should
always have a zero concat to the end.

Adjust all the respective boards to shift back by one and
adjust drivers/net friends to remove the 'special casing'
of idx == 0.

Background:
https://chromeos.google.com/partner/dlm/docs/factory/vpd.html#field-ethernet_macn

V.2: Fixup a code comment typo while we are here.
V.3: Vary special casing semantics for idx==0 => default mac addr is set.
V.4: Rework to still support the legacy path.

BUG=b:152157720
BRANCH=none
TEST=none

Change-Id: Idf83cc621a9333186dabb668b22c4b78e211930a
Signed-off-by: Edward O'Callaghan <quasisec@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/39771
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Furquan Shaikh <furquan@google.com>
diff --git a/src/drivers/net/Kconfig b/src/drivers/net/Kconfig
index 92de1ed..282075b 100644
--- a/src/drivers/net/Kconfig
+++ b/src/drivers/net/Kconfig
@@ -19,6 +19,14 @@
 	default n
 	select REALTEK_8168_RESET
 
+config RT8168_SUPPORT_LEGACY_VPD_MAC
+	bool
+	default n
+	help
+	  Previously VPD expected that device_indexes set to zero were
+	  special cased. Selecting this Kconfig restores the legacy
+	  VPD format and behaviour. If unsure, you likely do not need this!
+
 config RT8168_SET_LED_MODE
 	bool
 	default n
diff --git a/src/drivers/net/r8168.c b/src/drivers/net/r8168.c
index 99d472c..eaf33f2 100644
--- a/src/drivers/net/r8168.c
+++ b/src/drivers/net/r8168.c
@@ -141,16 +141,10 @@
 	char key[] = "ethernet_mac "; /* Leave a space at tail to stuff an index */
 
 	/*
-	 * The device_index 0 is treated as an special case matching to
-	 * "ethernet_mac" with single NIC on DUT. When there are mulitple
-	 * NICs on DUT, they are mapping to "ethernet_macN", where
-	 * N is [0-9].
+	 * Map each NIC on the DUT to "ethernet_macN", where N is [0-9].
+	 * Translate index number from integer to ascii by adding '0' char.
 	 */
-	if (device_index == 0)
-		key[DEVICE_INDEX_BYTE] = '\0';
-	else
-		/* Translate index number from integer to ascii */
-		key[DEVICE_INDEX_BYTE] = (device_index - 1) + '0';
+	key[DEVICE_INDEX_BYTE] = device_index + '0';
 
 	return fetch_mac_vpd_key(macstrbuf, key);
 }
@@ -167,12 +161,16 @@
 		return;
 	}
 
-	/* check "ethernet_mac" first when the device index is 1 */
-	if (config->device_index == 1 &&
-		fetch_mac_vpd_dev_idx(macstrbuf, 0) == CB_SUCCESS)
+	if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) == CB_SUCCESS)
 		return;
 
-	if (fetch_mac_vpd_dev_idx(macstrbuf, config->device_index) != CB_SUCCESS)
+	if (!CONFIG(RT8168_SUPPORT_LEGACY_VPD_MAC)) {
+		printk(BIOS_ERR, "r8168: mac address not found in VPD,"
+						 " using default 00:e0:4c:00:c0:b0\n");
+		return;
+	}
+
+	if (fetch_mac_vpd_key(macstrbuf, "ethernet_mac ") != CB_SUCCESS)
 		printk(BIOS_ERR, "r8168: mac address not found in VPD,"
 					 " using default 00:e0:4c:00:c0:b0\n");
 }
diff --git a/src/mainboard/google/fizz/Kconfig b/src/mainboard/google/fizz/Kconfig
index fbe98be..b6b34fd 100644
--- a/src/mainboard/google/fizz/Kconfig
+++ b/src/mainboard/google/fizz/Kconfig
@@ -24,6 +24,7 @@
 	select MAINBOARD_HAS_TPM2
 	select GENERIC_SPD_BIN
 	select RT8168_GET_MAC_FROM_VPD
+	select RT8168_SUPPORT_LEGACY_VPD_MAC
 	select RT8168_SET_LED_MODE
 	select SPD_READ_BY_WORD
 
diff --git a/src/mainboard/google/fizz/variants/baseboard/devicetree.cb b/src/mainboard/google/fizz/variants/baseboard/devicetree.cb
index c1afe3d..5da9997 100644
--- a/src/mainboard/google/fizz/variants/baseboard/devicetree.cb
+++ b/src/mainboard/google/fizz/variants/baseboard/devicetree.cb
@@ -411,8 +411,8 @@
 			chip drivers/net
 				register "customized_leds" = "0x0fa5"
 				register "wake" = "GPE0_PCI_EXP"
-				register "device_index" = "1"
 				device pci 00.0 on end
+				register "device_index" = "0"
 			end
 		end # PCI Express Port 3
 		device pci 1c.3 on
@@ -428,7 +428,7 @@
 		device pci 1d.0 on # PCI Express Port 9 for 2nd LAN
 			chip drivers/net
 				register "customized_leds" = "0x0fa5"
-				register "device_index" = "2"
+				register "device_index" = "1"
 				device pci 00.0 on end
 			end
 		end # PCI Express Port 9 for BtoB
diff --git a/src/mainboard/google/hatch/variants/puff/overridetree.cb b/src/mainboard/google/hatch/variants/puff/overridetree.cb
index d8df298..bf9120a 100644
--- a/src/mainboard/google/hatch/variants/puff/overridetree.cb
+++ b/src/mainboard/google/hatch/variants/puff/overridetree.cb
@@ -283,7 +283,7 @@
 				register "stop_delay_ms" = "12" # NIC needs time to quiesce
 				register "stop_off_delay_ms" = "1"
 				register "has_power_resource" = "1"
-				register "device_index" = "1"
+				register "device_index" = "0"
 				device pci 00.0 on end
 			end
 		end # FSP requires func0 be enabled.