blob: e14695c0f16d8b8eaec3fdfb4daf13b5e7cf4629 [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();
Sean Anderson35e82982020-09-12 17:45:43 -040078 if (!uc_priv)
79 return NULL;
80
Simon Glassc6011d22016-01-17 14:52:00 -070081 if (!uc_priv->current)
82 eth_errno = uclass_first_device(UCLASS_ETH,
83 &uc_priv->current);
84 return uc_priv->current;
85}
86
87/*
88 * Typically this will just store a device pointer.
89 * In case it was not probed, we will attempt to do so.
90 * dev may be NULL to unset the active device.
91 */
92void eth_set_dev(struct udevice *dev)
93{
94 if (dev && !device_active(dev)) {
95 eth_errno = device_probe(dev);
96 if (eth_errno)
97 dev = NULL;
98 }
99
100 eth_get_uclass_priv()->current = dev;
101}
102
103/*
104 * Find the udevice that either has the name passed in as devname or has an
105 * alias named devname.
106 */
107struct udevice *eth_get_dev_by_name(const char *devname)
108{
109 int seq = -1;
110 char *endp = NULL;
111 const char *startp = NULL;
112 struct udevice *it;
113 struct uclass *uc;
114 int len = strlen("eth");
Peng Fan46ec1042020-05-03 22:41:13 +0800115 int ret;
Simon Glassc6011d22016-01-17 14:52:00 -0700116
117 /* Must be longer than 3 to be an alias */
118 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
119 startp = devname + len;
120 seq = simple_strtoul(startp, &endp, 10);
121 }
122
Peng Fan46ec1042020-05-03 22:41:13 +0800123 ret = uclass_get(UCLASS_ETH, &uc);
124 if (ret)
125 return NULL;
126
Simon Glassc6011d22016-01-17 14:52:00 -0700127 uclass_foreach_dev(it, uc) {
128 /*
129 * We need the seq to be valid, so try to probe it.
130 * If the probe fails, the seq will not match since it will be
131 * -1 instead of what we are looking for.
132 * We don't care about errors from probe here. Either they won't
133 * match an alias or it will match a literal name and we'll pick
134 * up the error when we try to probe again in eth_set_dev().
135 */
136 if (device_probe(it))
137 continue;
138 /* Check for the name or the sequence number to match */
139 if (strcmp(it->name, devname) == 0 ||
140 (endp > startp && it->seq == seq))
141 return it;
142 }
143
144 return NULL;
145}
146
147unsigned char *eth_get_ethaddr(void)
148{
149 struct eth_pdata *pdata;
150
151 if (eth_get_dev()) {
152 pdata = eth_get_dev()->platdata;
153 return pdata->enetaddr;
154 }
155
156 return NULL;
157}
158
159/* Set active state without calling start on the driver */
160int eth_init_state_only(void)
161{
162 struct udevice *current;
163 struct eth_device_priv *priv;
164
165 current = eth_get_dev();
166 if (!current || !device_active(current))
167 return -EINVAL;
168
169 priv = current->uclass_priv;
170 priv->state = ETH_STATE_ACTIVE;
171
172 return 0;
173}
174
175/* Set passive state without calling stop on the driver */
176void eth_halt_state_only(void)
177{
178 struct udevice *current;
179 struct eth_device_priv *priv;
180
181 current = eth_get_dev();
182 if (!current || !device_active(current))
183 return;
184
185 priv = current->uclass_priv;
186 priv->state = ETH_STATE_PASSIVE;
187}
188
189int eth_get_dev_index(void)
190{
191 if (eth_get_dev())
192 return eth_get_dev()->seq;
193 return -1;
194}
195
196static int eth_write_hwaddr(struct udevice *dev)
197{
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200198 struct eth_pdata *pdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700199 int ret = 0;
200
201 if (!dev || !device_active(dev))
202 return -EINVAL;
203
204 /* seq is valid since the device is active */
205 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
xypron.glpk@gmx.de0211e392017-05-16 05:07:01 +0200206 pdata = dev->platdata;
Simon Glassc6011d22016-01-17 14:52:00 -0700207 if (!is_valid_ethaddr(pdata->enetaddr)) {
208 printf("\nError: %s address %pM illegal value\n",
209 dev->name, pdata->enetaddr);
210 return -EINVAL;
211 }
212
213 /*
214 * Drivers are allowed to decide not to implement this at
215 * run-time. E.g. Some devices may use it and some may not.
216 */
217 ret = eth_get_ops(dev)->write_hwaddr(dev);
218 if (ret == -ENOSYS)
219 ret = 0;
220 if (ret)
221 printf("\nWarning: %s failed to set MAC address\n",
222 dev->name);
223 }
224
225 return ret;
226}
227
228static int on_ethaddr(const char *name, const char *value, enum env_op op,
229 int flags)
230{
231 int index;
232 int retval;
233 struct udevice *dev;
234
235 /* look for an index after "eth" */
236 index = simple_strtoul(name + 3, NULL, 10);
237
238 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
239 if (!retval) {
240 struct eth_pdata *pdata = dev->platdata;
241 switch (op) {
242 case env_op_create:
243 case env_op_overwrite:
Joe Hershberger8e7545e2019-09-13 19:21:16 -0500244 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzer3071f072016-09-02 14:48:17 +0200245 eth_write_hwaddr(dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700246 break;
247 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100248 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700249 }
250 }
251
252 return 0;
253}
254U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
255
256int eth_init(void)
257{
Simon Glass64b723f2017-08-03 12:22:12 -0600258 char *ethact = env_get("ethact");
259 char *ethrotate = env_get("ethrotate");
Simon Glassc6011d22016-01-17 14:52:00 -0700260 struct udevice *current = NULL;
261 struct udevice *old_current;
262 int ret = -ENODEV;
263
264 /*
265 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
266 * is already set to an ethernet device, we should stick to 'ethact'.
267 */
268 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
269 if (ethact) {
270 current = eth_get_dev_by_name(ethact);
271 if (!current)
272 return -EINVAL;
273 }
274 }
275
276 if (!current) {
277 current = eth_get_dev();
278 if (!current) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200279 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700280 return -ENODEV;
281 }
282 }
283
284 old_current = current;
285 do {
286 if (current) {
287 debug("Trying %s\n", current->name);
288
289 if (device_active(current)) {
290 ret = eth_get_ops(current)->start(current);
291 if (ret >= 0) {
292 struct eth_device_priv *priv =
293 current->uclass_priv;
294
295 priv->state = ETH_STATE_ACTIVE;
296 return 0;
297 }
298 } else {
299 ret = eth_errno;
300 }
301
302 debug("FAIL\n");
303 } else {
304 debug("PROBE FAIL\n");
305 }
306
307 /*
308 * If ethrotate is enabled, this will change "current",
309 * otherwise we will drop out of this while loop immediately
310 */
311 eth_try_another(0);
312 /* This will ensure the new "current" attempted to probe */
313 current = eth_get_dev();
314 } while (old_current != current);
315
316 return ret;
317}
318
319void eth_halt(void)
320{
321 struct udevice *current;
322 struct eth_device_priv *priv;
323
324 current = eth_get_dev();
Joe Hershberger3c91d152018-07-02 14:47:46 -0500325 if (!current || !eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700326 return;
327
328 eth_get_ops(current)->stop(current);
329 priv = current->uclass_priv;
Jean-Jacques Hiblotd21faa52018-08-09 16:17:41 +0200330 if (priv)
331 priv->state = ETH_STATE_PASSIVE;
Simon Glassc6011d22016-01-17 14:52:00 -0700332}
333
334int eth_is_active(struct udevice *dev)
335{
336 struct eth_device_priv *priv;
337
338 if (!dev || !device_active(dev))
339 return 0;
340
341 priv = dev_get_uclass_priv(dev);
342 return priv->state == ETH_STATE_ACTIVE;
343}
344
345int eth_send(void *packet, int length)
346{
347 struct udevice *current;
348 int ret;
349
350 current = eth_get_dev();
351 if (!current)
352 return -ENODEV;
353
Alexander Graf71511492018-03-15 15:07:09 +0100354 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700355 return -EINVAL;
356
357 ret = eth_get_ops(current)->send(current, packet, length);
358 if (ret < 0) {
359 /* We cannot completely return the error at present */
360 debug("%s: send() returned error %d\n", __func__, ret);
361 }
Ramon Friedac598c12019-07-18 21:43:30 +0300362#if defined(CONFIG_CMD_PCAP)
363 if (ret >= 0)
364 pcap_post(packet, length, true);
365#endif
Simon Glassc6011d22016-01-17 14:52:00 -0700366 return ret;
367}
368
369int eth_rx(void)
370{
371 struct udevice *current;
372 uchar *packet;
373 int flags;
374 int ret;
375 int i;
376
377 current = eth_get_dev();
378 if (!current)
379 return -ENODEV;
380
Alexander Graf71511492018-03-15 15:07:09 +0100381 if (!eth_is_active(current))
Simon Glassc6011d22016-01-17 14:52:00 -0700382 return -EINVAL;
383
384 /* Process up to 32 packets at one time */
385 flags = ETH_RECV_CHECK_DEVICE;
Patrick Wildtd47430a2020-10-07 11:03:30 +0200386 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
Simon Glassc6011d22016-01-17 14:52:00 -0700387 ret = eth_get_ops(current)->recv(current, flags, &packet);
388 flags = 0;
389 if (ret > 0)
390 net_process_received_packet(packet, ret);
391 if (ret >= 0 && eth_get_ops(current)->free_pkt)
392 eth_get_ops(current)->free_pkt(current, packet, ret);
393 if (ret <= 0)
394 break;
395 }
396 if (ret == -EAGAIN)
397 ret = 0;
398 if (ret < 0) {
399 /* We cannot completely return the error at present */
400 debug("%s: recv() returned error %d\n", __func__, ret);
401 }
402 return ret;
403}
404
405int eth_initialize(void)
406{
407 int num_devices = 0;
408 struct udevice *dev;
409
410 eth_common_init();
411
412 /*
413 * Devices need to write the hwaddr even if not started so that Linux
414 * will have access to the hwaddr that u-boot stored for the device.
415 * This is accomplished by attempting to probe each device and calling
416 * their write_hwaddr() operation.
417 */
Mario Six865c3872018-04-27 14:52:56 +0200418 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700419 if (!dev) {
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200420 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700421 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
422 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600423 char *ethprime = env_get("ethprime");
Simon Glassc6011d22016-01-17 14:52:00 -0700424 struct udevice *prime_dev = NULL;
425
426 if (ethprime)
427 prime_dev = eth_get_dev_by_name(ethprime);
428 if (prime_dev) {
429 eth_set_dev(prime_dev);
430 eth_current_changed();
431 } else {
432 eth_set_dev(NULL);
433 }
434
435 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
436 do {
Michael Walle902c7962019-10-22 01:03:10 +0200437 if (dev->seq != -1) {
438 if (num_devices)
439 printf(", ");
Simon Glassc6011d22016-01-17 14:52:00 -0700440
Michael Walle902c7962019-10-22 01:03:10 +0200441 printf("eth%d: %s", dev->seq, dev->name);
Simon Glassc6011d22016-01-17 14:52:00 -0700442
Michael Walle902c7962019-10-22 01:03:10 +0200443 if (ethprime && dev == prime_dev)
444 printf(" [PRIME]");
445 }
Simon Glassc6011d22016-01-17 14:52:00 -0700446
447 eth_write_hwaddr(dev);
448
Michael Walle902c7962019-10-22 01:03:10 +0200449 if (dev->seq != -1)
450 num_devices++;
Mario Six865c3872018-04-27 14:52:56 +0200451 uclass_next_device_check(&dev);
Simon Glassc6011d22016-01-17 14:52:00 -0700452 } while (dev);
453
Michael Walle902c7962019-10-22 01:03:10 +0200454 if (!num_devices)
Heinrich Schuchardt653f3ee2020-09-14 11:00:18 +0200455 log_err("No ethernet found.\n");
Simon Glassc6011d22016-01-17 14:52:00 -0700456 putc('\n');
457 }
458
459 return num_devices;
460}
461
462static int eth_post_bind(struct udevice *dev)
463{
464 if (strchr(dev->name, ' ')) {
465 printf("\nError: eth device name \"%s\" has a space!\n",
466 dev->name);
467 return -EINVAL;
468 }
469
Ye Licd5bb772020-05-03 22:41:14 +0800470#ifdef CONFIG_DM_ETH_PHY
471 eth_phy_binds_nodes(dev);
472#endif
473
Simon Glassc6011d22016-01-17 14:52:00 -0700474 return 0;
475}
476
477static int eth_pre_unbind(struct udevice *dev)
478{
479 /* Don't hang onto a pointer that is going away */
480 if (dev == eth_get_uclass_priv()->current)
481 eth_set_dev(NULL);
482
483 return 0;
484}
485
Thierry Reding8fe08532019-05-20 17:59:57 +0200486static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
487{
488#if IS_ENABLED(CONFIG_OF_CONTROL)
489 const uint8_t *p;
490
491 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
492 if (!p)
493 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
494
495 if (!p)
496 return false;
497
498 memcpy(mac, p, ARP_HLEN);
499
500 return true;
501#else
502 return false;
503#endif
504}
505
Simon Glassc6011d22016-01-17 14:52:00 -0700506static int eth_post_probe(struct udevice *dev)
507{
508 struct eth_device_priv *priv = dev->uclass_priv;
509 struct eth_pdata *pdata = dev->platdata;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100510 unsigned char env_enetaddr[ARP_HLEN];
Michal Simek31411132020-03-16 11:36:17 +0100511 char *source = "DT";
Simon Glassc6011d22016-01-17 14:52:00 -0700512
513#if defined(CONFIG_NEEDS_MANUAL_RELOC)
514 struct eth_ops *ops = eth_get_ops(dev);
515 static int reloc_done;
516
517 if (!reloc_done) {
518 if (ops->start)
519 ops->start += gd->reloc_off;
520 if (ops->send)
521 ops->send += gd->reloc_off;
522 if (ops->recv)
523 ops->recv += gd->reloc_off;
524 if (ops->free_pkt)
525 ops->free_pkt += gd->reloc_off;
526 if (ops->stop)
527 ops->stop += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700528 if (ops->mcast)
529 ops->mcast += gd->reloc_off;
Simon Glassc6011d22016-01-17 14:52:00 -0700530 if (ops->write_hwaddr)
531 ops->write_hwaddr += gd->reloc_off;
532 if (ops->read_rom_hwaddr)
533 ops->read_rom_hwaddr += gd->reloc_off;
534
535 reloc_done++;
536 }
537#endif
538
539 priv->state = ETH_STATE_INIT;
540
Thierry Reding8fe08532019-05-20 17:59:57 +0200541 /* Check if the device has a valid MAC address in device tree */
542 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
543 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simek31411132020-03-16 11:36:17 +0100544 source = "ROM";
Thierry Reding8fe08532019-05-20 17:59:57 +0200545 /* Check if the device has a MAC address in ROM */
546 if (eth_get_ops(dev)->read_rom_hwaddr)
547 eth_get_ops(dev)->read_rom_hwaddr(dev);
548 }
Simon Glassc6011d22016-01-17 14:52:00 -0700549
Simon Glass399a9ce2017-08-03 12:22:14 -0600550 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Simon Glassc6011d22016-01-17 14:52:00 -0700551 if (!is_zero_ethaddr(env_enetaddr)) {
552 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100553 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700554 printf("\nWarning: %s MAC addresses don't match:\n",
555 dev->name);
Michal Simek31411132020-03-16 11:36:17 +0100556 printf("Address in %s is\t\t%pM\n",
557 source, pdata->enetaddr);
558 printf("Address in environment is\t%pM\n",
Simon Glassc6011d22016-01-17 14:52:00 -0700559 env_enetaddr);
560 }
561
562 /* Override the ROM MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100563 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700564 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glass8551d552017-08-03 12:22:11 -0600565 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
Siva Durga Prasad Paladugue0b7bc72016-11-02 12:52:13 +0100566 } else if (is_zero_ethaddr(pdata->enetaddr) ||
567 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassc6011d22016-01-17 14:52:00 -0700568#ifdef CONFIG_NET_RANDOM_ETHADDR
569 net_random_ethaddr(pdata->enetaddr);
570 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
571 dev->name, dev->seq, pdata->enetaddr);
572#else
573 printf("\nError: %s address not set.\n",
574 dev->name);
575 return -EINVAL;
576#endif
577 }
578
Thierry Reding13064872019-05-20 17:59:56 +0200579 eth_write_hwaddr(dev);
580
Simon Glassc6011d22016-01-17 14:52:00 -0700581 return 0;
582}
583
584static int eth_pre_remove(struct udevice *dev)
585{
586 struct eth_pdata *pdata = dev->platdata;
587
588 eth_get_ops(dev)->stop(dev);
589
590 /* clear the MAC address */
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100591 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassc6011d22016-01-17 14:52:00 -0700592
593 return 0;
594}
595
596UCLASS_DRIVER(eth) = {
597 .name = "eth",
598 .id = UCLASS_ETH,
599 .post_bind = eth_post_bind,
600 .pre_unbind = eth_pre_unbind,
601 .post_probe = eth_post_probe,
602 .pre_remove = eth_pre_remove,
603 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
604 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
605 .flags = DM_UC_FLAG_SEQ_ALIAS,
606};