]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/eth.c
Merge branch 'master' of git://www.denx.de/git/u-boot-imx
[people/ms/u-boot.git] / net / eth.c
CommitLineData
c609719b 1/*
05c3e68f 2 * (C) Copyright 2001-2015
c609719b 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
05c3e68f 4 * Joe Hershberger, National Instruments
c609719b 5 *
1a459660 6 * SPDX-License-Identifier: GPL-2.0+
c609719b
WD
7 */
8
9#include <common.h>
10#include <command.h>
05c3e68f 11#include <dm.h>
6e0d26c0 12#include <environment.h>
c609719b 13#include <net.h>
d9785c14 14#include <miiphy.h>
5f184715 15#include <phy.h>
75d9a45c 16#include <asm/errno.h>
05c3e68f 17#include <dm/device-internal.h>
6e0d26c0 18#include <dm/uclass-internal.h>
c609719b 19
d2eaec60
JH
20DECLARE_GLOBAL_DATA_PTR;
21
3f6e6993
MF
22void 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);
0adb5b76 37 return is_valid_ethaddr(enetaddr);
3f6e6993
MF
38}
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}
86848a74 48
7616e785
SG
49int eth_getenv_enetaddr_by_index(const char *base_name, int index,
50 uchar *enetaddr)
86848a74
MF
51{
52 char enetvar[32];
7616e785 53 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
86848a74
MF
54 return eth_getenv_enetaddr(enetvar, enetaddr);
55}
3f6e6993 56
154177e1 57static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
c88ef3c1
RH
58 uchar *enetaddr)
59{
60 char enetvar[32];
61 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
62 return eth_setenv_enetaddr(enetvar, enetaddr);
63}
64
ecee9324
BW
65static int eth_mac_skip(int index)
66{
67 char enetvar[15];
68 char *skip_state;
ff819a3a 69
ecee9324 70 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
ff819a3a
JH
71 skip_state = getenv(enetvar);
72 return skip_state != NULL;
ecee9324
BW
73}
74
84eb1fba
JH
75static void eth_current_changed(void);
76
3bc42700
SG
77/*
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
3bc42700
SG
99 /*
100 * If board-specific initialization exists, call it.
101 * If not, call a CPU-specific one
102 */
103 if (board_eth_init != __def_eth_init) {
104 if (board_eth_init(gd->bd) < 0)
105 printf("Board Net Initialization Failed\n");
106 } else if (cpu_eth_init != __def_eth_init) {
107 if (cpu_eth_init(gd->bd) < 0)
108 printf("CPU Net Initialization Failed\n");
109 } else {
d8f79afa 110#ifndef CONFIG_DM_ETH
3bc42700 111 printf("Net Initialization Skipped\n");
d8f79afa 112#endif
3bc42700
SG
113 }
114}
115
05c3e68f
JH
116#ifdef CONFIG_DM_ETH
117/**
118 * struct eth_device_priv - private structure for each Ethernet device
119 *
120 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
121 */
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
60304592
JH
135/* eth_errno - This stores the most recent failure code from DM functions */
136static int eth_errno;
137
05c3e68f
JH
138static 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
60304592
JH
158/*
159 * Typically this will simply return the active device.
160 * In the case where the most recent active device was unset, this will attempt
161 * to return the first device. If that device doesn't exist or fails to probe,
162 * this function will return NULL.
163 */
05c3e68f
JH
164struct 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)
60304592 170 eth_errno = uclass_first_device(UCLASS_ETH,
05c3e68f
JH
171 &uc_priv->current);
172 return uc_priv->current;
173}
174
60304592
JH
175/*
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 */
05c3e68f
JH
180static void eth_set_dev(struct udevice *dev)
181{
60304592
JH
182 if (dev && !device_active(dev))
183 eth_errno = device_probe(dev);
05c3e68f
JH
184 eth_get_uclass_priv()->current = dev;
185}
186
e58780dc
JH
187/*
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;
e408c421 198 int len = strlen("eth");
e58780dc
JH
199
200 /* Must be longer than 3 to be an alias */
e408c421
BM
201 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
202 startp = devname + len;
e58780dc
JH
203 seq = simple_strtoul(startp, &endp, 10);
204 }
205
206 uclass_get(UCLASS_ETH, &uc);
207 uclass_foreach_dev(it, uc) {
60304592
JH
208 /*
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 */
e58780dc
JH
216 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
05c3e68f
JH
228unsigned 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
f566c994
JH
277static 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
b86f795a
SG
293 /*
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 */
f566c994 297 ret = eth_get_ops(dev)->write_hwaddr(dev);
b86f795a
SG
298 if (ret == -ENOSYS)
299 ret = 0;
f566c994
JH
300 if (ret)
301 printf("\nWarning: %s failed to set MAC address\n",
302 dev->name);
303 }
304
305 return ret;
306}
307
6e0d26c0
JH
308static 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
05c3e68f
JH
335int eth_init(void)
336{
337 struct udevice *current;
338 struct udevice *old_current;
60304592 339 int ret = -ENODEV;
05c3e68f
JH
340
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)) {
60304592
JH
352 ret = eth_get_ops(current)->start(current);
353 if (ret >= 0) {
05c3e68f
JH
354 struct eth_device_priv *priv =
355 current->uclass_priv;
356
357 priv->state = ETH_STATE_ACTIVE;
358 return 0;
359 }
ff819a3a 360 } else {
60304592 361 ret = eth_errno;
ff819a3a 362 }
60304592 363
05c3e68f
JH
364 debug("FAIL\n");
365
60304592
JH
366 /*
367 * If ethrotate is enabled, this will change "current",
368 * otherwise we will drop out of this while loop immediately
369 */
05c3e68f 370 eth_try_another(0);
60304592 371 /* This will ensure the new "current" attempted to probe */
05c3e68f
JH
372 current = eth_get_dev();
373 } while (old_current != current);
374
60304592 375 return ret;
05c3e68f
JH
376}
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
eaa8a195
BN
392int 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
05c3e68f
JH
403int eth_send(void *packet, int length)
404{
405 struct udevice *current;
60304592 406 int ret;
05c3e68f
JH
407
408 current = eth_get_dev();
409 if (!current)
410 return -ENODEV;
411
412 if (!device_active(current))
413 return -EINVAL;
414
60304592
JH
415 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;
05c3e68f
JH
421}
422
423int eth_rx(void)
424{
425 struct udevice *current;
17591405 426 uchar *packet;
a1ca92ea 427 int flags;
17591405
JH
428 int ret;
429 int i;
05c3e68f
JH
430
431 current = eth_get_dev();
432 if (!current)
433 return -ENODEV;
434
435 if (!device_active(current))
436 return -EINVAL;
437
17591405 438 /* Process up to 32 packets at one time */
a1ca92ea 439 flags = ETH_RECV_CHECK_DEVICE;
17591405 440 for (i = 0; i < 32; i++) {
a1ca92ea
SG
441 ret = eth_get_ops(current)->recv(current, flags, &packet);
442 flags = 0;
17591405
JH
443 if (ret > 0)
444 net_process_received_packet(packet, ret);
63c9729a
JH
445 if (ret >= 0 && eth_get_ops(current)->free_pkt)
446 eth_get_ops(current)->free_pkt(current, packet, ret);
447 if (ret <= 0)
17591405
JH
448 break;
449 }
450 if (ret == -EAGAIN)
451 ret = 0;
60304592
JH
452 if (ret < 0) {
453 /* We cannot completely return the error at present */
454 debug("%s: recv() returned error %d\n", __func__, ret);
455 }
17591405 456 return ret;
05c3e68f
JH
457}
458
05c3e68f
JH
459int eth_initialize(void)
460{
461 int num_devices = 0;
462 struct udevice *dev;
463
3bc42700 464 eth_common_init();
05c3e68f
JH
465
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 {
6536b9bb
JH
477 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
05c3e68f
JH
489 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
6536b9bb
JH
496 if (ethprime && dev == prime_dev)
497 printf(" [PRIME]");
498
05c3e68f
JH
499 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);
0adb5b76
JH
544 if (!is_zero_ethaddr(env_enetaddr)) {
545 if (!is_zero_ethaddr(pdata->enetaddr) &&
05c3e68f
JH
546 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);
0adb5b76 557 } else if (is_valid_ethaddr(pdata->enetaddr)) {
05c3e68f
JH
558 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
559 printf("\nWarning: %s using MAC address from ROM\n",
560 dev->name);
0adb5b76 561 } else if (is_zero_ethaddr(pdata->enetaddr)) {
bef1014b
JH
562#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
05c3e68f
JH
567 printf("\nError: %s address not set.\n",
568 dev->name);
569 return -EINVAL;
bef1014b 570#endif
05c3e68f
JH
571 }
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),
e58780dc 592 .flags = DM_UC_FLAG_SEQ_ALIAS,
05c3e68f 593};
eaa8a195 594#endif /* #ifdef CONFIG_DM_ETH */
05c3e68f
JH
595
596#ifndef CONFIG_DM_ETH
dd35479a 597
f85b6071 598#ifdef CONFIG_API
f85b6071
RJ
599static struct {
600 uchar data[PKTSIZE];
601 int length;
602} eth_rcv_bufs[PKTBUFSRX];
603
66c7385a 604static unsigned int eth_rcv_current, eth_rcv_last;
f85b6071
RJ
605#endif
606
f8be7d65
JH
607static struct eth_device *eth_devices;
608struct eth_device *eth_current;
c609719b 609
84eb1fba
JH
610static void eth_set_current_to_next(void)
611{
612 eth_current = eth_current->next;
613}
614
e58780dc
JH
615static void eth_set_dev(struct eth_device *dev)
616{
617 eth_current = dev;
618}
619
d7fb9bcf 620struct eth_device *eth_get_dev_by_name(const char *devname)
63ff004c
MB
621{
622 struct eth_device *dev, *target_dev;
623
7e7f903f
HR
624 BUG_ON(devname == NULL);
625
63ff004c
MB
626 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
9e56986a
AF
642struct eth_device *eth_get_dev_by_index(int index)
643{
644 struct eth_device *dev, *target_dev;
9e56986a
AF
645
646 if (!eth_devices)
647 return NULL;
648
649 dev = eth_devices;
650 target_dev = NULL;
651 do {
fea7dcae 652 if (dev->index == index) {
9e56986a
AF
653 target_dev = dev;
654 break;
655 }
656 dev = dev->next;
9e56986a
AF
657 } while (dev != eth_devices);
658
659 return target_dev;
660}
661
66c7385a 662int eth_get_dev_index(void)
c609719b 663{
66c7385a 664 if (!eth_current)
fea7dcae 665 return -1;
c609719b 666
fea7dcae 667 return eth_current->index;
c609719b
WD
668}
669
6e0d26c0
JH
670static 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 }
694 } while (dev != eth_devices);
695
696 return 0;
697}
698U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
699
7616e785
SG
700int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
701 int eth_number)
702{
703 unsigned char env_enetaddr[6];
704 int ret = 0;
705
69376644 706 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
7616e785 707
0adb5b76
JH
708 if (!is_zero_ethaddr(env_enetaddr)) {
709 if (!is_zero_ethaddr(dev->enetaddr) &&
4c7c65af 710 memcmp(dev->enetaddr, env_enetaddr, 6)) {
7616e785 711 printf("\nWarning: %s MAC addresses don't match:\n",
ff819a3a 712 dev->name);
7616e785 713 printf("Address in SROM is %pM\n",
ff819a3a 714 dev->enetaddr);
7616e785 715 printf("Address in environment is %pM\n",
ff819a3a 716 env_enetaddr);
7616e785
SG
717 }
718
719 memcpy(dev->enetaddr, env_enetaddr, 6);
0adb5b76 720 } else if (is_valid_ethaddr(dev->enetaddr)) {
c88ef3c1
RH
721 eth_setenv_enetaddr_by_index(base_name, eth_number,
722 dev->enetaddr);
723 printf("\nWarning: %s using MAC address from net device\n",
ff819a3a 724 dev->name);
0adb5b76 725 } else if (is_zero_ethaddr(dev->enetaddr)) {
bef1014b
JH
726#ifdef CONFIG_NET_RANDOM_ETHADDR
727 net_random_ethaddr(dev->enetaddr);
728 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
729 dev->name, eth_number, dev->enetaddr);
730#else
75d9a45c
PM
731 printf("\nError: %s address not set.\n",
732 dev->name);
733 return -EINVAL;
bef1014b 734#endif
7616e785
SG
735 }
736
75d9a45c 737 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
0adb5b76 738 if (!is_valid_ethaddr(dev->enetaddr)) {
75d9a45c 739 printf("\nError: %s address %pM illegal value\n",
ff819a3a 740 dev->name, dev->enetaddr);
75d9a45c
PM
741 return -EINVAL;
742 }
460f949f 743
7616e785 744 ret = dev->write_hwaddr(dev);
75d9a45c 745 if (ret)
ff819a3a
JH
746 printf("\nWarning: %s failed to set MAC address\n",
747 dev->name);
460f949f 748 }
7616e785
SG
749
750 return ret;
751}
752
89d48367
SG
753int eth_register(struct eth_device *dev)
754{
755 struct eth_device *d;
66c7385a 756 static int index;
58c583b6 757
f6add132 758 assert(strlen(dev->name) < sizeof(dev->name));
58c583b6 759
89d48367 760 if (!eth_devices) {
ff819a3a
JH
761 eth_devices = dev;
762 eth_current = dev;
89d48367 763 eth_current_changed();
c609719b 764 } else {
66c7385a 765 for (d = eth_devices; d->next != eth_devices; d = d->next)
aba4b69d 766 ;
c609719b
WD
767 d->next = dev;
768 }
769
770 dev->state = ETH_STATE_INIT;
771 dev->next = eth_devices;
fea7dcae 772 dev->index = index++;
c609719b
WD
773
774 return 0;
775}
776
e7e982d6
VP
777int eth_unregister(struct eth_device *dev)
778{
779 struct eth_device *cur;
780
781 /* No device */
782 if (!eth_devices)
05324a48 783 return -ENODEV;
e7e982d6
VP
784
785 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
786 cur = cur->next)
787 ;
788
789 /* Device not found */
790 if (cur->next != dev)
05324a48 791 return -ENODEV;
e7e982d6
VP
792
793 cur->next = dev->next;
794
795 if (eth_devices == dev)
796 eth_devices = dev->next == eth_devices ? NULL : dev->next;
797
798 if (eth_current == dev) {
799 eth_current = eth_devices;
800 eth_current_changed();
801 }
802
803 return 0;
804}
805
d2eaec60 806int eth_initialize(void)
c609719b 807{
fea7dcae 808 int num_devices = 0;
3bc42700 809
c609719b
WD
810 eth_devices = NULL;
811 eth_current = NULL;
3bc42700 812 eth_common_init();
d9785c14 813
c609719b 814 if (!eth_devices) {
66c7385a 815 puts("No ethernet found.\n");
770605e4 816 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
c609719b
WD
817 } else {
818 struct eth_device *dev = eth_devices;
66c7385a 819 char *ethprime = getenv("ethprime");
c609719b 820
770605e4 821 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
c609719b 822 do {
fea7dcae 823 if (dev->index)
66c7385a 824 puts(", ");
c609719b
WD
825
826 printf("%s", dev->name);
827
66c7385a 828 if (ethprime && strcmp(dev->name, ethprime) == 0) {
c609719b 829 eth_current = dev;
66c7385a 830 puts(" [PRIME]");
c609719b
WD
831 }
832
1384f3bb 833 if (strchr(dev->name, ' '))
66c7385a
JH
834 puts("\nWarning: eth device name has a space!"
835 "\n");
1384f3bb 836
75d9a45c 837 eth_write_hwaddr(dev, "eth", dev->index);
c609719b 838
c609719b 839 dev = dev->next;
fea7dcae 840 num_devices++;
66c7385a 841 } while (dev != eth_devices);
c609719b 842
89d48367 843 eth_current_changed();
66c7385a 844 putc('\n');
c609719b
WD
845 }
846
fea7dcae 847 return num_devices;
c609719b
WD
848}
849
53a5c424
DU
850#ifdef CONFIG_MCAST_TFTP
851/* Multicast.
852 * mcast_addr: multicast ipaddr from which multicast Mac is made
85eb5caf 853 * join: 1=join, 0=leave.
53a5c424 854 */
049a95a7 855int eth_mcast_join(struct in_addr mcast_ip, int join)
53a5c424 856{
66c7385a 857 u8 mcast_mac[6];
85eb5caf 858 if (!eth_current || !eth_current->mcast)
53a5c424 859 return -1;
049a95a7
JH
860 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
861 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
862 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
53a5c424
DU
863 mcast_mac[2] = 0x5e;
864 mcast_mac[1] = 0x0;
865 mcast_mac[0] = 0x1;
866 return eth_current->mcast(eth_current, mcast_mac, join);
867}
868
85eb5caf
WD
869/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
870 * and this is the ethernet-crc method needed for TSEC -- and perhaps
53a5c424
DU
871 * some other adapter -- hash tables
872 */
873#define CRCPOLY_LE 0xedb88320
66c7385a 874u32 ether_crc(size_t len, unsigned char const *p)
53a5c424
DU
875{
876 int i;
877 u32 crc;
878 crc = ~0;
879 while (len--) {
880 crc ^= *p++;
881 for (i = 0; i < 8; i++)
882 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
883 }
884 /* an reverse the bits, cuz of way they arrive -- last-first */
885 crc = (crc >> 16) | (crc << 16);
886 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
887 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
888 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
889 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
890 return crc;
891}
892
893#endif
894
c609719b 895
d2eaec60 896int eth_init(void)
c609719b 897{
6e0d26c0 898 struct eth_device *old_current;
c609719b 899
6bc11388 900 if (!eth_current) {
66c7385a 901 puts("No ethernet found.\n");
05324a48 902 return -ENODEV;
6bc11388 903 }
c609719b
WD
904
905 old_current = eth_current;
906 do {
0ebf04c6 907 debug("Trying %s\n", eth_current->name);
c609719b 908
d2eaec60 909 if (eth_current->init(eth_current, gd->bd) >= 0) {
c609719b
WD
910 eth_current->state = ETH_STATE_ACTIVE;
911
505be87a 912 return 0;
c609719b 913 }
0ebf04c6 914 debug("FAIL\n");
c609719b
WD
915
916 eth_try_another(0);
917 } while (old_current != eth_current);
918
05324a48 919 return -ETIMEDOUT;
c609719b
WD
920}
921
922void eth_halt(void)
923{
924 if (!eth_current)
925 return;
926
927 eth_current->halt(eth_current);
928
929 eth_current->state = ETH_STATE_PASSIVE;
930}
931
eaa8a195
BN
932int eth_is_active(struct eth_device *dev)
933{
934 return dev && dev->state == ETH_STATE_ACTIVE;
935}
936
db288a96 937int eth_send(void *packet, int length)
c609719b
WD
938{
939 if (!eth_current)
05324a48 940 return -ENODEV;
c609719b
WD
941
942 return eth_current->send(eth_current, packet, length);
943}
944
945int eth_rx(void)
946{
947 if (!eth_current)
05324a48 948 return -ENODEV;
c609719b
WD
949
950 return eth_current->recv(eth_current);
951}
05c3e68f 952#endif /* ifndef CONFIG_DM_ETH */
c609719b 953
f85b6071 954#ifdef CONFIG_API
db288a96 955static void eth_save_packet(void *packet, int length)
f85b6071 956{
db288a96 957 char *p = packet;
f85b6071
RJ
958 int i;
959
960 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
961 return;
962
963 if (PKTSIZE < length)
964 return;
965
966 for (i = 0; i < length; i++)
967 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
968
969 eth_rcv_bufs[eth_rcv_last].length = length;
970 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
971}
972
db288a96 973int eth_receive(void *packet, int length)
f85b6071 974{
db288a96 975 char *p = packet;
f85b6071
RJ
976 void *pp = push_packet;
977 int i;
978
979 if (eth_rcv_current == eth_rcv_last) {
980 push_packet = eth_save_packet;
981 eth_rx();
982 push_packet = pp;
983
984 if (eth_rcv_current == eth_rcv_last)
985 return -1;
986 }
987
46c07bcf 988 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
f85b6071
RJ
989
990 for (i = 0; i < length; i++)
991 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
992
993 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
994 return length;
995}
996#endif /* CONFIG_API */
997
84eb1fba
JH
998static void eth_current_changed(void)
999{
1000 char *act = getenv("ethact");
1001 /* update current ethernet name */
1002 if (eth_get_dev()) {
1003 if (act == NULL || strcmp(act, eth_get_name()) != 0)
1004 setenv("ethact", eth_get_name());
1005 }
1006 /*
1007 * remove the variable completely if there is no active
1008 * interface
1009 */
1010 else if (act != NULL)
1011 setenv("ethact", NULL);
1012}
1013
c609719b
WD
1014void eth_try_another(int first_restart)
1015{
05c3e68f 1016 static void *first_failed;
c650e1be 1017 char *ethrotate;
e1692577
MF
1018
1019 /*
1020 * Do not rotate between network interfaces when
1021 * 'ethrotate' variable is set to 'no'.
1022 */
66c7385a
JH
1023 ethrotate = getenv("ethrotate");
1024 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
e1692577 1025 return;
c609719b 1026
84eb1fba 1027 if (!eth_get_dev())
c609719b
WD
1028 return;
1029
66c7385a 1030 if (first_restart)
84eb1fba 1031 first_failed = eth_get_dev();
c609719b 1032
84eb1fba 1033 eth_set_current_to_next();
c609719b 1034
89d48367 1035 eth_current_changed();
a3d991bd 1036
84eb1fba 1037 if (first_failed == eth_get_dev())
bc0571fc 1038 net_restart_wrap = 1;
c609719b
WD
1039}
1040
a3d991bd
WD
1041void eth_set_current(void)
1042{
66c7385a
JH
1043 static char *act;
1044 static int env_changed_id;
2f70c49e 1045 int env_id;
a3d991bd 1046
2f70c49e
HS
1047 env_id = get_env_id();
1048 if ((act == NULL) || (env_changed_id != env_id)) {
1049 act = getenv("ethact");
1050 env_changed_id = env_id;
1051 }
6536b9bb
JH
1052
1053 if (act == NULL) {
1054 char *ethprime = getenv("ethprime");
1055 void *dev = NULL;
1056
1057 if (ethprime)
1058 dev = eth_get_dev_by_name(ethprime);
1059 if (dev)
1060 eth_set_dev(dev);
1061 else
1062 eth_set_dev(NULL);
1063 } else {
e58780dc 1064 eth_set_dev(eth_get_dev_by_name(act));
6536b9bb 1065 }
a3d991bd 1066
89d48367 1067 eth_current_changed();
a3d991bd 1068}
a3d991bd 1069
84eb1fba 1070const char *eth_get_name(void)
5bb226e8 1071{
84eb1fba 1072 return eth_get_dev() ? eth_get_dev()->name : "unknown";
5bb226e8 1073}