blob: 5682b3a731cf279bf240f0fa3fc1e7c440a0acc5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdb9391e2016-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 Glassdb9391e2016-01-17 14:52:00 -07006 */
7
8#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070010#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060011#include <env.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070012#include <net.h>
13#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030015#include <net/pcap.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070016#include "eth_internal.h"
Ye Li5fe419e2020-05-03 22:41:14 +080017#include <eth_phy.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070018
Simon Glassa7c45ec2016-01-30 15:45:14 -070019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassdb9391e2016-01-17 14:52:00 -070021/**
22 * struct eth_device_priv - private structure for each Ethernet device
23 *
24 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
25 */
26struct eth_device_priv {
27 enum eth_state_t state;
28};
29
30/**
31 * struct eth_uclass_priv - The structure attached to the uclass itself
32 *
33 * @current: The Ethernet device that the network functions are using
34 */
35struct eth_uclass_priv {
36 struct udevice *current;
37};
38
39/* eth_errno - This stores the most recent failure code from DM functions */
40static int eth_errno;
41
42static struct eth_uclass_priv *eth_get_uclass_priv(void)
43{
44 struct uclass *uc;
Peng Fand2b70202020-05-03 22:41:13 +080045 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -070046
Peng Fand2b70202020-05-03 22:41:13 +080047 ret = uclass_get(UCLASS_ETH, &uc);
48 if (ret)
49 return NULL;
50
Simon Glassdb9391e2016-01-17 14:52:00 -070051 assert(uc);
52 return uc->priv;
53}
54
55void eth_set_current_to_next(void)
56{
57 struct eth_uclass_priv *uc_priv;
58
59 uc_priv = eth_get_uclass_priv();
60 if (uc_priv->current)
61 uclass_next_device(&uc_priv->current);
62 if (!uc_priv->current)
63 uclass_first_device(UCLASS_ETH, &uc_priv->current);
64}
65
66/*
67 * Typically this will simply return the active device.
68 * In the case where the most recent active device was unset, this will attempt
69 * to return the first device. If that device doesn't exist or fails to probe,
70 * this function will return NULL.
71 */
72struct udevice *eth_get_dev(void)
73{
74 struct eth_uclass_priv *uc_priv;
75
76 uc_priv = eth_get_uclass_priv();
77 if (!uc_priv->current)
78 eth_errno = uclass_first_device(UCLASS_ETH,
79 &uc_priv->current);
80 return uc_priv->current;
81}
82
83/*
84 * Typically this will just store a device pointer.
85 * In case it was not probed, we will attempt to do so.
86 * dev may be NULL to unset the active device.
87 */
88void eth_set_dev(struct udevice *dev)
89{
90 if (dev && !device_active(dev)) {
91 eth_errno = device_probe(dev);
92 if (eth_errno)
93 dev = NULL;
94 }
95
96 eth_get_uclass_priv()->current = dev;
97}
98
99/*
100 * Find the udevice that either has the name passed in as devname or has an
101 * alias named devname.
102 */
103struct udevice *eth_get_dev_by_name(const char *devname)
104{
105 int seq = -1;
106 char *endp = NULL;
107 const char *startp = NULL;
108 struct udevice *it;
109 struct uclass *uc;
110 int len = strlen("eth");
Peng Fand2b70202020-05-03 22:41:13 +0800111 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -0700112
113 /* Must be longer than 3 to be an alias */
114 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
115 startp = devname + len;
116 seq = simple_strtoul(startp, &endp, 10);
117 }
118
Peng Fand2b70202020-05-03 22:41:13 +0800119 ret = uclass_get(UCLASS_ETH, &uc);
120 if (ret)
121 return NULL;
122
Simon Glassdb9391e2016-01-17 14:52:00 -0700123 uclass_foreach_dev(it, uc) {
124 /*
125 * We need the seq to be valid, so try to probe it.
126 * If the probe fails, the seq will not match since it will be
127 * -1 instead of what we are looking for.
128 * We don't care about errors from probe here. Either they won't
129 * match an alias or it will match a literal name and we'll pick
130 * up the error when we try to probe again in eth_set_dev().
131 */
132 if (device_probe(it))
133 continue;
134 /* Check for the name or the sequence number to match */
135 if (strcmp(it->name, devname) == 0 ||
136 (endp > startp && it->seq == seq))
137 return it;
138 }
139
140 return NULL;
141}
142
143unsigned char *eth_get_ethaddr(void)
144{
145 struct eth_pdata *pdata;
146
147 if (eth_get_dev()) {
148 pdata = eth_get_dev()->platdata;
149 return pdata->enetaddr;
150 }
151
152 return NULL;
153}
154
155/* Set active state without calling start on the driver */
156int eth_init_state_only(void)
157{
158 struct udevice *current;
159 struct eth_device_priv *priv;
160
161 current = eth_get_dev();
162 if (!current || !device_active(current))
163 return -EINVAL;
164
165 priv = current->uclass_priv;
166 priv->state = ETH_STATE_ACTIVE;
167
168 return 0;
169}
170
171/* Set passive state without calling stop on the driver */
172void eth_halt_state_only(void)
173{
174 struct udevice *current;
175 struct eth_device_priv *priv;
176
177 current = eth_get_dev();
178 if (!current || !device_active(current))
179 return;
180
181 priv = current->uclass_priv;
182 priv->state = ETH_STATE_PASSIVE;
183}
184
185int eth_get_dev_index(void)
186{
187 if (eth_get_dev())
188 return eth_get_dev()->seq;
189 return -1;
190}
191
192static int eth_write_hwaddr(struct udevice *dev)
193{
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200194 struct eth_pdata *pdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700195 int ret = 0;
196
197 if (!dev || !device_active(dev))
198 return -EINVAL;
199
200 /* seq is valid since the device is active */
201 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200202 pdata = dev->platdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700203 if (!is_valid_ethaddr(pdata->enetaddr)) {
204 printf("\nError: %s address %pM illegal value\n",
205 dev->name, pdata->enetaddr);
206 return -EINVAL;
207 }
208
209 /*
210 * Drivers are allowed to decide not to implement this at
211 * run-time. E.g. Some devices may use it and some may not.
212 */
213 ret = eth_get_ops(dev)->write_hwaddr(dev);
214 if (ret == -ENOSYS)
215 ret = 0;
216 if (ret)
217 printf("\nWarning: %s failed to set MAC address\n",
218 dev->name);
219 }
220
221 return ret;
222}
223
224static int on_ethaddr(const char *name, const char *value, enum env_op op,
225 int flags)
226{
227 int index;
228 int retval;
229 struct udevice *dev;
230
231 /* look for an index after "eth" */
232 index = simple_strtoul(name + 3, NULL, 10);
233
234 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
235 if (!retval) {
236 struct eth_pdata *pdata = dev->platdata;
237 switch (op) {
238 case env_op_create:
239 case env_op_overwrite:
Joe Hershbergerfb8977c2019-09-13 19:21:16 -0500240 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzerc86ff7f2016-09-02 14:48:17 +0200241 eth_write_hwaddr(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700242 break;
243 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100244 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700245 }
246 }
247
248 return 0;
249}
250U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
251
252int eth_init(void)
253{
Simon Glass00caae62017-08-03 12:22:12 -0600254 char *ethact = env_get("ethact");
255 char *ethrotate = env_get("ethrotate");
Simon Glassdb9391e2016-01-17 14:52:00 -0700256 struct udevice *current = NULL;
257 struct udevice *old_current;
258 int ret = -ENODEV;
259
260 /*
261 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
262 * is already set to an ethernet device, we should stick to 'ethact'.
263 */
264 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
265 if (ethact) {
266 current = eth_get_dev_by_name(ethact);
267 if (!current)
268 return -EINVAL;
269 }
270 }
271
272 if (!current) {
273 current = eth_get_dev();
274 if (!current) {
275 printf("No ethernet found.\n");
276 return -ENODEV;
277 }
278 }
279
280 old_current = current;
281 do {
282 if (current) {
283 debug("Trying %s\n", current->name);
284
285 if (device_active(current)) {
286 ret = eth_get_ops(current)->start(current);
287 if (ret >= 0) {
288 struct eth_device_priv *priv =
289 current->uclass_priv;
290
291 priv->state = ETH_STATE_ACTIVE;
292 return 0;
293 }
294 } else {
295 ret = eth_errno;
296 }
297
298 debug("FAIL\n");
299 } else {
300 debug("PROBE FAIL\n");
301 }
302
303 /*
304 * If ethrotate is enabled, this will change "current",
305 * otherwise we will drop out of this while loop immediately
306 */
307 eth_try_another(0);
308 /* This will ensure the new "current" attempted to probe */
309 current = eth_get_dev();
310 } while (old_current != current);
311
312 return ret;
313}
314
315void eth_halt(void)
316{
317 struct udevice *current;
318 struct eth_device_priv *priv;
319
320 current = eth_get_dev();
Joe Hershberger68acb512018-07-02 14:47:46 -0500321 if (!current || !eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700322 return;
323
324 eth_get_ops(current)->stop(current);
325 priv = current->uclass_priv;
Jean-Jacques Hiblotc3211702018-08-09 16:17:41 +0200326 if (priv)
327 priv->state = ETH_STATE_PASSIVE;
Simon Glassdb9391e2016-01-17 14:52:00 -0700328}
329
330int eth_is_active(struct udevice *dev)
331{
332 struct eth_device_priv *priv;
333
334 if (!dev || !device_active(dev))
335 return 0;
336
337 priv = dev_get_uclass_priv(dev);
338 return priv->state == ETH_STATE_ACTIVE;
339}
340
341int eth_send(void *packet, int length)
342{
343 struct udevice *current;
344 int ret;
345
346 current = eth_get_dev();
347 if (!current)
348 return -ENODEV;
349
Alexander Grafa532e2f2018-03-15 15:07:09 +0100350 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700351 return -EINVAL;
352
353 ret = eth_get_ops(current)->send(current, packet, length);
354 if (ret < 0) {
355 /* We cannot completely return the error at present */
356 debug("%s: send() returned error %d\n", __func__, ret);
357 }
Ramon Fried3eaac632019-07-18 21:43:30 +0300358#if defined(CONFIG_CMD_PCAP)
359 if (ret >= 0)
360 pcap_post(packet, length, true);
361#endif
Simon Glassdb9391e2016-01-17 14:52:00 -0700362 return ret;
363}
364
365int eth_rx(void)
366{
367 struct udevice *current;
368 uchar *packet;
369 int flags;
370 int ret;
371 int i;
372
373 current = eth_get_dev();
374 if (!current)
375 return -ENODEV;
376
Alexander Grafa532e2f2018-03-15 15:07:09 +0100377 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700378 return -EINVAL;
379
380 /* Process up to 32 packets at one time */
381 flags = ETH_RECV_CHECK_DEVICE;
382 for (i = 0; i < 32; i++) {
383 ret = eth_get_ops(current)->recv(current, flags, &packet);
384 flags = 0;
385 if (ret > 0)
386 net_process_received_packet(packet, ret);
387 if (ret >= 0 && eth_get_ops(current)->free_pkt)
388 eth_get_ops(current)->free_pkt(current, packet, ret);
389 if (ret <= 0)
390 break;
391 }
392 if (ret == -EAGAIN)
393 ret = 0;
394 if (ret < 0) {
395 /* We cannot completely return the error at present */
396 debug("%s: recv() returned error %d\n", __func__, ret);
397 }
398 return ret;
399}
400
401int eth_initialize(void)
402{
403 int num_devices = 0;
404 struct udevice *dev;
405
406 eth_common_init();
407
408 /*
409 * Devices need to write the hwaddr even if not started so that Linux
410 * will have access to the hwaddr that u-boot stored for the device.
411 * This is accomplished by attempting to probe each device and calling
412 * their write_hwaddr() operation.
413 */
Mario Six3ce43042018-04-27 14:52:56 +0200414 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700415 if (!dev) {
416 printf("No ethernet found.\n");
417 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
418 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600419 char *ethprime = env_get("ethprime");
Simon Glassdb9391e2016-01-17 14:52:00 -0700420 struct udevice *prime_dev = NULL;
421
422 if (ethprime)
423 prime_dev = eth_get_dev_by_name(ethprime);
424 if (prime_dev) {
425 eth_set_dev(prime_dev);
426 eth_current_changed();
427 } else {
428 eth_set_dev(NULL);
429 }
430
431 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
432 do {
Michael Walle19820db2019-10-22 01:03:10 +0200433 if (dev->seq != -1) {
434 if (num_devices)
435 printf(", ");
Simon Glassdb9391e2016-01-17 14:52:00 -0700436
Michael Walle19820db2019-10-22 01:03:10 +0200437 printf("eth%d: %s", dev->seq, dev->name);
Simon Glassdb9391e2016-01-17 14:52:00 -0700438
Michael Walle19820db2019-10-22 01:03:10 +0200439 if (ethprime && dev == prime_dev)
440 printf(" [PRIME]");
441 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700442
443 eth_write_hwaddr(dev);
444
Michael Walle19820db2019-10-22 01:03:10 +0200445 if (dev->seq != -1)
446 num_devices++;
Mario Six3ce43042018-04-27 14:52:56 +0200447 uclass_next_device_check(&dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700448 } while (dev);
449
Michael Walle19820db2019-10-22 01:03:10 +0200450 if (!num_devices)
451 printf("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700452 putc('\n');
453 }
454
455 return num_devices;
456}
457
458static int eth_post_bind(struct udevice *dev)
459{
460 if (strchr(dev->name, ' ')) {
461 printf("\nError: eth device name \"%s\" has a space!\n",
462 dev->name);
463 return -EINVAL;
464 }
465
Ye Li5fe419e2020-05-03 22:41:14 +0800466#ifdef CONFIG_DM_ETH_PHY
467 eth_phy_binds_nodes(dev);
468#endif
469
Simon Glassdb9391e2016-01-17 14:52:00 -0700470 return 0;
471}
472
473static int eth_pre_unbind(struct udevice *dev)
474{
475 /* Don't hang onto a pointer that is going away */
476 if (dev == eth_get_uclass_priv()->current)
477 eth_set_dev(NULL);
478
479 return 0;
480}
481
Thierry Reding379af672019-05-20 17:59:57 +0200482static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
483{
484#if IS_ENABLED(CONFIG_OF_CONTROL)
485 const uint8_t *p;
486
487 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
488 if (!p)
489 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
490
491 if (!p)
492 return false;
493
494 memcpy(mac, p, ARP_HLEN);
495
496 return true;
497#else
498 return false;
499#endif
500}
501
Simon Glassdb9391e2016-01-17 14:52:00 -0700502static int eth_post_probe(struct udevice *dev)
503{
504 struct eth_device_priv *priv = dev->uclass_priv;
505 struct eth_pdata *pdata = dev->platdata;
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100506 unsigned char env_enetaddr[ARP_HLEN];
Michal Simekd4172c92020-03-16 11:36:17 +0100507 char *source = "DT";
Simon Glassdb9391e2016-01-17 14:52:00 -0700508
509#if defined(CONFIG_NEEDS_MANUAL_RELOC)
510 struct eth_ops *ops = eth_get_ops(dev);
511 static int reloc_done;
512
513 if (!reloc_done) {
514 if (ops->start)
515 ops->start += gd->reloc_off;
516 if (ops->send)
517 ops->send += gd->reloc_off;
518 if (ops->recv)
519 ops->recv += gd->reloc_off;
520 if (ops->free_pkt)
521 ops->free_pkt += gd->reloc_off;
522 if (ops->stop)
523 ops->stop += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700524 if (ops->mcast)
525 ops->mcast += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700526 if (ops->write_hwaddr)
527 ops->write_hwaddr += gd->reloc_off;
528 if (ops->read_rom_hwaddr)
529 ops->read_rom_hwaddr += gd->reloc_off;
530
531 reloc_done++;
532 }
533#endif
534
535 priv->state = ETH_STATE_INIT;
536
Thierry Reding379af672019-05-20 17:59:57 +0200537 /* Check if the device has a valid MAC address in device tree */
538 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
539 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simekd4172c92020-03-16 11:36:17 +0100540 source = "ROM";
Thierry Reding379af672019-05-20 17:59:57 +0200541 /* Check if the device has a MAC address in ROM */
542 if (eth_get_ops(dev)->read_rom_hwaddr)
543 eth_get_ops(dev)->read_rom_hwaddr(dev);
544 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700545
Simon Glass35affd72017-08-03 12:22:14 -0600546 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700547 if (!is_zero_ethaddr(env_enetaddr)) {
548 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100549 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700550 printf("\nWarning: %s MAC addresses don't match:\n",
551 dev->name);
Michal Simekd4172c92020-03-16 11:36:17 +0100552 printf("Address in %s is\t\t%pM\n",
553 source, pdata->enetaddr);
554 printf("Address in environment is\t%pM\n",
Simon Glassdb9391e2016-01-17 14:52:00 -0700555 env_enetaddr);
556 }
557
558 /* Override the ROM MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100559 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700560 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassfd1e9592017-08-03 12:22:11 -0600561 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
Michal Simekd4172c92020-03-16 11:36:17 +0100562 printf("\nWarning: %s using MAC address from %s\n",
563 dev->name, source);
Siva Durga Prasad Paladuguaa555fe2016-11-02 12:52:13 +0100564 } else if (is_zero_ethaddr(pdata->enetaddr) ||
565 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700566#ifdef CONFIG_NET_RANDOM_ETHADDR
567 net_random_ethaddr(pdata->enetaddr);
568 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
569 dev->name, dev->seq, pdata->enetaddr);
570#else
571 printf("\nError: %s address not set.\n",
572 dev->name);
573 return -EINVAL;
574#endif
575 }
576
Thierry Redingb743bbd2019-05-20 17:59:56 +0200577 eth_write_hwaddr(dev);
578
Simon Glassdb9391e2016-01-17 14:52:00 -0700579 return 0;
580}
581
582static int eth_pre_remove(struct udevice *dev)
583{
584 struct eth_pdata *pdata = dev->platdata;
585
586 eth_get_ops(dev)->stop(dev);
587
588 /* clear the MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100589 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700590
591 return 0;
592}
593
594UCLASS_DRIVER(eth) = {
595 .name = "eth",
596 .id = UCLASS_ETH,
597 .post_bind = eth_post_bind,
598 .pre_unbind = eth_pre_unbind,
599 .post_probe = eth_post_probe,
600 .pre_remove = eth_pre_remove,
601 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
602 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
603 .flags = DM_UC_FLAG_SEQ_ALIAS,
604};