r8169: add random MAC address fallback
authorHeiner Kallweit <hkallweit1@gmail.com>
Tue, 2 Jul 2019 18:46:09 +0000 (20:46 +0200)
committerDavid S. Miller <davem@davemloft.net>
Tue, 2 Jul 2019 22:28:19 +0000 (15:28 -0700)
It was reported that the GPD MicroPC is broken in a way that no valid
MAC address can be read from the network chip. The vendor driver deals
with this by assigning a random MAC address as fallback. So let's do
the same.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/realtek/r8169_main.c

index 22b2f3dcb41de6d8a288176b153b9a3744337814..495d40c8f20e7dbe543e3ffb831c0fea29a800ed 100644 (file)
@@ -6650,13 +6650,36 @@ static int rtl_get_ether_clk(struct rtl8169_private *tp)
        return rc;
 }
 
+static void rtl_init_mac_address(struct rtl8169_private *tp)
+{
+       struct net_device *dev = tp->dev;
+       u8 *mac_addr = dev->dev_addr;
+       int rc, i;
+
+       rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
+       if (!rc)
+               goto done;
+
+       rtl_read_mac_address(tp, mac_addr);
+       if (is_valid_ether_addr(mac_addr))
+               goto done;
+
+       for (i = 0; i < ETH_ALEN; i++)
+               mac_addr[i] = RTL_R8(tp, MAC0 + i);
+       if (is_valid_ether_addr(mac_addr))
+               goto done;
+
+       eth_hw_addr_random(dev);
+       dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
+done:
+       rtl_rar_set(tp, mac_addr);
+}
+
 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
-       /* align to u16 for is_valid_ether_addr() */
-       u8 mac_addr[ETH_ALEN] __aligned(2) = {};
        struct rtl8169_private *tp;
        struct net_device *dev;
-       int chipset, region, i;
+       int chipset, region;
        int jumbo_max, rc;
 
        dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
@@ -6748,16 +6771,7 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
        u64_stats_init(&tp->rx_stats.syncp);
        u64_stats_init(&tp->tx_stats.syncp);
 
-       /* get MAC address */
-       rc = eth_platform_get_mac_address(&pdev->dev, mac_addr);
-       if (rc)
-               rtl_read_mac_address(tp, mac_addr);
-
-       if (is_valid_ether_addr(mac_addr))
-               rtl_rar_set(tp, mac_addr);
-
-       for (i = 0; i < ETH_ALEN; i++)
-               dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
+       rtl_init_mac_address(tp);
 
        dev->ethtool_ops = &rtl8169_ethtool_ops;