net: mdio-uclass: rename arguments of dm_mdio_phy_connect for clarity
[oweals/u-boot.git] / net / eth-uclass.c
index d30b04ba862a8b90d370b97116a7683c1d752f36..ed81cbd53746527cb60e24370cb5e3837703bb69 100644 (file)
@@ -1,17 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2001-2015
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  * Joe Hershberger, National Instruments
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <dm.h>
-#include <environment.h>
+#include <env.h>
 #include <net.h>
 #include <dm/device-internal.h>
 #include <dm/uclass-internal.h>
+#include <net/pcap.h>
 #include "eth_internal.h"
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -227,7 +227,7 @@ static int on_ethaddr(const char *name, const char *value, enum env_op op,
                switch (op) {
                case env_op_create:
                case env_op_overwrite:
-                       eth_parse_enetaddr(value, pdata->enetaddr);
+                       string_to_enetaddr(value, pdata->enetaddr);
                        eth_write_hwaddr(dev);
                        break;
                case env_op_delete:
@@ -308,12 +308,13 @@ void eth_halt(void)
        struct eth_device_priv *priv;
 
        current = eth_get_dev();
-       if (!current || !device_active(current))
+       if (!current || !eth_is_active(current))
                return;
 
        eth_get_ops(current)->stop(current);
        priv = current->uclass_priv;
-       priv->state = ETH_STATE_PASSIVE;
+       if (priv)
+               priv->state = ETH_STATE_PASSIVE;
 }
 
 int eth_is_active(struct udevice *dev)
@@ -336,7 +337,7 @@ int eth_send(void *packet, int length)
        if (!current)
                return -ENODEV;
 
-       if (!device_active(current))
+       if (!eth_is_active(current))
                return -EINVAL;
 
        ret = eth_get_ops(current)->send(current, packet, length);
@@ -344,6 +345,10 @@ int eth_send(void *packet, int length)
                /* We cannot completely return the error at present */
                debug("%s: send() returned error %d\n", __func__, ret);
        }
+#if defined(CONFIG_CMD_PCAP)
+       if (ret >= 0)
+               pcap_post(packet, length, true);
+#endif
        return ret;
 }
 
@@ -359,7 +364,7 @@ int eth_rx(void)
        if (!current)
                return -ENODEV;
 
-       if (!device_active(current))
+       if (!eth_is_active(current))
                return -EINVAL;
 
        /* Process up to 32 packets at one time */
@@ -396,7 +401,7 @@ int eth_initialize(void)
         * This is accomplished by attempting to probe each device and calling
         * their write_hwaddr() operation.
         */
-       uclass_first_device(UCLASS_ETH, &dev);
+       uclass_first_device_check(UCLASS_ETH, &dev);
        if (!dev) {
                printf("No ethernet found.\n");
                bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
@@ -415,20 +420,25 @@ int eth_initialize(void)
 
                bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
                do {
-                       if (num_devices)
-                               printf(", ");
+                       if (dev->seq != -1) {
+                               if (num_devices)
+                                       printf(", ");
 
-                       printf("eth%d: %s", dev->seq, dev->name);
+                               printf("eth%d: %s", dev->seq, dev->name);
 
-                       if (ethprime && dev == prime_dev)
-                               printf(" [PRIME]");
+                               if (ethprime && dev == prime_dev)
+                                       printf(" [PRIME]");
+                       }
 
                        eth_write_hwaddr(dev);
 
-                       uclass_next_device(&dev);
-                       num_devices++;
+                       if (dev->seq != -1)
+                               num_devices++;
+                       uclass_next_device_check(&dev);
                } while (dev);
 
+               if (!num_devices)
+                       printf("No ethernet found.\n");
                putc('\n');
        }
 
@@ -455,6 +465,26 @@ static int eth_pre_unbind(struct udevice *dev)
        return 0;
 }
 
+static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
+{
+#if IS_ENABLED(CONFIG_OF_CONTROL)
+       const uint8_t *p;
+
+       p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
+       if (!p)
+               p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
+
+       if (!p)
+               return false;
+
+       memcpy(mac, p, ARP_HLEN);
+
+       return true;
+#else
+       return false;
+#endif
+}
+
 static int eth_post_probe(struct udevice *dev)
 {
        struct eth_device_priv *priv = dev->uclass_priv;
@@ -476,10 +506,8 @@ static int eth_post_probe(struct udevice *dev)
                        ops->free_pkt += gd->reloc_off;
                if (ops->stop)
                        ops->stop += gd->reloc_off;
-#ifdef CONFIG_MCAST_TFTP
                if (ops->mcast)
                        ops->mcast += gd->reloc_off;
-#endif
                if (ops->write_hwaddr)
                        ops->write_hwaddr += gd->reloc_off;
                if (ops->read_rom_hwaddr)
@@ -491,9 +519,13 @@ static int eth_post_probe(struct udevice *dev)
 
        priv->state = ETH_STATE_INIT;
 
-       /* Check if the device has a MAC address in ROM */
-       if (eth_get_ops(dev)->read_rom_hwaddr)
-               eth_get_ops(dev)->read_rom_hwaddr(dev);
+       /* Check if the device has a valid MAC address in device tree */
+       if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
+           !is_valid_ethaddr(pdata->enetaddr)) {
+               /* Check if the device has a MAC address in ROM */
+               if (eth_get_ops(dev)->read_rom_hwaddr)
+                       eth_get_ops(dev)->read_rom_hwaddr(dev);
+       }
 
        eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
        if (!is_zero_ethaddr(env_enetaddr)) {
@@ -526,6 +558,8 @@ static int eth_post_probe(struct udevice *dev)
 #endif
        }
 
+       eth_write_hwaddr(dev);
+
        return 0;
 }