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