]> git.sur5r.net Git - u-boot/blob - net/eth.c
net: Remove duplicate bootfile syncing functionality
[u-boot] / 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 int eth_mac_skip(int index)
64 {
65         char enetvar[15];
66         char *skip_state;
67
68         sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
69         skip_state = getenv(enetvar);
70         return skip_state != NULL;
71 }
72
73 static void eth_current_changed(void);
74
75 /*
76  * CPU and board-specific Ethernet initializations.  Aliased function
77  * signals caller to move on
78  */
79 static int __def_eth_init(bd_t *bis)
80 {
81         return -1;
82 }
83 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
84 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
85
86 static void eth_common_init(void)
87 {
88         bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
89 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
90         miiphy_init();
91 #endif
92
93 #ifdef CONFIG_PHYLIB
94         phy_init();
95 #endif
96
97         /*
98          * If board-specific initialization exists, call it.
99          * If not, call a CPU-specific one
100          */
101         if (board_eth_init != __def_eth_init) {
102                 if (board_eth_init(gd->bd) < 0)
103                         printf("Board Net Initialization Failed\n");
104         } else if (cpu_eth_init != __def_eth_init) {
105                 if (cpu_eth_init(gd->bd) < 0)
106                         printf("CPU Net Initialization Failed\n");
107         } else {
108                 printf("Net Initialization Skipped\n");
109         }
110 }
111
112 #ifdef CONFIG_DM_ETH
113 /**
114  * struct eth_device_priv - private structure for each Ethernet device
115  *
116  * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
117  */
118 struct eth_device_priv {
119         enum eth_state_t state;
120 };
121
122 /**
123  * struct eth_uclass_priv - The structure attached to the uclass itself
124  *
125  * @current: The Ethernet device that the network functions are using
126  */
127 struct eth_uclass_priv {
128         struct udevice *current;
129 };
130
131 /* eth_errno - This stores the most recent failure code from DM functions */
132 static int eth_errno;
133
134 static struct eth_uclass_priv *eth_get_uclass_priv(void)
135 {
136         struct uclass *uc;
137
138         uclass_get(UCLASS_ETH, &uc);
139         assert(uc);
140         return uc->priv;
141 }
142
143 static void eth_set_current_to_next(void)
144 {
145         struct eth_uclass_priv *uc_priv;
146
147         uc_priv = eth_get_uclass_priv();
148         if (uc_priv->current)
149                 uclass_next_device(&uc_priv->current);
150         if (!uc_priv->current)
151                 uclass_first_device(UCLASS_ETH, &uc_priv->current);
152 }
153
154 /*
155  * Typically this will simply return the active device.
156  * In the case where the most recent active device was unset, this will attempt
157  * to return the first device. If that device doesn't exist or fails to probe,
158  * this function will return NULL.
159  */
160 struct udevice *eth_get_dev(void)
161 {
162         struct eth_uclass_priv *uc_priv;
163
164         uc_priv = eth_get_uclass_priv();
165         if (!uc_priv->current)
166                 eth_errno = uclass_first_device(UCLASS_ETH,
167                                     &uc_priv->current);
168         return uc_priv->current;
169 }
170
171 /*
172  * Typically this will just store a device pointer.
173  * In case it was not probed, we will attempt to do so.
174  * dev may be NULL to unset the active device.
175  */
176 static void eth_set_dev(struct udevice *dev)
177 {
178         if (dev && !device_active(dev))
179                 eth_errno = device_probe(dev);
180         eth_get_uclass_priv()->current = dev;
181 }
182
183 /*
184  * Find the udevice that either has the name passed in as devname or has an
185  * alias named devname.
186  */
187 struct udevice *eth_get_dev_by_name(const char *devname)
188 {
189         int seq = -1;
190         char *endp = NULL;
191         const char *startp = NULL;
192         struct udevice *it;
193         struct uclass *uc;
194
195         /* Must be longer than 3 to be an alias */
196         if (strlen(devname) > strlen("eth")) {
197                 startp = devname + strlen("eth");
198                 seq = simple_strtoul(startp, &endp, 10);
199         }
200
201         uclass_get(UCLASS_ETH, &uc);
202         uclass_foreach_dev(it, uc) {
203                 /*
204                  * We need the seq to be valid, so try to probe it.
205                  * If the probe fails, the seq will not match since it will be
206                  * -1 instead of what we are looking for.
207                  * We don't care about errors from probe here. Either they won't
208                  * match an alias or it will match a literal name and we'll pick
209                  * up the error when we try to probe again in eth_set_dev().
210                  */
211                 device_probe(it);
212                 /*
213                  * Check for the name or the sequence number to match
214                  */
215                 if (strcmp(it->name, devname) == 0 ||
216                     (endp > startp && it->seq == seq))
217                         return it;
218         }
219
220         return NULL;
221 }
222
223 unsigned char *eth_get_ethaddr(void)
224 {
225         struct eth_pdata *pdata;
226
227         if (eth_get_dev()) {
228                 pdata = eth_get_dev()->platdata;
229                 return pdata->enetaddr;
230         }
231
232         return NULL;
233 }
234
235 /* Set active state without calling start on the driver */
236 int eth_init_state_only(void)
237 {
238         struct udevice *current;
239         struct eth_device_priv *priv;
240
241         current = eth_get_dev();
242         if (!current || !device_active(current))
243                 return -EINVAL;
244
245         priv = current->uclass_priv;
246         priv->state = ETH_STATE_ACTIVE;
247
248         return 0;
249 }
250
251 /* Set passive state without calling stop on the driver */
252 void eth_halt_state_only(void)
253 {
254         struct udevice *current;
255         struct eth_device_priv *priv;
256
257         current = eth_get_dev();
258         if (!current || !device_active(current))
259                 return;
260
261         priv = current->uclass_priv;
262         priv->state = ETH_STATE_PASSIVE;
263 }
264
265 int eth_get_dev_index(void)
266 {
267         if (eth_get_dev())
268                 return eth_get_dev()->seq;
269         return -1;
270 }
271
272 static int eth_write_hwaddr(struct udevice *dev)
273 {
274         struct eth_pdata *pdata = dev->platdata;
275         int ret = 0;
276
277         if (!dev || !device_active(dev))
278                 return -EINVAL;
279
280         /* seq is valid since the device is active */
281         if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
282                 if (!is_valid_ethaddr(pdata->enetaddr)) {
283                         printf("\nError: %s address %pM illegal value\n",
284                                dev->name, pdata->enetaddr);
285                         return -EINVAL;
286                 }
287
288                 ret = eth_get_ops(dev)->write_hwaddr(dev);
289                 if (ret)
290                         printf("\nWarning: %s failed to set MAC address\n",
291                                dev->name);
292         }
293
294         return ret;
295 }
296
297 int eth_init(void)
298 {
299         struct udevice *current;
300         struct udevice *old_current;
301         int ret = -ENODEV;
302
303         current = eth_get_dev();
304         if (!current) {
305                 printf("No ethernet found.\n");
306                 return -ENODEV;
307         }
308
309         old_current = current;
310         do {
311                 debug("Trying %s\n", current->name);
312
313                 if (device_active(current)) {
314                         uchar env_enetaddr[6];
315                         struct eth_pdata *pdata = current->platdata;
316                         int enetaddr_changed = 0;
317
318                         /* Sync environment with network device */
319                         if (eth_getenv_enetaddr_by_index("eth", current->seq,
320                                                          env_enetaddr)) {
321                                 enetaddr_changed = memcmp(pdata->enetaddr,
322                                         env_enetaddr, 6);
323                                 memcpy(pdata->enetaddr, env_enetaddr, 6);
324                         } else {
325                                 memset(env_enetaddr, 0, 6);
326                                 enetaddr_changed = memcmp(pdata->enetaddr,
327                                         env_enetaddr, 6);
328                                 memset(pdata->enetaddr, 0, 6);
329                         }
330                         if (enetaddr_changed)
331                                 eth_write_hwaddr(current);
332
333                         ret = eth_get_ops(current)->start(current);
334                         if (ret >= 0) {
335                                 struct eth_device_priv *priv =
336                                         current->uclass_priv;
337
338                                 priv->state = ETH_STATE_ACTIVE;
339                                 return 0;
340                         }
341                 } else {
342                         ret = eth_errno;
343                 }
344
345                 debug("FAIL\n");
346
347                 /*
348                  * If ethrotate is enabled, this will change "current",
349                  * otherwise we will drop out of this while loop immediately
350                  */
351                 eth_try_another(0);
352                 /* This will ensure the new "current" attempted to probe */
353                 current = eth_get_dev();
354         } while (old_current != current);
355
356         return ret;
357 }
358
359 void eth_halt(void)
360 {
361         struct udevice *current;
362         struct eth_device_priv *priv;
363
364         current = eth_get_dev();
365         if (!current || !device_active(current))
366                 return;
367
368         eth_get_ops(current)->stop(current);
369         priv = current->uclass_priv;
370         priv->state = ETH_STATE_PASSIVE;
371 }
372
373 int eth_send(void *packet, int length)
374 {
375         struct udevice *current;
376         int ret;
377
378         current = eth_get_dev();
379         if (!current)
380                 return -ENODEV;
381
382         if (!device_active(current))
383                 return -EINVAL;
384
385         ret = eth_get_ops(current)->send(current, packet, length);
386         if (ret < 0) {
387                 /* We cannot completely return the error at present */
388                 debug("%s: send() returned error %d\n", __func__, ret);
389         }
390         return ret;
391 }
392
393 int eth_rx(void)
394 {
395         struct udevice *current;
396         uchar *packet;
397         int ret;
398         int i;
399
400         current = eth_get_dev();
401         if (!current)
402                 return -ENODEV;
403
404         if (!device_active(current))
405                 return -EINVAL;
406
407         /* Process up to 32 packets at one time */
408         for (i = 0; i < 32; i++) {
409                 ret = eth_get_ops(current)->recv(current, &packet);
410                 if (ret > 0)
411                         net_process_received_packet(packet, ret);
412                 if (ret >= 0 && eth_get_ops(current)->free_pkt)
413                         eth_get_ops(current)->free_pkt(current, packet, ret);
414                 if (ret <= 0)
415                         break;
416         }
417         if (ret == -EAGAIN)
418                 ret = 0;
419         if (ret < 0) {
420                 /* We cannot completely return the error at present */
421                 debug("%s: recv() returned error %d\n", __func__, ret);
422         }
423         return ret;
424 }
425
426 int eth_initialize(void)
427 {
428         int num_devices = 0;
429         struct udevice *dev;
430
431         eth_common_init();
432
433         /*
434          * Devices need to write the hwaddr even if not started so that Linux
435          * will have access to the hwaddr that u-boot stored for the device.
436          * This is accomplished by attempting to probe each device and calling
437          * their write_hwaddr() operation.
438          */
439         uclass_first_device(UCLASS_ETH, &dev);
440         if (!dev) {
441                 printf("No ethernet found.\n");
442                 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
443         } else {
444                 char *ethprime = getenv("ethprime");
445                 struct udevice *prime_dev = NULL;
446
447                 if (ethprime)
448                         prime_dev = eth_get_dev_by_name(ethprime);
449                 if (prime_dev) {
450                         eth_set_dev(prime_dev);
451                         eth_current_changed();
452                 } else {
453                         eth_set_dev(NULL);
454                 }
455
456                 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
457                 do {
458                         if (num_devices)
459                                 printf(", ");
460
461                         printf("eth%d: %s", dev->seq, dev->name);
462
463                         if (ethprime && dev == prime_dev)
464                                 printf(" [PRIME]");
465
466                         eth_write_hwaddr(dev);
467
468                         uclass_next_device(&dev);
469                         num_devices++;
470                 } while (dev);
471
472                 putc('\n');
473         }
474
475         return num_devices;
476 }
477
478 static int eth_post_bind(struct udevice *dev)
479 {
480         if (strchr(dev->name, ' ')) {
481                 printf("\nError: eth device name \"%s\" has a space!\n",
482                        dev->name);
483                 return -EINVAL;
484         }
485
486         return 0;
487 }
488
489 static int eth_pre_unbind(struct udevice *dev)
490 {
491         /* Don't hang onto a pointer that is going away */
492         if (dev == eth_get_uclass_priv()->current)
493                 eth_set_dev(NULL);
494
495         return 0;
496 }
497
498 static int eth_post_probe(struct udevice *dev)
499 {
500         struct eth_device_priv *priv = dev->uclass_priv;
501         struct eth_pdata *pdata = dev->platdata;
502         unsigned char env_enetaddr[6];
503
504         priv->state = ETH_STATE_INIT;
505
506         /* Check if the device has a MAC address in ROM */
507         if (eth_get_ops(dev)->read_rom_hwaddr)
508                 eth_get_ops(dev)->read_rom_hwaddr(dev);
509
510         eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
511         if (!is_zero_ethaddr(env_enetaddr)) {
512                 if (!is_zero_ethaddr(pdata->enetaddr) &&
513                     memcmp(pdata->enetaddr, env_enetaddr, 6)) {
514                         printf("\nWarning: %s MAC addresses don't match:\n",
515                                dev->name);
516                         printf("Address in SROM is         %pM\n",
517                                pdata->enetaddr);
518                         printf("Address in environment is  %pM\n",
519                                env_enetaddr);
520                 }
521
522                 /* Override the ROM MAC address */
523                 memcpy(pdata->enetaddr, env_enetaddr, 6);
524         } else if (is_valid_ethaddr(pdata->enetaddr)) {
525                 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
526                 printf("\nWarning: %s using MAC address from ROM\n",
527                        dev->name);
528         } else if (is_zero_ethaddr(pdata->enetaddr)) {
529 #ifdef CONFIG_NET_RANDOM_ETHADDR
530                 net_random_ethaddr(pdata->enetaddr);
531                 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
532                        dev->name, dev->seq, pdata->enetaddr);
533 #else
534                 printf("\nError: %s address not set.\n",
535                        dev->name);
536                 return -EINVAL;
537 #endif
538         }
539
540         return 0;
541 }
542
543 static int eth_pre_remove(struct udevice *dev)
544 {
545         eth_get_ops(dev)->stop(dev);
546
547         return 0;
548 }
549
550 UCLASS_DRIVER(eth) = {
551         .name           = "eth",
552         .id             = UCLASS_ETH,
553         .post_bind      = eth_post_bind,
554         .pre_unbind     = eth_pre_unbind,
555         .post_probe     = eth_post_probe,
556         .pre_remove     = eth_pre_remove,
557         .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
558         .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
559         .flags          = DM_UC_FLAG_SEQ_ALIAS,
560 };
561 #endif
562
563 #ifndef CONFIG_DM_ETH
564
565 #ifdef CONFIG_API
566 static struct {
567         uchar data[PKTSIZE];
568         int length;
569 } eth_rcv_bufs[PKTBUFSRX];
570
571 static unsigned int eth_rcv_current, eth_rcv_last;
572 #endif
573
574 static struct eth_device *eth_devices;
575 struct eth_device *eth_current;
576
577 static void eth_set_current_to_next(void)
578 {
579         eth_current = eth_current->next;
580 }
581
582 static void eth_set_dev(struct eth_device *dev)
583 {
584         eth_current = dev;
585 }
586
587 struct eth_device *eth_get_dev_by_name(const char *devname)
588 {
589         struct eth_device *dev, *target_dev;
590
591         BUG_ON(devname == NULL);
592
593         if (!eth_devices)
594                 return NULL;
595
596         dev = eth_devices;
597         target_dev = NULL;
598         do {
599                 if (strcmp(devname, dev->name) == 0) {
600                         target_dev = dev;
601                         break;
602                 }
603                 dev = dev->next;
604         } while (dev != eth_devices);
605
606         return target_dev;
607 }
608
609 struct eth_device *eth_get_dev_by_index(int index)
610 {
611         struct eth_device *dev, *target_dev;
612
613         if (!eth_devices)
614                 return NULL;
615
616         dev = eth_devices;
617         target_dev = NULL;
618         do {
619                 if (dev->index == index) {
620                         target_dev = dev;
621                         break;
622                 }
623                 dev = dev->next;
624         } while (dev != eth_devices);
625
626         return target_dev;
627 }
628
629 int eth_get_dev_index(void)
630 {
631         if (!eth_current)
632                 return -1;
633
634         return eth_current->index;
635 }
636
637 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
638                    int eth_number)
639 {
640         unsigned char env_enetaddr[6];
641         int ret = 0;
642
643         eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
644
645         if (!is_zero_ethaddr(env_enetaddr)) {
646                 if (!is_zero_ethaddr(dev->enetaddr) &&
647                     memcmp(dev->enetaddr, env_enetaddr, 6)) {
648                         printf("\nWarning: %s MAC addresses don't match:\n",
649                                dev->name);
650                         printf("Address in SROM is         %pM\n",
651                                dev->enetaddr);
652                         printf("Address in environment is  %pM\n",
653                                env_enetaddr);
654                 }
655
656                 memcpy(dev->enetaddr, env_enetaddr, 6);
657         } else if (is_valid_ethaddr(dev->enetaddr)) {
658                 eth_setenv_enetaddr_by_index(base_name, eth_number,
659                                              dev->enetaddr);
660                 printf("\nWarning: %s using MAC address from net device\n",
661                        dev->name);
662         } else if (is_zero_ethaddr(dev->enetaddr)) {
663 #ifdef CONFIG_NET_RANDOM_ETHADDR
664                 net_random_ethaddr(dev->enetaddr);
665                 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
666                        dev->name, eth_number, dev->enetaddr);
667 #else
668                 printf("\nError: %s address not set.\n",
669                        dev->name);
670                 return -EINVAL;
671 #endif
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 }