blob: b978aae1016f05a2960870ecdb70244be0f54c45 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Joe Hershberger05c3e682015-03-22 17:09:10 -05002 * (C) Copyright 2001-2015
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Joe Hershberger05c3e682015-03-22 17:09:10 -05004 * Joe Hershberger, National Instruments
wdenkc6097192002-11-03 00:24:07 +00005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00007 */
8
9#include <common.h>
10#include <command.h>
Joe Hershberger05c3e682015-03-22 17:09:10 -050011#include <dm.h>
Joe Hershberger6e0d26c2015-05-20 14:27:26 -050012#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <net.h>
Marian Balakowiczd9785c12005-11-30 18:06:04 +010014#include <miiphy.h>
Andy Fleming5f184712011-04-08 02:10:27 -050015#include <phy.h>
Pavel Machek75d9a452014-07-13 10:27:02 +020016#include <asm/errno.h>
Joe Hershberger05c3e682015-03-22 17:09:10 -050017#include <dm/device-internal.h>
Joe Hershberger6e0d26c2015-05-20 14:27:26 -050018#include <dm/uclass-internal.h>
wdenkc6097192002-11-03 00:24:07 +000019
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050020DECLARE_GLOBAL_DATA_PTR;
21
Mike Frysinger3f6e6992009-01-29 19:43:44 -050022void 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 Wu219cc942015-09-01 18:22:55 +080034int eth_getenv_enetaddr(const char *name, uchar *enetaddr)
Mike Frysinger3f6e6992009-01-29 19:43:44 -050035{
36 eth_parse_enetaddr(getenv(name), enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -050037 return is_valid_ethaddr(enetaddr);
Mike Frysinger3f6e6992009-01-29 19:43:44 -050038}
39
Josh Wu219cc942015-09-01 18:22:55 +080040int eth_setenv_enetaddr(const char *name, const uchar *enetaddr)
Mike Frysinger3f6e6992009-01-29 19:43:44 -050041{
42 char buf[20];
43
44 sprintf(buf, "%pM", enetaddr);
45
46 return setenv(name, buf);
47}
Mike Frysinger86848a72009-07-15 21:31:28 -040048
Simon Glass7616e782011-06-13 16:13:10 -070049int eth_getenv_enetaddr_by_index(const char *base_name, int index,
50 uchar *enetaddr)
Mike Frysinger86848a72009-07-15 21:31:28 -040051{
52 char enetvar[32];
Simon Glass7616e782011-06-13 16:13:10 -070053 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Mike Frysinger86848a72009-07-15 21:31:28 -040054 return eth_getenv_enetaddr(enetvar, enetaddr);
55}
Mike Frysinger3f6e6992009-01-29 19:43:44 -050056
Joe Hershberger154177e2012-07-10 16:23:22 -050057static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
Rob Herringc88ef3c2012-04-14 18:06:49 +000058 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 Warrenecee9322010-04-26 11:11:46 -070065static int eth_mac_skip(int index)
66{
67 char enetvar[15];
68 char *skip_state;
Joe Hershbergerff819a32015-04-08 01:41:19 -050069
Ben Warrenecee9322010-04-26 11:11:46 -070070 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
Joe Hershbergerff819a32015-04-08 01:41:19 -050071 skip_state = getenv(enetvar);
72 return skip_state != NULL;
Ben Warrenecee9322010-04-26 11:11:46 -070073}
74
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050075static void eth_current_changed(void);
76
Simon Glass3bc42702015-04-05 16:07:37 -060077/*
78 * CPU and board-specific Ethernet initializations. Aliased function
79 * signals caller to move on
80 */
81static int __def_eth_init(bd_t *bis)
82{
83 return -1;
84}
85int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
86int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
87
88static 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 Glass3bc42702015-04-05 16:07:37 -060099 /*
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 Mengd8f79af2015-08-27 22:25:52 -0700110#ifndef CONFIG_DM_ETH
Simon Glass3bc42702015-04-05 16:07:37 -0600111 printf("Net Initialization Skipped\n");
Bin Mengd8f79af2015-08-27 22:25:52 -0700112#endif
Simon Glass3bc42702015-04-05 16:07:37 -0600113 }
114}
115
Joe Hershberger05c3e682015-03-22 17:09:10 -0500116#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 */
122struct 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 */
131struct eth_uclass_priv {
132 struct udevice *current;
133};
134
Joe Hershberger60304592015-03-22 17:09:24 -0500135/* eth_errno - This stores the most recent failure code from DM functions */
136static int eth_errno;
137
Joe Hershberger05c3e682015-03-22 17:09:10 -0500138static 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
147static 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 Hershberger60304592015-03-22 17:09:24 -0500158/*
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 Hershberger05c3e682015-03-22 17:09:10 -0500164struct udevice *eth_get_dev(void)
165{
166 struct eth_uclass_priv *uc_priv;
167
168 uc_priv = eth_get_uclass_priv();
169 if (!uc_priv->current)
Joe Hershberger60304592015-03-22 17:09:24 -0500170 eth_errno = uclass_first_device(UCLASS_ETH,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500171 &uc_priv->current);
172 return uc_priv->current;
173}
174
Joe Hershberger60304592015-03-22 17:09:24 -0500175/*
176 * Typically this will just store a device pointer.
177 * In case it was not probed, we will attempt to do so.
178 * dev may be NULL to unset the active device.
179 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500180static void eth_set_dev(struct udevice *dev)
181{
Joe Hershberger60304592015-03-22 17:09:24 -0500182 if (dev && !device_active(dev))
183 eth_errno = device_probe(dev);
Joe Hershberger05c3e682015-03-22 17:09:10 -0500184 eth_get_uclass_priv()->current = dev;
185}
186
Joe Hershbergere58780d2015-03-22 17:09:16 -0500187/*
188 * Find the udevice that either has the name passed in as devname or has an
189 * alias named devname.
190 */
191struct udevice *eth_get_dev_by_name(const char *devname)
192{
193 int seq = -1;
194 char *endp = NULL;
195 const char *startp = NULL;
196 struct udevice *it;
197 struct uclass *uc;
Bin Menge408c422015-08-27 22:25:54 -0700198 int len = strlen("eth");
Joe Hershbergere58780d2015-03-22 17:09:16 -0500199
200 /* Must be longer than 3 to be an alias */
Bin Menge408c422015-08-27 22:25:54 -0700201 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
202 startp = devname + len;
Joe Hershbergere58780d2015-03-22 17:09:16 -0500203 seq = simple_strtoul(startp, &endp, 10);
204 }
205
206 uclass_get(UCLASS_ETH, &uc);
207 uclass_foreach_dev(it, uc) {
Joe Hershberger60304592015-03-22 17:09:24 -0500208 /*
209 * We need the seq to be valid, so try to probe it.
210 * If the probe fails, the seq will not match since it will be
211 * -1 instead of what we are looking for.
212 * We don't care about errors from probe here. Either they won't
213 * match an alias or it will match a literal name and we'll pick
214 * up the error when we try to probe again in eth_set_dev().
215 */
Joe Hershbergere58780d2015-03-22 17:09:16 -0500216 device_probe(it);
217 /*
218 * Check for the name or the sequence number to match
219 */
220 if (strcmp(it->name, devname) == 0 ||
221 (endp > startp && it->seq == seq))
222 return it;
223 }
224
225 return NULL;
226}
227
Joe Hershberger05c3e682015-03-22 17:09:10 -0500228unsigned char *eth_get_ethaddr(void)
229{
230 struct eth_pdata *pdata;
231
232 if (eth_get_dev()) {
233 pdata = eth_get_dev()->platdata;
234 return pdata->enetaddr;
235 }
236
237 return NULL;
238}
239
240/* Set active state without calling start on the driver */
241int eth_init_state_only(void)
242{
243 struct udevice *current;
244 struct eth_device_priv *priv;
245
246 current = eth_get_dev();
247 if (!current || !device_active(current))
248 return -EINVAL;
249
250 priv = current->uclass_priv;
251 priv->state = ETH_STATE_ACTIVE;
252
253 return 0;
254}
255
256/* Set passive state without calling stop on the driver */
257void eth_halt_state_only(void)
258{
259 struct udevice *current;
260 struct eth_device_priv *priv;
261
262 current = eth_get_dev();
263 if (!current || !device_active(current))
264 return;
265
266 priv = current->uclass_priv;
267 priv->state = ETH_STATE_PASSIVE;
268}
269
270int eth_get_dev_index(void)
271{
272 if (eth_get_dev())
273 return eth_get_dev()->seq;
274 return -1;
275}
276
Joe Hershbergerf566c992015-03-24 02:41:49 -0500277static int eth_write_hwaddr(struct udevice *dev)
278{
279 struct eth_pdata *pdata = dev->platdata;
280 int ret = 0;
281
282 if (!dev || !device_active(dev))
283 return -EINVAL;
284
285 /* seq is valid since the device is active */
286 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
287 if (!is_valid_ethaddr(pdata->enetaddr)) {
288 printf("\nError: %s address %pM illegal value\n",
289 dev->name, pdata->enetaddr);
290 return -EINVAL;
291 }
292
Simon Glassb86f7952015-07-06 16:47:55 -0600293 /*
294 * Drivers are allowed to decide not to implement this at
295 * run-time. E.g. Some devices may use it and some may not.
296 */
Joe Hershbergerf566c992015-03-24 02:41:49 -0500297 ret = eth_get_ops(dev)->write_hwaddr(dev);
Simon Glassb86f7952015-07-06 16:47:55 -0600298 if (ret == -ENOSYS)
299 ret = 0;
Joe Hershbergerf566c992015-03-24 02:41:49 -0500300 if (ret)
301 printf("\nWarning: %s failed to set MAC address\n",
302 dev->name);
303 }
304
305 return ret;
306}
307
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500308static int on_ethaddr(const char *name, const char *value, enum env_op op,
309 int flags)
310{
311 int index;
312 int retval;
313 struct udevice *dev;
314
315 /* look for an index after "eth" */
316 index = simple_strtoul(name + 3, NULL, 10);
317
318 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
319 if (!retval) {
320 struct eth_pdata *pdata = dev->platdata;
321 switch (op) {
322 case env_op_create:
323 case env_op_overwrite:
324 eth_parse_enetaddr(value, pdata->enetaddr);
325 break;
326 case env_op_delete:
327 memset(pdata->enetaddr, 0, 6);
328 }
329 }
330
331 return 0;
332}
333U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
334
Joe Hershberger05c3e682015-03-22 17:09:10 -0500335int eth_init(void)
336{
337 struct udevice *current;
338 struct udevice *old_current;
Joe Hershberger60304592015-03-22 17:09:24 -0500339 int ret = -ENODEV;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500340
341 current = eth_get_dev();
342 if (!current) {
343 printf("No ethernet found.\n");
344 return -ENODEV;
345 }
346
347 old_current = current;
348 do {
349 debug("Trying %s\n", current->name);
350
351 if (device_active(current)) {
Joe Hershberger60304592015-03-22 17:09:24 -0500352 ret = eth_get_ops(current)->start(current);
353 if (ret >= 0) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500354 struct eth_device_priv *priv =
355 current->uclass_priv;
356
357 priv->state = ETH_STATE_ACTIVE;
358 return 0;
359 }
Joe Hershbergerff819a32015-04-08 01:41:19 -0500360 } else {
Joe Hershberger60304592015-03-22 17:09:24 -0500361 ret = eth_errno;
Joe Hershbergerff819a32015-04-08 01:41:19 -0500362 }
Joe Hershberger60304592015-03-22 17:09:24 -0500363
Joe Hershberger05c3e682015-03-22 17:09:10 -0500364 debug("FAIL\n");
365
Joe Hershberger60304592015-03-22 17:09:24 -0500366 /*
367 * If ethrotate is enabled, this will change "current",
368 * otherwise we will drop out of this while loop immediately
369 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500370 eth_try_another(0);
Joe Hershberger60304592015-03-22 17:09:24 -0500371 /* This will ensure the new "current" attempted to probe */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500372 current = eth_get_dev();
373 } while (old_current != current);
374
Joe Hershberger60304592015-03-22 17:09:24 -0500375 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500376}
377
378void eth_halt(void)
379{
380 struct udevice *current;
381 struct eth_device_priv *priv;
382
383 current = eth_get_dev();
384 if (!current || !device_active(current))
385 return;
386
387 eth_get_ops(current)->stop(current);
388 priv = current->uclass_priv;
389 priv->state = ETH_STATE_PASSIVE;
390}
391
Bernhard Nortmanneaa8a192015-09-14 15:29:43 +0200392int eth_is_active(struct udevice *dev)
393{
394 struct eth_device_priv *priv;
395
396 if (!dev || !device_active(dev))
397 return 0;
398
399 priv = dev_get_uclass_priv(dev);
400 return priv->state == ETH_STATE_ACTIVE;
401}
402
Joe Hershberger05c3e682015-03-22 17:09:10 -0500403int eth_send(void *packet, int length)
404{
405 struct udevice *current;
Joe Hershberger60304592015-03-22 17:09:24 -0500406 int ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500407
408 current = eth_get_dev();
409 if (!current)
410 return -ENODEV;
411
412 if (!device_active(current))
413 return -EINVAL;
414
Joe Hershberger60304592015-03-22 17:09:24 -0500415 ret = eth_get_ops(current)->send(current, packet, length);
416 if (ret < 0) {
417 /* We cannot completely return the error at present */
418 debug("%s: send() returned error %d\n", __func__, ret);
419 }
420 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500421}
422
423int eth_rx(void)
424{
425 struct udevice *current;
Joe Hershberger17591402015-03-22 17:09:12 -0500426 uchar *packet;
Simon Glassa1ca92e2015-07-06 16:47:49 -0600427 int flags;
Joe Hershberger17591402015-03-22 17:09:12 -0500428 int ret;
429 int i;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500430
431 current = eth_get_dev();
432 if (!current)
433 return -ENODEV;
434
435 if (!device_active(current))
436 return -EINVAL;
437
Joe Hershberger17591402015-03-22 17:09:12 -0500438 /* Process up to 32 packets at one time */
Simon Glassa1ca92e2015-07-06 16:47:49 -0600439 flags = ETH_RECV_CHECK_DEVICE;
Joe Hershberger17591402015-03-22 17:09:12 -0500440 for (i = 0; i < 32; i++) {
Simon Glassa1ca92e2015-07-06 16:47:49 -0600441 ret = eth_get_ops(current)->recv(current, flags, &packet);
442 flags = 0;
Joe Hershberger17591402015-03-22 17:09:12 -0500443 if (ret > 0)
444 net_process_received_packet(packet, ret);
Joe Hershberger63c97292015-04-03 20:09:46 -0500445 if (ret >= 0 && eth_get_ops(current)->free_pkt)
446 eth_get_ops(current)->free_pkt(current, packet, ret);
447 if (ret <= 0)
Joe Hershberger17591402015-03-22 17:09:12 -0500448 break;
449 }
450 if (ret == -EAGAIN)
451 ret = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500452 if (ret < 0) {
453 /* We cannot completely return the error at present */
454 debug("%s: recv() returned error %d\n", __func__, ret);
455 }
Joe Hershberger17591402015-03-22 17:09:12 -0500456 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500457}
458
Joe Hershberger05c3e682015-03-22 17:09:10 -0500459int eth_initialize(void)
460{
461 int num_devices = 0;
462 struct udevice *dev;
463
Simon Glass3bc42702015-04-05 16:07:37 -0600464 eth_common_init();
Joe Hershberger05c3e682015-03-22 17:09:10 -0500465
466 /*
467 * Devices need to write the hwaddr even if not started so that Linux
468 * will have access to the hwaddr that u-boot stored for the device.
469 * This is accomplished by attempting to probe each device and calling
470 * their write_hwaddr() operation.
471 */
472 uclass_first_device(UCLASS_ETH, &dev);
473 if (!dev) {
474 printf("No ethernet found.\n");
475 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
476 } else {
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500477 char *ethprime = getenv("ethprime");
478 struct udevice *prime_dev = NULL;
479
480 if (ethprime)
481 prime_dev = eth_get_dev_by_name(ethprime);
482 if (prime_dev) {
483 eth_set_dev(prime_dev);
484 eth_current_changed();
485 } else {
486 eth_set_dev(NULL);
487 }
488
Joe Hershberger05c3e682015-03-22 17:09:10 -0500489 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
490 do {
491 if (num_devices)
492 printf(", ");
493
494 printf("eth%d: %s", dev->seq, dev->name);
495
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500496 if (ethprime && dev == prime_dev)
497 printf(" [PRIME]");
498
Joe Hershberger05c3e682015-03-22 17:09:10 -0500499 eth_write_hwaddr(dev);
500
501 uclass_next_device(&dev);
502 num_devices++;
503 } while (dev);
504
505 putc('\n');
506 }
507
508 return num_devices;
509}
510
511static int eth_post_bind(struct udevice *dev)
512{
513 if (strchr(dev->name, ' ')) {
514 printf("\nError: eth device name \"%s\" has a space!\n",
515 dev->name);
516 return -EINVAL;
517 }
518
519 return 0;
520}
521
522static int eth_pre_unbind(struct udevice *dev)
523{
524 /* Don't hang onto a pointer that is going away */
525 if (dev == eth_get_uclass_priv()->current)
526 eth_set_dev(NULL);
527
528 return 0;
529}
530
531static int eth_post_probe(struct udevice *dev)
532{
533 struct eth_device_priv *priv = dev->uclass_priv;
534 struct eth_pdata *pdata = dev->platdata;
535 unsigned char env_enetaddr[6];
536
537 priv->state = ETH_STATE_INIT;
538
539 /* Check if the device has a MAC address in ROM */
540 if (eth_get_ops(dev)->read_rom_hwaddr)
541 eth_get_ops(dev)->read_rom_hwaddr(dev);
542
543 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500544 if (!is_zero_ethaddr(env_enetaddr)) {
545 if (!is_zero_ethaddr(pdata->enetaddr) &&
Joe Hershberger05c3e682015-03-22 17:09:10 -0500546 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
547 printf("\nWarning: %s MAC addresses don't match:\n",
548 dev->name);
549 printf("Address in SROM is %pM\n",
550 pdata->enetaddr);
551 printf("Address in environment is %pM\n",
552 env_enetaddr);
553 }
554
555 /* Override the ROM MAC address */
556 memcpy(pdata->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500557 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500558 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
559 printf("\nWarning: %s using MAC address from ROM\n",
560 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500561 } else if (is_zero_ethaddr(pdata->enetaddr)) {
Joe Hershbergerbef10142015-05-04 14:55:13 -0500562#ifdef CONFIG_NET_RANDOM_ETHADDR
563 net_random_ethaddr(pdata->enetaddr);
564 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
565 dev->name, dev->seq, pdata->enetaddr);
566#else
Joe Hershberger05c3e682015-03-22 17:09:10 -0500567 printf("\nError: %s address not set.\n",
568 dev->name);
569 return -EINVAL;
Joe Hershbergerbef10142015-05-04 14:55:13 -0500570#endif
Joe Hershberger05c3e682015-03-22 17:09:10 -0500571 }
572
573 return 0;
574}
575
576static int eth_pre_remove(struct udevice *dev)
577{
578 eth_get_ops(dev)->stop(dev);
579
580 return 0;
581}
582
583UCLASS_DRIVER(eth) = {
584 .name = "eth",
585 .id = UCLASS_ETH,
586 .post_bind = eth_post_bind,
587 .pre_unbind = eth_pre_unbind,
588 .post_probe = eth_post_probe,
589 .pre_remove = eth_pre_remove,
590 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
591 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershbergere58780d2015-03-22 17:09:16 -0500592 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500593};
Bernhard Nortmanneaa8a192015-09-14 15:29:43 +0200594#endif /* #ifdef CONFIG_DM_ETH */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500595
596#ifndef CONFIG_DM_ETH
Ben Warrendd354792008-06-23 22:57:27 -0700597
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100598#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100599static struct {
600 uchar data[PKTSIZE];
601 int length;
602} eth_rcv_bufs[PKTBUFSRX];
603
Joe Hershberger66c73852012-05-15 08:59:07 +0000604static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100605#endif
606
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000607static struct eth_device *eth_devices;
608struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000609
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500610static void eth_set_current_to_next(void)
611{
612 eth_current = eth_current->next;
613}
614
Joe Hershbergere58780d2015-03-22 17:09:16 -0500615static void eth_set_dev(struct eth_device *dev)
616{
617 eth_current = dev;
618}
619
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700620struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200621{
622 struct eth_device *dev, *target_dev;
623
Helmut Raiger7e7f9032011-08-22 00:17:17 +0000624 BUG_ON(devname == NULL);
625
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200626 if (!eth_devices)
627 return NULL;
628
629 dev = eth_devices;
630 target_dev = NULL;
631 do {
632 if (strcmp(devname, dev->name) == 0) {
633 target_dev = dev;
634 break;
635 }
636 dev = dev->next;
637 } while (dev != eth_devices);
638
639 return target_dev;
640}
641
Andy Fleming9e569862009-02-11 15:07:24 -0600642struct eth_device *eth_get_dev_by_index(int index)
643{
644 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -0600645
646 if (!eth_devices)
647 return NULL;
648
649 dev = eth_devices;
650 target_dev = NULL;
651 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000652 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -0600653 target_dev = dev;
654 break;
655 }
656 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -0600657 } while (dev != eth_devices);
658
659 return target_dev;
660}
661
Joe Hershberger66c73852012-05-15 08:59:07 +0000662int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000663{
Joe Hershberger66c73852012-05-15 08:59:07 +0000664 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +0000665 return -1;
wdenkc6097192002-11-03 00:24:07 +0000666
Michael Wallefea7dca2011-10-27 11:31:35 +0000667 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000668}
669
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500670static int on_ethaddr(const char *name, const char *value, enum env_op op,
671 int flags)
672{
673 int index;
674 struct eth_device *dev;
675
676 if (!eth_devices)
677 return 0;
678
679 /* look for an index after "eth" */
680 index = simple_strtoul(name + 3, NULL, 10);
681
682 dev = eth_devices;
683 do {
684 if (dev->index == index) {
685 switch (op) {
686 case env_op_create:
687 case env_op_overwrite:
688 eth_parse_enetaddr(value, dev->enetaddr);
689 break;
690 case env_op_delete:
691 memset(dev->enetaddr, 0, 6);
692 }
693 }
Gong Qianyu7aba0f22015-08-31 11:34:43 +0800694 dev = dev->next;
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500695 } while (dev != eth_devices);
696
697 return 0;
698}
699U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
700
Simon Glass7616e782011-06-13 16:13:10 -0700701int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
702 int eth_number)
703{
704 unsigned char env_enetaddr[6];
705 int ret = 0;
706
Eric Miao69376642012-01-18 22:56:33 +0000707 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700708
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500709 if (!is_zero_ethaddr(env_enetaddr)) {
710 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500711 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass7616e782011-06-13 16:13:10 -0700712 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500713 dev->name);
Simon Glass7616e782011-06-13 16:13:10 -0700714 printf("Address in SROM is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500715 dev->enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700716 printf("Address in environment is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500717 env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700718 }
719
720 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500721 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc88ef3c2012-04-14 18:06:49 +0000722 eth_setenv_enetaddr_by_index(base_name, eth_number,
723 dev->enetaddr);
724 printf("\nWarning: %s using MAC address from net device\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500725 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500726 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershbergerbef10142015-05-04 14:55:13 -0500727#ifdef CONFIG_NET_RANDOM_ETHADDR
728 net_random_ethaddr(dev->enetaddr);
729 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
730 dev->name, eth_number, dev->enetaddr);
731#else
Pavel Machek75d9a452014-07-13 10:27:02 +0200732 printf("\nError: %s address not set.\n",
733 dev->name);
734 return -EINVAL;
Joe Hershbergerbef10142015-05-04 14:55:13 -0500735#endif
Simon Glass7616e782011-06-13 16:13:10 -0700736 }
737
Pavel Machek75d9a452014-07-13 10:27:02 +0200738 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500739 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200740 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500741 dev->name, dev->enetaddr);
Pavel Machek75d9a452014-07-13 10:27:02 +0200742 return -EINVAL;
743 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000744
Simon Glass7616e782011-06-13 16:13:10 -0700745 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200746 if (ret)
Joe Hershbergerff819a32015-04-08 01:41:19 -0500747 printf("\nWarning: %s failed to set MAC address\n",
748 dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000749 }
Simon Glass7616e782011-06-13 16:13:10 -0700750
751 return ret;
752}
753
Simon Glass89d48362011-02-16 11:14:33 -0800754int eth_register(struct eth_device *dev)
755{
756 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000757 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000758
Mike Frysingerf6add132011-11-10 14:11:04 +0000759 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000760
Simon Glass89d48362011-02-16 11:14:33 -0800761 if (!eth_devices) {
Joe Hershbergerff819a32015-04-08 01:41:19 -0500762 eth_devices = dev;
763 eth_current = dev;
Simon Glass89d48362011-02-16 11:14:33 -0800764 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000765 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000766 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200767 ;
wdenkc6097192002-11-03 00:24:07 +0000768 d->next = dev;
769 }
770
771 dev->state = ETH_STATE_INIT;
772 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000773 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000774
775 return 0;
776}
777
Vincent Palatine7e982d2012-01-09 08:32:36 +0000778int eth_unregister(struct eth_device *dev)
779{
780 struct eth_device *cur;
781
782 /* No device */
783 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500784 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000785
786 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
787 cur = cur->next)
788 ;
789
790 /* Device not found */
791 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500792 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000793
794 cur->next = dev->next;
795
796 if (eth_devices == dev)
797 eth_devices = dev->next == eth_devices ? NULL : dev->next;
798
799 if (eth_current == dev) {
800 eth_current = eth_devices;
801 eth_current_changed();
802 }
803
804 return 0;
805}
806
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500807int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000808{
Michael Wallefea7dca2011-10-27 11:31:35 +0000809 int num_devices = 0;
Simon Glass3bc42702015-04-05 16:07:37 -0600810
wdenkc6097192002-11-03 00:24:07 +0000811 eth_devices = NULL;
812 eth_current = NULL;
Simon Glass3bc42702015-04-05 16:07:37 -0600813 eth_common_init();
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100814
wdenkc6097192002-11-03 00:24:07 +0000815 if (!eth_devices) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000816 puts("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000817 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000818 } else {
819 struct eth_device *dev = eth_devices;
Joe Hershberger66c73852012-05-15 08:59:07 +0000820 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000821
Simon Glass770605e2012-02-13 13:51:18 +0000822 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000823 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000824 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000825 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000826
827 printf("%s", dev->name);
828
Joe Hershberger66c73852012-05-15 08:59:07 +0000829 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000830 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000831 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000832 }
833
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400834 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000835 puts("\nWarning: eth device name has a space!"
836 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400837
Pavel Machek75d9a452014-07-13 10:27:02 +0200838 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000839
wdenkc6097192002-11-03 00:24:07 +0000840 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000841 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000842 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000843
Simon Glass89d48362011-02-16 11:14:33 -0800844 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000845 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000846 }
847
Michael Wallefea7dca2011-10-27 11:31:35 +0000848 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000849}
850
David Updegraff53a5c422007-06-11 10:41:07 -0500851#ifdef CONFIG_MCAST_TFTP
852/* Multicast.
853 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200854 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500855 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500856int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500857{
Joe Hershberger66c73852012-05-15 08:59:07 +0000858 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200859 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500860 return -1;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500861 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
862 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
863 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff53a5c422007-06-11 10:41:07 -0500864 mcast_mac[2] = 0x5e;
865 mcast_mac[1] = 0x0;
866 mcast_mac[0] = 0x1;
867 return eth_current->mcast(eth_current, mcast_mac, join);
868}
869
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200870/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
871 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500872 * some other adapter -- hash tables
873 */
874#define CRCPOLY_LE 0xedb88320
Joe Hershberger66c73852012-05-15 08:59:07 +0000875u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff53a5c422007-06-11 10:41:07 -0500876{
877 int i;
878 u32 crc;
879 crc = ~0;
880 while (len--) {
881 crc ^= *p++;
882 for (i = 0; i < 8; i++)
883 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
884 }
885 /* an reverse the bits, cuz of way they arrive -- last-first */
886 crc = (crc >> 16) | (crc << 16);
887 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
888 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
889 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
890 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
891 return crc;
892}
893
894#endif
895
wdenkc6097192002-11-03 00:24:07 +0000896
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500897int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000898{
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500899 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000900
Stefan Roese6bc11382008-03-04 17:40:41 +0100901 if (!eth_current) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000902 puts("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500903 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100904 }
wdenkc6097192002-11-03 00:24:07 +0000905
906 old_current = eth_current;
907 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400908 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000909
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500910 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000911 eth_current->state = ETH_STATE_ACTIVE;
912
Upakul Barkakaty505be872007-11-29 12:16:13 +0530913 return 0;
wdenkc6097192002-11-03 00:24:07 +0000914 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400915 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000916
917 eth_try_another(0);
918 } while (old_current != eth_current);
919
Joe Hershberger05324a42015-03-22 17:09:04 -0500920 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000921}
922
923void eth_halt(void)
924{
925 if (!eth_current)
926 return;
927
928 eth_current->halt(eth_current);
929
930 eth_current->state = ETH_STATE_PASSIVE;
931}
932
Bernhard Nortmanneaa8a192015-09-14 15:29:43 +0200933int eth_is_active(struct eth_device *dev)
934{
935 return dev && dev->state == ETH_STATE_ACTIVE;
936}
937
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000938int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000939{
940 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500941 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000942
943 return eth_current->send(eth_current, packet, length);
944}
945
946int eth_rx(void)
947{
948 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500949 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000950
951 return eth_current->recv(eth_current);
952}
Joe Hershberger05c3e682015-03-22 17:09:10 -0500953#endif /* ifndef CONFIG_DM_ETH */
wdenkc6097192002-11-03 00:24:07 +0000954
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100955#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000956static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100957{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000958 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100959 int i;
960
961 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
962 return;
963
964 if (PKTSIZE < length)
965 return;
966
967 for (i = 0; i < length; i++)
968 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
969
970 eth_rcv_bufs[eth_rcv_last].length = length;
971 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
972}
973
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000974int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100975{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000976 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100977 void *pp = push_packet;
978 int i;
979
980 if (eth_rcv_current == eth_rcv_last) {
981 push_packet = eth_save_packet;
982 eth_rx();
983 push_packet = pp;
984
985 if (eth_rcv_current == eth_rcv_last)
986 return -1;
987 }
988
Michael Walle46c07bc2012-06-22 11:24:28 +0000989 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100990
991 for (i = 0; i < length; i++)
992 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
993
994 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
995 return length;
996}
997#endif /* CONFIG_API */
998
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500999static void eth_current_changed(void)
1000{
1001 char *act = getenv("ethact");
1002 /* update current ethernet name */
1003 if (eth_get_dev()) {
1004 if (act == NULL || strcmp(act, eth_get_name()) != 0)
1005 setenv("ethact", eth_get_name());
1006 }
1007 /*
1008 * remove the variable completely if there is no active
1009 * interface
1010 */
1011 else if (act != NULL)
1012 setenv("ethact", NULL);
1013}
1014
wdenkc6097192002-11-03 00:24:07 +00001015void eth_try_another(int first_restart)
1016{
Joe Hershberger05c3e682015-03-22 17:09:10 -05001017 static void *first_failed;
Remy Bohmerc650e1b2011-02-19 20:15:14 +01001018 char *ethrotate;
Matthias Fuchse1692572008-01-17 07:45:05 +01001019
1020 /*
1021 * Do not rotate between network interfaces when
1022 * 'ethrotate' variable is set to 'no'.
1023 */
Joe Hershberger66c73852012-05-15 08:59:07 +00001024 ethrotate = getenv("ethrotate");
1025 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchse1692572008-01-17 07:45:05 +01001026 return;
wdenkc6097192002-11-03 00:24:07 +00001027
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001028 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +00001029 return;
1030
Joe Hershberger66c73852012-05-15 08:59:07 +00001031 if (first_restart)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001032 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +00001033
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001034 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +00001035
Simon Glass89d48362011-02-16 11:14:33 -08001036 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +00001037
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001038 if (first_failed == eth_get_dev())
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001039 net_restart_wrap = 1;
wdenkc6097192002-11-03 00:24:07 +00001040}
1041
wdenka3d991b2004-04-15 21:48:45 +00001042void eth_set_current(void)
1043{
Joe Hershberger66c73852012-05-15 08:59:07 +00001044 static char *act;
1045 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +01001046 int env_id;
wdenka3d991b2004-04-15 21:48:45 +00001047
Heiko Schocher2f70c492009-02-10 09:38:52 +01001048 env_id = get_env_id();
1049 if ((act == NULL) || (env_changed_id != env_id)) {
1050 act = getenv("ethact");
1051 env_changed_id = env_id;
1052 }
Joe Hershberger6536b9b2015-03-22 17:09:17 -05001053
1054 if (act == NULL) {
1055 char *ethprime = getenv("ethprime");
1056 void *dev = NULL;
1057
1058 if (ethprime)
1059 dev = eth_get_dev_by_name(ethprime);
1060 if (dev)
1061 eth_set_dev(dev);
1062 else
1063 eth_set_dev(NULL);
1064 } else {
Joe Hershbergere58780d2015-03-22 17:09:16 -05001065 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershberger6536b9b2015-03-22 17:09:17 -05001066 }
wdenka3d991b2004-04-15 21:48:45 +00001067
Simon Glass89d48362011-02-16 11:14:33 -08001068 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +00001069}
wdenka3d991b2004-04-15 21:48:45 +00001070
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001071const char *eth_get_name(void)
wdenk5bb226e2003-11-17 21:14:37 +00001072{
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001073 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenk5bb226e2003-11-17 21:14:37 +00001074}