]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - net/eth.c
net: Update hardware MAC address if it changes in env
[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                 printf("\nError: %s address not set.\n",
542                        dev->name);
543                 return -EINVAL;
544         }
545
546         return 0;
547 }
548
549 static int eth_pre_remove(struct udevice *dev)
550 {
551         eth_get_ops(dev)->stop(dev);
552
553         return 0;
554 }
555
556 UCLASS_DRIVER(eth) = {
557         .name           = "eth",
558         .id             = UCLASS_ETH,
559         .post_bind      = eth_post_bind,
560         .pre_unbind     = eth_pre_unbind,
561         .post_probe     = eth_post_probe,
562         .pre_remove     = eth_pre_remove,
563         .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
564         .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
565         .flags          = DM_UC_FLAG_SEQ_ALIAS,
566 };
567 #endif
568
569 #ifndef CONFIG_DM_ETH
570
571 #ifdef CONFIG_API
572 static struct {
573         uchar data[PKTSIZE];
574         int length;
575 } eth_rcv_bufs[PKTBUFSRX];
576
577 static unsigned int eth_rcv_current, eth_rcv_last;
578 #endif
579
580 static struct eth_device *eth_devices;
581 struct eth_device *eth_current;
582
583 static void eth_set_current_to_next(void)
584 {
585         eth_current = eth_current->next;
586 }
587
588 static void eth_set_dev(struct eth_device *dev)
589 {
590         eth_current = dev;
591 }
592
593 struct eth_device *eth_get_dev_by_name(const char *devname)
594 {
595         struct eth_device *dev, *target_dev;
596
597         BUG_ON(devname == NULL);
598
599         if (!eth_devices)
600                 return NULL;
601
602         dev = eth_devices;
603         target_dev = NULL;
604         do {
605                 if (strcmp(devname, dev->name) == 0) {
606                         target_dev = dev;
607                         break;
608                 }
609                 dev = dev->next;
610         } while (dev != eth_devices);
611
612         return target_dev;
613 }
614
615 struct eth_device *eth_get_dev_by_index(int index)
616 {
617         struct eth_device *dev, *target_dev;
618
619         if (!eth_devices)
620                 return NULL;
621
622         dev = eth_devices;
623         target_dev = NULL;
624         do {
625                 if (dev->index == index) {
626                         target_dev = dev;
627                         break;
628                 }
629                 dev = dev->next;
630         } while (dev != eth_devices);
631
632         return target_dev;
633 }
634
635 int eth_get_dev_index(void)
636 {
637         if (!eth_current)
638                 return -1;
639
640         return eth_current->index;
641 }
642
643 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
644                    int eth_number)
645 {
646         unsigned char env_enetaddr[6];
647         int ret = 0;
648
649         eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
650
651         if (!is_zero_ethaddr(env_enetaddr)) {
652                 if (!is_zero_ethaddr(dev->enetaddr) &&
653                     memcmp(dev->enetaddr, env_enetaddr, 6)) {
654                         printf("\nWarning: %s MAC addresses don't match:\n",
655                                dev->name);
656                         printf("Address in SROM is         %pM\n",
657                                dev->enetaddr);
658                         printf("Address in environment is  %pM\n",
659                                env_enetaddr);
660                 }
661
662                 memcpy(dev->enetaddr, env_enetaddr, 6);
663         } else if (is_valid_ethaddr(dev->enetaddr)) {
664                 eth_setenv_enetaddr_by_index(base_name, eth_number,
665                                              dev->enetaddr);
666                 printf("\nWarning: %s using MAC address from net device\n",
667                        dev->name);
668         } else if (is_zero_ethaddr(dev->enetaddr)) {
669                 printf("\nError: %s address not set.\n",
670                        dev->name);
671                 return -EINVAL;
672         }
673
674         if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
675                 if (!is_valid_ethaddr(dev->enetaddr)) {
676                         printf("\nError: %s address %pM illegal value\n",
677                                dev->name, dev->enetaddr);
678                         return -EINVAL;
679                 }
680
681                 ret = dev->write_hwaddr(dev);
682                 if (ret)
683                         printf("\nWarning: %s failed to set MAC address\n",
684                                dev->name);
685         }
686
687         return ret;
688 }
689
690 int eth_register(struct eth_device *dev)
691 {
692         struct eth_device *d;
693         static int index;
694
695         assert(strlen(dev->name) < sizeof(dev->name));
696
697         if (!eth_devices) {
698                 eth_devices = dev;
699                 eth_current = dev;
700                 eth_current_changed();
701         } else {
702                 for (d = eth_devices; d->next != eth_devices; d = d->next)
703                         ;
704                 d->next = dev;
705         }
706
707         dev->state = ETH_STATE_INIT;
708         dev->next  = eth_devices;
709         dev->index = index++;
710
711         return 0;
712 }
713
714 int eth_unregister(struct eth_device *dev)
715 {
716         struct eth_device *cur;
717
718         /* No device */
719         if (!eth_devices)
720                 return -ENODEV;
721
722         for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
723              cur = cur->next)
724                 ;
725
726         /* Device not found */
727         if (cur->next != dev)
728                 return -ENODEV;
729
730         cur->next = dev->next;
731
732         if (eth_devices == dev)
733                 eth_devices = dev->next == eth_devices ? NULL : dev->next;
734
735         if (eth_current == dev) {
736                 eth_current = eth_devices;
737                 eth_current_changed();
738         }
739
740         return 0;
741 }
742
743 int eth_initialize(void)
744 {
745         int num_devices = 0;
746
747         eth_devices = NULL;
748         eth_current = NULL;
749         eth_common_init();
750
751         if (!eth_devices) {
752                 puts("No ethernet found.\n");
753                 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
754         } else {
755                 struct eth_device *dev = eth_devices;
756                 char *ethprime = getenv("ethprime");
757
758                 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
759                 do {
760                         if (dev->index)
761                                 puts(", ");
762
763                         printf("%s", dev->name);
764
765                         if (ethprime && strcmp(dev->name, ethprime) == 0) {
766                                 eth_current = dev;
767                                 puts(" [PRIME]");
768                         }
769
770                         if (strchr(dev->name, ' '))
771                                 puts("\nWarning: eth device name has a space!"
772                                         "\n");
773
774                         eth_write_hwaddr(dev, "eth", dev->index);
775
776                         dev = dev->next;
777                         num_devices++;
778                 } while (dev != eth_devices);
779
780                 eth_current_changed();
781                 putc('\n');
782         }
783
784         return num_devices;
785 }
786
787 #ifdef CONFIG_MCAST_TFTP
788 /* Multicast.
789  * mcast_addr: multicast ipaddr from which multicast Mac is made
790  * join: 1=join, 0=leave.
791  */
792 int eth_mcast_join(struct in_addr mcast_ip, int join)
793 {
794         u8 mcast_mac[6];
795         if (!eth_current || !eth_current->mcast)
796                 return -1;
797         mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
798         mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
799         mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
800         mcast_mac[2] = 0x5e;
801         mcast_mac[1] = 0x0;
802         mcast_mac[0] = 0x1;
803         return eth_current->mcast(eth_current, mcast_mac, join);
804 }
805
806 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
807  * and this is the ethernet-crc method needed for TSEC -- and perhaps
808  * some other adapter -- hash tables
809  */
810 #define CRCPOLY_LE 0xedb88320
811 u32 ether_crc(size_t len, unsigned char const *p)
812 {
813         int i;
814         u32 crc;
815         crc = ~0;
816         while (len--) {
817                 crc ^= *p++;
818                 for (i = 0; i < 8; i++)
819                         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
820         }
821         /* an reverse the bits, cuz of way they arrive -- last-first */
822         crc = (crc >> 16) | (crc << 16);
823         crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
824         crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
825         crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
826         crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
827         return crc;
828 }
829
830 #endif
831
832
833 int eth_init(void)
834 {
835         struct eth_device *old_current, *dev;
836
837         if (!eth_current) {
838                 puts("No ethernet found.\n");
839                 return -ENODEV;
840         }
841
842         /* Sync environment with network devices */
843         dev = eth_devices;
844         do {
845                 uchar env_enetaddr[6];
846                 int enetaddr_changed = 0;
847
848                 if (eth_getenv_enetaddr_by_index("eth", dev->index,
849                                                  env_enetaddr)) {
850                         enetaddr_changed = memcmp(dev->enetaddr,
851                                 env_enetaddr, 6);
852                         memcpy(dev->enetaddr, env_enetaddr, 6);
853                 } else {
854                         memset(env_enetaddr, 0, 6);
855                         enetaddr_changed = memcmp(dev->enetaddr,
856                                 env_enetaddr, 6);
857                         memset(dev->enetaddr, 0, 6);
858                 }
859                 if (enetaddr_changed)
860                         eth_write_hwaddr(dev, "eth", dev->index);
861
862                 dev = dev->next;
863         } while (dev != eth_devices);
864
865         old_current = eth_current;
866         do {
867                 debug("Trying %s\n", eth_current->name);
868
869                 if (eth_current->init(eth_current, gd->bd) >= 0) {
870                         eth_current->state = ETH_STATE_ACTIVE;
871
872                         return 0;
873                 }
874                 debug("FAIL\n");
875
876                 eth_try_another(0);
877         } while (old_current != eth_current);
878
879         return -ETIMEDOUT;
880 }
881
882 void eth_halt(void)
883 {
884         if (!eth_current)
885                 return;
886
887         eth_current->halt(eth_current);
888
889         eth_current->state = ETH_STATE_PASSIVE;
890 }
891
892 int eth_send(void *packet, int length)
893 {
894         if (!eth_current)
895                 return -ENODEV;
896
897         return eth_current->send(eth_current, packet, length);
898 }
899
900 int eth_rx(void)
901 {
902         if (!eth_current)
903                 return -ENODEV;
904
905         return eth_current->recv(eth_current);
906 }
907 #endif /* ifndef CONFIG_DM_ETH */
908
909 #ifdef CONFIG_API
910 static void eth_save_packet(void *packet, int length)
911 {
912         char *p = packet;
913         int i;
914
915         if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
916                 return;
917
918         if (PKTSIZE < length)
919                 return;
920
921         for (i = 0; i < length; i++)
922                 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
923
924         eth_rcv_bufs[eth_rcv_last].length = length;
925         eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
926 }
927
928 int eth_receive(void *packet, int length)
929 {
930         char *p = packet;
931         void *pp = push_packet;
932         int i;
933
934         if (eth_rcv_current == eth_rcv_last) {
935                 push_packet = eth_save_packet;
936                 eth_rx();
937                 push_packet = pp;
938
939                 if (eth_rcv_current == eth_rcv_last)
940                         return -1;
941         }
942
943         length = min(eth_rcv_bufs[eth_rcv_current].length, length);
944
945         for (i = 0; i < length; i++)
946                 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
947
948         eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
949         return length;
950 }
951 #endif /* CONFIG_API */
952
953 static void eth_current_changed(void)
954 {
955         char *act = getenv("ethact");
956         /* update current ethernet name */
957         if (eth_get_dev()) {
958                 if (act == NULL || strcmp(act, eth_get_name()) != 0)
959                         setenv("ethact", eth_get_name());
960         }
961         /*
962          * remove the variable completely if there is no active
963          * interface
964          */
965         else if (act != NULL)
966                 setenv("ethact", NULL);
967 }
968
969 void eth_try_another(int first_restart)
970 {
971         static void *first_failed;
972         char *ethrotate;
973
974         /*
975          * Do not rotate between network interfaces when
976          * 'ethrotate' variable is set to 'no'.
977          */
978         ethrotate = getenv("ethrotate");
979         if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
980                 return;
981
982         if (!eth_get_dev())
983                 return;
984
985         if (first_restart)
986                 first_failed = eth_get_dev();
987
988         eth_set_current_to_next();
989
990         eth_current_changed();
991
992         if (first_failed == eth_get_dev())
993                 net_restart_wrap = 1;
994 }
995
996 void eth_set_current(void)
997 {
998         static char *act;
999         static int  env_changed_id;
1000         int     env_id;
1001
1002         env_id = get_env_id();
1003         if ((act == NULL) || (env_changed_id != env_id)) {
1004                 act = getenv("ethact");
1005                 env_changed_id = env_id;
1006         }
1007
1008         if (act == NULL) {
1009                 char *ethprime = getenv("ethprime");
1010                 void *dev = NULL;
1011
1012                 if (ethprime)
1013                         dev = eth_get_dev_by_name(ethprime);
1014                 if (dev)
1015                         eth_set_dev(dev);
1016                 else
1017                         eth_set_dev(NULL);
1018         } else {
1019                 eth_set_dev(eth_get_dev_by_name(act));
1020         }
1021
1022         eth_current_changed();
1023 }
1024
1025 const char *eth_get_name(void)
1026 {
1027         return eth_get_dev() ? eth_get_dev()->name : "unknown";
1028 }