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