blob: 7f89f65c92a3ad4ac1bcc61b23044c050631b205 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc6011d22016-01-17 14:52:00 -07002/*
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
Simon Glassc6011d22016-01-17 14:52:00 -07006 */
7
8#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glassc6011d22016-01-17 14:52:00 -070010#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassc6011d22016-01-17 14:52:00 -070013#include <net.h>
14#include <dm/device-internal.h>
15#include <dm/uclass-internal.h>
Ramon Friedac598c12019-07-18 21:43:30 +030016#include <net/pcap.h>
Simon Glassc6011d22016-01-17 14:52:00 -070017#include "eth_internal.h"
Ye Licd5bb772020-05-03 22:41:14 +080018#include <eth_phy.h>
Simon Glassc6011d22016-01-17 14:52:00 -070019
Simon Glass004b8912016-01-30 15:45:14 -070020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glassc6011d22016-01-17 14:52:00 -070022/**
23 * struct eth_device_priv - private structure for each Ethernet device
24 *
25 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
26 */
27struct eth_device_priv {
28 enum eth_state_t state;
29};
30
31/**
32 * struct eth_uclass_priv - The structure attached to the uclass itself
33 *
34 * @current: The Ethernet device that the network functions are using
35 */
36struct eth_uclass_priv {
37 struct udevice *current;
38};
39
40/* eth_errno - This stores the most recent failure code from DM functions */
41static int eth_errno;
42
43static struct eth_uclass_priv *eth_get_uclass_priv(void)
44{
45 struct uclass *uc;
Peng Fan46ec1042020-05-03 22:41:13 +080046 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -070047
Peng Fan46ec1042020-05-03 22:41:13 +080048 ret = uclass_get(UCLASS_ETH, &uc);
49 if (ret)
50 return NULL;
51
Simon Glassc6011d22016-01-17 14:52:00 -070052 assert(uc);
53 return uc->priv;
54}
55
56void eth_set_current_to_next(void)
57{
58 struct eth_uclass_priv *uc_priv;
59
60 uc_priv = eth_get_uclass_priv();
61 if (uc_priv->current)
62 uclass_next_device(&uc_priv->current);
63 if (!uc_priv->current)
64 uclass_first_device(UCLASS_ETH, &uc_priv->current);
65}
66
67/*
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.
72 */
73struct udevice *eth_get_dev(void)
74{
75 struct eth_uclass_priv *uc_priv;
76
77 uc_priv = eth_get_uclass_priv();
78 if (!uc_priv->current)
79 eth_errno = uclass_first_device(UCLASS_ETH,
80 &uc_priv->current);
81 return uc_priv->current;
82}
83
84/*
85 * Typically this will just store a device pointer.
86 * In case it was not probed, we will attempt to do so.
87 * dev may be NULL to unset the active device.
88 */
89void eth_set_dev(struct udevice *dev)
90{
91 if (dev && !device_active(dev)) {
92 eth_errno = device_probe(dev);
93 if (eth_errno)
94 dev = NULL;
95 }
96
97 eth_get_uclass_priv()->current = dev;
98}
99
100/*
101 * Find the udevice that either has the name passed in as devname or has an
102 * alias named devname.
103 */
104struct udevice *eth_get_dev_by_name(const char *devname)
105{
106 int seq = -1;
107 char *endp = NULL;
108 const char *startp = NULL;
109 struct udevice *it;
110 struct uclass *uc;
111 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800112 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700113
114 /* Must be longer than 3 to be an alias */
115 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
116 startp = devname + len;
117 seq = simple_strtoul(startp, &endp, 10);
118 }
119
Peng Fan46ec1042020-05-03 22:41:13 +0800120 ret = uclass_get(UCLASS_ETH, &uc);
121 if (ret)
122 return NULL;
123
Simon Glassc6011d22016-01-17 14:52:00 -0700124 uclass_foreach_dev(it, uc) {
125 /*
126 * We need the seq to be valid, so try to probe it.
127 * If the probe fails, the seq will not match since it will be
128 * -1 instead of what we are looking for.
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().
132 */
133 if (device_probe(it))
134 continue;
135 /* Check for the name or the sequence number to match */
136 if (strcmp(it->name, devname) == 0 ||
137 (endp > startp && it->seq == seq))
138 return it;
139 }
140
141 return NULL;
142}
143
144unsigned char *eth_get_ethaddr(void)
145{
146 struct eth_pdata *pdata;
147
148 if (eth_get_dev()) {
149 pdata = eth_get_dev()->platdata;
150 return pdata->enetaddr;
151 }
152
153 return NULL;
154}
155
156/* Set active state without calling start on the driver */
157int eth_init_state_only(void)
158{
159 struct udevice *current;
160 struct eth_device_priv *priv;
161
162 current = eth_get_dev();
163 if (!current || !device_active(current))
164 return -EINVAL;
165
166 priv = current->uclass_priv;
167 priv->state = ETH_STATE_ACTIVE;
168
169 return 0;
170}
171
172/* Set passive state without calling stop on the driver */
173void eth_halt_state_only(void)
174{
175 struct udevice *current;
176 struct eth_device_priv *priv;
177
178 current = eth_get_dev();
179 if (!current || !device_active(current))
180 return;
181
182 priv = current->uclass_priv;
183 priv->state = ETH_STATE_PASSIVE;
184}
185
186int eth_get_dev_index(void)
187{
188 if (eth_get_dev())
189 return eth_get_dev()->seq;
190 return -1;
191}
192
193static int eth_write_hwaddr(struct udevice *dev)
194{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200195 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700196 int ret = 0;
197
198 if (!dev || !device_active(dev))
199 return -EINVAL;
200
201 /* seq is valid since the device is active */
202 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200203 pdata = dev->platdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700204 if (!is_valid_ethaddr(pdata->enetaddr)) {
205 printf("\nError: %s address %pM illegal value\n",
206 dev->name, pdata->enetaddr);
207 return -EINVAL;
208 }
209
210 /*
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.
213 */
214 ret = eth_get_ops(dev)->write_hwaddr(dev);
215 if (ret == -ENOSYS)
216 ret = 0;
217 if (ret)
218 printf("\nWarning: %s failed to set MAC address\n",
219 dev->name);
220 }
221
222 return ret;
223}
224
225static int on_ethaddr(const char *name, const char *value, enum env_op op,
226 int flags)
227{
228 int index;
229 int retval;
230 struct udevice *dev;
231
232 /* look for an index after "eth" */
233 index = simple_strtoul(name + 3, NULL, 10);
234
235 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
236 if (!retval) {
237 struct eth_pdata *pdata = dev->platdata;
238 switch (op) {
239 case env_op_create:
240 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500241 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200242 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700243 break;
244 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100245 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700246 }
247 }
248
249 return 0;
250}
251U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
252
253int eth_init(void)
254{
Simon Glass64b723f2017-08-03 12:22:12 -0600255 char *ethact = env_get("ethact");
256 char *ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700257 struct udevice *current = NULL;
258 struct udevice *old_current;
259 int ret = -ENODEV;
260
261 /*
262 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
263 * is already set to an ethernet device, we should stick to 'ethact'.
264 */
265 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
266 if (ethact) {
267 current = eth_get_dev_by_name(ethact);
268 if (!current)
269 return -EINVAL;
270 }
271 }
272
273 if (!current) {
274 current = eth_get_dev();
275 if (!current) {
276 printf("No ethernet found.\n");
277 return -ENODEV;
278 }
279 }
280
281 old_current = current;
282 do {
283 if (current) {
284 debug("Trying %s\n", current->name);
285
286 if (device_active(current)) {
287 ret = eth_get_ops(current)->start(current);
288 if (ret >= 0) {
289 struct eth_device_priv *priv =
290 current->uclass_priv;
291
292 priv->state = ETH_STATE_ACTIVE;
293 return 0;
294 }
295 } else {
296 ret = eth_errno;
297 }
298
299 debug("FAIL\n");
300 } else {
301 debug("PROBE FAIL\n");
302 }
303
304 /*
305 * If ethrotate is enabled, this will change "current",
306 * otherwise we will drop out of this while loop immediately
307 */
308 eth_try_another(0);
309 /* This will ensure the new "current" attempted to probe */
310 current = eth_get_dev();
311 } while (old_current != current);
312
313 return ret;
314}
315
316void eth_halt(void)
317{
318 struct udevice *current;
319 struct eth_device_priv *priv;
320
321 current = eth_get_dev();
Joe Hershberger3c91d152018-07-02 14:47:46 -0500322 if (!current || !eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700323 return;
324
325 eth_get_ops(current)->stop(current);
326 priv = current->uclass_priv;
Jean-Jacques Hiblotd21faa52018-08-09 16:17:41 +0200327 if (priv)
328 priv->state = ETH_STATE_PASSIVE;
Simon Glassc6011d22016-01-17 14:52:00 -0700329}
330
331int eth_is_active(struct udevice *dev)
332{
333 struct eth_device_priv *priv;
334
335 if (!dev || !device_active(dev))
336 return 0;
337
338 priv = dev_get_uclass_priv(dev);
339 return priv->state == ETH_STATE_ACTIVE;
340}
341
342int eth_send(void *packet, int length)
343{
344 struct udevice *current;
345 int ret;
346
347 current = eth_get_dev();
348 if (!current)
349 return -ENODEV;
350
Alexander Graf71511492018-03-15 15:07:09 +0100351 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700352 return -EINVAL;
353
354 ret = eth_get_ops(current)->send(current, packet, length);
355 if (ret < 0) {
356 /* We cannot completely return the error at present */
357 debug("%s: send() returned error %d\n", __func__, ret);
358 }
Ramon Friedac598c12019-07-18 21:43:30 +0300359#if defined(CONFIG_CMD_PCAP)
360 if (ret >= 0)
361 pcap_post(packet, length, true);
362#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700363 return ret;
364}
365
366int eth_rx(void)
367{
368 struct udevice *current;
369 uchar *packet;
370 int flags;
371 int ret;
372 int i;
373
374 current = eth_get_dev();
375 if (!current)
376 return -ENODEV;
377
Alexander Graf71511492018-03-15 15:07:09 +0100378 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700379 return -EINVAL;
380
381 /* Process up to 32 packets at one time */
382 flags = ETH_RECV_CHECK_DEVICE;
383 for (i = 0; i < 32; i++) {
384 ret = eth_get_ops(current)->recv(current, flags, &packet);
385 flags = 0;
386 if (ret > 0)
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);
390 if (ret <= 0)
391 break;
392 }
393 if (ret == -EAGAIN)
394 ret = 0;
395 if (ret < 0) {
396 /* We cannot completely return the error at present */
397 debug("%s: recv() returned error %d\n", __func__, ret);
398 }
399 return ret;
400}
401
402int eth_initialize(void)
403{
404 int num_devices = 0;
405 struct udevice *dev;
406
407 eth_common_init();
408
409 /*
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.
414 */
Mario Six865c3872018-04-27 14:52:56 +0200415 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700416 if (!dev) {
417 printf("No ethernet found.\n");
418 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
419 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600420 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700421 struct udevice *prime_dev = NULL;
422
423 if (ethprime)
424 prime_dev = eth_get_dev_by_name(ethprime);
425 if (prime_dev) {
426 eth_set_dev(prime_dev);
427 eth_current_changed();
428 } else {
429 eth_set_dev(NULL);
430 }
431
432 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
433 do {
Michael Walle902c7962019-10-22 01:03:10 +0200434 if (dev->seq != -1) {
435 if (num_devices)
436 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700437
Michael Walle902c7962019-10-22 01:03:10 +0200438 printf("eth%d: %s", dev->seq, dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700439
Michael Walle902c7962019-10-22 01:03:10 +0200440 if (ethprime && dev == prime_dev)
441 printf(" [PRIME]");
442 }
Simon Glassc6011d22016-01-17 14:52:00 -0700443
444 eth_write_hwaddr(dev);
445
Michael Walle902c7962019-10-22 01:03:10 +0200446 if (dev->seq != -1)
447 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200448 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700449 } while (dev);
450
Michael Walle902c7962019-10-22 01:03:10 +0200451 if (!num_devices)
452 printf("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700453 putc('\n');
454 }
455
456 return num_devices;
457}
458
459static int eth_post_bind(struct udevice *dev)
460{
461 if (strchr(dev->name, ' ')) {
462 printf("\nError: eth device name \"%s\" has a space!\n",
463 dev->name);
464 return -EINVAL;
465 }
466
Ye Licd5bb772020-05-03 22:41:14 +0800467#ifdef CONFIG_DM_ETH_PHY
468 eth_phy_binds_nodes(dev);
469#endif
470
Simon Glassc6011d22016-01-17 14:52:00 -0700471 return 0;
472}
473
474static int eth_pre_unbind(struct udevice *dev)
475{
476 /* Don't hang onto a pointer that is going away */
477 if (dev == eth_get_uclass_priv()->current)
478 eth_set_dev(NULL);
479
480 return 0;
481}
482
Thierry Reding8fe08532019-05-20 17:59:57 +0200483static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
484{
485#if IS_ENABLED(CONFIG_OF_CONTROL)
486 const uint8_t *p;
487
488 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
489 if (!p)
490 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
491
492 if (!p)
493 return false;
494
495 memcpy(mac, p, ARP_HLEN);
496
497 return true;
498#else
499 return false;
500#endif
501}
502
Simon Glassc6011d22016-01-17 14:52:00 -0700503static int eth_post_probe(struct udevice *dev)
504{
505 struct eth_device_priv *priv = dev->uclass_priv;
506 struct eth_pdata *pdata = dev->platdata;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100507 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100508 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700509
510#if defined(CONFIG_NEEDS_MANUAL_RELOC)
511 struct eth_ops *ops = eth_get_ops(dev);
512 static int reloc_done;
513
514 if (!reloc_done) {
515 if (ops->start)
516 ops->start += gd->reloc_off;
517 if (ops->send)
518 ops->send += gd->reloc_off;
519 if (ops->recv)
520 ops->recv += gd->reloc_off;
521 if (ops->free_pkt)
522 ops->free_pkt += gd->reloc_off;
523 if (ops->stop)
524 ops->stop += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700525 if (ops->mcast)
526 ops->mcast += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700527 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;
531
532 reloc_done++;
533 }
534#endif
535
536 priv->state = ETH_STATE_INIT;
537
Thierry Reding8fe08532019-05-20 17:59:57 +0200538 /* 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)) {
Michal Simek31411132020-03-16 11:36:17 +0100541 source = "ROM";
Thierry Reding8fe08532019-05-20 17:59:57 +0200542 /* 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);
545 }
Simon Glassc6011d22016-01-17 14:52:00 -0700546
Simon Glass399a9ce2017-08-03 12:22:14 -0600547 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700548 if (!is_zero_ethaddr(env_enetaddr)) {
549 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100550 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700551 printf("\nWarning: %s MAC addresses don't match:\n",
552 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100553 printf("Address in %s is\t\t%pM\n",
554 source, pdata->enetaddr);
555 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700556 env_enetaddr);
557 }
558
559 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100560 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700561 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass8551d552017-08-03 12:22:11 -0600562 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
Michal Simek31411132020-03-16 11:36:17 +0100563 printf("\nWarning: %s using MAC address from %s\n",
564 dev->name, source);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100565 } else if (is_zero_ethaddr(pdata->enetaddr) ||
566 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700567#ifdef CONFIG_NET_RANDOM_ETHADDR
568 net_random_ethaddr(pdata->enetaddr);
569 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
570 dev->name, dev->seq, pdata->enetaddr);
571#else
572 printf("\nError: %s address not set.\n",
573 dev->name);
574 return -EINVAL;
575#endif
576 }
577
Thierry Reding13064872019-05-20 17:59:56 +0200578 eth_write_hwaddr(dev);
579
Simon Glassc6011d22016-01-17 14:52:00 -0700580 return 0;
581}
582
583static int eth_pre_remove(struct udevice *dev)
584{
585 struct eth_pdata *pdata = dev->platdata;
586
587 eth_get_ops(dev)->stop(dev);
588
589 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100590 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700591
592 return 0;
593}
594
595UCLASS_DRIVER(eth) = {
596 .name = "eth",
597 .id = UCLASS_ETH,
598 .post_bind = eth_post_bind,
599 .pre_unbind = eth_pre_unbind,
600 .post_probe = eth_post_probe,
601 .pre_remove = eth_pre_remove,
602 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
603 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
604 .flags = DM_UC_FLAG_SEQ_ALIAS,
605};