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