blob: c542f4aa3b3b1e4f2e6fbb5db4c0ea579aefe45e [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
Josh Wu1bcee662015-09-01 18:22:55 +080034int eth_getenv_enetaddr(const char *name, uchar *enetaddr)
Mike Frysingerc34319c2009-01-29 19:43:44 -050035{
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
Josh Wu1bcee662015-09-01 18:22:55 +080040int eth_setenv_enetaddr(const char *name, const uchar *enetaddr)
Mike Frysingerc34319c2009-01-29 19:43:44 -050041{
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{
Bin Mengcecd3942015-10-07 21:45:44 -0700182 if (dev && !device_active(dev)) {
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500183 eth_errno = device_probe(dev);
Bin Mengcecd3942015-10-07 21:45:44 -0700184 if (eth_errno)
185 dev = NULL;
186 }
187
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500188 eth_get_uclass_priv()->current = dev;
189}
190
Joe Hershberger279d2f62015-03-22 17:09:16 -0500191/*
192 * Find the udevice that either has the name passed in as devname or has an
193 * alias named devname.
194 */
195struct udevice *eth_get_dev_by_name(const char *devname)
196{
197 int seq = -1;
198 char *endp = NULL;
199 const char *startp = NULL;
200 struct udevice *it;
201 struct uclass *uc;
Bin Meng9ac7bbf2015-08-27 22:25:54 -0700202 int len = strlen("eth");
Joe Hershberger279d2f62015-03-22 17:09:16 -0500203
204 /* Must be longer than 3 to be an alias */
Bin Meng9ac7bbf2015-08-27 22:25:54 -0700205 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
206 startp = devname + len;
Joe Hershberger279d2f62015-03-22 17:09:16 -0500207 seq = simple_strtoul(startp, &endp, 10);
208 }
209
210 uclass_get(UCLASS_ETH, &uc);
211 uclass_foreach_dev(it, uc) {
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500212 /*
213 * We need the seq to be valid, so try to probe it.
214 * If the probe fails, the seq will not match since it will be
215 * -1 instead of what we are looking for.
216 * We don't care about errors from probe here. Either they won't
217 * match an alias or it will match a literal name and we'll pick
218 * up the error when we try to probe again in eth_set_dev().
219 */
Bin Mengcecd3942015-10-07 21:45:44 -0700220 if (device_probe(it))
221 continue;
222 /* Check for the name or the sequence number to match */
Joe Hershberger279d2f62015-03-22 17:09:16 -0500223 if (strcmp(it->name, devname) == 0 ||
224 (endp > startp && it->seq == seq))
225 return it;
226 }
227
228 return NULL;
229}
230
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500231unsigned char *eth_get_ethaddr(void)
232{
233 struct eth_pdata *pdata;
234
235 if (eth_get_dev()) {
236 pdata = eth_get_dev()->platdata;
237 return pdata->enetaddr;
238 }
239
240 return NULL;
241}
242
243/* Set active state without calling start on the driver */
244int eth_init_state_only(void)
245{
246 struct udevice *current;
247 struct eth_device_priv *priv;
248
249 current = eth_get_dev();
250 if (!current || !device_active(current))
251 return -EINVAL;
252
253 priv = current->uclass_priv;
254 priv->state = ETH_STATE_ACTIVE;
255
256 return 0;
257}
258
259/* Set passive state without calling stop on the driver */
260void eth_halt_state_only(void)
261{
262 struct udevice *current;
263 struct eth_device_priv *priv;
264
265 current = eth_get_dev();
266 if (!current || !device_active(current))
267 return;
268
269 priv = current->uclass_priv;
270 priv->state = ETH_STATE_PASSIVE;
271}
272
273int eth_get_dev_index(void)
274{
275 if (eth_get_dev())
276 return eth_get_dev()->seq;
277 return -1;
278}
279
Joe Hershbergere84319a2015-03-24 02:41:49 -0500280static int eth_write_hwaddr(struct udevice *dev)
281{
282 struct eth_pdata *pdata = dev->platdata;
283 int ret = 0;
284
285 if (!dev || !device_active(dev))
286 return -EINVAL;
287
288 /* seq is valid since the device is active */
289 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
290 if (!is_valid_ethaddr(pdata->enetaddr)) {
291 printf("\nError: %s address %pM illegal value\n",
292 dev->name, pdata->enetaddr);
293 return -EINVAL;
294 }
295
Simon Glassc91b2b72015-07-06 16:47:55 -0600296 /*
297 * Drivers are allowed to decide not to implement this at
298 * run-time. E.g. Some devices may use it and some may not.
299 */
Joe Hershbergere84319a2015-03-24 02:41:49 -0500300 ret = eth_get_ops(dev)->write_hwaddr(dev);
Simon Glassc91b2b72015-07-06 16:47:55 -0600301 if (ret == -ENOSYS)
302 ret = 0;
Joe Hershbergere84319a2015-03-24 02:41:49 -0500303 if (ret)
304 printf("\nWarning: %s failed to set MAC address\n",
305 dev->name);
306 }
307
308 return ret;
309}
310
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500311static int on_ethaddr(const char *name, const char *value, enum env_op op,
312 int flags)
313{
314 int index;
315 int retval;
316 struct udevice *dev;
317
318 /* look for an index after "eth" */
319 index = simple_strtoul(name + 3, NULL, 10);
320
321 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
322 if (!retval) {
323 struct eth_pdata *pdata = dev->platdata;
324 switch (op) {
325 case env_op_create:
326 case env_op_overwrite:
327 eth_parse_enetaddr(value, pdata->enetaddr);
328 break;
329 case env_op_delete:
330 memset(pdata->enetaddr, 0, 6);
331 }
332 }
333
334 return 0;
335}
336U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
337
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500338int eth_init(void)
339{
340 struct udevice *current;
341 struct udevice *old_current;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500342 int ret = -ENODEV;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500343
344 current = eth_get_dev();
345 if (!current) {
346 printf("No ethernet found.\n");
347 return -ENODEV;
348 }
349
350 old_current = current;
351 do {
Bin Mengcecd3942015-10-07 21:45:44 -0700352 if (current) {
353 debug("Trying %s\n", current->name);
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500354
Bin Mengcecd3942015-10-07 21:45:44 -0700355 if (device_active(current)) {
356 ret = eth_get_ops(current)->start(current);
357 if (ret >= 0) {
358 struct eth_device_priv *priv =
359 current->uclass_priv;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500360
Bin Mengcecd3942015-10-07 21:45:44 -0700361 priv->state = ETH_STATE_ACTIVE;
362 return 0;
363 }
364 } else {
365 ret = eth_errno;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500366 }
Bin Mengcecd3942015-10-07 21:45:44 -0700367
368 debug("FAIL\n");
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500369 } else {
Bin Mengcecd3942015-10-07 21:45:44 -0700370 debug("PROBE FAIL\n");
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500371 }
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500372
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500373 /*
374 * If ethrotate is enabled, this will change "current",
375 * otherwise we will drop out of this while loop immediately
376 */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500377 eth_try_another(0);
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500378 /* This will ensure the new "current" attempted to probe */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500379 current = eth_get_dev();
380 } while (old_current != current);
381
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500382 return ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500383}
384
385void eth_halt(void)
386{
387 struct udevice *current;
388 struct eth_device_priv *priv;
389
390 current = eth_get_dev();
391 if (!current || !device_active(current))
392 return;
393
394 eth_get_ops(current)->stop(current);
395 priv = current->uclass_priv;
396 priv->state = ETH_STATE_PASSIVE;
397}
398
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200399int eth_is_active(struct udevice *dev)
400{
401 struct eth_device_priv *priv;
402
403 if (!dev || !device_active(dev))
404 return 0;
405
406 priv = dev_get_uclass_priv(dev);
407 return priv->state == ETH_STATE_ACTIVE;
408}
409
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500410int eth_send(void *packet, int length)
411{
412 struct udevice *current;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500413 int ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500414
415 current = eth_get_dev();
416 if (!current)
417 return -ENODEV;
418
419 if (!device_active(current))
420 return -EINVAL;
421
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500422 ret = eth_get_ops(current)->send(current, packet, length);
423 if (ret < 0) {
424 /* We cannot completely return the error at present */
425 debug("%s: send() returned error %d\n", __func__, ret);
426 }
427 return ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500428}
429
430int eth_rx(void)
431{
432 struct udevice *current;
Joe Hershberger8207c542015-03-22 17:09:12 -0500433 uchar *packet;
Simon Glassdc6eda32015-07-06 16:47:49 -0600434 int flags;
Joe Hershberger8207c542015-03-22 17:09:12 -0500435 int ret;
436 int i;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500437
438 current = eth_get_dev();
439 if (!current)
440 return -ENODEV;
441
442 if (!device_active(current))
443 return -EINVAL;
444
Joe Hershberger8207c542015-03-22 17:09:12 -0500445 /* Process up to 32 packets at one time */
Simon Glassdc6eda32015-07-06 16:47:49 -0600446 flags = ETH_RECV_CHECK_DEVICE;
Joe Hershberger8207c542015-03-22 17:09:12 -0500447 for (i = 0; i < 32; i++) {
Simon Glassdc6eda32015-07-06 16:47:49 -0600448 ret = eth_get_ops(current)->recv(current, flags, &packet);
449 flags = 0;
Joe Hershberger8207c542015-03-22 17:09:12 -0500450 if (ret > 0)
451 net_process_received_packet(packet, ret);
Joe Hershbergera892dc12015-04-03 20:09:46 -0500452 if (ret >= 0 && eth_get_ops(current)->free_pkt)
453 eth_get_ops(current)->free_pkt(current, packet, ret);
454 if (ret <= 0)
Joe Hershberger8207c542015-03-22 17:09:12 -0500455 break;
456 }
457 if (ret == -EAGAIN)
458 ret = 0;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500459 if (ret < 0) {
460 /* We cannot completely return the error at present */
461 debug("%s: recv() returned error %d\n", __func__, ret);
462 }
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500463 return ret;
464}
465
466int eth_initialize(void)
467{
468 int num_devices = 0;
469 struct udevice *dev;
470
Simon Glassb6121f22015-04-05 16:07:37 -0600471 eth_common_init();
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500472
473 /*
474 * Devices need to write the hwaddr even if not started so that Linux
475 * will have access to the hwaddr that u-boot stored for the device.
476 * This is accomplished by attempting to probe each device and calling
477 * their write_hwaddr() operation.
478 */
479 uclass_first_device(UCLASS_ETH, &dev);
480 if (!dev) {
481 printf("No ethernet found.\n");
482 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
483 } else {
Joe Hershbergeraa52edd2015-03-22 17:09:17 -0500484 char *ethprime = getenv("ethprime");
485 struct udevice *prime_dev = NULL;
486
487 if (ethprime)
488 prime_dev = eth_get_dev_by_name(ethprime);
489 if (prime_dev) {
490 eth_set_dev(prime_dev);
491 eth_current_changed();
492 } else {
493 eth_set_dev(NULL);
494 }
495
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500496 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
497 do {
498 if (num_devices)
499 printf(", ");
500
501 printf("eth%d: %s", dev->seq, dev->name);
502
Joe Hershbergeraa52edd2015-03-22 17:09:17 -0500503 if (ethprime && dev == prime_dev)
504 printf(" [PRIME]");
505
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500506 eth_write_hwaddr(dev);
507
508 uclass_next_device(&dev);
509 num_devices++;
510 } while (dev);
511
512 putc('\n');
513 }
514
515 return num_devices;
516}
517
518static int eth_post_bind(struct udevice *dev)
519{
520 if (strchr(dev->name, ' ')) {
521 printf("\nError: eth device name \"%s\" has a space!\n",
522 dev->name);
523 return -EINVAL;
524 }
525
526 return 0;
527}
528
529static int eth_pre_unbind(struct udevice *dev)
530{
531 /* Don't hang onto a pointer that is going away */
532 if (dev == eth_get_uclass_priv()->current)
533 eth_set_dev(NULL);
534
535 return 0;
536}
537
538static int eth_post_probe(struct udevice *dev)
539{
540 struct eth_device_priv *priv = dev->uclass_priv;
541 struct eth_pdata *pdata = dev->platdata;
542 unsigned char env_enetaddr[6];
543
544 priv->state = ETH_STATE_INIT;
545
546 /* Check if the device has a MAC address in ROM */
547 if (eth_get_ops(dev)->read_rom_hwaddr)
548 eth_get_ops(dev)->read_rom_hwaddr(dev);
549
550 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500551 if (!is_zero_ethaddr(env_enetaddr)) {
552 if (!is_zero_ethaddr(pdata->enetaddr) &&
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500553 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
554 printf("\nWarning: %s MAC addresses don't match:\n",
555 dev->name);
556 printf("Address in SROM is %pM\n",
557 pdata->enetaddr);
558 printf("Address in environment is %pM\n",
559 env_enetaddr);
560 }
561
562 /* Override the ROM MAC address */
563 memcpy(pdata->enetaddr, env_enetaddr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500564 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500565 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
566 printf("\nWarning: %s using MAC address from ROM\n",
567 dev->name);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500568 } else if (is_zero_ethaddr(pdata->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500569#ifdef CONFIG_NET_RANDOM_ETHADDR
570 net_random_ethaddr(pdata->enetaddr);
571 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
572 dev->name, dev->seq, pdata->enetaddr);
573#else
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500574 printf("\nError: %s address not set.\n",
575 dev->name);
576 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500577#endif
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500578 }
579
580 return 0;
581}
582
583static int eth_pre_remove(struct udevice *dev)
584{
Bin Menge59ecbc2015-10-07 21:45:42 -0700585 struct eth_pdata *pdata = dev->platdata;
586
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500587 eth_get_ops(dev)->stop(dev);
588
Bin Menge59ecbc2015-10-07 21:45:42 -0700589 /* clear the MAC address */
590 memset(pdata->enetaddr, 0, 6);
591
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500592 return 0;
593}
594
595UCLASS_DRIVER(eth) = {
596 .name = "eth",
597 .id = UCLASS_ETH,
598 .post_bind = eth_post_bind,
599 .pre_unbind = eth_pre_unbind,
600 .post_probe = eth_post_probe,
601 .pre_remove = eth_pre_remove,
602 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
603 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershberger279d2f62015-03-22 17:09:16 -0500604 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500605};
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200606#endif /* #ifdef CONFIG_DM_ETH */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500607
608#ifndef CONFIG_DM_ETH
Ben Warrend448a492008-06-23 22:57:27 -0700609
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100610#ifdef CONFIG_API
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100611static struct {
612 uchar data[PKTSIZE];
613 int length;
614} eth_rcv_bufs[PKTBUFSRX];
615
Joe Hershberger5956ded2012-05-15 08:59:07 +0000616static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100617#endif
618
Joe Hershberger9f374062012-08-03 10:59:08 +0000619static struct eth_device *eth_devices;
620struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000621
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -0500622static void eth_set_current_to_next(void)
623{
624 eth_current = eth_current->next;
625}
626
Joe Hershberger279d2f62015-03-22 17:09:16 -0500627static void eth_set_dev(struct eth_device *dev)
628{
629 eth_current = dev;
630}
631
Ben Warren97824d62010-07-29 12:56:11 -0700632struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200633{
634 struct eth_device *dev, *target_dev;
635
Helmut Raiger153bf3a2011-08-22 00:17:17 +0000636 BUG_ON(devname == NULL);
637
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200638 if (!eth_devices)
639 return NULL;
640
641 dev = eth_devices;
642 target_dev = NULL;
643 do {
644 if (strcmp(devname, dev->name) == 0) {
645 target_dev = dev;
646 break;
647 }
648 dev = dev->next;
649 } while (dev != eth_devices);
650
651 return target_dev;
652}
653
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600654struct eth_device *eth_get_dev_by_index(int index)
655{
656 struct eth_device *dev, *target_dev;
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600657
658 if (!eth_devices)
659 return NULL;
660
661 dev = eth_devices;
662 target_dev = NULL;
663 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000664 if (dev->index == index) {
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600665 target_dev = dev;
666 break;
667 }
668 dev = dev->next;
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600669 } while (dev != eth_devices);
670
671 return target_dev;
672}
673
Joe Hershberger5956ded2012-05-15 08:59:07 +0000674int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000675{
Joe Hershberger5956ded2012-05-15 08:59:07 +0000676 if (!eth_current)
Michael Walle391da4c2011-10-27 11:31:35 +0000677 return -1;
wdenkc6097192002-11-03 00:24:07 +0000678
Michael Walle391da4c2011-10-27 11:31:35 +0000679 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000680}
681
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500682static int on_ethaddr(const char *name, const char *value, enum env_op op,
683 int flags)
684{
685 int index;
686 struct eth_device *dev;
687
688 if (!eth_devices)
689 return 0;
690
691 /* look for an index after "eth" */
692 index = simple_strtoul(name + 3, NULL, 10);
693
694 dev = eth_devices;
695 do {
696 if (dev->index == index) {
697 switch (op) {
698 case env_op_create:
699 case env_op_overwrite:
700 eth_parse_enetaddr(value, dev->enetaddr);
701 break;
702 case env_op_delete:
703 memset(dev->enetaddr, 0, 6);
704 }
705 }
Gong Qianyua4f23972015-08-31 11:34:43 +0800706 dev = dev->next;
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500707 } while (dev != eth_devices);
708
709 return 0;
710}
711U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
712
Simon Glass62b36c92011-06-13 16:13:10 -0700713int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
714 int eth_number)
715{
716 unsigned char env_enetaddr[6];
717 int ret = 0;
718
Eric Miaoae97ef62012-01-18 22:56:33 +0000719 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700720
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500721 if (!is_zero_ethaddr(env_enetaddr)) {
722 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershbergerca044ad2015-03-22 17:09:01 -0500723 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass62b36c92011-06-13 16:13:10 -0700724 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500725 dev->name);
Simon Glass62b36c92011-06-13 16:13:10 -0700726 printf("Address in SROM is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500727 dev->enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700728 printf("Address in environment is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500729 env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700730 }
731
732 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500733 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc2938c02012-04-14 18:06:49 +0000734 eth_setenv_enetaddr_by_index(base_name, eth_number,
735 dev->enetaddr);
736 printf("\nWarning: %s using MAC address from net device\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500737 dev->name);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500738 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500739#ifdef CONFIG_NET_RANDOM_ETHADDR
740 net_random_ethaddr(dev->enetaddr);
741 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
742 dev->name, eth_number, dev->enetaddr);
743#else
Pavel Machek222981f2014-07-13 10:27:02 +0200744 printf("\nError: %s address not set.\n",
745 dev->name);
746 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500747#endif
Simon Glass62b36c92011-06-13 16:13:10 -0700748 }
749
Pavel Machek222981f2014-07-13 10:27:02 +0200750 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500751 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek222981f2014-07-13 10:27:02 +0200752 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500753 dev->name, dev->enetaddr);
Pavel Machek222981f2014-07-13 10:27:02 +0200754 return -EINVAL;
755 }
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000756
Simon Glass62b36c92011-06-13 16:13:10 -0700757 ret = dev->write_hwaddr(dev);
Pavel Machek222981f2014-07-13 10:27:02 +0200758 if (ret)
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500759 printf("\nWarning: %s failed to set MAC address\n",
760 dev->name);
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000761 }
Simon Glass62b36c92011-06-13 16:13:10 -0700762
763 return ret;
764}
765
Simon Glass1e9961d2011-02-16 11:14:33 -0800766int eth_register(struct eth_device *dev)
767{
768 struct eth_device *d;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000769 static int index;
Michal Simek5b4945b2011-08-29 23:30:13 +0000770
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000771 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek5b4945b2011-08-29 23:30:13 +0000772
Simon Glass1e9961d2011-02-16 11:14:33 -0800773 if (!eth_devices) {
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500774 eth_devices = dev;
775 eth_current = dev;
Simon Glass1e9961d2011-02-16 11:14:33 -0800776 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000777 } else {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000778 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundeld46bcd32010-03-31 17:56:08 +0200779 ;
wdenkc6097192002-11-03 00:24:07 +0000780 d->next = dev;
781 }
782
783 dev->state = ETH_STATE_INIT;
784 dev->next = eth_devices;
Michael Walle391da4c2011-10-27 11:31:35 +0000785 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000786
787 return 0;
788}
789
Vincent Palatin451be502012-01-09 08:32:36 +0000790int eth_unregister(struct eth_device *dev)
791{
792 struct eth_device *cur;
793
794 /* No device */
795 if (!eth_devices)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500796 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000797
798 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
799 cur = cur->next)
800 ;
801
802 /* Device not found */
803 if (cur->next != dev)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500804 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000805
806 cur->next = dev->next;
807
808 if (eth_devices == dev)
809 eth_devices = dev->next == eth_devices ? NULL : dev->next;
810
811 if (eth_current == dev) {
812 eth_current = eth_devices;
813 eth_current_changed();
814 }
815
816 return 0;
817}
818
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500819int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000820{
Michael Walle391da4c2011-10-27 11:31:35 +0000821 int num_devices = 0;
Simon Glassb6121f22015-04-05 16:07:37 -0600822
wdenkc6097192002-11-03 00:24:07 +0000823 eth_devices = NULL;
824 eth_current = NULL;
Simon Glassb6121f22015-04-05 16:07:37 -0600825 eth_common_init();
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100826
wdenkc6097192002-11-03 00:24:07 +0000827 if (!eth_devices) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000828 puts("No ethernet found.\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000829 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000830 } else {
831 struct eth_device *dev = eth_devices;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000832 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000833
Simon Glass0169e6b2012-02-13 13:51:18 +0000834 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000835 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000836 if (dev->index)
Joe Hershberger5956ded2012-05-15 08:59:07 +0000837 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000838
839 printf("%s", dev->name);
840
Joe Hershberger5956ded2012-05-15 08:59:07 +0000841 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000842 eth_current = dev;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000843 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000844 }
845
Mike Frysinger2843ed92010-06-09 22:10:48 -0400846 if (strchr(dev->name, ' '))
Joe Hershberger5956ded2012-05-15 08:59:07 +0000847 puts("\nWarning: eth device name has a space!"
848 "\n");
Mike Frysinger2843ed92010-06-09 22:10:48 -0400849
Pavel Machek222981f2014-07-13 10:27:02 +0200850 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000851
wdenkc6097192002-11-03 00:24:07 +0000852 dev = dev->next;
Michael Walle391da4c2011-10-27 11:31:35 +0000853 num_devices++;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000854 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000855
Simon Glass1e9961d2011-02-16 11:14:33 -0800856 eth_current_changed();
Joe Hershberger5956ded2012-05-15 08:59:07 +0000857 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000858 }
859
Michael Walle391da4c2011-10-27 11:31:35 +0000860 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000861}
862
David Updegraff7280da72007-06-11 10:41:07 -0500863#ifdef CONFIG_MCAST_TFTP
864/* Multicast.
865 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200866 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500867 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500868int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff7280da72007-06-11 10:41:07 -0500869{
Joe Hershberger5956ded2012-05-15 08:59:07 +0000870 u8 mcast_mac[6];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200871 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500872 return -1;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500873 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
874 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
875 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff7280da72007-06-11 10:41:07 -0500876 mcast_mac[2] = 0x5e;
877 mcast_mac[1] = 0x0;
878 mcast_mac[0] = 0x1;
879 return eth_current->mcast(eth_current, mcast_mac, join);
880}
881
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200882/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
883 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff7280da72007-06-11 10:41:07 -0500884 * some other adapter -- hash tables
885 */
886#define CRCPOLY_LE 0xedb88320
Joe Hershberger5956ded2012-05-15 08:59:07 +0000887u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff7280da72007-06-11 10:41:07 -0500888{
889 int i;
890 u32 crc;
891 crc = ~0;
892 while (len--) {
893 crc ^= *p++;
894 for (i = 0; i < 8; i++)
895 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
896 }
897 /* an reverse the bits, cuz of way they arrive -- last-first */
898 crc = (crc >> 16) | (crc << 16);
899 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
900 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
901 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
902 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
903 return crc;
904}
wdenkc6097192002-11-03 00:24:07 +0000905
David Updegraff7280da72007-06-11 10:41:07 -0500906#endif
907
wdenkc6097192002-11-03 00:24:07 +0000908
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500909int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000910{
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500911 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000912
Stefan Roese11da2be2008-03-04 17:40:41 +0100913 if (!eth_current) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000914 puts("No ethernet found.\n");
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500915 return -ENODEV;
Stefan Roese11da2be2008-03-04 17:40:41 +0100916 }
wdenkc6097192002-11-03 00:24:07 +0000917
918 old_current = eth_current;
919 do {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400920 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000921
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500922 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000923 eth_current->state = ETH_STATE_ACTIVE;
924
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530925 return 0;
wdenkc6097192002-11-03 00:24:07 +0000926 }
Robin Getz9e0a4d62009-07-23 03:01:03 -0400927 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000928
929 eth_try_another(0);
930 } while (old_current != eth_current);
931
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500932 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000933}
934
935void eth_halt(void)
936{
937 if (!eth_current)
938 return;
939
940 eth_current->halt(eth_current);
941
942 eth_current->state = ETH_STATE_PASSIVE;
943}
944
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200945int eth_is_active(struct eth_device *dev)
946{
947 return dev && dev->state == ETH_STATE_ACTIVE;
948}
949
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000950int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000951{
952 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500953 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000954
955 return eth_current->send(eth_current, packet, length);
956}
957
958int eth_rx(void)
959{
960 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500961 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000962
963 return eth_current->recv(eth_current);
964}
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500965#endif /* ifndef CONFIG_DM_ETH */
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100966
967#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000968static void eth_save_packet(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100969{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000970 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100971 int i;
972
973 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
974 return;
975
976 if (PKTSIZE < length)
977 return;
978
979 for (i = 0; i < length; i++)
980 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
981
982 eth_rcv_bufs[eth_rcv_last].length = length;
983 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
984}
985
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000986int eth_receive(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100987{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000988 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100989 void *pp = push_packet;
990 int i;
991
992 if (eth_rcv_current == eth_rcv_last) {
993 push_packet = eth_save_packet;
994 eth_rx();
995 push_packet = pp;
996
997 if (eth_rcv_current == eth_rcv_last)
998 return -1;
999 }
1000
Michael Walledf7c98a2012-06-22 11:24:28 +00001001 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001002
1003 for (i = 0; i < length; i++)
1004 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
1005
1006 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
1007 return length;
1008}
1009#endif /* CONFIG_API */
wdenkc6097192002-11-03 00:24:07 +00001010
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001011static void eth_current_changed(void)
1012{
1013 char *act = getenv("ethact");
1014 /* update current ethernet name */
1015 if (eth_get_dev()) {
1016 if (act == NULL || strcmp(act, eth_get_name()) != 0)
1017 setenv("ethact", eth_get_name());
1018 }
1019 /*
1020 * remove the variable completely if there is no active
1021 * interface
1022 */
1023 else if (act != NULL)
1024 setenv("ethact", NULL);
1025}
1026
wdenkc6097192002-11-03 00:24:07 +00001027void eth_try_another(int first_restart)
1028{
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05001029 static void *first_failed;
Remy Bohmer0fce5152011-02-19 20:15:14 +01001030 char *ethrotate;
Matthias Fuchs204f0ec2008-01-17 07:45:05 +01001031
1032 /*
1033 * Do not rotate between network interfaces when
1034 * 'ethrotate' variable is set to 'no'.
1035 */
Joe Hershberger5956ded2012-05-15 08:59:07 +00001036 ethrotate = getenv("ethrotate");
1037 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchs204f0ec2008-01-17 07:45:05 +01001038 return;
wdenkc6097192002-11-03 00:24:07 +00001039
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001040 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +00001041 return;
1042
Joe Hershberger5956ded2012-05-15 08:59:07 +00001043 if (first_restart)
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001044 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +00001045
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001046 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +00001047
Simon Glass1e9961d2011-02-16 11:14:33 -08001048 eth_current_changed();
wdenk145d2c12004-04-15 21:48:45 +00001049
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001050 if (first_failed == eth_get_dev())
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001051 net_restart_wrap = 1;
wdenkc6097192002-11-03 00:24:07 +00001052}
1053
wdenk145d2c12004-04-15 21:48:45 +00001054void eth_set_current(void)
1055{
Joe Hershberger5956ded2012-05-15 08:59:07 +00001056 static char *act;
1057 static int env_changed_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +01001058 int env_id;
wdenk145d2c12004-04-15 21:48:45 +00001059
Heiko Schocher0c303fa2009-02-10 09:38:52 +01001060 env_id = get_env_id();
1061 if ((act == NULL) || (env_changed_id != env_id)) {
1062 act = getenv("ethact");
1063 env_changed_id = env_id;
1064 }
Joe Hershbergeraa52edd2015-03-22 17:09:17 -05001065
1066 if (act == NULL) {
1067 char *ethprime = getenv("ethprime");
1068 void *dev = NULL;
1069
1070 if (ethprime)
1071 dev = eth_get_dev_by_name(ethprime);
1072 if (dev)
1073 eth_set_dev(dev);
1074 else
1075 eth_set_dev(NULL);
1076 } else {
Joe Hershberger279d2f62015-03-22 17:09:16 -05001077 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershbergeraa52edd2015-03-22 17:09:17 -05001078 }
wdenk145d2c12004-04-15 21:48:45 +00001079
Simon Glass1e9961d2011-02-16 11:14:33 -08001080 eth_current_changed();
wdenk145d2c12004-04-15 21:48:45 +00001081}
wdenk145d2c12004-04-15 21:48:45 +00001082
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001083const char *eth_get_name(void)
wdenkc6abb7e2003-11-17 21:14:37 +00001084{
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001085 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenkc6abb7e2003-11-17 21:14:37 +00001086}