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