1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
8 #define LOG_CATEGORY UCLASS_ETH
12 #include <bootstage.h>
18 #include <asm/global_data.h>
19 #include <dm/device-internal.h>
20 #include <dm/uclass-internal.h>
22 #include "eth_internal.h"
25 DECLARE_GLOBAL_DATA_PTR;
28 * struct eth_device_priv - private structure for each Ethernet device
30 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
32 struct eth_device_priv {
33 enum eth_state_t state;
38 * struct eth_uclass_priv - The structure attached to the uclass itself
40 * @current: The Ethernet device that the network functions are using
42 struct eth_uclass_priv {
43 struct udevice *current;
46 /* eth_errno - This stores the most recent failure code from DM functions */
49 static struct eth_uclass_priv *eth_get_uclass_priv(void)
54 ret = uclass_get(UCLASS_ETH, &uc);
59 return uclass_get_priv(uc);
62 void eth_set_current_to_next(void)
64 struct eth_uclass_priv *uc_priv;
66 uc_priv = eth_get_uclass_priv();
68 uclass_next_device(&uc_priv->current);
69 if (!uc_priv->current)
70 uclass_first_device(UCLASS_ETH, &uc_priv->current);
74 * Typically this will simply return the active device.
75 * In the case where the most recent active device was unset, this will attempt
76 * to return the device with sequence id 0 (which can be configured by the
77 * device tree). If this fails, fall back to just getting the first device.
78 * The latter is non-deterministic and depends on the order of the probing.
79 * If that device doesn't exist or fails to probe, this function will return
82 struct udevice *eth_get_dev(void)
84 struct eth_uclass_priv *uc_priv;
86 uc_priv = eth_get_uclass_priv();
90 if (!uc_priv->current) {
91 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
94 eth_errno = uclass_first_device_err(UCLASS_ETH,
97 uc_priv->current = NULL;
99 return uc_priv->current;
103 * Typically this will just store a device pointer.
104 * In case it was not probed, we will attempt to do so.
105 * dev may be NULL to unset the active device.
107 void eth_set_dev(struct udevice *dev)
109 if (dev && !device_active(dev)) {
110 eth_errno = device_probe(dev);
115 eth_get_uclass_priv()->current = dev;
119 * Find the udevice that either has the name passed in as devname or has an
120 * alias named devname.
122 struct udevice *eth_get_dev_by_name(const char *devname)
126 const char *startp = NULL;
129 int len = strlen("eth");
132 /* Must be longer than 3 to be an alias */
133 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
134 startp = devname + len;
135 seq = dectoul(startp, &endp);
138 ret = uclass_get(UCLASS_ETH, &uc);
142 uclass_foreach_dev(it, uc) {
144 * We don't care about errors from probe here. Either they won't
145 * match an alias or it will match a literal name and we'll pick
146 * up the error when we try to probe again in eth_set_dev().
148 if (device_probe(it))
150 /* Check for the name or the sequence number to match */
151 if (strcmp(it->name, devname) == 0 ||
152 (endp > startp && dev_seq(it) == seq))
159 unsigned char *eth_get_ethaddr(void)
161 struct eth_pdata *pdata;
164 pdata = dev_get_plat(eth_get_dev());
165 return pdata->enetaddr;
171 /* Set active state without calling start on the driver */
172 int eth_init_state_only(void)
174 struct udevice *current;
175 struct eth_device_priv *priv;
177 current = eth_get_dev();
178 if (!current || !device_active(current))
181 priv = dev_get_uclass_priv(current);
182 priv->state = ETH_STATE_ACTIVE;
187 /* Set passive state without calling stop on the driver */
188 void eth_halt_state_only(void)
190 struct udevice *current;
191 struct eth_device_priv *priv;
193 current = eth_get_dev();
194 if (!current || !device_active(current))
197 priv = dev_get_uclass_priv(current);
198 priv->state = ETH_STATE_PASSIVE;
201 int eth_get_dev_index(void)
204 return dev_seq(eth_get_dev());
208 static int eth_write_hwaddr(struct udevice *dev)
210 struct eth_pdata *pdata;
213 if (!dev || !device_active(dev))
216 /* seq is valid since the device is active */
217 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
218 pdata = dev_get_plat(dev);
219 if (!is_valid_ethaddr(pdata->enetaddr)) {
220 printf("\nError: %s address %pM illegal value\n",
221 dev->name, pdata->enetaddr);
226 * Drivers are allowed to decide not to implement this at
227 * run-time. E.g. Some devices may use it and some may not.
229 ret = eth_get_ops(dev)->write_hwaddr(dev);
233 printf("\nWarning: %s failed to set MAC address\n",
240 static int on_ethaddr(const char *name, const char *value, enum env_op op,
247 /* look for an index after "eth" */
248 index = dectoul(name + 3, NULL);
250 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
252 struct eth_pdata *pdata = dev_get_plat(dev);
255 case env_op_overwrite:
256 string_to_enetaddr(value, pdata->enetaddr);
257 eth_write_hwaddr(dev);
260 memset(pdata->enetaddr, 0, ARP_HLEN);
266 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
270 char *ethact = env_get("ethact");
271 char *ethrotate = env_get("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 log_err("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 dev_get_uclass_priv(current);
307 priv->state = ETH_STATE_ACTIVE;
308 priv->running = true;
317 debug("PROBE FAIL\n");
321 * If ethrotate is enabled, this will change "current",
322 * otherwise we will drop out of this while loop immediately
325 /* This will ensure the new "current" attempted to probe */
326 current = eth_get_dev();
327 } while (old_current != current);
334 struct udevice *current;
335 struct eth_device_priv *priv;
337 current = eth_get_dev();
341 priv = dev_get_uclass_priv(current);
342 if (!priv || !priv->running)
345 eth_get_ops(current)->stop(current);
346 priv->state = ETH_STATE_PASSIVE;
347 priv->running = false;
350 int eth_is_active(struct udevice *dev)
352 struct eth_device_priv *priv;
354 if (!dev || !device_active(dev))
357 priv = dev_get_uclass_priv(dev);
358 return priv->state == ETH_STATE_ACTIVE;
361 int eth_send(void *packet, int length)
363 struct udevice *current;
366 current = eth_get_dev();
370 if (!eth_is_active(current))
373 ret = eth_get_ops(current)->send(current, packet, length);
375 /* We cannot completely return the error at present */
376 debug("%s: send() returned error %d\n", __func__, ret);
378 #if defined(CONFIG_CMD_PCAP)
380 pcap_post(packet, length, true);
387 struct udevice *current;
393 current = eth_get_dev();
397 if (!eth_is_active(current))
400 /* Process up to 32 packets at one time */
401 flags = ETH_RECV_CHECK_DEVICE;
402 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
403 ret = eth_get_ops(current)->recv(current, flags, &packet);
406 net_process_received_packet(packet, ret);
407 if (ret >= 0 && eth_get_ops(current)->free_pkt)
408 eth_get_ops(current)->free_pkt(current, packet, ret);
415 /* We cannot completely return the error at present */
416 debug("%s: recv() returned error %d\n", __func__, ret);
421 int eth_initialize(void)
429 * Devices need to write the hwaddr even if not started so that Linux
430 * will have access to the hwaddr that u-boot stored for the device.
431 * This is accomplished by attempting to probe each device and calling
432 * their write_hwaddr() operation.
434 uclass_first_device_check(UCLASS_ETH, &dev);
436 log_err("No ethernet found.\n");
437 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
439 char *ethprime = env_get("ethprime");
440 struct udevice *prime_dev = NULL;
443 prime_dev = eth_get_dev_by_name(ethprime);
445 eth_set_dev(prime_dev);
446 eth_current_changed();
451 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
453 if (device_active(dev)) {
457 printf("eth%d: %s", dev_seq(dev), dev->name);
459 if (ethprime && dev == prime_dev)
463 eth_write_hwaddr(dev);
465 if (device_active(dev))
467 uclass_next_device_check(&dev);
471 log_err("No ethernet found.\n");
478 static int eth_post_bind(struct udevice *dev)
482 if (strchr(dev->name, ' ')) {
483 printf("\nError: eth device name \"%s\" has a space!\n",
488 #ifdef CONFIG_DM_ETH_PHY
489 eth_phy_binds_nodes(dev);
491 if (CONFIG_IS_ENABLED(BOOTDEV_ETH)) {
492 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
494 return log_msg_ret("bootdev", ret);
500 static int eth_pre_unbind(struct udevice *dev)
502 /* Don't hang onto a pointer that is going away */
503 if (dev == eth_get_uclass_priv()->current)
509 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
511 #if CONFIG_IS_ENABLED(OF_CONTROL)
513 struct nvmem_cell mac_cell;
515 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
517 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
520 memcpy(mac, p, ARP_HLEN);
524 if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
527 return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
533 static int eth_post_probe(struct udevice *dev)
535 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
536 struct eth_pdata *pdata = dev_get_plat(dev);
537 unsigned char env_enetaddr[ARP_HLEN];
540 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
541 struct eth_ops *ops = eth_get_ops(dev);
542 static int reloc_done;
546 ops->start += gd->reloc_off;
548 ops->send += gd->reloc_off;
550 ops->recv += gd->reloc_off;
552 ops->free_pkt += gd->reloc_off;
554 ops->stop += gd->reloc_off;
556 ops->mcast += gd->reloc_off;
557 if (ops->write_hwaddr)
558 ops->write_hwaddr += gd->reloc_off;
559 if (ops->read_rom_hwaddr)
560 ops->read_rom_hwaddr += gd->reloc_off;
566 priv->state = ETH_STATE_INIT;
567 priv->running = false;
569 /* Check if the device has a valid MAC address in device tree */
570 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
571 !is_valid_ethaddr(pdata->enetaddr)) {
573 /* Check if the device has a MAC address in ROM */
574 if (eth_get_ops(dev)->read_rom_hwaddr)
575 eth_get_ops(dev)->read_rom_hwaddr(dev);
578 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
579 if (!is_zero_ethaddr(env_enetaddr)) {
580 if (!is_zero_ethaddr(pdata->enetaddr) &&
581 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
582 printf("\nWarning: %s MAC addresses don't match:\n",
584 printf("Address in %s is\t\t%pM\n",
585 source, pdata->enetaddr);
586 printf("Address in environment is\t%pM\n",
590 /* Override the ROM MAC address */
591 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
592 } else if (is_valid_ethaddr(pdata->enetaddr)) {
593 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
595 } else if (is_zero_ethaddr(pdata->enetaddr) ||
596 !is_valid_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(dev), pdata->enetaddr);
601 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
604 printf("\nError: %s address not set.\n",
610 eth_write_hwaddr(dev);
615 static int eth_pre_remove(struct udevice *dev)
617 struct eth_pdata *pdata = dev_get_plat(dev);
619 eth_get_ops(dev)->stop(dev);
621 /* clear the MAC address */
622 memset(pdata->enetaddr, 0, ARP_HLEN);
627 UCLASS_DRIVER(ethernet) = {
630 .post_bind = eth_post_bind,
631 .pre_unbind = eth_pre_unbind,
632 .post_probe = eth_post_probe,
633 .pre_remove = eth_pre_remove,
634 .priv_auto = sizeof(struct eth_uclass_priv),
635 .per_device_auto = sizeof(struct eth_device_priv),
636 .flags = DM_UC_FLAG_SEQ_ALIAS,