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