2 * (C) Copyright 2001-2015
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * Joe Hershberger, National Instruments
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
16 #include <asm/errno.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
19 #include "eth_internal.h"
21 DECLARE_GLOBAL_DATA_PTR;
23 static int eth_mac_skip(int index)
28 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
29 skip_state = getenv(enetvar);
30 return skip_state != NULL;
33 static void eth_current_changed(void);
36 * CPU and board-specific Ethernet initializations. Aliased function
37 * signals caller to move on
39 static int __def_eth_init(bd_t *bis)
43 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
44 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
48 * struct eth_device_priv - private structure for each Ethernet device
50 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
52 struct eth_device_priv {
53 enum eth_state_t state;
57 * struct eth_uclass_priv - The structure attached to the uclass itself
59 * @current: The Ethernet device that the network functions are using
61 struct eth_uclass_priv {
62 struct udevice *current;
65 /* eth_errno - This stores the most recent failure code from DM functions */
68 static struct eth_uclass_priv *eth_get_uclass_priv(void)
72 uclass_get(UCLASS_ETH, &uc);
77 static void eth_set_current_to_next(void)
79 struct eth_uclass_priv *uc_priv;
81 uc_priv = eth_get_uclass_priv();
83 uclass_next_device(&uc_priv->current);
84 if (!uc_priv->current)
85 uclass_first_device(UCLASS_ETH, &uc_priv->current);
89 * Typically this will simply return the active device.
90 * In the case where the most recent active device was unset, this will attempt
91 * to return the first device. If that device doesn't exist or fails to probe,
92 * this function will return NULL.
94 struct udevice *eth_get_dev(void)
96 struct eth_uclass_priv *uc_priv;
98 uc_priv = eth_get_uclass_priv();
99 if (!uc_priv->current)
100 eth_errno = uclass_first_device(UCLASS_ETH,
102 return uc_priv->current;
106 * Typically this will just store a device pointer.
107 * In case it was not probed, we will attempt to do so.
108 * dev may be NULL to unset the active device.
110 static void eth_set_dev(struct udevice *dev)
112 if (dev && !device_active(dev)) {
113 eth_errno = device_probe(dev);
118 eth_get_uclass_priv()->current = dev;
122 * Find the udevice that either has the name passed in as devname or has an
123 * alias named devname.
125 struct udevice *eth_get_dev_by_name(const char *devname)
129 const char *startp = NULL;
132 int len = strlen("eth");
134 /* Must be longer than 3 to be an alias */
135 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
136 startp = devname + len;
137 seq = simple_strtoul(startp, &endp, 10);
140 uclass_get(UCLASS_ETH, &uc);
141 uclass_foreach_dev(it, uc) {
143 * We need the seq to be valid, so try to probe it.
144 * If the probe fails, the seq will not match since it will be
145 * -1 instead of what we are looking for.
146 * We don't care about errors from probe here. Either they won't
147 * match an alias or it will match a literal name and we'll pick
148 * up the error when we try to probe again in eth_set_dev().
150 if (device_probe(it))
152 /* Check for the name or the sequence number to match */
153 if (strcmp(it->name, devname) == 0 ||
154 (endp > startp && it->seq == seq))
161 unsigned char *eth_get_ethaddr(void)
163 struct eth_pdata *pdata;
166 pdata = eth_get_dev()->platdata;
167 return pdata->enetaddr;
173 /* Set active state without calling start on the driver */
174 int eth_init_state_only(void)
176 struct udevice *current;
177 struct eth_device_priv *priv;
179 current = eth_get_dev();
180 if (!current || !device_active(current))
183 priv = current->uclass_priv;
184 priv->state = ETH_STATE_ACTIVE;
189 /* Set passive state without calling stop on the driver */
190 void eth_halt_state_only(void)
192 struct udevice *current;
193 struct eth_device_priv *priv;
195 current = eth_get_dev();
196 if (!current || !device_active(current))
199 priv = current->uclass_priv;
200 priv->state = ETH_STATE_PASSIVE;
203 int eth_get_dev_index(void)
206 return eth_get_dev()->seq;
210 static int eth_write_hwaddr(struct udevice *dev)
212 struct eth_pdata *pdata = dev->platdata;
215 if (!dev || !device_active(dev))
218 /* seq is valid since the device is active */
219 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
220 if (!is_valid_ethaddr(pdata->enetaddr)) {
221 printf("\nError: %s address %pM illegal value\n",
222 dev->name, pdata->enetaddr);
227 * Drivers are allowed to decide not to implement this at
228 * run-time. E.g. Some devices may use it and some may not.
230 ret = eth_get_ops(dev)->write_hwaddr(dev);
234 printf("\nWarning: %s failed to set MAC address\n",
241 static int on_ethaddr(const char *name, const char *value, enum env_op op,
248 /* look for an index after "eth" */
249 index = simple_strtoul(name + 3, NULL, 10);
251 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
253 struct eth_pdata *pdata = dev->platdata;
256 case env_op_overwrite:
257 eth_parse_enetaddr(value, pdata->enetaddr);
260 memset(pdata->enetaddr, 0, 6);
266 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
270 char *ethact = getenv("ethact");
271 char *ethrotate = getenv("ethrotate");
272 struct udevice *current = NULL;
273 struct udevice *old_current;
277 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
278 * is already set to an ethernet device, we should stick to 'ethact'.
280 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
282 current = eth_get_dev_by_name(ethact);
289 current = eth_get_dev();
291 printf("No ethernet found.\n");
296 old_current = current;
299 debug("Trying %s\n", current->name);
301 if (device_active(current)) {
302 ret = eth_get_ops(current)->start(current);
304 struct eth_device_priv *priv =
305 current->uclass_priv;
307 priv->state = ETH_STATE_ACTIVE;
316 debug("PROBE FAIL\n");
320 * If ethrotate is enabled, this will change "current",
321 * otherwise we will drop out of this while loop immediately
324 /* This will ensure the new "current" attempted to probe */
325 current = eth_get_dev();
326 } while (old_current != current);
333 struct udevice *current;
334 struct eth_device_priv *priv;
336 current = eth_get_dev();
337 if (!current || !device_active(current))
340 eth_get_ops(current)->stop(current);
341 priv = current->uclass_priv;
342 priv->state = ETH_STATE_PASSIVE;
345 int eth_is_active(struct udevice *dev)
347 struct eth_device_priv *priv;
349 if (!dev || !device_active(dev))
352 priv = dev_get_uclass_priv(dev);
353 return priv->state == ETH_STATE_ACTIVE;
356 int eth_send(void *packet, int length)
358 struct udevice *current;
361 current = eth_get_dev();
365 if (!device_active(current))
368 ret = eth_get_ops(current)->send(current, packet, length);
370 /* We cannot completely return the error at present */
371 debug("%s: send() returned error %d\n", __func__, ret);
378 struct udevice *current;
384 current = eth_get_dev();
388 if (!device_active(current))
391 /* Process up to 32 packets at one time */
392 flags = ETH_RECV_CHECK_DEVICE;
393 for (i = 0; i < 32; i++) {
394 ret = eth_get_ops(current)->recv(current, flags, &packet);
397 net_process_received_packet(packet, ret);
398 if (ret >= 0 && eth_get_ops(current)->free_pkt)
399 eth_get_ops(current)->free_pkt(current, packet, ret);
406 /* We cannot completely return the error at present */
407 debug("%s: recv() returned error %d\n", __func__, ret);
412 int eth_initialize(void)
420 * Devices need to write the hwaddr even if not started so that Linux
421 * will have access to the hwaddr that u-boot stored for the device.
422 * This is accomplished by attempting to probe each device and calling
423 * their write_hwaddr() operation.
425 uclass_first_device(UCLASS_ETH, &dev);
427 printf("No ethernet found.\n");
428 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
430 char *ethprime = getenv("ethprime");
431 struct udevice *prime_dev = NULL;
434 prime_dev = eth_get_dev_by_name(ethprime);
436 eth_set_dev(prime_dev);
437 eth_current_changed();
442 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
447 printf("eth%d: %s", dev->seq, dev->name);
449 if (ethprime && dev == prime_dev)
452 eth_write_hwaddr(dev);
454 uclass_next_device(&dev);
464 static int eth_post_bind(struct udevice *dev)
466 if (strchr(dev->name, ' ')) {
467 printf("\nError: eth device name \"%s\" has a space!\n",
475 static int eth_pre_unbind(struct udevice *dev)
477 /* Don't hang onto a pointer that is going away */
478 if (dev == eth_get_uclass_priv()->current)
484 static int eth_post_probe(struct udevice *dev)
486 struct eth_device_priv *priv = dev->uclass_priv;
487 struct eth_pdata *pdata = dev->platdata;
488 unsigned char env_enetaddr[6];
490 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
491 struct eth_ops *ops = eth_get_ops(dev);
492 static int reloc_done;
496 ops->start += gd->reloc_off;
498 ops->send += gd->reloc_off;
500 ops->recv += gd->reloc_off;
502 ops->free_pkt += gd->reloc_off;
504 ops->stop += gd->reloc_off;
505 #ifdef CONFIG_MCAST_TFTP
507 ops->mcast += gd->reloc_off;
509 if (ops->write_hwaddr)
510 ops->write_hwaddr += gd->reloc_off;
511 if (ops->read_rom_hwaddr)
512 ops->read_rom_hwaddr += gd->reloc_off;
518 priv->state = ETH_STATE_INIT;
520 /* Check if the device has a MAC address in ROM */
521 if (eth_get_ops(dev)->read_rom_hwaddr)
522 eth_get_ops(dev)->read_rom_hwaddr(dev);
524 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
525 if (!is_zero_ethaddr(env_enetaddr)) {
526 if (!is_zero_ethaddr(pdata->enetaddr) &&
527 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
528 printf("\nWarning: %s MAC addresses don't match:\n",
530 printf("Address in SROM is %pM\n",
532 printf("Address in environment is %pM\n",
536 /* Override the ROM MAC address */
537 memcpy(pdata->enetaddr, env_enetaddr, 6);
538 } else if (is_valid_ethaddr(pdata->enetaddr)) {
539 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
540 printf("\nWarning: %s using MAC address from ROM\n",
542 } else if (is_zero_ethaddr(pdata->enetaddr)) {
543 #ifdef CONFIG_NET_RANDOM_ETHADDR
544 net_random_ethaddr(pdata->enetaddr);
545 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
546 dev->name, dev->seq, pdata->enetaddr);
548 printf("\nError: %s address not set.\n",
557 static int eth_pre_remove(struct udevice *dev)
559 struct eth_pdata *pdata = dev->platdata;
561 eth_get_ops(dev)->stop(dev);
563 /* clear the MAC address */
564 memset(pdata->enetaddr, 0, 6);
569 UCLASS_DRIVER(eth) = {
572 .post_bind = eth_post_bind,
573 .pre_unbind = eth_pre_unbind,
574 .post_probe = eth_post_probe,
575 .pre_remove = eth_pre_remove,
576 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
577 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
578 .flags = DM_UC_FLAG_SEQ_ALIAS,
580 #endif /* #ifdef CONFIG_DM_ETH */
582 #ifndef CONFIG_DM_ETH
588 } eth_rcv_bufs[PKTBUFSRX];
590 static unsigned int eth_rcv_current, eth_rcv_last;
593 static struct eth_device *eth_devices;
594 struct eth_device *eth_current;
596 static void eth_set_current_to_next(void)
598 eth_current = eth_current->next;
601 static void eth_set_dev(struct eth_device *dev)
606 struct eth_device *eth_get_dev_by_name(const char *devname)
608 struct eth_device *dev, *target_dev;
610 BUG_ON(devname == NULL);
618 if (strcmp(devname, dev->name) == 0) {
623 } while (dev != eth_devices);
628 struct eth_device *eth_get_dev_by_index(int index)
630 struct eth_device *dev, *target_dev;
638 if (dev->index == index) {
643 } while (dev != eth_devices);
648 int eth_get_dev_index(void)
653 return eth_current->index;
656 static int on_ethaddr(const char *name, const char *value, enum env_op op,
660 struct eth_device *dev;
665 /* look for an index after "eth" */
666 index = simple_strtoul(name + 3, NULL, 10);
670 if (dev->index == index) {
673 case env_op_overwrite:
674 eth_parse_enetaddr(value, dev->enetaddr);
677 memset(dev->enetaddr, 0, 6);
681 } while (dev != eth_devices);
685 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
687 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
690 unsigned char env_enetaddr[6];
693 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
695 if (!is_zero_ethaddr(env_enetaddr)) {
696 if (!is_zero_ethaddr(dev->enetaddr) &&
697 memcmp(dev->enetaddr, env_enetaddr, 6)) {
698 printf("\nWarning: %s MAC addresses don't match:\n",
700 printf("Address in SROM is %pM\n",
702 printf("Address in environment is %pM\n",
706 memcpy(dev->enetaddr, env_enetaddr, 6);
707 } else if (is_valid_ethaddr(dev->enetaddr)) {
708 eth_setenv_enetaddr_by_index(base_name, eth_number,
710 } else if (is_zero_ethaddr(dev->enetaddr)) {
711 #ifdef CONFIG_NET_RANDOM_ETHADDR
712 net_random_ethaddr(dev->enetaddr);
713 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
714 dev->name, eth_number, dev->enetaddr);
716 printf("\nError: %s address not set.\n",
722 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
723 if (!is_valid_ethaddr(dev->enetaddr)) {
724 printf("\nError: %s address %pM illegal value\n",
725 dev->name, dev->enetaddr);
729 ret = dev->write_hwaddr(dev);
731 printf("\nWarning: %s failed to set MAC address\n",
738 int eth_register(struct eth_device *dev)
740 struct eth_device *d;
743 assert(strlen(dev->name) < sizeof(dev->name));
748 eth_current_changed();
750 for (d = eth_devices; d->next != eth_devices; d = d->next)
755 dev->state = ETH_STATE_INIT;
756 dev->next = eth_devices;
757 dev->index = index++;
762 int eth_unregister(struct eth_device *dev)
764 struct eth_device *cur;
770 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
774 /* Device not found */
775 if (cur->next != dev)
778 cur->next = dev->next;
780 if (eth_devices == dev)
781 eth_devices = dev->next == eth_devices ? NULL : dev->next;
783 if (eth_current == dev) {
784 eth_current = eth_devices;
785 eth_current_changed();
791 int eth_initialize(void)
799 * If board-specific initialization exists, call it.
800 * If not, call a CPU-specific one
802 if (board_eth_init != __def_eth_init) {
803 if (board_eth_init(gd->bd) < 0)
804 printf("Board Net Initialization Failed\n");
805 } else if (cpu_eth_init != __def_eth_init) {
806 if (cpu_eth_init(gd->bd) < 0)
807 printf("CPU Net Initialization Failed\n");
809 printf("Net Initialization Skipped\n");
813 puts("No ethernet found.\n");
814 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
816 struct eth_device *dev = eth_devices;
817 char *ethprime = getenv("ethprime");
819 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
824 printf("%s", dev->name);
826 if (ethprime && strcmp(dev->name, ethprime) == 0) {
831 if (strchr(dev->name, ' '))
832 puts("\nWarning: eth device name has a space!"
835 eth_write_hwaddr(dev, "eth", dev->index);
839 } while (dev != eth_devices);
841 eth_current_changed();
848 #ifdef CONFIG_MCAST_TFTP
850 * mcast_addr: multicast ipaddr from which multicast Mac is made
851 * join: 1=join, 0=leave.
853 int eth_mcast_join(struct in_addr mcast_ip, int join)
856 if (!eth_current || !eth_current->mcast)
858 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
859 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
860 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
864 return eth_current->mcast(eth_current, mcast_mac, join);
867 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
868 * and this is the ethernet-crc method needed for TSEC -- and perhaps
869 * some other adapter -- hash tables
871 #define CRCPOLY_LE 0xedb88320
872 u32 ether_crc(size_t len, unsigned char const *p)
879 for (i = 0; i < 8; i++)
880 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
882 /* an reverse the bits, cuz of way they arrive -- last-first */
883 crc = (crc >> 16) | (crc << 16);
884 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
885 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
886 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
887 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
896 struct eth_device *old_current;
899 puts("No ethernet found.\n");
903 old_current = eth_current;
905 debug("Trying %s\n", eth_current->name);
907 if (eth_current->init(eth_current, gd->bd) >= 0) {
908 eth_current->state = ETH_STATE_ACTIVE;
915 } while (old_current != eth_current);
925 eth_current->halt(eth_current);
927 eth_current->state = ETH_STATE_PASSIVE;
930 int eth_is_active(struct eth_device *dev)
932 return dev && dev->state == ETH_STATE_ACTIVE;
935 int eth_send(void *packet, int length)
940 return eth_current->send(eth_current, packet, length);
948 return eth_current->recv(eth_current);
950 #endif /* ifndef CONFIG_DM_ETH */
953 static void eth_save_packet(void *packet, int length)
958 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
961 if (PKTSIZE < length)
964 for (i = 0; i < length; i++)
965 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
967 eth_rcv_bufs[eth_rcv_last].length = length;
968 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
971 int eth_receive(void *packet, int length)
974 void *pp = push_packet;
977 if (eth_rcv_current == eth_rcv_last) {
978 push_packet = eth_save_packet;
982 if (eth_rcv_current == eth_rcv_last)
986 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
988 for (i = 0; i < length; i++)
989 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
991 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
994 #endif /* CONFIG_API */
996 static void eth_current_changed(void)
998 char *act = getenv("ethact");
1002 * The call to eth_get_dev() below has a side effect of rotating
1003 * ethernet device if uc_priv->current == NULL. This is not what
1004 * we want when 'ethrotate' variable is 'no'.
1006 ethrotate = getenv("ethrotate");
1007 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
1010 /* update current ethernet name */
1011 if (eth_get_dev()) {
1012 if (act == NULL || strcmp(act, eth_get_name()) != 0)
1013 setenv("ethact", eth_get_name());
1016 * remove the variable completely if there is no active
1019 else if (act != NULL)
1020 setenv("ethact", NULL);
1023 void eth_try_another(int first_restart)
1025 static void *first_failed;
1029 * Do not rotate between network interfaces when
1030 * 'ethrotate' variable is set to 'no'.
1032 ethrotate = getenv("ethrotate");
1033 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
1040 first_failed = eth_get_dev();
1042 eth_set_current_to_next();
1044 eth_current_changed();
1046 if (first_failed == eth_get_dev())
1047 net_restart_wrap = 1;
1050 void eth_set_current(void)
1053 static int env_changed_id;
1056 env_id = get_env_id();
1057 if ((act == NULL) || (env_changed_id != env_id)) {
1058 act = getenv("ethact");
1059 env_changed_id = env_id;
1063 char *ethprime = getenv("ethprime");
1067 dev = eth_get_dev_by_name(ethprime);
1073 eth_set_dev(eth_get_dev_by_name(act));
1076 eth_current_changed();
1079 const char *eth_get_name(void)
1081 return eth_get_dev() ? eth_get_dev()->name : "unknown";