1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001-2015
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  * Joe Hershberger, National Instruments
6  */
7 
8 #include <common.h>
9 #include <bootstage.h>
10 #include <dm.h>
11 #include <env.h>
12 #include <log.h>
13 #include <net.h>
14 #include <asm/global_data.h>
15 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
17 #include <net/pcap.h>
18 #include "eth_internal.h"
19 #include <eth_phy.h>
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 /**
24  * struct eth_device_priv - private structure for each Ethernet device
25  *
26  * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
27  */
28 struct eth_device_priv {
29 	enum eth_state_t state;
30 	bool running;
31 };
32 
33 /**
34  * struct eth_uclass_priv - The structure attached to the uclass itself
35  *
36  * @current: The Ethernet device that the network functions are using
37  */
38 struct eth_uclass_priv {
39 	struct udevice *current;
40 };
41 
42 /* eth_errno - This stores the most recent failure code from DM functions */
43 static int eth_errno;
44 
eth_get_uclass_priv(void)45 static struct eth_uclass_priv *eth_get_uclass_priv(void)
46 {
47 	struct uclass *uc;
48 	int ret;
49 
50 	ret = uclass_get(UCLASS_ETH, &uc);
51 	if (ret)
52 		return NULL;
53 
54 	assert(uc);
55 	return uclass_get_priv(uc);
56 }
57 
eth_set_current_to_next(void)58 void eth_set_current_to_next(void)
59 {
60 	struct eth_uclass_priv *uc_priv;
61 
62 	uc_priv = eth_get_uclass_priv();
63 	if (uc_priv->current)
64 		uclass_next_device(&uc_priv->current);
65 	if (!uc_priv->current)
66 		uclass_first_device(UCLASS_ETH, &uc_priv->current);
67 }
68 
69 /*
70  * Typically this will simply return the active device.
71  * In the case where the most recent active device was unset, this will attempt
72  * to return the device with sequence id 0 (which can be configured by the
73  * device tree). If this fails, fall back to just getting the first device.
74  * The latter is non-deterministic and depends on the order of the probing.
75  * If that device doesn't exist or fails to probe, this function will return
76  * NULL.
77  */
eth_get_dev(void)78 struct udevice *eth_get_dev(void)
79 {
80 	struct eth_uclass_priv *uc_priv;
81 
82 	uc_priv = eth_get_uclass_priv();
83 	if (!uc_priv)
84 		return NULL;
85 
86 	if (!uc_priv->current) {
87 		eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
88 						     &uc_priv->current);
89 		if (eth_errno)
90 			eth_errno = uclass_first_device(UCLASS_ETH,
91 							&uc_priv->current);
92 	}
93 	return uc_priv->current;
94 }
95 
96 /*
97  * Typically this will just store a device pointer.
98  * In case it was not probed, we will attempt to do so.
99  * dev may be NULL to unset the active device.
100  */
eth_set_dev(struct udevice * dev)101 void eth_set_dev(struct udevice *dev)
102 {
103 	if (dev && !device_active(dev)) {
104 		eth_errno = device_probe(dev);
105 		if (eth_errno)
106 			dev = NULL;
107 	}
108 
109 	eth_get_uclass_priv()->current = dev;
110 }
111 
112 /*
113  * Find the udevice that either has the name passed in as devname or has an
114  * alias named devname.
115  */
eth_get_dev_by_name(const char * devname)116 struct udevice *eth_get_dev_by_name(const char *devname)
117 {
118 	int seq = -1;
119 	char *endp = NULL;
120 	const char *startp = NULL;
121 	struct udevice *it;
122 	struct uclass *uc;
123 	int len = strlen("eth");
124 	int ret;
125 
126 	/* Must be longer than 3 to be an alias */
127 	if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
128 		startp = devname + len;
129 		seq = simple_strtoul(startp, &endp, 10);
130 	}
131 
132 	ret = uclass_get(UCLASS_ETH, &uc);
133 	if (ret)
134 		return NULL;
135 
136 	uclass_foreach_dev(it, uc) {
137 		/*
138 		 * We don't care about errors from probe here. Either they won't
139 		 * match an alias or it will match a literal name and we'll pick
140 		 * up the error when we try to probe again in eth_set_dev().
141 		 */
142 		if (device_probe(it))
143 			continue;
144 		/* Check for the name or the sequence number to match */
145 		if (strcmp(it->name, devname) == 0 ||
146 		    (endp > startp && dev_seq(it) == seq))
147 			return it;
148 	}
149 
150 	return NULL;
151 }
152 
eth_get_ethaddr(void)153 unsigned char *eth_get_ethaddr(void)
154 {
155 	struct eth_pdata *pdata;
156 
157 	if (eth_get_dev()) {
158 		pdata = dev_get_plat(eth_get_dev());
159 		return pdata->enetaddr;
160 	}
161 
162 	return NULL;
163 }
164 
165 /* Set active state without calling start on the driver */
eth_init_state_only(void)166 int eth_init_state_only(void)
167 {
168 	struct udevice *current;
169 	struct eth_device_priv *priv;
170 
171 	current = eth_get_dev();
172 	if (!current || !device_active(current))
173 		return -EINVAL;
174 
175 	priv = dev_get_uclass_priv(current);
176 	priv->state = ETH_STATE_ACTIVE;
177 
178 	return 0;
179 }
180 
181 /* Set passive state without calling stop on the driver */
eth_halt_state_only(void)182 void eth_halt_state_only(void)
183 {
184 	struct udevice *current;
185 	struct eth_device_priv *priv;
186 
187 	current = eth_get_dev();
188 	if (!current || !device_active(current))
189 		return;
190 
191 	priv = dev_get_uclass_priv(current);
192 	priv->state = ETH_STATE_PASSIVE;
193 }
194 
eth_get_dev_index(void)195 int eth_get_dev_index(void)
196 {
197 	if (eth_get_dev())
198 		return dev_seq(eth_get_dev());
199 	return -1;
200 }
201 
eth_write_hwaddr(struct udevice * dev)202 static int eth_write_hwaddr(struct udevice *dev)
203 {
204 	struct eth_pdata *pdata;
205 	int ret = 0;
206 
207 	if (!dev || !device_active(dev))
208 		return -EINVAL;
209 
210 	/* seq is valid since the device is active */
211 	if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
212 		pdata = dev_get_plat(dev);
213 		if (!is_valid_ethaddr(pdata->enetaddr)) {
214 			printf("\nError: %s address %pM illegal value\n",
215 			       dev->name, pdata->enetaddr);
216 			return -EINVAL;
217 		}
218 
219 		/*
220 		 * Drivers are allowed to decide not to implement this at
221 		 * run-time. E.g. Some devices may use it and some may not.
222 		 */
223 		ret = eth_get_ops(dev)->write_hwaddr(dev);
224 		if (ret == -ENOSYS)
225 			ret = 0;
226 		if (ret)
227 			printf("\nWarning: %s failed to set MAC address\n",
228 			       dev->name);
229 	}
230 
231 	return ret;
232 }
233 
on_ethaddr(const char * name,const char * value,enum env_op op,int flags)234 static int on_ethaddr(const char *name, const char *value, enum env_op op,
235 	int flags)
236 {
237 	int index;
238 	int retval;
239 	struct udevice *dev;
240 
241 	/* look for an index after "eth" */
242 	index = simple_strtoul(name + 3, NULL, 10);
243 
244 	retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
245 	if (!retval) {
246 		struct eth_pdata *pdata = dev_get_plat(dev);
247 		switch (op) {
248 		case env_op_create:
249 		case env_op_overwrite:
250 			string_to_enetaddr(value, pdata->enetaddr);
251 			eth_write_hwaddr(dev);
252 			break;
253 		case env_op_delete:
254 			memset(pdata->enetaddr, 0, ARP_HLEN);
255 		}
256 	}
257 
258 	return 0;
259 }
260 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
261 
eth_init(void)262 int eth_init(void)
263 {
264 	char *ethact = env_get("ethact");
265 	char *ethrotate = env_get("ethrotate");
266 	struct udevice *current = NULL;
267 	struct udevice *old_current;
268 	int ret = -ENODEV;
269 
270 	/*
271 	 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
272 	 * is already set to an ethernet device, we should stick to 'ethact'.
273 	 */
274 	if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
275 		if (ethact) {
276 			current = eth_get_dev_by_name(ethact);
277 			if (!current)
278 				return -EINVAL;
279 		}
280 	}
281 
282 	if (!current) {
283 		current = eth_get_dev();
284 		if (!current) {
285 			log_err("No ethernet found.\n");
286 			return -ENODEV;
287 		}
288 	}
289 
290 	old_current = current;
291 	do {
292 		if (current) {
293 			debug("Trying %s\n", current->name);
294 
295 			if (device_active(current)) {
296 				ret = eth_get_ops(current)->start(current);
297 				if (ret >= 0) {
298 					struct eth_device_priv *priv =
299 						dev_get_uclass_priv(current);
300 
301 					priv->state = ETH_STATE_ACTIVE;
302 					priv->running = true;
303 					return 0;
304 				}
305 			} else {
306 				ret = eth_errno;
307 			}
308 
309 			debug("FAIL\n");
310 		} else {
311 			debug("PROBE FAIL\n");
312 		}
313 
314 		/*
315 		 * If ethrotate is enabled, this will change "current",
316 		 * otherwise we will drop out of this while loop immediately
317 		 */
318 		eth_try_another(0);
319 		/* This will ensure the new "current" attempted to probe */
320 		current = eth_get_dev();
321 	} while (old_current != current);
322 
323 	return ret;
324 }
325 
eth_halt(void)326 void eth_halt(void)
327 {
328 	struct udevice *current;
329 	struct eth_device_priv *priv;
330 
331 	current = eth_get_dev();
332 	if (!current)
333 		return;
334 
335 	priv = dev_get_uclass_priv(current);
336 	if (!priv || !priv->running)
337 		return;
338 
339 	eth_get_ops(current)->stop(current);
340 	priv->state = ETH_STATE_PASSIVE;
341 	priv->running = false;
342 }
343 
eth_is_active(struct udevice * dev)344 int eth_is_active(struct udevice *dev)
345 {
346 	struct eth_device_priv *priv;
347 
348 	if (!dev || !device_active(dev))
349 		return 0;
350 
351 	priv = dev_get_uclass_priv(dev);
352 	return priv->state == ETH_STATE_ACTIVE;
353 }
354 
eth_send(void * packet,int length)355 int eth_send(void *packet, int length)
356 {
357 	struct udevice *current;
358 	int ret;
359 
360 	current = eth_get_dev();
361 	if (!current)
362 		return -ENODEV;
363 
364 	if (!eth_is_active(current))
365 		return -EINVAL;
366 
367 	ret = eth_get_ops(current)->send(current, packet, length);
368 	if (ret < 0) {
369 		/* We cannot completely return the error at present */
370 		debug("%s: send() returned error %d\n", __func__, ret);
371 	}
372 #if defined(CONFIG_CMD_PCAP)
373 	if (ret >= 0)
374 		pcap_post(packet, length, true);
375 #endif
376 	return ret;
377 }
378 
eth_rx(void)379 int eth_rx(void)
380 {
381 	struct udevice *current;
382 	uchar *packet;
383 	int flags;
384 	int ret;
385 	int i;
386 
387 	current = eth_get_dev();
388 	if (!current)
389 		return -ENODEV;
390 
391 	if (!eth_is_active(current))
392 		return -EINVAL;
393 
394 	/* Process up to 32 packets at one time */
395 	flags = ETH_RECV_CHECK_DEVICE;
396 	for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
397 		ret = eth_get_ops(current)->recv(current, flags, &packet);
398 		flags = 0;
399 		if (ret > 0)
400 			net_process_received_packet(packet, ret);
401 		if (ret >= 0 && eth_get_ops(current)->free_pkt)
402 			eth_get_ops(current)->free_pkt(current, packet, ret);
403 		if (ret <= 0)
404 			break;
405 	}
406 	if (ret == -EAGAIN)
407 		ret = 0;
408 	if (ret < 0) {
409 		/* We cannot completely return the error at present */
410 		debug("%s: recv() returned error %d\n", __func__, ret);
411 	}
412 	return ret;
413 }
414 
eth_initialize(void)415 int eth_initialize(void)
416 {
417 	int num_devices = 0;
418 	struct udevice *dev;
419 
420 	eth_common_init();
421 
422 	/*
423 	 * Devices need to write the hwaddr even if not started so that Linux
424 	 * will have access to the hwaddr that u-boot stored for the device.
425 	 * This is accomplished by attempting to probe each device and calling
426 	 * their write_hwaddr() operation.
427 	 */
428 	uclass_first_device_check(UCLASS_ETH, &dev);
429 	if (!dev) {
430 		log_err("No ethernet found.\n");
431 		bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
432 	} else {
433 		char *ethprime = env_get("ethprime");
434 		struct udevice *prime_dev = NULL;
435 
436 		if (ethprime)
437 			prime_dev = eth_get_dev_by_name(ethprime);
438 		if (prime_dev) {
439 			eth_set_dev(prime_dev);
440 			eth_current_changed();
441 		} else {
442 			eth_set_dev(NULL);
443 		}
444 
445 		bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
446 		do {
447 			if (device_active(dev)) {
448 				if (num_devices)
449 					printf(", ");
450 
451 				printf("eth%d: %s", dev_seq(dev), dev->name);
452 
453 				if (ethprime && dev == prime_dev)
454 					printf(" [PRIME]");
455 			}
456 
457 			eth_write_hwaddr(dev);
458 
459 			if (device_active(dev))
460 				num_devices++;
461 			uclass_next_device_check(&dev);
462 		} while (dev);
463 
464 		if (!num_devices)
465 			log_err("No ethernet found.\n");
466 		putc('\n');
467 	}
468 
469 	return num_devices;
470 }
471 
eth_post_bind(struct udevice * dev)472 static int eth_post_bind(struct udevice *dev)
473 {
474 	if (strchr(dev->name, ' ')) {
475 		printf("\nError: eth device name \"%s\" has a space!\n",
476 		       dev->name);
477 		return -EINVAL;
478 	}
479 
480 #ifdef CONFIG_DM_ETH_PHY
481 	eth_phy_binds_nodes(dev);
482 #endif
483 
484 	return 0;
485 }
486 
eth_pre_unbind(struct udevice * dev)487 static int eth_pre_unbind(struct udevice *dev)
488 {
489 	/* Don't hang onto a pointer that is going away */
490 	if (dev == eth_get_uclass_priv()->current)
491 		eth_set_dev(NULL);
492 
493 	return 0;
494 }
495 
eth_dev_get_mac_address(struct udevice * dev,u8 mac[ARP_HLEN])496 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
497 {
498 #if CONFIG_IS_ENABLED(OF_CONTROL)
499 	const uint8_t *p;
500 
501 	p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
502 	if (!p)
503 		p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
504 
505 	if (!p)
506 		return false;
507 
508 	memcpy(mac, p, ARP_HLEN);
509 
510 	return true;
511 #else
512 	return false;
513 #endif
514 }
515 
eth_post_probe(struct udevice * dev)516 static int eth_post_probe(struct udevice *dev)
517 {
518 	struct eth_device_priv *priv = dev_get_uclass_priv(dev);
519 	struct eth_pdata *pdata = dev_get_plat(dev);
520 	unsigned char env_enetaddr[ARP_HLEN];
521 	char *source = "DT";
522 
523 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
524 	struct eth_ops *ops = eth_get_ops(dev);
525 	static int reloc_done;
526 
527 	if (!reloc_done) {
528 		if (ops->start)
529 			ops->start += gd->reloc_off;
530 		if (ops->send)
531 			ops->send += gd->reloc_off;
532 		if (ops->recv)
533 			ops->recv += gd->reloc_off;
534 		if (ops->free_pkt)
535 			ops->free_pkt += gd->reloc_off;
536 		if (ops->stop)
537 			ops->stop += gd->reloc_off;
538 		if (ops->mcast)
539 			ops->mcast += gd->reloc_off;
540 		if (ops->write_hwaddr)
541 			ops->write_hwaddr += gd->reloc_off;
542 		if (ops->read_rom_hwaddr)
543 			ops->read_rom_hwaddr += gd->reloc_off;
544 
545 		reloc_done++;
546 	}
547 #endif
548 
549 	priv->state = ETH_STATE_INIT;
550 	priv->running = false;
551 
552 	/* Check if the device has a valid MAC address in device tree */
553 	if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
554 	    !is_valid_ethaddr(pdata->enetaddr)) {
555 		source = "ROM";
556 		/* Check if the device has a MAC address in ROM */
557 		if (eth_get_ops(dev)->read_rom_hwaddr)
558 			eth_get_ops(dev)->read_rom_hwaddr(dev);
559 	}
560 
561 	eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
562 	if (!is_zero_ethaddr(env_enetaddr)) {
563 		if (!is_zero_ethaddr(pdata->enetaddr) &&
564 		    memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
565 			printf("\nWarning: %s MAC addresses don't match:\n",
566 			       dev->name);
567 			printf("Address in %s is\t\t%pM\n",
568 			       source, pdata->enetaddr);
569 			printf("Address in environment is\t%pM\n",
570 			       env_enetaddr);
571 		}
572 
573 		/* Override the ROM MAC address */
574 		memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
575 	} else if (is_valid_ethaddr(pdata->enetaddr)) {
576 		eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
577 					      pdata->enetaddr);
578 	} else if (is_zero_ethaddr(pdata->enetaddr) ||
579 		   !is_valid_ethaddr(pdata->enetaddr)) {
580 #ifdef CONFIG_NET_RANDOM_ETHADDR
581 		net_random_ethaddr(pdata->enetaddr);
582 		printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
583 		       dev->name, dev_seq(dev), pdata->enetaddr);
584 #else
585 		printf("\nError: %s address not set.\n",
586 		       dev->name);
587 		return -EINVAL;
588 #endif
589 	}
590 
591 	eth_write_hwaddr(dev);
592 
593 	return 0;
594 }
595 
eth_pre_remove(struct udevice * dev)596 static int eth_pre_remove(struct udevice *dev)
597 {
598 	struct eth_pdata *pdata = dev_get_plat(dev);
599 
600 	eth_get_ops(dev)->stop(dev);
601 
602 	/* clear the MAC address */
603 	memset(pdata->enetaddr, 0, ARP_HLEN);
604 
605 	return 0;
606 }
607 
608 UCLASS_DRIVER(ethernet) = {
609 	.name		= "ethernet",
610 	.id		= UCLASS_ETH,
611 	.post_bind	= eth_post_bind,
612 	.pre_unbind	= eth_pre_unbind,
613 	.post_probe	= eth_post_probe,
614 	.pre_remove	= eth_pre_remove,
615 	.priv_auto	= sizeof(struct eth_uclass_priv),
616 	.per_device_auto	= sizeof(struct eth_device_priv),
617 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
618 };
619