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;
33 * struct eth_uclass_priv - The structure attached to the uclass itself
35 * @current: The Ethernet device that the network functions are using
37 struct eth_uclass_priv {
38 struct udevice *current;
41 /* eth_errno - This stores the most recent failure code from DM functions */
44 static struct eth_uclass_priv *eth_get_uclass_priv(void)
49 ret = uclass_get(UCLASS_ETH, &uc);
54 return uclass_get_priv(uc);
57 void eth_set_current_to_next(void)
59 struct eth_uclass_priv *uc_priv;
61 uc_priv = eth_get_uclass_priv();
63 uclass_next_device(&uc_priv->current);
64 if (!uc_priv->current)
65 uclass_first_device(UCLASS_ETH, &uc_priv->current);
69 * Typically this will simply return the active device.
70 * In the case where the most recent active device was unset, this will attempt
71 * to return the first device. If that device doesn't exist or fails to probe,
72 * this function will return NULL.
74 struct udevice *eth_get_dev(void)
76 struct eth_uclass_priv *uc_priv;
78 uc_priv = eth_get_uclass_priv();
82 if (!uc_priv->current)
83 eth_errno = uclass_first_device(UCLASS_ETH,
85 return uc_priv->current;
89 * Typically this will just store a device pointer.
90 * In case it was not probed, we will attempt to do so.
91 * dev may be NULL to unset the active device.
93 void eth_set_dev(struct udevice *dev)
95 if (dev && !device_active(dev)) {
96 eth_errno = device_probe(dev);
101 eth_get_uclass_priv()->current = dev;
105 * Find the udevice that either has the name passed in as devname or has an
106 * alias named devname.
108 struct udevice *eth_get_dev_by_name(const char *devname)
112 const char *startp = NULL;
115 int len = strlen("eth");
118 /* Must be longer than 3 to be an alias */
119 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
120 startp = devname + len;
121 seq = simple_strtoul(startp, &endp, 10);
124 ret = uclass_get(UCLASS_ETH, &uc);
128 uclass_foreach_dev(it, uc) {
130 * We don't care about errors from probe here. Either they won't
131 * match an alias or it will match a literal name and we'll pick
132 * up the error when we try to probe again in eth_set_dev().
134 if (device_probe(it))
136 /* Check for the name or the sequence number to match */
137 if (strcmp(it->name, devname) == 0 ||
138 (endp > startp && dev_seq(it) == seq))
145 unsigned char *eth_get_ethaddr(void)
147 struct eth_pdata *pdata;
150 pdata = dev_get_plat(eth_get_dev());
151 return pdata->enetaddr;
157 /* Set active state without calling start on the driver */
158 int eth_init_state_only(void)
160 struct udevice *current;
161 struct eth_device_priv *priv;
163 current = eth_get_dev();
164 if (!current || !device_active(current))
167 priv = dev_get_uclass_priv(current);
168 priv->state = ETH_STATE_ACTIVE;
173 /* Set passive state without calling stop on the driver */
174 void eth_halt_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 = dev_get_uclass_priv(current);
184 priv->state = ETH_STATE_PASSIVE;
187 int eth_get_dev_index(void)
190 return dev_seq(eth_get_dev());
194 static int eth_write_hwaddr(struct udevice *dev)
196 struct eth_pdata *pdata;
199 if (!dev || !device_active(dev))
202 /* seq is valid since the device is active */
203 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
204 pdata = dev_get_plat(dev);
205 if (!is_valid_ethaddr(pdata->enetaddr)) {
206 printf("\nError: %s address %pM illegal value\n",
207 dev->name, pdata->enetaddr);
212 * Drivers are allowed to decide not to implement this at
213 * run-time. E.g. Some devices may use it and some may not.
215 ret = eth_get_ops(dev)->write_hwaddr(dev);
219 printf("\nWarning: %s failed to set MAC address\n",
226 static int on_ethaddr(const char *name, const char *value, enum env_op op,
233 /* look for an index after "eth" */
234 index = simple_strtoul(name + 3, NULL, 10);
236 retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
238 struct eth_pdata *pdata = dev_get_plat(dev);
241 case env_op_overwrite:
242 string_to_enetaddr(value, pdata->enetaddr);
243 eth_write_hwaddr(dev);
246 memset(pdata->enetaddr, 0, ARP_HLEN);
252 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
256 char *ethact = env_get("ethact");
257 char *ethrotate = env_get("ethrotate");
258 struct udevice *current = NULL;
259 struct udevice *old_current;
263 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
264 * is already set to an ethernet device, we should stick to 'ethact'.
266 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
268 current = eth_get_dev_by_name(ethact);
275 current = eth_get_dev();
277 log_err("No ethernet found.\n");
282 old_current = current;
285 debug("Trying %s\n", current->name);
287 if (device_active(current)) {
288 ret = eth_get_ops(current)->start(current);
290 struct eth_device_priv *priv =
291 dev_get_uclass_priv(current);
293 priv->state = ETH_STATE_ACTIVE;
294 priv->running = true;
303 debug("PROBE FAIL\n");
307 * If ethrotate is enabled, this will change "current",
308 * otherwise we will drop out of this while loop immediately
311 /* This will ensure the new "current" attempted to probe */
312 current = eth_get_dev();
313 } while (old_current != current);
320 struct udevice *current;
321 struct eth_device_priv *priv;
323 current = eth_get_dev();
327 priv = dev_get_uclass_priv(current);
328 if (!priv || !priv->running)
331 eth_get_ops(current)->stop(current);
332 priv->state = ETH_STATE_PASSIVE;
333 priv->running = false;
336 int eth_is_active(struct udevice *dev)
338 struct eth_device_priv *priv;
340 if (!dev || !device_active(dev))
343 priv = dev_get_uclass_priv(dev);
344 return priv->state == ETH_STATE_ACTIVE;
347 int eth_send(void *packet, int length)
349 struct udevice *current;
352 current = eth_get_dev();
356 if (!eth_is_active(current))
359 ret = eth_get_ops(current)->send(current, packet, length);
361 /* We cannot completely return the error at present */
362 debug("%s: send() returned error %d\n", __func__, ret);
364 #if defined(CONFIG_CMD_PCAP)
366 pcap_post(packet, length, true);
373 struct udevice *current;
379 current = eth_get_dev();
383 if (!eth_is_active(current))
386 /* Process up to 32 packets at one time */
387 flags = ETH_RECV_CHECK_DEVICE;
388 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
389 ret = eth_get_ops(current)->recv(current, flags, &packet);
392 net_process_received_packet(packet, ret);
393 if (ret >= 0 && eth_get_ops(current)->free_pkt)
394 eth_get_ops(current)->free_pkt(current, packet, ret);
401 /* We cannot completely return the error at present */
402 debug("%s: recv() returned error %d\n", __func__, ret);
407 int eth_initialize(void)
415 * Devices need to write the hwaddr even if not started so that Linux
416 * will have access to the hwaddr that u-boot stored for the device.
417 * This is accomplished by attempting to probe each device and calling
418 * their write_hwaddr() operation.
420 uclass_first_device_check(UCLASS_ETH, &dev);
422 log_err("No ethernet found.\n");
423 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
425 char *ethprime = env_get("ethprime");
426 struct udevice *prime_dev = NULL;
429 prime_dev = eth_get_dev_by_name(ethprime);
431 eth_set_dev(prime_dev);
432 eth_current_changed();
437 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
439 if (device_active(dev)) {
443 printf("eth%d: %s", dev_seq(dev), dev->name);
445 if (ethprime && dev == prime_dev)
449 eth_write_hwaddr(dev);
451 if (device_active(dev))
453 uclass_next_device_check(&dev);
457 log_err("No ethernet found.\n");
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",
472 #ifdef CONFIG_DM_ETH_PHY
473 eth_phy_binds_nodes(dev);
479 static int eth_pre_unbind(struct udevice *dev)
481 /* Don't hang onto a pointer that is going away */
482 if (dev == eth_get_uclass_priv()->current)
488 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
490 #if IS_ENABLED(CONFIG_OF_CONTROL)
493 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
495 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
500 memcpy(mac, p, ARP_HLEN);
508 static int eth_post_probe(struct udevice *dev)
510 struct eth_device_priv *priv = dev_get_uclass_priv(dev);
511 struct eth_pdata *pdata = dev_get_plat(dev);
512 unsigned char env_enetaddr[ARP_HLEN];
515 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
516 struct eth_ops *ops = eth_get_ops(dev);
517 static int reloc_done;
521 ops->start += gd->reloc_off;
523 ops->send += gd->reloc_off;
525 ops->recv += gd->reloc_off;
527 ops->free_pkt += gd->reloc_off;
529 ops->stop += gd->reloc_off;
531 ops->mcast += gd->reloc_off;
532 if (ops->write_hwaddr)
533 ops->write_hwaddr += gd->reloc_off;
534 if (ops->read_rom_hwaddr)
535 ops->read_rom_hwaddr += gd->reloc_off;
541 priv->state = ETH_STATE_INIT;
542 priv->running = false;
544 /* Check if the device has a valid MAC address in device tree */
545 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
546 !is_valid_ethaddr(pdata->enetaddr)) {
548 /* Check if the device has a MAC address in ROM */
549 if (eth_get_ops(dev)->read_rom_hwaddr)
550 eth_get_ops(dev)->read_rom_hwaddr(dev);
553 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
554 if (!is_zero_ethaddr(env_enetaddr)) {
555 if (!is_zero_ethaddr(pdata->enetaddr) &&
556 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
557 printf("\nWarning: %s MAC addresses don't match:\n",
559 printf("Address in %s is\t\t%pM\n",
560 source, pdata->enetaddr);
561 printf("Address in environment is\t%pM\n",
565 /* Override the ROM MAC address */
566 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
567 } else if (is_valid_ethaddr(pdata->enetaddr)) {
568 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
570 } else if (is_zero_ethaddr(pdata->enetaddr) ||
571 !is_valid_ethaddr(pdata->enetaddr)) {
572 #ifdef CONFIG_NET_RANDOM_ETHADDR
573 net_random_ethaddr(pdata->enetaddr);
574 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
575 dev->name, dev_seq(dev), pdata->enetaddr);
577 printf("\nError: %s address not set.\n",
583 eth_write_hwaddr(dev);
588 static int eth_pre_remove(struct udevice *dev)
590 struct eth_pdata *pdata = dev_get_plat(dev);
592 eth_get_ops(dev)->stop(dev);
594 /* clear the MAC address */
595 memset(pdata->enetaddr, 0, ARP_HLEN);
600 UCLASS_DRIVER(eth) = {
603 .post_bind = eth_post_bind,
604 .pre_unbind = eth_pre_unbind,
605 .post_probe = eth_post_probe,
606 .pre_remove = eth_pre_remove,
607 .priv_auto = sizeof(struct eth_uclass_priv),
608 .per_device_auto = sizeof(struct eth_device_priv),
609 .flags = DM_UC_FLAG_SEQ_ALIAS,