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