1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
5 * Joe Hershberger, National Instruments
14 #include "eth_internal.h"
16 int eth_env_get_enetaddr_by_index(const char *base_name, int index,
20 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
21 return eth_env_get_enetaddr(enetvar, enetaddr);
24 int eth_env_set_enetaddr_by_index(const char *base_name, int index,
28 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
29 return eth_env_set_enetaddr(enetvar, enetaddr);
32 void eth_common_init(void)
34 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
35 #if CONFIG_IS_ENABLED(ETH)
36 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
46 int eth_mac_skip(int index)
51 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
52 skip_state = env_get(enetvar);
53 return skip_state != NULL;
56 void eth_current_changed(void)
58 char *act = env_get("ethact");
62 * The call to eth_get_dev() below has a side effect of rotating
63 * ethernet device if uc_priv->current == NULL. This is not what
64 * we want when 'ethrotate' variable is 'no'.
66 ethrotate = env_get("ethrotate");
67 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
70 /* update current ethernet name */
72 if (act == NULL || strcmp(act, eth_get_name()) != 0)
73 env_set("ethact", eth_get_name());
76 * remove the variable completely if there is no active
80 env_set("ethact", NULL);
83 void eth_try_another(int first_restart)
85 static void *first_failed;
89 * Do not rotate between network interfaces when
90 * 'ethrotate' variable is set to 'no'.
92 ethrotate = env_get("ethrotate");
93 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
100 first_failed = eth_get_dev();
102 eth_set_current_to_next();
104 eth_current_changed();
106 if (first_failed == eth_get_dev())
107 net_restart_wrap = 1;
110 void eth_set_current(void)
113 static int env_changed_id;
116 env_id = env_get_id();
117 if ((act == NULL) || (env_changed_id != env_id)) {
118 act = env_get("ethact");
119 env_changed_id = env_id;
123 char *ethprime = env_get("ethprime");
127 dev = eth_get_dev_by_name(ethprime);
133 eth_set_dev(eth_get_dev_by_name(act));
136 eth_current_changed();
139 const char *eth_get_name(void)
141 return eth_get_dev() ? eth_get_dev()->name : "unknown";