Patchwork [2/2] r8169: checks against wrong mac addresse init

login
register
mail settings
Submitter françois romieu
Date Oct. 16, 2008, 9:48 p.m.
Message ID <20081016214808.GC27208@electric-eye.fr.zoreil.com>
Download mbox | patch
Permalink /patch/4768/
State Accepted
Delegated to: Jeff Garzik
Headers show

Comments

françois romieu - Oct. 16, 2008, 9:48 p.m.
Checking the signature of the eeprom and the validity of the
MAC address should be enough to filter out the bad addresses
observed so far.

Contributed by Ivan Vecera and Martin Capitanio.

Tested on 8102el, 8168b and 8169 for a start.

Signed-off-by: Francois Romieu <romieu@fr.zoreil.com>
Cc: Edward Hsu <edward_hsu@realtek.com.tw>
---
 drivers/net/r8169.c |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)
françois romieu - Oct. 17, 2008, 8:01 p.m.
Martin Capitanio <c4p7n@capitanio.org> :
[...]
> Please take a look at the realtek r8101_n aka RealTek RTL8101E,
> RTL8102E(L) code. Only CFG_METHOD_1, CFG_METHOD_2
> and #(ioaddr, 0x00) == 0x8128 are here allowed to EEPROM access.

So Realtek's 810x driver eeprom code targets the 8101Eb and the
8101Ec only. It does not include Ivan's 8102el.

[...]
> static void
> rtl8101_check_eeprom(struct rtl8101_private *tp)
> {
[...]
> 	mac_addr_1 = rtl_eeprom_read_sc(ioaddr, 0x08);
> 	mac_addr_1 = mac_addr_1 << 16;
> 
> 	mac_addr_2 = rtl_eeprom_read_sc(ioaddr, 0x07);
> 	mac_addr_1 |= mac_addr_2;

The mac address is at the usual place.
Ivan Vecera - Oct. 21, 2008, 5:10 p.m.
Martin Capitanio wrote:
> Please take a look at the realtek r8101_n aka RealTek RTL8101E,
> RTL8102E(L) code. Only CFG_METHOD_1, CFG_METHOD_2
> and #(ioaddr, 0x00) == 0x8128 are here allowed to EEPROM access.
> 
> ...
> 	rtl_eeprom_write_sc(ioaddr, 0x00, 0x8129);
> 
> 	RTL_W8(Cfg9346, Cfg9346_EEM0);
> 	mdelay(15);
> 	rtl_eeprom_write_sc(ioaddr, 0x00, 0x8128);

1) According specification when EEM0 is set to 1 and EEM1 is set to 0
then adapter enters "auto load" mode. Entering this mode will make the
adapter load the contents of the 93C46 (93C56) as when the PCI RSTB
signal is asserted. This auto-load operation will take about 2 ms.
Upon completion, the it automatically returns to normal mode
(EEM1 = EEM0 = 0) and all of the other registers are reset to default
values.
2) First 2 bytes of the EEPROM contain ID code words for the adapter.
It will load the contents of the EEPROM into the corresponding location
if the ID word (0x8129) is correct.

So the Realtek's driver at first sets these bytes to value 0x8129 to ensure
that auto-load will be a success. Then it initiates auto-load (EMM0 = 1 and
EMM1 = 0) then wait some time and finally writes back original value (0x8128).

Ivan
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/drivers/net/r8169.c b/drivers/net/r8169.c
index cd9a215..2b4e975 100644
--- a/drivers/net/r8169.c
+++ b/drivers/net/r8169.c
@@ -81,6 +81,10 @@  static const int multicast_filter_limit = 32;
 #define RTL8169_TX_TIMEOUT	(6*HZ)
 #define RTL8169_PHY_TIMEOUT	(10*HZ)
 
+#define RTL_EEPROM_SIG		cpu_to_le32(0x8129)
+#define RTL_EEPROM_SIG_MASK	cpu_to_le32(0xffff)
+#define RTL_EEPROM_SIG_ADDR	0x0000
+
 /* write/read MMIO register */
 #define RTL_W8(reg, val8)	writeb ((val8), ioaddr + (reg))
 #define RTL_W16(reg, val16)	writew ((val16), ioaddr + (reg))
@@ -1944,9 +1948,10 @@  static void rtl_init_mac_address(struct rtl8169_private *tp,
 				 void __iomem *ioaddr)
 {
 	struct pci_dev *pdev = tp->pci_dev;
-	u8 cfg1;
 	int vpd_cap;
+	__le32 sig;
 	u8 mac[8];
+	u8 cfg1;
 
 	cfg1 = RTL_R8(Config1);
 	if (!(cfg1  & VPD)) {
@@ -1961,7 +1966,16 @@  static void rtl_init_mac_address(struct rtl8169_private *tp,
 	if (!vpd_cap)
 		return;
 
-	/* MAC address is stored in EEPROM at offset 0x0e
+	if (rtl_eeprom_read(pdev, vpd_cap, RTL_EEPROM_SIG_ADDR, &sig) < 0)
+		return;
+
+	if ((sig & RTL_EEPROM_SIG_MASK) != RTL_EEPROM_SIG) {
+		dev_info(&pdev->dev, "Missing EEPROM signature: %08x\n", sig);
+		return;
+	}
+
+	/*
+	 * MAC address is stored in EEPROM at offset 0x0e
 	 * Realtek says: "The VPD address does not have to be a DWORD-aligned
 	 * address as defined in the PCI 2.2 Specifications, but the VPD data
 	 * is always consecutive 4-byte data starting from the VPD address
@@ -1983,8 +1997,8 @@  static void rtl_init_mac_address(struct rtl8169_private *tp,
 			 print_mac(buf, mac));
 	}
 
-	/* Write MAC address */
-	rtl_rar_set(tp, mac);
+	if (is_valid_ether_addr(mac))
+		rtl_rar_set(tp, mac);
 }
 
 static int __devinit