wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 2 | * (C) Copyright 2001-2015 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 4 | * Joe Hershberger, National Instruments |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 11 | #include <dm.h> |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 12 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | #include <net.h> |
Marian Balakowicz | d9785c1 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 14 | #include <miiphy.h> |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 15 | #include <phy.h> |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 16 | #include <asm/errno.h> |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 18 | #include <dm/uclass-internal.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 19 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 22 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
| 23 | { |
| 24 | char *end; |
| 25 | int i; |
| 26 | |
| 27 | for (i = 0; i < 6; ++i) { |
| 28 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; |
| 29 | if (addr) |
| 30 | addr = (*end) ? end + 1 : end; |
| 31 | } |
| 32 | } |
| 33 | |
Josh Wu | 219cc94 | 2015-09-01 18:22:55 +0800 | [diff] [blame] | 34 | int eth_getenv_enetaddr(const char *name, uchar *enetaddr) |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 35 | { |
| 36 | eth_parse_enetaddr(getenv(name), enetaddr); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 37 | return is_valid_ethaddr(enetaddr); |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Josh Wu | 219cc94 | 2015-09-01 18:22:55 +0800 | [diff] [blame] | 40 | int eth_setenv_enetaddr(const char *name, const uchar *enetaddr) |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 41 | { |
| 42 | char buf[20]; |
| 43 | |
| 44 | sprintf(buf, "%pM", enetaddr); |
| 45 | |
| 46 | return setenv(name, buf); |
| 47 | } |
Mike Frysinger | 86848a7 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 48 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 49 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
| 50 | uchar *enetaddr) |
Mike Frysinger | 86848a7 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 51 | { |
| 52 | char enetvar[32]; |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 53 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Mike Frysinger | 86848a7 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 54 | return eth_getenv_enetaddr(enetvar, enetaddr); |
| 55 | } |
Mike Frysinger | 3f6e699 | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 56 | |
Joe Hershberger | 154177e | 2012-07-10 16:23:22 -0500 | [diff] [blame] | 57 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
Rob Herring | c88ef3c | 2012-04-14 18:06:49 +0000 | [diff] [blame] | 58 | uchar *enetaddr) |
| 59 | { |
| 60 | char enetvar[32]; |
| 61 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
| 62 | return eth_setenv_enetaddr(enetvar, enetaddr); |
| 63 | } |
| 64 | |
Ben Warren | ecee932 | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 65 | static int eth_mac_skip(int index) |
| 66 | { |
| 67 | char enetvar[15]; |
| 68 | char *skip_state; |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 69 | |
Ben Warren | ecee932 | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 70 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 71 | skip_state = getenv(enetvar); |
| 72 | return skip_state != NULL; |
Ben Warren | ecee932 | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 75 | static void eth_current_changed(void); |
| 76 | |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 77 | /* |
| 78 | * CPU and board-specific Ethernet initializations. Aliased function |
| 79 | * signals caller to move on |
| 80 | */ |
| 81 | static int __def_eth_init(bd_t *bis) |
| 82 | { |
| 83 | return -1; |
| 84 | } |
| 85 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 86 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 87 | |
| 88 | static void eth_common_init(void) |
| 89 | { |
| 90 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
| 91 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
| 92 | miiphy_init(); |
| 93 | #endif |
| 94 | |
| 95 | #ifdef CONFIG_PHYLIB |
| 96 | phy_init(); |
| 97 | #endif |
| 98 | |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 99 | /* |
| 100 | * If board-specific initialization exists, call it. |
| 101 | * If not, call a CPU-specific one |
| 102 | */ |
| 103 | if (board_eth_init != __def_eth_init) { |
| 104 | if (board_eth_init(gd->bd) < 0) |
| 105 | printf("Board Net Initialization Failed\n"); |
| 106 | } else if (cpu_eth_init != __def_eth_init) { |
| 107 | if (cpu_eth_init(gd->bd) < 0) |
| 108 | printf("CPU Net Initialization Failed\n"); |
| 109 | } else { |
Bin Meng | d8f79af | 2015-08-27 22:25:52 -0700 | [diff] [blame] | 110 | #ifndef CONFIG_DM_ETH |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 111 | printf("Net Initialization Skipped\n"); |
Bin Meng | d8f79af | 2015-08-27 22:25:52 -0700 | [diff] [blame] | 112 | #endif |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 116 | #ifdef CONFIG_DM_ETH |
| 117 | /** |
| 118 | * struct eth_device_priv - private structure for each Ethernet device |
| 119 | * |
| 120 | * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t) |
| 121 | */ |
| 122 | struct eth_device_priv { |
| 123 | enum eth_state_t state; |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | * struct eth_uclass_priv - The structure attached to the uclass itself |
| 128 | * |
| 129 | * @current: The Ethernet device that the network functions are using |
| 130 | */ |
| 131 | struct eth_uclass_priv { |
| 132 | struct udevice *current; |
| 133 | }; |
| 134 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 135 | /* eth_errno - This stores the most recent failure code from DM functions */ |
| 136 | static int eth_errno; |
| 137 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 138 | static struct eth_uclass_priv *eth_get_uclass_priv(void) |
| 139 | { |
| 140 | struct uclass *uc; |
| 141 | |
| 142 | uclass_get(UCLASS_ETH, &uc); |
| 143 | assert(uc); |
| 144 | return uc->priv; |
| 145 | } |
| 146 | |
| 147 | static void eth_set_current_to_next(void) |
| 148 | { |
| 149 | struct eth_uclass_priv *uc_priv; |
| 150 | |
| 151 | uc_priv = eth_get_uclass_priv(); |
| 152 | if (uc_priv->current) |
| 153 | uclass_next_device(&uc_priv->current); |
| 154 | if (!uc_priv->current) |
| 155 | uclass_first_device(UCLASS_ETH, &uc_priv->current); |
| 156 | } |
| 157 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 158 | /* |
| 159 | * Typically this will simply return the active device. |
| 160 | * In the case where the most recent active device was unset, this will attempt |
| 161 | * to return the first device. If that device doesn't exist or fails to probe, |
| 162 | * this function will return NULL. |
| 163 | */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 164 | struct udevice *eth_get_dev(void) |
| 165 | { |
| 166 | struct eth_uclass_priv *uc_priv; |
| 167 | |
| 168 | uc_priv = eth_get_uclass_priv(); |
| 169 | if (!uc_priv->current) |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 170 | eth_errno = uclass_first_device(UCLASS_ETH, |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 171 | &uc_priv->current); |
| 172 | return uc_priv->current; |
| 173 | } |
| 174 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 175 | /* |
| 176 | * Typically this will just store a device pointer. |
| 177 | * In case it was not probed, we will attempt to do so. |
| 178 | * dev may be NULL to unset the active device. |
| 179 | */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 180 | static void eth_set_dev(struct udevice *dev) |
| 181 | { |
Bin Meng | ac1d313 | 2015-10-07 21:45:44 -0700 | [diff] [blame] | 182 | if (dev && !device_active(dev)) { |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 183 | eth_errno = device_probe(dev); |
Bin Meng | ac1d313 | 2015-10-07 21:45:44 -0700 | [diff] [blame] | 184 | if (eth_errno) |
| 185 | dev = NULL; |
| 186 | } |
| 187 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 188 | eth_get_uclass_priv()->current = dev; |
| 189 | } |
| 190 | |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 191 | /* |
| 192 | * Find the udevice that either has the name passed in as devname or has an |
| 193 | * alias named devname. |
| 194 | */ |
| 195 | struct udevice *eth_get_dev_by_name(const char *devname) |
| 196 | { |
| 197 | int seq = -1; |
| 198 | char *endp = NULL; |
| 199 | const char *startp = NULL; |
| 200 | struct udevice *it; |
| 201 | struct uclass *uc; |
Bin Meng | e408c42 | 2015-08-27 22:25:54 -0700 | [diff] [blame] | 202 | int len = strlen("eth"); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 203 | |
| 204 | /* Must be longer than 3 to be an alias */ |
Bin Meng | e408c42 | 2015-08-27 22:25:54 -0700 | [diff] [blame] | 205 | if (!strncmp(devname, "eth", len) && strlen(devname) > len) { |
| 206 | startp = devname + len; |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 207 | seq = simple_strtoul(startp, &endp, 10); |
| 208 | } |
| 209 | |
| 210 | uclass_get(UCLASS_ETH, &uc); |
| 211 | uclass_foreach_dev(it, uc) { |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 212 | /* |
| 213 | * We need the seq to be valid, so try to probe it. |
| 214 | * If the probe fails, the seq will not match since it will be |
| 215 | * -1 instead of what we are looking for. |
| 216 | * We don't care about errors from probe here. Either they won't |
| 217 | * match an alias or it will match a literal name and we'll pick |
| 218 | * up the error when we try to probe again in eth_set_dev(). |
| 219 | */ |
Bin Meng | ac1d313 | 2015-10-07 21:45:44 -0700 | [diff] [blame] | 220 | if (device_probe(it)) |
| 221 | continue; |
| 222 | /* Check for the name or the sequence number to match */ |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 223 | if (strcmp(it->name, devname) == 0 || |
| 224 | (endp > startp && it->seq == seq)) |
| 225 | return it; |
| 226 | } |
| 227 | |
| 228 | return NULL; |
| 229 | } |
| 230 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 231 | unsigned char *eth_get_ethaddr(void) |
| 232 | { |
| 233 | struct eth_pdata *pdata; |
| 234 | |
| 235 | if (eth_get_dev()) { |
| 236 | pdata = eth_get_dev()->platdata; |
| 237 | return pdata->enetaddr; |
| 238 | } |
| 239 | |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | /* Set active state without calling start on the driver */ |
| 244 | int eth_init_state_only(void) |
| 245 | { |
| 246 | struct udevice *current; |
| 247 | struct eth_device_priv *priv; |
| 248 | |
| 249 | current = eth_get_dev(); |
| 250 | if (!current || !device_active(current)) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | priv = current->uclass_priv; |
| 254 | priv->state = ETH_STATE_ACTIVE; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* Set passive state without calling stop on the driver */ |
| 260 | void eth_halt_state_only(void) |
| 261 | { |
| 262 | struct udevice *current; |
| 263 | struct eth_device_priv *priv; |
| 264 | |
| 265 | current = eth_get_dev(); |
| 266 | if (!current || !device_active(current)) |
| 267 | return; |
| 268 | |
| 269 | priv = current->uclass_priv; |
| 270 | priv->state = ETH_STATE_PASSIVE; |
| 271 | } |
| 272 | |
| 273 | int eth_get_dev_index(void) |
| 274 | { |
| 275 | if (eth_get_dev()) |
| 276 | return eth_get_dev()->seq; |
| 277 | return -1; |
| 278 | } |
| 279 | |
Joe Hershberger | f566c99 | 2015-03-24 02:41:49 -0500 | [diff] [blame] | 280 | static int eth_write_hwaddr(struct udevice *dev) |
| 281 | { |
| 282 | struct eth_pdata *pdata = dev->platdata; |
| 283 | int ret = 0; |
| 284 | |
| 285 | if (!dev || !device_active(dev)) |
| 286 | return -EINVAL; |
| 287 | |
| 288 | /* seq is valid since the device is active */ |
| 289 | if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) { |
| 290 | if (!is_valid_ethaddr(pdata->enetaddr)) { |
| 291 | printf("\nError: %s address %pM illegal value\n", |
| 292 | dev->name, pdata->enetaddr); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
Simon Glass | b86f795 | 2015-07-06 16:47:55 -0600 | [diff] [blame] | 296 | /* |
| 297 | * Drivers are allowed to decide not to implement this at |
| 298 | * run-time. E.g. Some devices may use it and some may not. |
| 299 | */ |
Joe Hershberger | f566c99 | 2015-03-24 02:41:49 -0500 | [diff] [blame] | 300 | ret = eth_get_ops(dev)->write_hwaddr(dev); |
Simon Glass | b86f795 | 2015-07-06 16:47:55 -0600 | [diff] [blame] | 301 | if (ret == -ENOSYS) |
| 302 | ret = 0; |
Joe Hershberger | f566c99 | 2015-03-24 02:41:49 -0500 | [diff] [blame] | 303 | if (ret) |
| 304 | printf("\nWarning: %s failed to set MAC address\n", |
| 305 | dev->name); |
| 306 | } |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 311 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
| 312 | int flags) |
| 313 | { |
| 314 | int index; |
| 315 | int retval; |
| 316 | struct udevice *dev; |
| 317 | |
| 318 | /* look for an index after "eth" */ |
| 319 | index = simple_strtoul(name + 3, NULL, 10); |
| 320 | |
| 321 | retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev); |
| 322 | if (!retval) { |
| 323 | struct eth_pdata *pdata = dev->platdata; |
| 324 | switch (op) { |
| 325 | case env_op_create: |
| 326 | case env_op_overwrite: |
| 327 | eth_parse_enetaddr(value, pdata->enetaddr); |
| 328 | break; |
| 329 | case env_op_delete: |
| 330 | memset(pdata->enetaddr, 0, 6); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); |
| 337 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 338 | int eth_init(void) |
| 339 | { |
Bin Meng | 4cdc2c8 | 2015-12-21 22:43:39 -0800 | [diff] [blame] | 340 | char *ethact = getenv("ethact"); |
| 341 | char *ethrotate = getenv("ethrotate"); |
| 342 | struct udevice *current = NULL; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 343 | struct udevice *old_current; |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 344 | int ret = -ENODEV; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 345 | |
Bin Meng | 4cdc2c8 | 2015-12-21 22:43:39 -0800 | [diff] [blame] | 346 | /* |
| 347 | * When 'ethrotate' variable is set to 'no' and 'ethact' variable |
| 348 | * is already set to an ethernet device, we should stick to 'ethact'. |
| 349 | */ |
| 350 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) { |
| 351 | if (ethact) { |
| 352 | current = eth_get_dev_by_name(ethact); |
| 353 | if (!current) |
| 354 | return -EINVAL; |
| 355 | } |
| 356 | } |
| 357 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 358 | if (!current) { |
Bin Meng | 4cdc2c8 | 2015-12-21 22:43:39 -0800 | [diff] [blame] | 359 | current = eth_get_dev(); |
| 360 | if (!current) { |
| 361 | printf("No ethernet found.\n"); |
| 362 | return -ENODEV; |
| 363 | } |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | old_current = current; |
| 367 | do { |
Bin Meng | ac1d313 | 2015-10-07 21:45:44 -0700 | [diff] [blame] | 368 | if (current) { |
| 369 | debug("Trying %s\n", current->name); |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 370 | |
Bin Meng | ac1d313 | 2015-10-07 21:45:44 -0700 | [diff] [blame] | 371 | if (device_active(current)) { |
| 372 | ret = eth_get_ops(current)->start(current); |
| 373 | if (ret >= 0) { |
| 374 | struct eth_device_priv *priv = |
| 375 | current->uclass_priv; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 376 | |
Bin Meng | ac1d313 | 2015-10-07 21:45:44 -0700 | [diff] [blame] | 377 | priv->state = ETH_STATE_ACTIVE; |
| 378 | return 0; |
| 379 | } |
| 380 | } else { |
| 381 | ret = eth_errno; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 382 | } |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 383 | |
Bin Meng | ac1d313 | 2015-10-07 21:45:44 -0700 | [diff] [blame] | 384 | debug("FAIL\n"); |
| 385 | } else { |
| 386 | debug("PROBE FAIL\n"); |
| 387 | } |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 388 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 389 | /* |
| 390 | * If ethrotate is enabled, this will change "current", |
| 391 | * otherwise we will drop out of this while loop immediately |
| 392 | */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 393 | eth_try_another(0); |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 394 | /* This will ensure the new "current" attempted to probe */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 395 | current = eth_get_dev(); |
| 396 | } while (old_current != current); |
| 397 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 398 | return ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void eth_halt(void) |
| 402 | { |
| 403 | struct udevice *current; |
| 404 | struct eth_device_priv *priv; |
| 405 | |
| 406 | current = eth_get_dev(); |
| 407 | if (!current || !device_active(current)) |
| 408 | return; |
| 409 | |
| 410 | eth_get_ops(current)->stop(current); |
| 411 | priv = current->uclass_priv; |
| 412 | priv->state = ETH_STATE_PASSIVE; |
| 413 | } |
| 414 | |
Bernhard Nortmann | eaa8a19 | 2015-09-14 15:29:43 +0200 | [diff] [blame] | 415 | int eth_is_active(struct udevice *dev) |
| 416 | { |
| 417 | struct eth_device_priv *priv; |
| 418 | |
| 419 | if (!dev || !device_active(dev)) |
| 420 | return 0; |
| 421 | |
| 422 | priv = dev_get_uclass_priv(dev); |
| 423 | return priv->state == ETH_STATE_ACTIVE; |
| 424 | } |
| 425 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 426 | int eth_send(void *packet, int length) |
| 427 | { |
| 428 | struct udevice *current; |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 429 | int ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 430 | |
| 431 | current = eth_get_dev(); |
| 432 | if (!current) |
| 433 | return -ENODEV; |
| 434 | |
| 435 | if (!device_active(current)) |
| 436 | return -EINVAL; |
| 437 | |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 438 | ret = eth_get_ops(current)->send(current, packet, length); |
| 439 | if (ret < 0) { |
| 440 | /* We cannot completely return the error at present */ |
| 441 | debug("%s: send() returned error %d\n", __func__, ret); |
| 442 | } |
| 443 | return ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | int eth_rx(void) |
| 447 | { |
| 448 | struct udevice *current; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 449 | uchar *packet; |
Simon Glass | a1ca92e | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 450 | int flags; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 451 | int ret; |
| 452 | int i; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 453 | |
| 454 | current = eth_get_dev(); |
| 455 | if (!current) |
| 456 | return -ENODEV; |
| 457 | |
| 458 | if (!device_active(current)) |
| 459 | return -EINVAL; |
| 460 | |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 461 | /* Process up to 32 packets at one time */ |
Simon Glass | a1ca92e | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 462 | flags = ETH_RECV_CHECK_DEVICE; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 463 | for (i = 0; i < 32; i++) { |
Simon Glass | a1ca92e | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 464 | ret = eth_get_ops(current)->recv(current, flags, &packet); |
| 465 | flags = 0; |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 466 | if (ret > 0) |
| 467 | net_process_received_packet(packet, ret); |
Joe Hershberger | 63c9729 | 2015-04-03 20:09:46 -0500 | [diff] [blame] | 468 | if (ret >= 0 && eth_get_ops(current)->free_pkt) |
| 469 | eth_get_ops(current)->free_pkt(current, packet, ret); |
| 470 | if (ret <= 0) |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | if (ret == -EAGAIN) |
| 474 | ret = 0; |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 475 | if (ret < 0) { |
| 476 | /* We cannot completely return the error at present */ |
| 477 | debug("%s: recv() returned error %d\n", __func__, ret); |
| 478 | } |
Joe Hershberger | 1759140 | 2015-03-22 17:09:12 -0500 | [diff] [blame] | 479 | return ret; |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 480 | } |
| 481 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 482 | int eth_initialize(void) |
| 483 | { |
| 484 | int num_devices = 0; |
| 485 | struct udevice *dev; |
| 486 | |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 487 | eth_common_init(); |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * Devices need to write the hwaddr even if not started so that Linux |
| 491 | * will have access to the hwaddr that u-boot stored for the device. |
| 492 | * This is accomplished by attempting to probe each device and calling |
| 493 | * their write_hwaddr() operation. |
| 494 | */ |
| 495 | uclass_first_device(UCLASS_ETH, &dev); |
| 496 | if (!dev) { |
| 497 | printf("No ethernet found.\n"); |
| 498 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
| 499 | } else { |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 500 | char *ethprime = getenv("ethprime"); |
| 501 | struct udevice *prime_dev = NULL; |
| 502 | |
| 503 | if (ethprime) |
| 504 | prime_dev = eth_get_dev_by_name(ethprime); |
| 505 | if (prime_dev) { |
| 506 | eth_set_dev(prime_dev); |
| 507 | eth_current_changed(); |
| 508 | } else { |
| 509 | eth_set_dev(NULL); |
| 510 | } |
| 511 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 512 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
| 513 | do { |
| 514 | if (num_devices) |
| 515 | printf(", "); |
| 516 | |
| 517 | printf("eth%d: %s", dev->seq, dev->name); |
| 518 | |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 519 | if (ethprime && dev == prime_dev) |
| 520 | printf(" [PRIME]"); |
| 521 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 522 | eth_write_hwaddr(dev); |
| 523 | |
| 524 | uclass_next_device(&dev); |
| 525 | num_devices++; |
| 526 | } while (dev); |
| 527 | |
| 528 | putc('\n'); |
| 529 | } |
| 530 | |
| 531 | return num_devices; |
| 532 | } |
| 533 | |
| 534 | static int eth_post_bind(struct udevice *dev) |
| 535 | { |
| 536 | if (strchr(dev->name, ' ')) { |
| 537 | printf("\nError: eth device name \"%s\" has a space!\n", |
| 538 | dev->name); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int eth_pre_unbind(struct udevice *dev) |
| 546 | { |
| 547 | /* Don't hang onto a pointer that is going away */ |
| 548 | if (dev == eth_get_uclass_priv()->current) |
| 549 | eth_set_dev(NULL); |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int eth_post_probe(struct udevice *dev) |
| 555 | { |
| 556 | struct eth_device_priv *priv = dev->uclass_priv; |
| 557 | struct eth_pdata *pdata = dev->platdata; |
| 558 | unsigned char env_enetaddr[6]; |
| 559 | |
Michal Simek | 9406758 | 2015-12-08 16:45:30 +0100 | [diff] [blame] | 560 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 561 | struct eth_ops *ops = eth_get_ops(dev); |
| 562 | static int reloc_done; |
| 563 | |
| 564 | if (!reloc_done) { |
| 565 | if (ops->start) |
| 566 | ops->start += gd->reloc_off; |
| 567 | if (ops->send) |
| 568 | ops->send += gd->reloc_off; |
| 569 | if (ops->recv) |
| 570 | ops->recv += gd->reloc_off; |
| 571 | if (ops->free_pkt) |
| 572 | ops->free_pkt += gd->reloc_off; |
| 573 | if (ops->stop) |
| 574 | ops->stop += gd->reloc_off; |
| 575 | #ifdef CONFIG_MCAST_TFTP |
| 576 | if (ops->mcast) |
| 577 | ops->mcast += gd->reloc_off; |
| 578 | #endif |
| 579 | if (ops->write_hwaddr) |
| 580 | ops->write_hwaddr += gd->reloc_off; |
| 581 | if (ops->read_rom_hwaddr) |
| 582 | ops->read_rom_hwaddr += gd->reloc_off; |
| 583 | |
| 584 | reloc_done++; |
| 585 | } |
| 586 | #endif |
| 587 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 588 | priv->state = ETH_STATE_INIT; |
| 589 | |
| 590 | /* Check if the device has a MAC address in ROM */ |
| 591 | if (eth_get_ops(dev)->read_rom_hwaddr) |
| 592 | eth_get_ops(dev)->read_rom_hwaddr(dev); |
| 593 | |
| 594 | eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 595 | if (!is_zero_ethaddr(env_enetaddr)) { |
| 596 | if (!is_zero_ethaddr(pdata->enetaddr) && |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 597 | memcmp(pdata->enetaddr, env_enetaddr, 6)) { |
| 598 | printf("\nWarning: %s MAC addresses don't match:\n", |
| 599 | dev->name); |
| 600 | printf("Address in SROM is %pM\n", |
| 601 | pdata->enetaddr); |
| 602 | printf("Address in environment is %pM\n", |
| 603 | env_enetaddr); |
| 604 | } |
| 605 | |
| 606 | /* Override the ROM MAC address */ |
| 607 | memcpy(pdata->enetaddr, env_enetaddr, 6); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 608 | } else if (is_valid_ethaddr(pdata->enetaddr)) { |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 609 | eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr); |
| 610 | printf("\nWarning: %s using MAC address from ROM\n", |
| 611 | dev->name); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 612 | } else if (is_zero_ethaddr(pdata->enetaddr)) { |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 613 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
| 614 | net_random_ethaddr(pdata->enetaddr); |
| 615 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", |
| 616 | dev->name, dev->seq, pdata->enetaddr); |
| 617 | #else |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 618 | printf("\nError: %s address not set.\n", |
| 619 | dev->name); |
| 620 | return -EINVAL; |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 621 | #endif |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int eth_pre_remove(struct udevice *dev) |
| 628 | { |
Bin Meng | a16edab | 2015-10-07 21:45:42 -0700 | [diff] [blame] | 629 | struct eth_pdata *pdata = dev->platdata; |
| 630 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 631 | eth_get_ops(dev)->stop(dev); |
| 632 | |
Bin Meng | a16edab | 2015-10-07 21:45:42 -0700 | [diff] [blame] | 633 | /* clear the MAC address */ |
| 634 | memset(pdata->enetaddr, 0, 6); |
| 635 | |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | UCLASS_DRIVER(eth) = { |
| 640 | .name = "eth", |
| 641 | .id = UCLASS_ETH, |
| 642 | .post_bind = eth_post_bind, |
| 643 | .pre_unbind = eth_pre_unbind, |
| 644 | .post_probe = eth_post_probe, |
| 645 | .pre_remove = eth_pre_remove, |
| 646 | .priv_auto_alloc_size = sizeof(struct eth_uclass_priv), |
| 647 | .per_device_auto_alloc_size = sizeof(struct eth_device_priv), |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 648 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 649 | }; |
Bernhard Nortmann | eaa8a19 | 2015-09-14 15:29:43 +0200 | [diff] [blame] | 650 | #endif /* #ifdef CONFIG_DM_ETH */ |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 651 | |
| 652 | #ifndef CONFIG_DM_ETH |
Ben Warren | dd35479 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 653 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 654 | #ifdef CONFIG_API |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 655 | static struct { |
| 656 | uchar data[PKTSIZE]; |
| 657 | int length; |
| 658 | } eth_rcv_bufs[PKTBUFSRX]; |
| 659 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 660 | static unsigned int eth_rcv_current, eth_rcv_last; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 661 | #endif |
| 662 | |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 663 | static struct eth_device *eth_devices; |
| 664 | struct eth_device *eth_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 665 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 666 | static void eth_set_current_to_next(void) |
| 667 | { |
| 668 | eth_current = eth_current->next; |
| 669 | } |
| 670 | |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 671 | static void eth_set_dev(struct eth_device *dev) |
| 672 | { |
| 673 | eth_current = dev; |
| 674 | } |
| 675 | |
Ben Warren | d7fb9bc | 2010-07-29 12:56:11 -0700 | [diff] [blame] | 676 | struct eth_device *eth_get_dev_by_name(const char *devname) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 677 | { |
| 678 | struct eth_device *dev, *target_dev; |
| 679 | |
Helmut Raiger | 7e7f903 | 2011-08-22 00:17:17 +0000 | [diff] [blame] | 680 | BUG_ON(devname == NULL); |
| 681 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 682 | if (!eth_devices) |
| 683 | return NULL; |
| 684 | |
| 685 | dev = eth_devices; |
| 686 | target_dev = NULL; |
| 687 | do { |
| 688 | if (strcmp(devname, dev->name) == 0) { |
| 689 | target_dev = dev; |
| 690 | break; |
| 691 | } |
| 692 | dev = dev->next; |
| 693 | } while (dev != eth_devices); |
| 694 | |
| 695 | return target_dev; |
| 696 | } |
| 697 | |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 698 | struct eth_device *eth_get_dev_by_index(int index) |
| 699 | { |
| 700 | struct eth_device *dev, *target_dev; |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 701 | |
| 702 | if (!eth_devices) |
| 703 | return NULL; |
| 704 | |
| 705 | dev = eth_devices; |
| 706 | target_dev = NULL; |
| 707 | do { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 708 | if (dev->index == index) { |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 709 | target_dev = dev; |
| 710 | break; |
| 711 | } |
| 712 | dev = dev->next; |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 713 | } while (dev != eth_devices); |
| 714 | |
| 715 | return target_dev; |
| 716 | } |
| 717 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 718 | int eth_get_dev_index(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 719 | { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 720 | if (!eth_current) |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 721 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 722 | |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 723 | return eth_current->index; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 726 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
| 727 | int flags) |
| 728 | { |
| 729 | int index; |
| 730 | struct eth_device *dev; |
| 731 | |
| 732 | if (!eth_devices) |
| 733 | return 0; |
| 734 | |
| 735 | /* look for an index after "eth" */ |
| 736 | index = simple_strtoul(name + 3, NULL, 10); |
| 737 | |
| 738 | dev = eth_devices; |
| 739 | do { |
| 740 | if (dev->index == index) { |
| 741 | switch (op) { |
| 742 | case env_op_create: |
| 743 | case env_op_overwrite: |
| 744 | eth_parse_enetaddr(value, dev->enetaddr); |
| 745 | break; |
| 746 | case env_op_delete: |
| 747 | memset(dev->enetaddr, 0, 6); |
| 748 | } |
| 749 | } |
Gong Qianyu | 7aba0f2 | 2015-08-31 11:34:43 +0800 | [diff] [blame] | 750 | dev = dev->next; |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 751 | } while (dev != eth_devices); |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); |
| 756 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 757 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
| 758 | int eth_number) |
| 759 | { |
| 760 | unsigned char env_enetaddr[6]; |
| 761 | int ret = 0; |
| 762 | |
Eric Miao | 6937664 | 2012-01-18 22:56:33 +0000 | [diff] [blame] | 763 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 764 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 765 | if (!is_zero_ethaddr(env_enetaddr)) { |
| 766 | if (!is_zero_ethaddr(dev->enetaddr) && |
Joe Hershberger | 4c7c65a | 2015-03-22 17:09:01 -0500 | [diff] [blame] | 767 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 768 | printf("\nWarning: %s MAC addresses don't match:\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 769 | dev->name); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 770 | printf("Address in SROM is %pM\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 771 | dev->enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 772 | printf("Address in environment is %pM\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 773 | env_enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | memcpy(dev->enetaddr, env_enetaddr, 6); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 777 | } else if (is_valid_ethaddr(dev->enetaddr)) { |
Rob Herring | c88ef3c | 2012-04-14 18:06:49 +0000 | [diff] [blame] | 778 | eth_setenv_enetaddr_by_index(base_name, eth_number, |
| 779 | dev->enetaddr); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 780 | } else if (is_zero_ethaddr(dev->enetaddr)) { |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 781 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
| 782 | net_random_ethaddr(dev->enetaddr); |
| 783 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", |
| 784 | dev->name, eth_number, dev->enetaddr); |
| 785 | #else |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 786 | printf("\nError: %s address not set.\n", |
| 787 | dev->name); |
| 788 | return -EINVAL; |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 789 | #endif |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 792 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 793 | if (!is_valid_ethaddr(dev->enetaddr)) { |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 794 | printf("\nError: %s address %pM illegal value\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 795 | dev->name, dev->enetaddr); |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 796 | return -EINVAL; |
| 797 | } |
Benoît Thébaudeau | 460f949 | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 798 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 799 | ret = dev->write_hwaddr(dev); |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 800 | if (ret) |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 801 | printf("\nWarning: %s failed to set MAC address\n", |
| 802 | dev->name); |
Benoît Thébaudeau | 460f949 | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 803 | } |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 804 | |
| 805 | return ret; |
| 806 | } |
| 807 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 808 | int eth_register(struct eth_device *dev) |
| 809 | { |
| 810 | struct eth_device *d; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 811 | static int index; |
Michal Simek | 58c583b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 812 | |
Mike Frysinger | f6add13 | 2011-11-10 14:11:04 +0000 | [diff] [blame] | 813 | assert(strlen(dev->name) < sizeof(dev->name)); |
Michal Simek | 58c583b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 814 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 815 | if (!eth_devices) { |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 816 | eth_devices = dev; |
| 817 | eth_current = dev; |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 818 | eth_current_changed(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 819 | } else { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 820 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
Detlev Zundel | aba4b69 | 2010-03-31 17:56:08 +0200 | [diff] [blame] | 821 | ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 822 | d->next = dev; |
| 823 | } |
| 824 | |
| 825 | dev->state = ETH_STATE_INIT; |
| 826 | dev->next = eth_devices; |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 827 | dev->index = index++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 832 | int eth_unregister(struct eth_device *dev) |
| 833 | { |
| 834 | struct eth_device *cur; |
| 835 | |
| 836 | /* No device */ |
| 837 | if (!eth_devices) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 838 | return -ENODEV; |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 839 | |
| 840 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; |
| 841 | cur = cur->next) |
| 842 | ; |
| 843 | |
| 844 | /* Device not found */ |
| 845 | if (cur->next != dev) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 846 | return -ENODEV; |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 847 | |
| 848 | cur->next = dev->next; |
| 849 | |
| 850 | if (eth_devices == dev) |
| 851 | eth_devices = dev->next == eth_devices ? NULL : dev->next; |
| 852 | |
| 853 | if (eth_current == dev) { |
| 854 | eth_current = eth_devices; |
| 855 | eth_current_changed(); |
| 856 | } |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 861 | int eth_initialize(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 862 | { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 863 | int num_devices = 0; |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 864 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 865 | eth_devices = NULL; |
| 866 | eth_current = NULL; |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 867 | eth_common_init(); |
Marian Balakowicz | d9785c1 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 868 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 869 | if (!eth_devices) { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 870 | puts("No ethernet found.\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 871 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 872 | } else { |
| 873 | struct eth_device *dev = eth_devices; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 874 | char *ethprime = getenv("ethprime"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 875 | |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 876 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 877 | do { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 878 | if (dev->index) |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 879 | puts(", "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 880 | |
| 881 | printf("%s", dev->name); |
| 882 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 883 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 884 | eth_current = dev; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 885 | puts(" [PRIME]"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Mike Frysinger | 1384f3b | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 888 | if (strchr(dev->name, ' ')) |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 889 | puts("\nWarning: eth device name has a space!" |
| 890 | "\n"); |
Mike Frysinger | 1384f3b | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 891 | |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 892 | eth_write_hwaddr(dev, "eth", dev->index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 893 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 894 | dev = dev->next; |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 895 | num_devices++; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 896 | } while (dev != eth_devices); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 897 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 898 | eth_current_changed(); |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 899 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 902 | return num_devices; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 903 | } |
| 904 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 905 | #ifdef CONFIG_MCAST_TFTP |
| 906 | /* Multicast. |
| 907 | * mcast_addr: multicast ipaddr from which multicast Mac is made |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 908 | * join: 1=join, 0=leave. |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 909 | */ |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 910 | int eth_mcast_join(struct in_addr mcast_ip, int join) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 911 | { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 912 | u8 mcast_mac[6]; |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 913 | if (!eth_current || !eth_current->mcast) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 914 | return -1; |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 915 | mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff; |
| 916 | mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff; |
| 917 | mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f; |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 918 | mcast_mac[2] = 0x5e; |
| 919 | mcast_mac[1] = 0x0; |
| 920 | mcast_mac[0] = 0x1; |
| 921 | return eth_current->mcast(eth_current, mcast_mac, join); |
| 922 | } |
| 923 | |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 924 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
| 925 | * and this is the ethernet-crc method needed for TSEC -- and perhaps |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 926 | * some other adapter -- hash tables |
| 927 | */ |
| 928 | #define CRCPOLY_LE 0xedb88320 |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 929 | u32 ether_crc(size_t len, unsigned char const *p) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 930 | { |
| 931 | int i; |
| 932 | u32 crc; |
| 933 | crc = ~0; |
| 934 | while (len--) { |
| 935 | crc ^= *p++; |
| 936 | for (i = 0; i < 8; i++) |
| 937 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 938 | } |
| 939 | /* an reverse the bits, cuz of way they arrive -- last-first */ |
| 940 | crc = (crc >> 16) | (crc << 16); |
| 941 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); |
| 942 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); |
| 943 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); |
| 944 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); |
| 945 | return crc; |
| 946 | } |
| 947 | |
| 948 | #endif |
| 949 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 950 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 951 | int eth_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 952 | { |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 953 | struct eth_device *old_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 954 | |
Stefan Roese | 6bc1138 | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 955 | if (!eth_current) { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 956 | puts("No ethernet found.\n"); |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 957 | return -ENODEV; |
Stefan Roese | 6bc1138 | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 958 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 959 | |
| 960 | old_current = eth_current; |
| 961 | do { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 962 | debug("Trying %s\n", eth_current->name); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 963 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 964 | if (eth_current->init(eth_current, gd->bd) >= 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 965 | eth_current->state = ETH_STATE_ACTIVE; |
| 966 | |
Upakul Barkakaty | 505be87 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 967 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 968 | } |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 969 | debug("FAIL\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 970 | |
| 971 | eth_try_another(0); |
| 972 | } while (old_current != eth_current); |
| 973 | |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 974 | return -ETIMEDOUT; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | void eth_halt(void) |
| 978 | { |
| 979 | if (!eth_current) |
| 980 | return; |
| 981 | |
| 982 | eth_current->halt(eth_current); |
| 983 | |
| 984 | eth_current->state = ETH_STATE_PASSIVE; |
| 985 | } |
| 986 | |
Bernhard Nortmann | eaa8a19 | 2015-09-14 15:29:43 +0200 | [diff] [blame] | 987 | int eth_is_active(struct eth_device *dev) |
| 988 | { |
| 989 | return dev && dev->state == ETH_STATE_ACTIVE; |
| 990 | } |
| 991 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 992 | int eth_send(void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 993 | { |
| 994 | if (!eth_current) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 995 | return -ENODEV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 996 | |
| 997 | return eth_current->send(eth_current, packet, length); |
| 998 | } |
| 999 | |
| 1000 | int eth_rx(void) |
| 1001 | { |
| 1002 | if (!eth_current) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 1003 | return -ENODEV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1004 | |
| 1005 | return eth_current->recv(eth_current); |
| 1006 | } |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 1007 | #endif /* ifndef CONFIG_DM_ETH */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1008 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1009 | #ifdef CONFIG_API |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1010 | static void eth_save_packet(void *packet, int length) |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1011 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1012 | char *p = packet; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1013 | int i; |
| 1014 | |
| 1015 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) |
| 1016 | return; |
| 1017 | |
| 1018 | if (PKTSIZE < length) |
| 1019 | return; |
| 1020 | |
| 1021 | for (i = 0; i < length; i++) |
| 1022 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; |
| 1023 | |
| 1024 | eth_rcv_bufs[eth_rcv_last].length = length; |
| 1025 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; |
| 1026 | } |
| 1027 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1028 | int eth_receive(void *packet, int length) |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1029 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 1030 | char *p = packet; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1031 | void *pp = push_packet; |
| 1032 | int i; |
| 1033 | |
| 1034 | if (eth_rcv_current == eth_rcv_last) { |
| 1035 | push_packet = eth_save_packet; |
| 1036 | eth_rx(); |
| 1037 | push_packet = pp; |
| 1038 | |
| 1039 | if (eth_rcv_current == eth_rcv_last) |
| 1040 | return -1; |
| 1041 | } |
| 1042 | |
Michael Walle | 46c07bc | 2012-06-22 11:24:28 +0000 | [diff] [blame] | 1043 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 1044 | |
| 1045 | for (i = 0; i < length; i++) |
| 1046 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; |
| 1047 | |
| 1048 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; |
| 1049 | return length; |
| 1050 | } |
| 1051 | #endif /* CONFIG_API */ |
| 1052 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1053 | static void eth_current_changed(void) |
| 1054 | { |
| 1055 | char *act = getenv("ethact"); |
Bin Meng | a671c4f | 2015-12-21 22:43:38 -0800 | [diff] [blame] | 1056 | char *ethrotate; |
| 1057 | |
| 1058 | /* |
| 1059 | * The call to eth_get_dev() below has a side effect of rotating |
| 1060 | * ethernet device if uc_priv->current == NULL. This is not what |
| 1061 | * we want when 'ethrotate' variable is 'no'. |
| 1062 | */ |
| 1063 | ethrotate = getenv("ethrotate"); |
| 1064 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
| 1065 | return; |
| 1066 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1067 | /* update current ethernet name */ |
| 1068 | if (eth_get_dev()) { |
| 1069 | if (act == NULL || strcmp(act, eth_get_name()) != 0) |
| 1070 | setenv("ethact", eth_get_name()); |
| 1071 | } |
| 1072 | /* |
| 1073 | * remove the variable completely if there is no active |
| 1074 | * interface |
| 1075 | */ |
| 1076 | else if (act != NULL) |
| 1077 | setenv("ethact", NULL); |
| 1078 | } |
| 1079 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1080 | void eth_try_another(int first_restart) |
| 1081 | { |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 1082 | static void *first_failed; |
Remy Bohmer | c650e1b | 2011-02-19 20:15:14 +0100 | [diff] [blame] | 1083 | char *ethrotate; |
Matthias Fuchs | e169257 | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 1084 | |
| 1085 | /* |
| 1086 | * Do not rotate between network interfaces when |
| 1087 | * 'ethrotate' variable is set to 'no'. |
| 1088 | */ |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 1089 | ethrotate = getenv("ethrotate"); |
| 1090 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
Matthias Fuchs | e169257 | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 1091 | return; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1092 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1093 | if (!eth_get_dev()) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1094 | return; |
| 1095 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 1096 | if (first_restart) |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1097 | first_failed = eth_get_dev(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1098 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1099 | eth_set_current_to_next(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1100 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 1101 | eth_current_changed(); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1102 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1103 | if (first_failed == eth_get_dev()) |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 1104 | net_restart_wrap = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1107 | void eth_set_current(void) |
| 1108 | { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 1109 | static char *act; |
| 1110 | static int env_changed_id; |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 1111 | int env_id; |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1112 | |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 1113 | env_id = get_env_id(); |
| 1114 | if ((act == NULL) || (env_changed_id != env_id)) { |
| 1115 | act = getenv("ethact"); |
| 1116 | env_changed_id = env_id; |
| 1117 | } |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 1118 | |
| 1119 | if (act == NULL) { |
| 1120 | char *ethprime = getenv("ethprime"); |
| 1121 | void *dev = NULL; |
| 1122 | |
| 1123 | if (ethprime) |
| 1124 | dev = eth_get_dev_by_name(ethprime); |
| 1125 | if (dev) |
| 1126 | eth_set_dev(dev); |
| 1127 | else |
| 1128 | eth_set_dev(NULL); |
| 1129 | } else { |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 1130 | eth_set_dev(eth_get_dev_by_name(act)); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 1131 | } |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1132 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 1133 | eth_current_changed(); |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1134 | } |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 1135 | |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1136 | const char *eth_get_name(void) |
wdenk | 5bb226e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 1137 | { |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 1138 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
wdenk | 5bb226e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 1139 | } |