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