blob: 26520d303885ea1e77e396b7f175d13f0299417b [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Joe Hershbergerc7eceaf2015-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 Hershbergerc7eceaf2015-03-22 17:09:10 -05004 * Joe Hershberger, National Instruments
wdenkc6097192002-11-03 00:24:07 +00005 *
Wolfgang Denkd79de1d2013-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 Hershbergerc7eceaf2015-03-22 17:09:10 -050011#include <dm.h>
Joe Hershberger7e0b0152015-05-20 14:27:26 -050012#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <net.h>
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +010014#include <miiphy.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050015#include <phy.h>
Pavel Machek222981f2014-07-13 10:27:02 +020016#include <asm/errno.h>
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -050017#include <dm/device-internal.h>
Joe Hershberger7e0b0152015-05-20 14:27:26 -050018#include <dm/uclass-internal.h>
wdenkc6097192002-11-03 00:24:07 +000019
Joe Hershberger3dbe17e2015-03-22 17:09:06 -050020DECLARE_GLOBAL_DATA_PTR;
21
Mike Frysingerc34319c2009-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
34int eth_getenv_enetaddr(char *name, uchar *enetaddr)
35{
36 eth_parse_enetaddr(getenv(name), enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050037 return is_valid_ethaddr(enetaddr);
Mike Frysingerc34319c2009-01-29 19:43:44 -050038}
39
40int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
41{
42 char buf[20];
43
44 sprintf(buf, "%pM", enetaddr);
45
46 return setenv(name, buf);
47}
Mike Frysingere129d562009-07-15 21:31:28 -040048
Simon Glass62b36c92011-06-13 16:13:10 -070049int eth_getenv_enetaddr_by_index(const char *base_name, int index,
50 uchar *enetaddr)
Mike Frysingere129d562009-07-15 21:31:28 -040051{
52 char enetvar[32];
Simon Glass62b36c92011-06-13 16:13:10 -070053 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Mike Frysingere129d562009-07-15 21:31:28 -040054 return eth_getenv_enetaddr(enetvar, enetaddr);
55}
Mike Frysingerc34319c2009-01-29 19:43:44 -050056
Joe Hershberger6c6fe5f2012-07-10 16:23:22 -050057static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
Rob Herringc2938c02012-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 Warren6db991a2010-04-26 11:11:46 -070065static int eth_mac_skip(int index)
66{
67 char enetvar[15];
68 char *skip_state;
Joe Hershbergerc10c0b12015-04-08 01:41:19 -050069
Ben Warren6db991a2010-04-26 11:11:46 -070070 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
Joe Hershbergerc10c0b12015-04-08 01:41:19 -050071 skip_state = getenv(enetvar);
72 return skip_state != NULL;
Ben Warren6db991a2010-04-26 11:11:46 -070073}
74
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -050075static void eth_current_changed(void);
76
Simon Glassb6121f22015-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 Glassb6121f22015-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 Meng124e1382015-08-27 22:25:52 -0700110#ifndef CONFIG_DM_ETH
Simon Glassb6121f22015-04-05 16:07:37 -0600111 printf("Net Initialization Skipped\n");
Bin Meng124e1382015-08-27 22:25:52 -0700112#endif
Simon Glassb6121f22015-04-05 16:07:37 -0600113 }
114}
115
Joe Hershbergerc7eceaf2015-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 Hershbergerf4dc96a2015-03-22 17:09:24 -0500135/* eth_errno - This stores the most recent failure code from DM functions */
136static int eth_errno;
137
Joe Hershbergerc7eceaf2015-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 Hershbergerf4dc96a2015-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 Hershbergerc7eceaf2015-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 Hershbergerf4dc96a2015-03-22 17:09:24 -0500170 eth_errno = uclass_first_device(UCLASS_ETH,
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500171 &uc_priv->current);
172 return uc_priv->current;
173}
174
Joe Hershbergerf4dc96a2015-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 Hershbergerc7eceaf2015-03-22 17:09:10 -0500180static void eth_set_dev(struct udevice *dev)
181{
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500182 if (dev && !device_active(dev))
183 eth_errno = device_probe(dev);
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500184 eth_get_uclass_priv()->current = dev;
185}
186
Joe Hershberger279d2f62015-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 Meng9ac7bbf2015-08-27 22:25:54 -0700198 int len = strlen("eth");
Joe Hershberger279d2f62015-03-22 17:09:16 -0500199
200 /* Must be longer than 3 to be an alias */
Bin Meng9ac7bbf2015-08-27 22:25:54 -0700201 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
202 startp = devname + len;
Joe Hershberger279d2f62015-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 Hershbergerf4dc96a2015-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 Hershberger279d2f62015-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 Hershbergerc7eceaf2015-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 Hershbergere84319a2015-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 Glassc91b2b72015-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 Hershbergere84319a2015-03-24 02:41:49 -0500297 ret = eth_get_ops(dev)->write_hwaddr(dev);
Simon Glassc91b2b72015-07-06 16:47:55 -0600298 if (ret == -ENOSYS)
299 ret = 0;
Joe Hershbergere84319a2015-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 Hershberger7e0b0152015-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 Hershbergerc7eceaf2015-03-22 17:09:10 -0500335int eth_init(void)
336{
337 struct udevice *current;
338 struct udevice *old_current;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500339 int ret = -ENODEV;
Joe Hershbergerc7eceaf2015-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 Hershbergerf4dc96a2015-03-22 17:09:24 -0500352 ret = eth_get_ops(current)->start(current);
353 if (ret >= 0) {
Joe Hershbergerc7eceaf2015-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 Hershbergerc10c0b12015-04-08 01:41:19 -0500360 } else {
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500361 ret = eth_errno;
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500362 }
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500363
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500364 debug("FAIL\n");
365
Joe Hershbergerf4dc96a2015-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 Hershbergerc7eceaf2015-03-22 17:09:10 -0500370 eth_try_another(0);
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500371 /* This will ensure the new "current" attempted to probe */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500372 current = eth_get_dev();
373 } while (old_current != current);
374
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500375 return ret;
Joe Hershbergerc7eceaf2015-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
392int eth_send(void *packet, int length)
393{
394 struct udevice *current;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500395 int ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500396
397 current = eth_get_dev();
398 if (!current)
399 return -ENODEV;
400
401 if (!device_active(current))
402 return -EINVAL;
403
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500404 ret = eth_get_ops(current)->send(current, packet, length);
405 if (ret < 0) {
406 /* We cannot completely return the error at present */
407 debug("%s: send() returned error %d\n", __func__, ret);
408 }
409 return ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500410}
411
412int eth_rx(void)
413{
414 struct udevice *current;
Joe Hershberger8207c542015-03-22 17:09:12 -0500415 uchar *packet;
Simon Glassdc6eda32015-07-06 16:47:49 -0600416 int flags;
Joe Hershberger8207c542015-03-22 17:09:12 -0500417 int ret;
418 int i;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500419
420 current = eth_get_dev();
421 if (!current)
422 return -ENODEV;
423
424 if (!device_active(current))
425 return -EINVAL;
426
Joe Hershberger8207c542015-03-22 17:09:12 -0500427 /* Process up to 32 packets at one time */
Simon Glassdc6eda32015-07-06 16:47:49 -0600428 flags = ETH_RECV_CHECK_DEVICE;
Joe Hershberger8207c542015-03-22 17:09:12 -0500429 for (i = 0; i < 32; i++) {
Simon Glassdc6eda32015-07-06 16:47:49 -0600430 ret = eth_get_ops(current)->recv(current, flags, &packet);
431 flags = 0;
Joe Hershberger8207c542015-03-22 17:09:12 -0500432 if (ret > 0)
433 net_process_received_packet(packet, ret);
Joe Hershbergera892dc12015-04-03 20:09:46 -0500434 if (ret >= 0 && eth_get_ops(current)->free_pkt)
435 eth_get_ops(current)->free_pkt(current, packet, ret);
436 if (ret <= 0)
Joe Hershberger8207c542015-03-22 17:09:12 -0500437 break;
438 }
439 if (ret == -EAGAIN)
440 ret = 0;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500441 if (ret < 0) {
442 /* We cannot completely return the error at present */
443 debug("%s: recv() returned error %d\n", __func__, ret);
444 }
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500445 return ret;
446}
447
448int eth_initialize(void)
449{
450 int num_devices = 0;
451 struct udevice *dev;
452
Simon Glassb6121f22015-04-05 16:07:37 -0600453 eth_common_init();
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500454
455 /*
456 * Devices need to write the hwaddr even if not started so that Linux
457 * will have access to the hwaddr that u-boot stored for the device.
458 * This is accomplished by attempting to probe each device and calling
459 * their write_hwaddr() operation.
460 */
461 uclass_first_device(UCLASS_ETH, &dev);
462 if (!dev) {
463 printf("No ethernet found.\n");
464 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
465 } else {
Joe Hershbergeraa52edd2015-03-22 17:09:17 -0500466 char *ethprime = getenv("ethprime");
467 struct udevice *prime_dev = NULL;
468
469 if (ethprime)
470 prime_dev = eth_get_dev_by_name(ethprime);
471 if (prime_dev) {
472 eth_set_dev(prime_dev);
473 eth_current_changed();
474 } else {
475 eth_set_dev(NULL);
476 }
477
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500478 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
479 do {
480 if (num_devices)
481 printf(", ");
482
483 printf("eth%d: %s", dev->seq, dev->name);
484
Joe Hershbergeraa52edd2015-03-22 17:09:17 -0500485 if (ethprime && dev == prime_dev)
486 printf(" [PRIME]");
487
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500488 eth_write_hwaddr(dev);
489
490 uclass_next_device(&dev);
491 num_devices++;
492 } while (dev);
493
494 putc('\n');
495 }
496
497 return num_devices;
498}
499
500static int eth_post_bind(struct udevice *dev)
501{
502 if (strchr(dev->name, ' ')) {
503 printf("\nError: eth device name \"%s\" has a space!\n",
504 dev->name);
505 return -EINVAL;
506 }
507
508 return 0;
509}
510
511static int eth_pre_unbind(struct udevice *dev)
512{
513 /* Don't hang onto a pointer that is going away */
514 if (dev == eth_get_uclass_priv()->current)
515 eth_set_dev(NULL);
516
517 return 0;
518}
519
520static int eth_post_probe(struct udevice *dev)
521{
522 struct eth_device_priv *priv = dev->uclass_priv;
523 struct eth_pdata *pdata = dev->platdata;
524 unsigned char env_enetaddr[6];
525
526 priv->state = ETH_STATE_INIT;
527
528 /* Check if the device has a MAC address in ROM */
529 if (eth_get_ops(dev)->read_rom_hwaddr)
530 eth_get_ops(dev)->read_rom_hwaddr(dev);
531
532 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500533 if (!is_zero_ethaddr(env_enetaddr)) {
534 if (!is_zero_ethaddr(pdata->enetaddr) &&
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500535 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
536 printf("\nWarning: %s MAC addresses don't match:\n",
537 dev->name);
538 printf("Address in SROM is %pM\n",
539 pdata->enetaddr);
540 printf("Address in environment is %pM\n",
541 env_enetaddr);
542 }
543
544 /* Override the ROM MAC address */
545 memcpy(pdata->enetaddr, env_enetaddr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500546 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500547 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
548 printf("\nWarning: %s using MAC address from ROM\n",
549 dev->name);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500550 } else if (is_zero_ethaddr(pdata->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500551#ifdef CONFIG_NET_RANDOM_ETHADDR
552 net_random_ethaddr(pdata->enetaddr);
553 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
554 dev->name, dev->seq, pdata->enetaddr);
555#else
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500556 printf("\nError: %s address not set.\n",
557 dev->name);
558 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500559#endif
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500560 }
561
562 return 0;
563}
564
565static int eth_pre_remove(struct udevice *dev)
566{
567 eth_get_ops(dev)->stop(dev);
568
569 return 0;
570}
571
572UCLASS_DRIVER(eth) = {
573 .name = "eth",
574 .id = UCLASS_ETH,
575 .post_bind = eth_post_bind,
576 .pre_unbind = eth_pre_unbind,
577 .post_probe = eth_post_probe,
578 .pre_remove = eth_pre_remove,
579 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
580 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershberger279d2f62015-03-22 17:09:16 -0500581 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500582};
583#endif
584
585#ifndef CONFIG_DM_ETH
Ben Warrend448a492008-06-23 22:57:27 -0700586
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100587#ifdef CONFIG_API
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100588static struct {
589 uchar data[PKTSIZE];
590 int length;
591} eth_rcv_bufs[PKTBUFSRX];
592
Joe Hershberger5956ded2012-05-15 08:59:07 +0000593static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100594#endif
595
Joe Hershberger9f374062012-08-03 10:59:08 +0000596static struct eth_device *eth_devices;
597struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000598
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -0500599static void eth_set_current_to_next(void)
600{
601 eth_current = eth_current->next;
602}
603
Joe Hershberger279d2f62015-03-22 17:09:16 -0500604static void eth_set_dev(struct eth_device *dev)
605{
606 eth_current = dev;
607}
608
Ben Warren97824d62010-07-29 12:56:11 -0700609struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200610{
611 struct eth_device *dev, *target_dev;
612
Helmut Raiger153bf3a2011-08-22 00:17:17 +0000613 BUG_ON(devname == NULL);
614
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200615 if (!eth_devices)
616 return NULL;
617
618 dev = eth_devices;
619 target_dev = NULL;
620 do {
621 if (strcmp(devname, dev->name) == 0) {
622 target_dev = dev;
623 break;
624 }
625 dev = dev->next;
626 } while (dev != eth_devices);
627
628 return target_dev;
629}
630
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600631struct eth_device *eth_get_dev_by_index(int index)
632{
633 struct eth_device *dev, *target_dev;
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600634
635 if (!eth_devices)
636 return NULL;
637
638 dev = eth_devices;
639 target_dev = NULL;
640 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000641 if (dev->index == index) {
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600642 target_dev = dev;
643 break;
644 }
645 dev = dev->next;
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600646 } while (dev != eth_devices);
647
648 return target_dev;
649}
650
Joe Hershberger5956ded2012-05-15 08:59:07 +0000651int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000652{
Joe Hershberger5956ded2012-05-15 08:59:07 +0000653 if (!eth_current)
Michael Walle391da4c2011-10-27 11:31:35 +0000654 return -1;
wdenkc6097192002-11-03 00:24:07 +0000655
Michael Walle391da4c2011-10-27 11:31:35 +0000656 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000657}
658
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500659static int on_ethaddr(const char *name, const char *value, enum env_op op,
660 int flags)
661{
662 int index;
663 struct eth_device *dev;
664
665 if (!eth_devices)
666 return 0;
667
668 /* look for an index after "eth" */
669 index = simple_strtoul(name + 3, NULL, 10);
670
671 dev = eth_devices;
672 do {
673 if (dev->index == index) {
674 switch (op) {
675 case env_op_create:
676 case env_op_overwrite:
677 eth_parse_enetaddr(value, dev->enetaddr);
678 break;
679 case env_op_delete:
680 memset(dev->enetaddr, 0, 6);
681 }
682 }
683 } while (dev != eth_devices);
684
685 return 0;
686}
687U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
688
Simon Glass62b36c92011-06-13 16:13:10 -0700689int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
690 int eth_number)
691{
692 unsigned char env_enetaddr[6];
693 int ret = 0;
694
Eric Miaoae97ef62012-01-18 22:56:33 +0000695 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700696
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500697 if (!is_zero_ethaddr(env_enetaddr)) {
698 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershbergerca044ad2015-03-22 17:09:01 -0500699 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass62b36c92011-06-13 16:13:10 -0700700 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500701 dev->name);
Simon Glass62b36c92011-06-13 16:13:10 -0700702 printf("Address in SROM is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500703 dev->enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700704 printf("Address in environment is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500705 env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700706 }
707
708 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500709 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc2938c02012-04-14 18:06:49 +0000710 eth_setenv_enetaddr_by_index(base_name, eth_number,
711 dev->enetaddr);
712 printf("\nWarning: %s using MAC address from net device\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500713 dev->name);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500714 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500715#ifdef CONFIG_NET_RANDOM_ETHADDR
716 net_random_ethaddr(dev->enetaddr);
717 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
718 dev->name, eth_number, dev->enetaddr);
719#else
Pavel Machek222981f2014-07-13 10:27:02 +0200720 printf("\nError: %s address not set.\n",
721 dev->name);
722 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500723#endif
Simon Glass62b36c92011-06-13 16:13:10 -0700724 }
725
Pavel Machek222981f2014-07-13 10:27:02 +0200726 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500727 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek222981f2014-07-13 10:27:02 +0200728 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500729 dev->name, dev->enetaddr);
Pavel Machek222981f2014-07-13 10:27:02 +0200730 return -EINVAL;
731 }
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000732
Simon Glass62b36c92011-06-13 16:13:10 -0700733 ret = dev->write_hwaddr(dev);
Pavel Machek222981f2014-07-13 10:27:02 +0200734 if (ret)
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500735 printf("\nWarning: %s failed to set MAC address\n",
736 dev->name);
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000737 }
Simon Glass62b36c92011-06-13 16:13:10 -0700738
739 return ret;
740}
741
Simon Glass1e9961d2011-02-16 11:14:33 -0800742int eth_register(struct eth_device *dev)
743{
744 struct eth_device *d;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000745 static int index;
Michal Simek5b4945b2011-08-29 23:30:13 +0000746
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000747 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek5b4945b2011-08-29 23:30:13 +0000748
Simon Glass1e9961d2011-02-16 11:14:33 -0800749 if (!eth_devices) {
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500750 eth_devices = dev;
751 eth_current = dev;
Simon Glass1e9961d2011-02-16 11:14:33 -0800752 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000753 } else {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000754 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundeld46bcd32010-03-31 17:56:08 +0200755 ;
wdenkc6097192002-11-03 00:24:07 +0000756 d->next = dev;
757 }
758
759 dev->state = ETH_STATE_INIT;
760 dev->next = eth_devices;
Michael Walle391da4c2011-10-27 11:31:35 +0000761 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000762
763 return 0;
764}
765
Vincent Palatin451be502012-01-09 08:32:36 +0000766int eth_unregister(struct eth_device *dev)
767{
768 struct eth_device *cur;
769
770 /* No device */
771 if (!eth_devices)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500772 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000773
774 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
775 cur = cur->next)
776 ;
777
778 /* Device not found */
779 if (cur->next != dev)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500780 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000781
782 cur->next = dev->next;
783
784 if (eth_devices == dev)
785 eth_devices = dev->next == eth_devices ? NULL : dev->next;
786
787 if (eth_current == dev) {
788 eth_current = eth_devices;
789 eth_current_changed();
790 }
791
792 return 0;
793}
794
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500795int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000796{
Michael Walle391da4c2011-10-27 11:31:35 +0000797 int num_devices = 0;
Simon Glassb6121f22015-04-05 16:07:37 -0600798
wdenkc6097192002-11-03 00:24:07 +0000799 eth_devices = NULL;
800 eth_current = NULL;
Simon Glassb6121f22015-04-05 16:07:37 -0600801 eth_common_init();
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100802
wdenkc6097192002-11-03 00:24:07 +0000803 if (!eth_devices) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000804 puts("No ethernet found.\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000805 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000806 } else {
807 struct eth_device *dev = eth_devices;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000808 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000809
Simon Glass0169e6b2012-02-13 13:51:18 +0000810 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000811 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000812 if (dev->index)
Joe Hershberger5956ded2012-05-15 08:59:07 +0000813 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000814
815 printf("%s", dev->name);
816
Joe Hershberger5956ded2012-05-15 08:59:07 +0000817 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000818 eth_current = dev;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000819 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000820 }
821
Mike Frysinger2843ed92010-06-09 22:10:48 -0400822 if (strchr(dev->name, ' '))
Joe Hershberger5956ded2012-05-15 08:59:07 +0000823 puts("\nWarning: eth device name has a space!"
824 "\n");
Mike Frysinger2843ed92010-06-09 22:10:48 -0400825
Pavel Machek222981f2014-07-13 10:27:02 +0200826 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000827
wdenkc6097192002-11-03 00:24:07 +0000828 dev = dev->next;
Michael Walle391da4c2011-10-27 11:31:35 +0000829 num_devices++;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000830 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000831
Simon Glass1e9961d2011-02-16 11:14:33 -0800832 eth_current_changed();
Joe Hershberger5956ded2012-05-15 08:59:07 +0000833 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000834 }
835
Michael Walle391da4c2011-10-27 11:31:35 +0000836 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000837}
838
David Updegraff7280da72007-06-11 10:41:07 -0500839#ifdef CONFIG_MCAST_TFTP
840/* Multicast.
841 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200842 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500843 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500844int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff7280da72007-06-11 10:41:07 -0500845{
Joe Hershberger5956ded2012-05-15 08:59:07 +0000846 u8 mcast_mac[6];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200847 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500848 return -1;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500849 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
850 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
851 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff7280da72007-06-11 10:41:07 -0500852 mcast_mac[2] = 0x5e;
853 mcast_mac[1] = 0x0;
854 mcast_mac[0] = 0x1;
855 return eth_current->mcast(eth_current, mcast_mac, join);
856}
857
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200858/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
859 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff7280da72007-06-11 10:41:07 -0500860 * some other adapter -- hash tables
861 */
862#define CRCPOLY_LE 0xedb88320
Joe Hershberger5956ded2012-05-15 08:59:07 +0000863u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff7280da72007-06-11 10:41:07 -0500864{
865 int i;
866 u32 crc;
867 crc = ~0;
868 while (len--) {
869 crc ^= *p++;
870 for (i = 0; i < 8; i++)
871 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
872 }
873 /* an reverse the bits, cuz of way they arrive -- last-first */
874 crc = (crc >> 16) | (crc << 16);
875 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
876 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
877 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
878 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
879 return crc;
880}
wdenkc6097192002-11-03 00:24:07 +0000881
David Updegraff7280da72007-06-11 10:41:07 -0500882#endif
883
wdenkc6097192002-11-03 00:24:07 +0000884
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500885int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000886{
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500887 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000888
Stefan Roese11da2be2008-03-04 17:40:41 +0100889 if (!eth_current) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000890 puts("No ethernet found.\n");
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500891 return -ENODEV;
Stefan Roese11da2be2008-03-04 17:40:41 +0100892 }
wdenkc6097192002-11-03 00:24:07 +0000893
894 old_current = eth_current;
895 do {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400896 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000897
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500898 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000899 eth_current->state = ETH_STATE_ACTIVE;
900
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530901 return 0;
wdenkc6097192002-11-03 00:24:07 +0000902 }
Robin Getz9e0a4d62009-07-23 03:01:03 -0400903 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000904
905 eth_try_another(0);
906 } while (old_current != eth_current);
907
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500908 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000909}
910
911void eth_halt(void)
912{
913 if (!eth_current)
914 return;
915
916 eth_current->halt(eth_current);
917
918 eth_current->state = ETH_STATE_PASSIVE;
919}
920
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000921int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000922{
923 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500924 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000925
926 return eth_current->send(eth_current, packet, length);
927}
928
929int eth_rx(void)
930{
931 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500932 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000933
934 return eth_current->recv(eth_current);
935}
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500936#endif /* ifndef CONFIG_DM_ETH */
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100937
938#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000939static void eth_save_packet(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100940{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000941 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100942 int i;
943
944 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
945 return;
946
947 if (PKTSIZE < length)
948 return;
949
950 for (i = 0; i < length; i++)
951 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
952
953 eth_rcv_bufs[eth_rcv_last].length = length;
954 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
955}
956
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000957int eth_receive(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100958{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000959 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100960 void *pp = push_packet;
961 int i;
962
963 if (eth_rcv_current == eth_rcv_last) {
964 push_packet = eth_save_packet;
965 eth_rx();
966 push_packet = pp;
967
968 if (eth_rcv_current == eth_rcv_last)
969 return -1;
970 }
971
Michael Walledf7c98a2012-06-22 11:24:28 +0000972 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100973
974 for (i = 0; i < length; i++)
975 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
976
977 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
978 return length;
979}
980#endif /* CONFIG_API */
wdenkc6097192002-11-03 00:24:07 +0000981
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -0500982static void eth_current_changed(void)
983{
984 char *act = getenv("ethact");
985 /* update current ethernet name */
986 if (eth_get_dev()) {
987 if (act == NULL || strcmp(act, eth_get_name()) != 0)
988 setenv("ethact", eth_get_name());
989 }
990 /*
991 * remove the variable completely if there is no active
992 * interface
993 */
994 else if (act != NULL)
995 setenv("ethact", NULL);
996}
997
wdenkc6097192002-11-03 00:24:07 +0000998void eth_try_another(int first_restart)
999{
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05001000 static void *first_failed;
Remy Bohmer0fce5152011-02-19 20:15:14 +01001001 char *ethrotate;
Matthias Fuchs204f0ec2008-01-17 07:45:05 +01001002
1003 /*
1004 * Do not rotate between network interfaces when
1005 * 'ethrotate' variable is set to 'no'.
1006 */
Joe Hershberger5956ded2012-05-15 08:59:07 +00001007 ethrotate = getenv("ethrotate");
1008 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchs204f0ec2008-01-17 07:45:05 +01001009 return;
wdenkc6097192002-11-03 00:24:07 +00001010
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001011 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +00001012 return;
1013
Joe Hershberger5956ded2012-05-15 08:59:07 +00001014 if (first_restart)
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001015 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +00001016
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001017 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +00001018
Simon Glass1e9961d2011-02-16 11:14:33 -08001019 eth_current_changed();
wdenk145d2c12004-04-15 21:48:45 +00001020
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001021 if (first_failed == eth_get_dev())
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001022 net_restart_wrap = 1;
wdenkc6097192002-11-03 00:24:07 +00001023}
1024
wdenk145d2c12004-04-15 21:48:45 +00001025void eth_set_current(void)
1026{
Joe Hershberger5956ded2012-05-15 08:59:07 +00001027 static char *act;
1028 static int env_changed_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +01001029 int env_id;
wdenk145d2c12004-04-15 21:48:45 +00001030
Heiko Schocher0c303fa2009-02-10 09:38:52 +01001031 env_id = get_env_id();
1032 if ((act == NULL) || (env_changed_id != env_id)) {
1033 act = getenv("ethact");
1034 env_changed_id = env_id;
1035 }
Joe Hershbergeraa52edd2015-03-22 17:09:17 -05001036
1037 if (act == NULL) {
1038 char *ethprime = getenv("ethprime");
1039 void *dev = NULL;
1040
1041 if (ethprime)
1042 dev = eth_get_dev_by_name(ethprime);
1043 if (dev)
1044 eth_set_dev(dev);
1045 else
1046 eth_set_dev(NULL);
1047 } else {
Joe Hershberger279d2f62015-03-22 17:09:16 -05001048 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershbergeraa52edd2015-03-22 17:09:17 -05001049 }
wdenk145d2c12004-04-15 21:48:45 +00001050
Simon Glass1e9961d2011-02-16 11:14:33 -08001051 eth_current_changed();
wdenk145d2c12004-04-15 21:48:45 +00001052}
wdenk145d2c12004-04-15 21:48:45 +00001053
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001054const char *eth_get_name(void)
wdenkc6abb7e2003-11-17 21:14:37 +00001055{
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001056 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenkc6abb7e2003-11-17 21:14:37 +00001057}