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
12 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
15 #include "eth_internal.h"
17 DECLARE_GLOBAL_DATA_PTR;
20 * struct eth_device_priv - private structure for each Ethernet device
22 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
24 struct eth_device_priv {
25 enum eth_state_t state;
29 * struct eth_uclass_priv - The structure attached to the uclass itself
31 * @current: The Ethernet device that the network functions are using
33 struct eth_uclass_priv {
34 struct udevice *current;
37 /* eth_errno - This stores the most recent failure code from DM functions */
40 static struct eth_uclass_priv *eth_get_uclass_priv(void)
45 ret = uclass_get(UCLASS_ETH, &uc);
53 void eth_set_current_to_next(void)
55 struct eth_uclass_priv *uc_priv;
57 uc_priv = eth_get_uclass_priv();
59 uclass_next_device(&uc_priv->current);
60 if (!uc_priv->current)
61 uclass_first_device(UCLASS_ETH, &uc_priv->current);
65 * Typically this will simply return the active device.
66 * In the case where the most recent active device was unset, this will attempt
67 * to return the first device. If that device doesn't exist or fails to probe,
68 * this function will return NULL.
70 struct udevice *eth_get_dev(void)
72 struct eth_uclass_priv *uc_priv;
74 uc_priv = eth_get_uclass_priv();
75 if (!uc_priv->current)
76 eth_errno = uclass_first_device(UCLASS_ETH,
78 return uc_priv->current;
82 * Typically this will just store a device pointer.
83 * In case it was not probed, we will attempt to do so.
84 * dev may be NULL to unset the active device.
86 void eth_set_dev(struct udevice *dev)
88 if (dev && !device_active(dev)) {
89 eth_errno = device_probe(dev);
94 eth_get_uclass_priv()->current = dev;
98 * Find the udevice that either has the name passed in as devname or has an
99 * alias named devname.
101 struct udevice *eth_get_dev_by_name(const char *devname)
105 const char *startp = NULL;
108 int len = strlen("eth");
111 /* Must be longer than 3 to be an alias */
112 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
113 startp = devname + len;
114 seq = simple_strtoul(startp, &endp, 10);
117 ret = uclass_get(UCLASS_ETH, &uc);
121 uclass_foreach_dev(it, uc) {
123 * We need the seq to be valid, so try to probe it.
124 * If the probe fails, the seq will not match since it will be
125 * -1 instead of what we are looking for.
126 * We don't care about errors from probe here. Either they won't
127 * match an alias or it will match a literal name and we'll pick
128 * up the error when we try to probe again in eth_set_dev().
130 if (device_probe(it))
132 /* Check for the name or the sequence number to match */
133 if (strcmp(it->name, devname) == 0 ||
134 (endp > startp && it->seq == seq))
141 unsigned char *eth_get_ethaddr(void)
143 struct eth_pdata *pdata;
146 pdata = eth_get_dev()->platdata;
147 return pdata->enetaddr;
153 /* Set active state without calling start on the driver */
154 int eth_init_state_only(void)
156 struct udevice *current;
157 struct eth_device_priv *priv;
159 current = eth_get_dev();
160 if (!current || !device_active(current))
163 priv = current->uclass_priv;
164 priv->state = ETH_STATE_ACTIVE;
169 /* Set passive state without calling stop on the driver */
170 void eth_halt_state_only(void)
172 struct udevice *current;
173 struct eth_device_priv *priv;
175 current = eth_get_dev();
176 if (!current || !device_active(current))
179 priv = current->uclass_priv;
180 priv->state = ETH_STATE_PASSIVE;
183 int eth_get_dev_index(void)
186 return eth_get_dev()->seq;
190 static int eth_write_hwaddr(struct udevice *dev)
192 struct eth_pdata *pdata;
195 if (!dev || !device_active(dev))
198 /* seq is valid since the device is active */
199 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
200 pdata = dev->platdata;
201 if (!is_valid_ethaddr(pdata->enetaddr)) {
202 printf("\nError: %s address %pM illegal value\n",
203 dev->name, pdata->enetaddr);
208 * Drivers are allowed to decide not to implement this at
209 * run-time. E.g. Some devices may use it and some may not.
211 ret = eth_get_ops(dev)->write_hwaddr(dev);
215 printf("\nWarning: %s failed to set MAC address\n",
222 static int on_ethaddr(const char *name, const char *value, enum env_op op,
229 /* look for an index after "eth" */
230 index = simple_strtoul(name + 3, NULL, 10);
232 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
234 struct eth_pdata *pdata = dev->platdata;
237 case env_op_overwrite:
238 string_to_enetaddr(value, pdata->enetaddr);
239 eth_write_hwaddr(dev);
242 memset(pdata->enetaddr, 0, ARP_HLEN);
248 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
252 char *ethact = env_get("ethact");
253 char *ethrotate = env_get("ethrotate");
254 struct udevice *current = NULL;
255 struct udevice *old_current;
259 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
260 * is already set to an ethernet device, we should stick to 'ethact'.
262 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
264 current = eth_get_dev_by_name(ethact);
271 current = eth_get_dev();
273 printf("No ethernet found.\n");
278 old_current = current;
281 debug("Trying %s\n", current->name);
283 if (device_active(current)) {
284 ret = eth_get_ops(current)->start(current);
286 struct eth_device_priv *priv =
287 current->uclass_priv;
289 priv->state = ETH_STATE_ACTIVE;
298 debug("PROBE FAIL\n");
302 * If ethrotate is enabled, this will change "current",
303 * otherwise we will drop out of this while loop immediately
306 /* This will ensure the new "current" attempted to probe */
307 current = eth_get_dev();
308 } while (old_current != current);
315 struct udevice *current;
316 struct eth_device_priv *priv;
318 current = eth_get_dev();
319 if (!current || !eth_is_active(current))
322 eth_get_ops(current)->stop(current);
323 priv = current->uclass_priv;
325 priv->state = ETH_STATE_PASSIVE;
328 int eth_is_active(struct udevice *dev)
330 struct eth_device_priv *priv;
332 if (!dev || !device_active(dev))
335 priv = dev_get_uclass_priv(dev);
336 return priv->state == ETH_STATE_ACTIVE;
339 int eth_send(void *packet, int length)
341 struct udevice *current;
344 current = eth_get_dev();
348 if (!eth_is_active(current))
351 ret = eth_get_ops(current)->send(current, packet, length);
353 /* We cannot completely return the error at present */
354 debug("%s: send() returned error %d\n", __func__, ret);
356 #if defined(CONFIG_CMD_PCAP)
358 pcap_post(packet, length, true);
365 struct udevice *current;
371 current = eth_get_dev();
375 if (!eth_is_active(current))
378 /* Process up to 32 packets at one time */
379 flags = ETH_RECV_CHECK_DEVICE;
380 for (i = 0; i < 32; i++) {
381 ret = eth_get_ops(current)->recv(current, flags, &packet);
384 net_process_received_packet(packet, ret);
385 if (ret >= 0 && eth_get_ops(current)->free_pkt)
386 eth_get_ops(current)->free_pkt(current, packet, ret);
393 /* We cannot completely return the error at present */
394 debug("%s: recv() returned error %d\n", __func__, ret);
399 int eth_initialize(void)
407 * Devices need to write the hwaddr even if not started so that Linux
408 * will have access to the hwaddr that u-boot stored for the device.
409 * This is accomplished by attempting to probe each device and calling
410 * their write_hwaddr() operation.
412 uclass_first_device_check(UCLASS_ETH, &dev);
414 printf("No ethernet found.\n");
415 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
417 char *ethprime = env_get("ethprime");
418 struct udevice *prime_dev = NULL;
421 prime_dev = eth_get_dev_by_name(ethprime);
423 eth_set_dev(prime_dev);
424 eth_current_changed();
429 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
431 if (dev->seq != -1) {
435 printf("eth%d: %s", dev->seq, dev->name);
437 if (ethprime && dev == prime_dev)
441 eth_write_hwaddr(dev);
445 uclass_next_device_check(&dev);
449 printf("No ethernet found.\n");
456 static int eth_post_bind(struct udevice *dev)
458 if (strchr(dev->name, ' ')) {
459 printf("\nError: eth device name \"%s\" has a space!\n",
467 static int eth_pre_unbind(struct udevice *dev)
469 /* Don't hang onto a pointer that is going away */
470 if (dev == eth_get_uclass_priv()->current)
476 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
478 #if IS_ENABLED(CONFIG_OF_CONTROL)
481 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
483 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
488 memcpy(mac, p, ARP_HLEN);
496 static int eth_post_probe(struct udevice *dev)
498 struct eth_device_priv *priv = dev->uclass_priv;
499 struct eth_pdata *pdata = dev->platdata;
500 unsigned char env_enetaddr[ARP_HLEN];
503 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
504 struct eth_ops *ops = eth_get_ops(dev);
505 static int reloc_done;
509 ops->start += gd->reloc_off;
511 ops->send += gd->reloc_off;
513 ops->recv += gd->reloc_off;
515 ops->free_pkt += gd->reloc_off;
517 ops->stop += gd->reloc_off;
519 ops->mcast += gd->reloc_off;
520 if (ops->write_hwaddr)
521 ops->write_hwaddr += gd->reloc_off;
522 if (ops->read_rom_hwaddr)
523 ops->read_rom_hwaddr += gd->reloc_off;
529 priv->state = ETH_STATE_INIT;
531 /* Check if the device has a valid MAC address in device tree */
532 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
533 !is_valid_ethaddr(pdata->enetaddr)) {
535 /* Check if the device has a MAC address in ROM */
536 if (eth_get_ops(dev)->read_rom_hwaddr)
537 eth_get_ops(dev)->read_rom_hwaddr(dev);
540 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
541 if (!is_zero_ethaddr(env_enetaddr)) {
542 if (!is_zero_ethaddr(pdata->enetaddr) &&
543 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
544 printf("\nWarning: %s MAC addresses don't match:\n",
546 printf("Address in %s is\t\t%pM\n",
547 source, pdata->enetaddr);
548 printf("Address in environment is\t%pM\n",
552 /* Override the ROM MAC address */
553 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
554 } else if (is_valid_ethaddr(pdata->enetaddr)) {
555 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
556 printf("\nWarning: %s using MAC address from %s\n",
558 } else if (is_zero_ethaddr(pdata->enetaddr) ||
559 !is_valid_ethaddr(pdata->enetaddr)) {
560 #ifdef CONFIG_NET_RANDOM_ETHADDR
561 net_random_ethaddr(pdata->enetaddr);
562 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
563 dev->name, dev->seq, pdata->enetaddr);
565 printf("\nError: %s address not set.\n",
571 eth_write_hwaddr(dev);
576 static int eth_pre_remove(struct udevice *dev)
578 struct eth_pdata *pdata = dev->platdata;
580 eth_get_ops(dev)->stop(dev);
582 /* clear the MAC address */
583 memset(pdata->enetaddr, 0, ARP_HLEN);
588 UCLASS_DRIVER(eth) = {
591 .post_bind = eth_post_bind,
592 .pre_unbind = eth_pre_unbind,
593 .post_probe = eth_post_probe,
594 .pre_remove = eth_pre_remove,
595 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
596 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
597 .flags = DM_UC_FLAG_SEQ_ALIAS,