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
11 #include <bootstage.h>
16 #include <asm/global_data.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
20 #include "eth_internal.h"
23 DECLARE_GLOBAL_DATA_PTR;
26 * struct eth_device_priv - private structure for each Ethernet device
28 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
30 struct eth_device_priv {
31 enum eth_state_t state;
36 * struct eth_uclass_priv - The structure attached to the uclass itself
38 * @current: The Ethernet device that the network functions are using
40 struct eth_uclass_priv {
41 struct udevice *current;
44 /* eth_errno - This stores the most recent failure code from DM functions */
47 static struct eth_uclass_priv *eth_get_uclass_priv(void)
52 ret = uclass_get(UCLASS_ETH, &uc);
57 return uclass_get_priv(uc);
60 void eth_set_current_to_next(void)
62 struct eth_uclass_priv *uc_priv;
64 uc_priv = eth_get_uclass_priv();
66 uclass_next_device(&uc_priv->current);
67 if (!uc_priv->current)
68 uclass_first_device(UCLASS_ETH, &uc_priv->current);
72 * Typically this will simply return the active device.
73 * In the case where the most recent active device was unset, this will attempt
74 * to return the device with sequence id 0 (which can be configured by the
75 * device tree). If this fails, fall back to just getting the first device.
76 * The latter is non-deterministic and depends on the order of the probing.
77 * If that device doesn't exist or fails to probe, this function will return
80 struct udevice *eth_get_dev(void)
82 struct eth_uclass_priv *uc_priv;
84 uc_priv = eth_get_uclass_priv();
88 if (!uc_priv->current) {
89 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
92 eth_errno = uclass_first_device(UCLASS_ETH,
95 return uc_priv->current;
99 * Typically this will just store a device pointer.
100 * In case it was not probed, we will attempt to do so.
101 * dev may be NULL to unset the active device.
103 void eth_set_dev(struct udevice *dev)
105 if (dev && !device_active(dev)) {
106 eth_errno = device_probe(dev);
111 eth_get_uclass_priv()->current = dev;
115 * Find the udevice that either has the name passed in as devname or has an
116 * alias named devname.
118 struct udevice *eth_get_dev_by_name(const char *devname)
122 const char *startp = NULL;
125 int len = strlen("eth");
128 /* Must be longer than 3 to be an alias */
129 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
130 startp = devname + len;
131 seq = dectoul(startp, &endp);
134 ret = uclass_get(UCLASS_ETH, &uc);
138 uclass_foreach_dev(it, uc) {
140 * We don't care about errors from probe here. Either they won't
141 * match an alias or it will match a literal name and we'll pick
142 * up the error when we try to probe again in eth_set_dev().
144 if (device_probe(it))
146 /* Check for the name or the sequence number to match */
147 if (strcmp(it->name, devname) == 0 ||
148 (endp > startp && dev_seq(it) == seq))
155 unsigned char *eth_get_ethaddr(void)
157 struct eth_pdata *pdata;
160 pdata = dev_get_plat(eth_get_dev());
161 return pdata->enetaddr;
167 /* Set active state without calling start on the driver */
168 int eth_init_state_only(void)
170 struct udevice *current;
171 struct eth_device_priv *priv;
173 current = eth_get_dev();
174 if (!current || !device_active(current))
177 priv = dev_get_uclass_priv(current);
178 priv->state = ETH_STATE_ACTIVE;
183 /* Set passive state without calling stop on the driver */
184 void eth_halt_state_only(void)
186 struct udevice *current;
187 struct eth_device_priv *priv;
189 current = eth_get_dev();
190 if (!current || !device_active(current))
193 priv = dev_get_uclass_priv(current);
194 priv->state = ETH_STATE_PASSIVE;
197 int eth_get_dev_index(void)
200 return dev_seq(eth_get_dev());
204 static int eth_write_hwaddr(struct udevice *dev)
206 struct eth_pdata *pdata;
209 if (!dev || !device_active(dev))
212 /* seq is valid since the device is active */
213 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
214 pdata = dev_get_plat(dev);
215 if (!is_valid_ethaddr(pdata->enetaddr)) {
216 printf("\nError: %s address %pM illegal value\n",
217 dev->name, pdata->enetaddr);
222 * Drivers are allowed to decide not to implement this at
223 * run-time. E.g. Some devices may use it and some may not.
225 ret = eth_get_ops(dev)->write_hwaddr(dev);
229 printf("\nWarning: %s failed to set MAC address\n",
236 static int on_ethaddr(const char *name, const char *value, enum env_op op,
243 /* look for an index after "eth" */
244 index = dectoul(name + 3, NULL);
246 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
248 struct eth_pdata *pdata = dev_get_plat(dev);
251 case env_op_overwrite:
252 string_to_enetaddr(value, pdata->enetaddr);
253 eth_write_hwaddr(dev);
256 memset(pdata->enetaddr, 0, ARP_HLEN);
262 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
266 char *ethact = env_get("ethact");
267 char *ethrotate = env_get("ethrotate");
268 struct udevice *current = NULL;
269 struct udevice *old_current;
273 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
274 * is already set to an ethernet device, we should stick to 'ethact'.
276 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
278 current = eth_get_dev_by_name(ethact);
285 current = eth_get_dev();
287 log_err("No ethernet found.\n");
292 old_current = current;
295 debug("Trying %s\n", current->name);
297 if (device_active(current)) {
298 ret = eth_get_ops(current)->start(current);
300 struct eth_device_priv *priv =
301 dev_get_uclass_priv(current);
303 priv->state = ETH_STATE_ACTIVE;
304 priv->running = true;
313 debug("PROBE FAIL\n");
317 * If ethrotate is enabled, this will change "current",
318 * otherwise we will drop out of this while loop immediately
321 /* This will ensure the new "current" attempted to probe */
322 current = eth_get_dev();
323 } while (old_current != current);
330 struct udevice *current;
331 struct eth_device_priv *priv;
333 current = eth_get_dev();
337 priv = dev_get_uclass_priv(current);
338 if (!priv || !priv->running)
341 eth_get_ops(current)->stop(current);
342 priv->state = ETH_STATE_PASSIVE;
343 priv->running = false;
346 int eth_is_active(struct udevice *dev)
348 struct eth_device_priv *priv;
350 if (!dev || !device_active(dev))
353 priv = dev_get_uclass_priv(dev);
354 return priv->state == ETH_STATE_ACTIVE;
357 int eth_send(void *packet, int length)
359 struct udevice *current;
362 current = eth_get_dev();
366 if (!eth_is_active(current))
369 ret = eth_get_ops(current)->send(current, packet, length);
371 /* We cannot completely return the error at present */
372 debug("%s: send() returned error %d\n", __func__, ret);
374 #if defined(CONFIG_CMD_PCAP)
376 pcap_post(packet, length, true);
383 struct udevice *current;
389 current = eth_get_dev();
393 if (!eth_is_active(current))
396 /* Process up to 32 packets at one time */
397 flags = ETH_RECV_CHECK_DEVICE;
398 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
399 ret = eth_get_ops(current)->recv(current, flags, &packet);
402 net_process_received_packet(packet, ret);
403 if (ret >= 0 && eth_get_ops(current)->free_pkt)
404 eth_get_ops(current)->free_pkt(current, packet, ret);
411 /* We cannot completely return the error at present */
412 debug("%s: recv() returned error %d\n", __func__, ret);
417 int eth_initialize(void)
425 * Devices need to write the hwaddr even if not started so that Linux
426 * will have access to the hwaddr that u-boot stored for the device.
427 * This is accomplished by attempting to probe each device and calling
428 * their write_hwaddr() operation.
430 uclass_first_device_check(UCLASS_ETH, &dev);
432 log_err("No ethernet found.\n");
433 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
435 char *ethprime = env_get("ethprime");
436 struct udevice *prime_dev = NULL;
439 prime_dev = eth_get_dev_by_name(ethprime);
441 eth_set_dev(prime_dev);
442 eth_current_changed();
447 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
449 if (device_active(dev)) {
453 printf("eth%d: %s", dev_seq(dev), dev->name);
455 if (ethprime && dev == prime_dev)
459 eth_write_hwaddr(dev);
461 if (device_active(dev))
463 uclass_next_device_check(&dev);
467 log_err("No ethernet found.\n");
474 static int eth_post_bind(struct udevice *dev)
476 if (strchr(dev->name, ' ')) {
477 printf("\nError: eth device name \"%s\" has a space!\n",
482 #ifdef CONFIG_DM_ETH_PHY
483 eth_phy_binds_nodes(dev);
489 static int eth_pre_unbind(struct udevice *dev)
491 /* Don't hang onto a pointer that is going away */
492 if (dev == eth_get_uclass_priv()->current)
498 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
500 #if CONFIG_IS_ENABLED(OF_CONTROL)
503 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
505 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
510 memcpy(mac, p, ARP_HLEN);
518 static int eth_post_probe(struct udevice *dev)
520 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
521 struct eth_pdata *pdata = dev_get_plat(dev);
522 unsigned char env_enetaddr[ARP_HLEN];
525 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
526 struct eth_ops *ops = eth_get_ops(dev);
527 static int reloc_done;
531 ops->start += gd->reloc_off;
533 ops->send += gd->reloc_off;
535 ops->recv += gd->reloc_off;
537 ops->free_pkt += gd->reloc_off;
539 ops->stop += gd->reloc_off;
541 ops->mcast += gd->reloc_off;
542 if (ops->write_hwaddr)
543 ops->write_hwaddr += gd->reloc_off;
544 if (ops->read_rom_hwaddr)
545 ops->read_rom_hwaddr += gd->reloc_off;
551 priv->state = ETH_STATE_INIT;
552 priv->running = false;
554 /* Check if the device has a valid MAC address in device tree */
555 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
556 !is_valid_ethaddr(pdata->enetaddr)) {
558 /* Check if the device has a MAC address in ROM */
559 if (eth_get_ops(dev)->read_rom_hwaddr)
560 eth_get_ops(dev)->read_rom_hwaddr(dev);
563 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
564 if (!is_zero_ethaddr(env_enetaddr)) {
565 if (!is_zero_ethaddr(pdata->enetaddr) &&
566 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
567 printf("\nWarning: %s MAC addresses don't match:\n",
569 printf("Address in %s is\t\t%pM\n",
570 source, pdata->enetaddr);
571 printf("Address in environment is\t%pM\n",
575 /* Override the ROM MAC address */
576 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
577 } else if (is_valid_ethaddr(pdata->enetaddr)) {
578 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
580 } else if (is_zero_ethaddr(pdata->enetaddr) ||
581 !is_valid_ethaddr(pdata->enetaddr)) {
582 #ifdef CONFIG_NET_RANDOM_ETHADDR
583 net_random_ethaddr(pdata->enetaddr);
584 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
585 dev->name, dev_seq(dev), pdata->enetaddr);
587 printf("\nError: %s address not set.\n",
593 eth_write_hwaddr(dev);
598 static int eth_pre_remove(struct udevice *dev)
600 struct eth_pdata *pdata = dev_get_plat(dev);
602 eth_get_ops(dev)->stop(dev);
604 /* clear the MAC address */
605 memset(pdata->enetaddr, 0, ARP_HLEN);
610 UCLASS_DRIVER(ethernet) = {
613 .post_bind = eth_post_bind,
614 .pre_unbind = eth_pre_unbind,
615 .post_probe = eth_post_probe,
616 .pre_remove = eth_pre_remove,
617 .priv_auto = sizeof(struct eth_uclass_priv),
618 .per_device_auto = sizeof(struct eth_device_priv),
619 .flags = DM_UC_FLAG_SEQ_ALIAS,