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 don't care about errors from probe here. Either they won't
130 * match an alias or it will match a literal name and we'll pick
131 * up the error when we try to probe again in eth_set_dev().
133 if (device_probe(it))
135 /* Check for the name or the sequence number to match */
136 if (strcmp(it->name, devname) == 0 ||
137 (endp > startp && dev_seq(it) == seq))
144 unsigned char *eth_get_ethaddr(void)
146 struct eth_pdata *pdata;
149 pdata = eth_get_dev()->plat;
150 return pdata->enetaddr;
156 /* Set active state without calling start on the driver */
157 int eth_init_state_only(void)
159 struct udevice *current;
160 struct eth_device_priv *priv;
162 current = eth_get_dev();
163 if (!current || !device_active(current))
166 priv = current->uclass_priv;
167 priv->state = ETH_STATE_ACTIVE;
172 /* Set passive state without calling stop on the driver */
173 void eth_halt_state_only(void)
175 struct udevice *current;
176 struct eth_device_priv *priv;
178 current = eth_get_dev();
179 if (!current || !device_active(current))
182 priv = current->uclass_priv;
183 priv->state = ETH_STATE_PASSIVE;
186 int eth_get_dev_index(void)
189 return dev_seq(eth_get_dev());
193 static int eth_write_hwaddr(struct udevice *dev)
195 struct eth_pdata *pdata;
198 if (!dev || !device_active(dev))
201 /* seq is valid since the device is active */
202 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
204 if (!is_valid_ethaddr(pdata->enetaddr)) {
205 printf("\nError: %s address %pM illegal value\n",
206 dev->name, pdata->enetaddr);
211 * Drivers are allowed to decide not to implement this at
212 * run-time. E.g. Some devices may use it and some may not.
214 ret = eth_get_ops(dev)->write_hwaddr(dev);
218 printf("\nWarning: %s failed to set MAC address\n",
225 static int on_ethaddr(const char *name, const char *value, enum env_op op,
232 /* look for an index after "eth" */
233 index = simple_strtoul(name + 3, NULL, 10);
235 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
237 struct eth_pdata *pdata = dev->plat;
240 case env_op_overwrite:
241 string_to_enetaddr(value, pdata->enetaddr);
242 eth_write_hwaddr(dev);
245 memset(pdata->enetaddr, 0, ARP_HLEN);
251 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
255 char *ethact = env_get("ethact");
256 char *ethrotate = env_get("ethrotate");
257 struct udevice *current = NULL;
258 struct udevice *old_current;
262 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
263 * is already set to an ethernet device, we should stick to 'ethact'.
265 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
267 current = eth_get_dev_by_name(ethact);
274 current = eth_get_dev();
276 log_err("No ethernet found.\n");
281 old_current = current;
284 debug("Trying %s\n", current->name);
286 if (device_active(current)) {
287 ret = eth_get_ops(current)->start(current);
289 struct eth_device_priv *priv =
290 current->uclass_priv;
292 priv->state = ETH_STATE_ACTIVE;
301 debug("PROBE FAIL\n");
305 * If ethrotate is enabled, this will change "current",
306 * otherwise we will drop out of this while loop immediately
309 /* This will ensure the new "current" attempted to probe */
310 current = eth_get_dev();
311 } while (old_current != current);
318 struct udevice *current;
319 struct eth_device_priv *priv;
321 current = eth_get_dev();
322 if (!current || !eth_is_active(current))
325 eth_get_ops(current)->stop(current);
326 priv = current->uclass_priv;
328 priv->state = ETH_STATE_PASSIVE;
331 int eth_is_active(struct udevice *dev)
333 struct eth_device_priv *priv;
335 if (!dev || !device_active(dev))
338 priv = dev_get_uclass_priv(dev);
339 return priv->state == ETH_STATE_ACTIVE;
342 int eth_send(void *packet, int length)
344 struct udevice *current;
347 current = eth_get_dev();
351 if (!eth_is_active(current))
354 ret = eth_get_ops(current)->send(current, packet, length);
356 /* We cannot completely return the error at present */
357 debug("%s: send() returned error %d\n", __func__, ret);
359 #if defined(CONFIG_CMD_PCAP)
361 pcap_post(packet, length, true);
368 struct udevice *current;
374 current = eth_get_dev();
378 if (!eth_is_active(current))
381 /* Process up to 32 packets at one time */
382 flags = ETH_RECV_CHECK_DEVICE;
383 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
384 ret = eth_get_ops(current)->recv(current, flags, &packet);
387 net_process_received_packet(packet, ret);
388 if (ret >= 0 && eth_get_ops(current)->free_pkt)
389 eth_get_ops(current)->free_pkt(current, packet, ret);
396 /* We cannot completely return the error at present */
397 debug("%s: recv() returned error %d\n", __func__, ret);
402 int eth_initialize(void)
410 * Devices need to write the hwaddr even if not started so that Linux
411 * will have access to the hwaddr that u-boot stored for the device.
412 * This is accomplished by attempting to probe each device and calling
413 * their write_hwaddr() operation.
415 uclass_first_device_check(UCLASS_ETH, &dev);
417 log_err("No ethernet found.\n");
418 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
420 char *ethprime = env_get("ethprime");
421 struct udevice *prime_dev = NULL;
424 prime_dev = eth_get_dev_by_name(ethprime);
426 eth_set_dev(prime_dev);
427 eth_current_changed();
432 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
434 if (device_active(dev)) {
438 printf("eth%d: %s", dev_seq(dev), dev->name);
440 if (ethprime && dev == prime_dev)
444 eth_write_hwaddr(dev);
446 if (device_active(dev))
448 uclass_next_device_check(&dev);
452 log_err("No ethernet found.\n");
459 static int eth_post_bind(struct udevice *dev)
461 if (strchr(dev->name, ' ')) {
462 printf("\nError: eth device name \"%s\" has a space!\n",
467 #ifdef CONFIG_DM_ETH_PHY
468 eth_phy_binds_nodes(dev);
474 static int eth_pre_unbind(struct udevice *dev)
476 /* Don't hang onto a pointer that is going away */
477 if (dev == eth_get_uclass_priv()->current)
483 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
485 #if IS_ENABLED(CONFIG_OF_CONTROL)
488 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
490 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
495 memcpy(mac, p, ARP_HLEN);
503 static int eth_post_probe(struct udevice *dev)
505 struct eth_device_priv *priv = dev->uclass_priv;
506 struct eth_pdata *pdata = dev->plat;
507 unsigned char env_enetaddr[ARP_HLEN];
510 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
511 struct eth_ops *ops = eth_get_ops(dev);
512 static int reloc_done;
516 ops->start += gd->reloc_off;
518 ops->send += gd->reloc_off;
520 ops->recv += gd->reloc_off;
522 ops->free_pkt += gd->reloc_off;
524 ops->stop += gd->reloc_off;
526 ops->mcast += gd->reloc_off;
527 if (ops->write_hwaddr)
528 ops->write_hwaddr += gd->reloc_off;
529 if (ops->read_rom_hwaddr)
530 ops->read_rom_hwaddr += gd->reloc_off;
536 priv->state = ETH_STATE_INIT;
538 /* Check if the device has a valid MAC address in device tree */
539 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
540 !is_valid_ethaddr(pdata->enetaddr)) {
542 /* Check if the device has a MAC address in ROM */
543 if (eth_get_ops(dev)->read_rom_hwaddr)
544 eth_get_ops(dev)->read_rom_hwaddr(dev);
547 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
548 if (!is_zero_ethaddr(env_enetaddr)) {
549 if (!is_zero_ethaddr(pdata->enetaddr) &&
550 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
551 printf("\nWarning: %s MAC addresses don't match:\n",
553 printf("Address in %s is\t\t%pM\n",
554 source, pdata->enetaddr);
555 printf("Address in environment is\t%pM\n",
559 /* Override the ROM MAC address */
560 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
561 } else if (is_valid_ethaddr(pdata->enetaddr)) {
562 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
564 } else if (is_zero_ethaddr(pdata->enetaddr) ||
565 !is_valid_ethaddr(pdata->enetaddr)) {
566 #ifdef CONFIG_NET_RANDOM_ETHADDR
567 net_random_ethaddr(pdata->enetaddr);
568 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
569 dev->name, dev_seq(dev), pdata->enetaddr);
571 printf("\nError: %s address not set.\n",
577 eth_write_hwaddr(dev);
582 static int eth_pre_remove(struct udevice *dev)
584 struct eth_pdata *pdata = dev->plat;
586 eth_get_ops(dev)->stop(dev);
588 /* clear the MAC address */
589 memset(pdata->enetaddr, 0, ARP_HLEN);
594 UCLASS_DRIVER(eth) = {
597 .post_bind = eth_post_bind,
598 .pre_unbind = eth_pre_unbind,
599 .post_probe = eth_post_probe,
600 .pre_remove = eth_pre_remove,
601 .priv_auto = sizeof(struct eth_uclass_priv),
602 .per_device_auto = sizeof(struct eth_device_priv),
603 .flags = DM_UC_FLAG_SEQ_ALIAS,