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
14 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
17 #include "eth_internal.h"
20 DECLARE_GLOBAL_DATA_PTR;
23 * struct eth_device_priv - private structure for each Ethernet device
25 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
27 struct eth_device_priv {
28 enum eth_state_t state;
32 * struct eth_uclass_priv - The structure attached to the uclass itself
34 * @current: The Ethernet device that the network functions are using
36 struct eth_uclass_priv {
37 struct udevice *current;
40 /* eth_errno - This stores the most recent failure code from DM functions */
43 static struct eth_uclass_priv *eth_get_uclass_priv(void)
48 ret = uclass_get(UCLASS_ETH, &uc);
56 void eth_set_current_to_next(void)
58 struct eth_uclass_priv *uc_priv;
60 uc_priv = eth_get_uclass_priv();
62 uclass_next_device(&uc_priv->current);
63 if (!uc_priv->current)
64 uclass_first_device(UCLASS_ETH, &uc_priv->current);
68 * Typically this will simply return the active device.
69 * In the case where the most recent active device was unset, this will attempt
70 * to return the first device. If that device doesn't exist or fails to probe,
71 * this function will return NULL.
73 struct udevice *eth_get_dev(void)
75 struct eth_uclass_priv *uc_priv;
77 uc_priv = eth_get_uclass_priv();
81 if (!uc_priv->current)
82 eth_errno = uclass_first_device(UCLASS_ETH,
84 return uc_priv->current;
88 * Typically this will just store a device pointer.
89 * In case it was not probed, we will attempt to do so.
90 * dev may be NULL to unset the active device.
92 void eth_set_dev(struct udevice *dev)
94 if (dev && !device_active(dev)) {
95 eth_errno = device_probe(dev);
100 eth_get_uclass_priv()->current = dev;
104 * Find the udevice that either has the name passed in as devname or has an
105 * alias named devname.
107 struct udevice *eth_get_dev_by_name(const char *devname)
111 const char *startp = NULL;
114 int len = strlen("eth");
117 /* Must be longer than 3 to be an alias */
118 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
119 startp = devname + len;
120 seq = simple_strtoul(startp, &endp, 10);
123 ret = uclass_get(UCLASS_ETH, &uc);
127 uclass_foreach_dev(it, uc) {
129 * We need the seq to be valid, so try to probe it.
130 * If the probe fails, the seq will not match since it will be
131 * -1 instead of what we are looking for.
132 * We don't care about errors from probe here. Either they won't
133 * match an alias or it will match a literal name and we'll pick
134 * up the error when we try to probe again in eth_set_dev().
136 if (device_probe(it))
138 /* Check for the name or the sequence number to match */
139 if (strcmp(it->name, devname) == 0 ||
140 (endp > startp && it->seq == seq))
147 unsigned char *eth_get_ethaddr(void)
149 struct eth_pdata *pdata;
152 pdata = eth_get_dev()->platdata;
153 return pdata->enetaddr;
159 /* Set active state without calling start on the driver */
160 int eth_init_state_only(void)
162 struct udevice *current;
163 struct eth_device_priv *priv;
165 current = eth_get_dev();
166 if (!current || !device_active(current))
169 priv = current->uclass_priv;
170 priv->state = ETH_STATE_ACTIVE;
175 /* Set passive state without calling stop on the driver */
176 void eth_halt_state_only(void)
178 struct udevice *current;
179 struct eth_device_priv *priv;
181 current = eth_get_dev();
182 if (!current || !device_active(current))
185 priv = current->uclass_priv;
186 priv->state = ETH_STATE_PASSIVE;
189 int eth_get_dev_index(void)
192 return eth_get_dev()->seq;
196 static int eth_write_hwaddr(struct udevice *dev)
198 struct eth_pdata *pdata;
201 if (!dev || !device_active(dev))
204 /* seq is valid since the device is active */
205 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
206 pdata = dev->platdata;
207 if (!is_valid_ethaddr(pdata->enetaddr)) {
208 printf("\nError: %s address %pM illegal value\n",
209 dev->name, pdata->enetaddr);
214 * Drivers are allowed to decide not to implement this at
215 * run-time. E.g. Some devices may use it and some may not.
217 ret = eth_get_ops(dev)->write_hwaddr(dev);
221 printf("\nWarning: %s failed to set MAC address\n",
228 static int on_ethaddr(const char *name, const char *value, enum env_op op,
235 /* look for an index after "eth" */
236 index = simple_strtoul(name + 3, NULL, 10);
238 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
240 struct eth_pdata *pdata = dev->platdata;
243 case env_op_overwrite:
244 string_to_enetaddr(value, pdata->enetaddr);
245 eth_write_hwaddr(dev);
248 memset(pdata->enetaddr, 0, ARP_HLEN);
254 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
258 char *ethact = env_get("ethact");
259 char *ethrotate = env_get("ethrotate");
260 struct udevice *current = NULL;
261 struct udevice *old_current;
265 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
266 * is already set to an ethernet device, we should stick to 'ethact'.
268 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
270 current = eth_get_dev_by_name(ethact);
277 current = eth_get_dev();
279 log_err("No ethernet found.\n");
284 old_current = current;
287 debug("Trying %s\n", current->name);
289 if (device_active(current)) {
290 ret = eth_get_ops(current)->start(current);
292 struct eth_device_priv *priv =
293 current->uclass_priv;
295 priv->state = ETH_STATE_ACTIVE;
304 debug("PROBE FAIL\n");
308 * If ethrotate is enabled, this will change "current",
309 * otherwise we will drop out of this while loop immediately
312 /* This will ensure the new "current" attempted to probe */
313 current = eth_get_dev();
314 } while (old_current != current);
321 struct udevice *current;
322 struct eth_device_priv *priv;
324 current = eth_get_dev();
325 if (!current || !eth_is_active(current))
328 eth_get_ops(current)->stop(current);
329 priv = current->uclass_priv;
331 priv->state = ETH_STATE_PASSIVE;
334 int eth_is_active(struct udevice *dev)
336 struct eth_device_priv *priv;
338 if (!dev || !device_active(dev))
341 priv = dev_get_uclass_priv(dev);
342 return priv->state == ETH_STATE_ACTIVE;
345 int eth_send(void *packet, int length)
347 struct udevice *current;
350 current = eth_get_dev();
354 if (!eth_is_active(current))
357 ret = eth_get_ops(current)->send(current, packet, length);
359 /* We cannot completely return the error at present */
360 debug("%s: send() returned error %d\n", __func__, ret);
362 #if defined(CONFIG_CMD_PCAP)
364 pcap_post(packet, length, true);
371 struct udevice *current;
377 current = eth_get_dev();
381 if (!eth_is_active(current))
384 /* Process up to 32 packets at one time */
385 flags = ETH_RECV_CHECK_DEVICE;
386 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
387 ret = eth_get_ops(current)->recv(current, flags, &packet);
390 net_process_received_packet(packet, ret);
391 if (ret >= 0 && eth_get_ops(current)->free_pkt)
392 eth_get_ops(current)->free_pkt(current, packet, ret);
399 /* We cannot completely return the error at present */
400 debug("%s: recv() returned error %d\n", __func__, ret);
405 int eth_initialize(void)
413 * Devices need to write the hwaddr even if not started so that Linux
414 * will have access to the hwaddr that u-boot stored for the device.
415 * This is accomplished by attempting to probe each device and calling
416 * their write_hwaddr() operation.
418 uclass_first_device_check(UCLASS_ETH, &dev);
420 log_err("No ethernet found.\n");
421 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
423 char *ethprime = env_get("ethprime");
424 struct udevice *prime_dev = NULL;
427 prime_dev = eth_get_dev_by_name(ethprime);
429 eth_set_dev(prime_dev);
430 eth_current_changed();
435 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
437 if (dev->seq != -1) {
441 printf("eth%d: %s", dev->seq, dev->name);
443 if (ethprime && dev == prime_dev)
447 eth_write_hwaddr(dev);
451 uclass_next_device_check(&dev);
455 log_err("No ethernet found.\n");
462 static int eth_post_bind(struct udevice *dev)
464 if (strchr(dev->name, ' ')) {
465 printf("\nError: eth device name \"%s\" has a space!\n",
470 #ifdef CONFIG_DM_ETH_PHY
471 eth_phy_binds_nodes(dev);
477 static int eth_pre_unbind(struct udevice *dev)
479 /* Don't hang onto a pointer that is going away */
480 if (dev == eth_get_uclass_priv()->current)
486 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
488 #if IS_ENABLED(CONFIG_OF_CONTROL)
491 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
493 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
498 memcpy(mac, p, ARP_HLEN);
506 static int eth_post_probe(struct udevice *dev)
508 struct eth_device_priv *priv = dev->uclass_priv;
509 struct eth_pdata *pdata = dev->platdata;
510 unsigned char env_enetaddr[ARP_HLEN];
513 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
514 struct eth_ops *ops = eth_get_ops(dev);
515 static int reloc_done;
519 ops->start += gd->reloc_off;
521 ops->send += gd->reloc_off;
523 ops->recv += gd->reloc_off;
525 ops->free_pkt += gd->reloc_off;
527 ops->stop += gd->reloc_off;
529 ops->mcast += gd->reloc_off;
530 if (ops->write_hwaddr)
531 ops->write_hwaddr += gd->reloc_off;
532 if (ops->read_rom_hwaddr)
533 ops->read_rom_hwaddr += gd->reloc_off;
539 priv->state = ETH_STATE_INIT;
541 /* Check if the device has a valid MAC address in device tree */
542 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
543 !is_valid_ethaddr(pdata->enetaddr)) {
545 /* Check if the device has a MAC address in ROM */
546 if (eth_get_ops(dev)->read_rom_hwaddr)
547 eth_get_ops(dev)->read_rom_hwaddr(dev);
550 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
551 if (!is_zero_ethaddr(env_enetaddr)) {
552 if (!is_zero_ethaddr(pdata->enetaddr) &&
553 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
554 printf("\nWarning: %s MAC addresses don't match:\n",
556 printf("Address in %s is\t\t%pM\n",
557 source, pdata->enetaddr);
558 printf("Address in environment is\t%pM\n",
562 /* Override the ROM MAC address */
563 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
564 } else if (is_valid_ethaddr(pdata->enetaddr)) {
565 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
566 } else if (is_zero_ethaddr(pdata->enetaddr) ||
567 !is_valid_ethaddr(pdata->enetaddr)) {
568 #ifdef CONFIG_NET_RANDOM_ETHADDR
569 net_random_ethaddr(pdata->enetaddr);
570 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
571 dev->name, dev->seq, pdata->enetaddr);
573 printf("\nError: %s address not set.\n",
579 eth_write_hwaddr(dev);
584 static int eth_pre_remove(struct udevice *dev)
586 struct eth_pdata *pdata = dev->platdata;
588 eth_get_ops(dev)->stop(dev);
590 /* clear the MAC address */
591 memset(pdata->enetaddr, 0, ARP_HLEN);
596 UCLASS_DRIVER(eth) = {
599 .post_bind = eth_post_bind,
600 .pre_unbind = eth_pre_unbind,
601 .post_probe = eth_post_probe,
602 .pre_remove = eth_pre_remove,
603 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
604 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
605 .flags = DM_UC_FLAG_SEQ_ALIAS,