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