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