blob: 48560d59f6608ff9da4c7c6312776d9047005a9e [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>
9#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060010#include <env.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070011#include <net.h>
12#include <dm/device-internal.h>
13#include <dm/uclass-internal.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030014#include <net/pcap.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070015#include "eth_internal.h"
Ye Li5fe419e2020-05-03 22:41:14 +080016#include <eth_phy.h>
Simon Glassdb9391e2016-01-17 14:52:00 -070017
Simon Glassa7c45ec2016-01-30 15:45:14 -070018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glassdb9391e2016-01-17 14:52:00 -070020/**
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 */
25struct 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 */
34struct eth_uclass_priv {
35 struct udevice *current;
36};
37
38/* eth_errno - This stores the most recent failure code from DM functions */
39static int eth_errno;
40
41static struct eth_uclass_priv *eth_get_uclass_priv(void)
42{
43 struct uclass *uc;
Peng Fand2b70202020-05-03 22:41:13 +080044 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -070045
Peng Fand2b70202020-05-03 22:41:13 +080046 ret = uclass_get(UCLASS_ETH, &uc);
47 if (ret)
48 return NULL;
49
Simon Glassdb9391e2016-01-17 14:52:00 -070050 assert(uc);
51 return uc->priv;
52}
53
54void eth_set_current_to_next(void)
55{
56 struct eth_uclass_priv *uc_priv;
57
58 uc_priv = eth_get_uclass_priv();
59 if (uc_priv->current)
60 uclass_next_device(&uc_priv->current);
61 if (!uc_priv->current)
62 uclass_first_device(UCLASS_ETH, &uc_priv->current);
63}
64
65/*
66 * Typically this will simply return the active device.
67 * In the case where the most recent active device was unset, this will attempt
68 * to return the first device. If that device doesn't exist or fails to probe,
69 * this function will return NULL.
70 */
71struct udevice *eth_get_dev(void)
72{
73 struct eth_uclass_priv *uc_priv;
74
75 uc_priv = eth_get_uclass_priv();
76 if (!uc_priv->current)
77 eth_errno = uclass_first_device(UCLASS_ETH,
78 &uc_priv->current);
79 return uc_priv->current;
80}
81
82/*
83 * Typically this will just store a device pointer.
84 * In case it was not probed, we will attempt to do so.
85 * dev may be NULL to unset the active device.
86 */
87void eth_set_dev(struct udevice *dev)
88{
89 if (dev && !device_active(dev)) {
90 eth_errno = device_probe(dev);
91 if (eth_errno)
92 dev = NULL;
93 }
94
95 eth_get_uclass_priv()->current = dev;
96}
97
98/*
99 * Find the udevice that either has the name passed in as devname or has an
100 * alias named devname.
101 */
102struct udevice *eth_get_dev_by_name(const char *devname)
103{
104 int seq = -1;
105 char *endp = NULL;
106 const char *startp = NULL;
107 struct udevice *it;
108 struct uclass *uc;
109 int len = strlen("eth");
Peng Fand2b70202020-05-03 22:41:13 +0800110 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -0700111
112 /* Must be longer than 3 to be an alias */
113 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
114 startp = devname + len;
115 seq = simple_strtoul(startp, &endp, 10);
116 }
117
Peng Fand2b70202020-05-03 22:41:13 +0800118 ret = uclass_get(UCLASS_ETH, &uc);
119 if (ret)
120 return NULL;
121
Simon Glassdb9391e2016-01-17 14:52:00 -0700122 uclass_foreach_dev(it, uc) {
123 /*
124 * We need the seq to be valid, so try to probe it.
125 * If the probe fails, the seq will not match since it will be
126 * -1 instead of what we are looking for.
127 * We don't care about errors from probe here. Either they won't
128 * match an alias or it will match a literal name and we'll pick
129 * up the error when we try to probe again in eth_set_dev().
130 */
131 if (device_probe(it))
132 continue;
133 /* Check for the name or the sequence number to match */
134 if (strcmp(it->name, devname) == 0 ||
135 (endp > startp && it->seq == seq))
136 return it;
137 }
138
139 return NULL;
140}
141
142unsigned char *eth_get_ethaddr(void)
143{
144 struct eth_pdata *pdata;
145
146 if (eth_get_dev()) {
147 pdata = eth_get_dev()->platdata;
148 return pdata->enetaddr;
149 }
150
151 return NULL;
152}
153
154/* Set active state without calling start on the driver */
155int eth_init_state_only(void)
156{
157 struct udevice *current;
158 struct eth_device_priv *priv;
159
160 current = eth_get_dev();
161 if (!current || !device_active(current))
162 return -EINVAL;
163
164 priv = current->uclass_priv;
165 priv->state = ETH_STATE_ACTIVE;
166
167 return 0;
168}
169
170/* Set passive state without calling stop on the driver */
171void eth_halt_state_only(void)
172{
173 struct udevice *current;
174 struct eth_device_priv *priv;
175
176 current = eth_get_dev();
177 if (!current || !device_active(current))
178 return;
179
180 priv = current->uclass_priv;
181 priv->state = ETH_STATE_PASSIVE;
182}
183
184int eth_get_dev_index(void)
185{
186 if (eth_get_dev())
187 return eth_get_dev()->seq;
188 return -1;
189}
190
191static int eth_write_hwaddr(struct udevice *dev)
192{
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200193 struct eth_pdata *pdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700194 int ret = 0;
195
196 if (!dev || !device_active(dev))
197 return -EINVAL;
198
199 /* seq is valid since the device is active */
200 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200201 pdata = dev->platdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700202 if (!is_valid_ethaddr(pdata->enetaddr)) {
203 printf("\nError: %s address %pM illegal value\n",
204 dev->name, pdata->enetaddr);
205 return -EINVAL;
206 }
207
208 /*
209 * Drivers are allowed to decide not to implement this at
210 * run-time. E.g. Some devices may use it and some may not.
211 */
212 ret = eth_get_ops(dev)->write_hwaddr(dev);
213 if (ret == -ENOSYS)
214 ret = 0;
215 if (ret)
216 printf("\nWarning: %s failed to set MAC address\n",
217 dev->name);
218 }
219
220 return ret;
221}
222
223static int on_ethaddr(const char *name, const char *value, enum env_op op,
224 int flags)
225{
226 int index;
227 int retval;
228 struct udevice *dev;
229
230 /* look for an index after "eth" */
231 index = simple_strtoul(name + 3, NULL, 10);
232
233 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
234 if (!retval) {
235 struct eth_pdata *pdata = dev->platdata;
236 switch (op) {
237 case env_op_create:
238 case env_op_overwrite:
Joe Hershbergerfb8977c2019-09-13 19:21:16 -0500239 string_to_enetaddr(value, pdata->enetaddr);
Hannes Schmelzerc86ff7f2016-09-02 14:48:17 +0200240 eth_write_hwaddr(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700241 break;
242 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100243 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700244 }
245 }
246
247 return 0;
248}
249U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
250
251int eth_init(void)
252{
Simon Glass00caae62017-08-03 12:22:12 -0600253 char *ethact = env_get("ethact");
254 char *ethrotate = env_get("ethrotate");
Simon Glassdb9391e2016-01-17 14:52:00 -0700255 struct udevice *current = NULL;
256 struct udevice *old_current;
257 int ret = -ENODEV;
258
259 /*
260 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
261 * is already set to an ethernet device, we should stick to 'ethact'.
262 */
263 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
264 if (ethact) {
265 current = eth_get_dev_by_name(ethact);
266 if (!current)
267 return -EINVAL;
268 }
269 }
270
271 if (!current) {
272 current = eth_get_dev();
273 if (!current) {
274 printf("No ethernet found.\n");
275 return -ENODEV;
276 }
277 }
278
279 old_current = current;
280 do {
281 if (current) {
282 debug("Trying %s\n", current->name);
283
284 if (device_active(current)) {
285 ret = eth_get_ops(current)->start(current);
286 if (ret >= 0) {
287 struct eth_device_priv *priv =
288 current->uclass_priv;
289
290 priv->state = ETH_STATE_ACTIVE;
291 return 0;
292 }
293 } else {
294 ret = eth_errno;
295 }
296
297 debug("FAIL\n");
298 } else {
299 debug("PROBE FAIL\n");
300 }
301
302 /*
303 * If ethrotate is enabled, this will change "current",
304 * otherwise we will drop out of this while loop immediately
305 */
306 eth_try_another(0);
307 /* This will ensure the new "current" attempted to probe */
308 current = eth_get_dev();
309 } while (old_current != current);
310
311 return ret;
312}
313
314void eth_halt(void)
315{
316 struct udevice *current;
317 struct eth_device_priv *priv;
318
319 current = eth_get_dev();
Joe Hershberger68acb512018-07-02 14:47:46 -0500320 if (!current || !eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700321 return;
322
323 eth_get_ops(current)->stop(current);
324 priv = current->uclass_priv;
Jean-Jacques Hiblotc3211702018-08-09 16:17:41 +0200325 if (priv)
326 priv->state = ETH_STATE_PASSIVE;
Simon Glassdb9391e2016-01-17 14:52:00 -0700327}
328
329int eth_is_active(struct udevice *dev)
330{
331 struct eth_device_priv *priv;
332
333 if (!dev || !device_active(dev))
334 return 0;
335
336 priv = dev_get_uclass_priv(dev);
337 return priv->state == ETH_STATE_ACTIVE;
338}
339
340int eth_send(void *packet, int length)
341{
342 struct udevice *current;
343 int ret;
344
345 current = eth_get_dev();
346 if (!current)
347 return -ENODEV;
348
Alexander Grafa532e2f2018-03-15 15:07:09 +0100349 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700350 return -EINVAL;
351
352 ret = eth_get_ops(current)->send(current, packet, length);
353 if (ret < 0) {
354 /* We cannot completely return the error at present */
355 debug("%s: send() returned error %d\n", __func__, ret);
356 }
Ramon Fried3eaac632019-07-18 21:43:30 +0300357#if defined(CONFIG_CMD_PCAP)
358 if (ret >= 0)
359 pcap_post(packet, length, true);
360#endif
Simon Glassdb9391e2016-01-17 14:52:00 -0700361 return ret;
362}
363
364int eth_rx(void)
365{
366 struct udevice *current;
367 uchar *packet;
368 int flags;
369 int ret;
370 int i;
371
372 current = eth_get_dev();
373 if (!current)
374 return -ENODEV;
375
Alexander Grafa532e2f2018-03-15 15:07:09 +0100376 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700377 return -EINVAL;
378
379 /* Process up to 32 packets at one time */
380 flags = ETH_RECV_CHECK_DEVICE;
381 for (i = 0; i < 32; i++) {
382 ret = eth_get_ops(current)->recv(current, flags, &packet);
383 flags = 0;
384 if (ret > 0)
385 net_process_received_packet(packet, ret);
386 if (ret >= 0 && eth_get_ops(current)->free_pkt)
387 eth_get_ops(current)->free_pkt(current, packet, ret);
388 if (ret <= 0)
389 break;
390 }
391 if (ret == -EAGAIN)
392 ret = 0;
393 if (ret < 0) {
394 /* We cannot completely return the error at present */
395 debug("%s: recv() returned error %d\n", __func__, ret);
396 }
397 return ret;
398}
399
400int eth_initialize(void)
401{
402 int num_devices = 0;
403 struct udevice *dev;
404
405 eth_common_init();
406
407 /*
408 * Devices need to write the hwaddr even if not started so that Linux
409 * will have access to the hwaddr that u-boot stored for the device.
410 * This is accomplished by attempting to probe each device and calling
411 * their write_hwaddr() operation.
412 */
Mario Six3ce43042018-04-27 14:52:56 +0200413 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700414 if (!dev) {
415 printf("No ethernet found.\n");
416 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
417 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600418 char *ethprime = env_get("ethprime");
Simon Glassdb9391e2016-01-17 14:52:00 -0700419 struct udevice *prime_dev = NULL;
420
421 if (ethprime)
422 prime_dev = eth_get_dev_by_name(ethprime);
423 if (prime_dev) {
424 eth_set_dev(prime_dev);
425 eth_current_changed();
426 } else {
427 eth_set_dev(NULL);
428 }
429
430 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
431 do {
Michael Walle19820db2019-10-22 01:03:10 +0200432 if (dev->seq != -1) {
433 if (num_devices)
434 printf(", ");
Simon Glassdb9391e2016-01-17 14:52:00 -0700435
Michael Walle19820db2019-10-22 01:03:10 +0200436 printf("eth%d: %s", dev->seq, dev->name);
Simon Glassdb9391e2016-01-17 14:52:00 -0700437
Michael Walle19820db2019-10-22 01:03:10 +0200438 if (ethprime && dev == prime_dev)
439 printf(" [PRIME]");
440 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700441
442 eth_write_hwaddr(dev);
443
Michael Walle19820db2019-10-22 01:03:10 +0200444 if (dev->seq != -1)
445 num_devices++;
Mario Six3ce43042018-04-27 14:52:56 +0200446 uclass_next_device_check(&dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700447 } while (dev);
448
Michael Walle19820db2019-10-22 01:03:10 +0200449 if (!num_devices)
450 printf("No ethernet found.\n");
Simon Glassdb9391e2016-01-17 14:52:00 -0700451 putc('\n');
452 }
453
454 return num_devices;
455}
456
457static int eth_post_bind(struct udevice *dev)
458{
459 if (strchr(dev->name, ' ')) {
460 printf("\nError: eth device name \"%s\" has a space!\n",
461 dev->name);
462 return -EINVAL;
463 }
464
Ye Li5fe419e2020-05-03 22:41:14 +0800465#ifdef CONFIG_DM_ETH_PHY
466 eth_phy_binds_nodes(dev);
467#endif
468
Simon Glassdb9391e2016-01-17 14:52:00 -0700469 return 0;
470}
471
472static int eth_pre_unbind(struct udevice *dev)
473{
474 /* Don't hang onto a pointer that is going away */
475 if (dev == eth_get_uclass_priv()->current)
476 eth_set_dev(NULL);
477
478 return 0;
479}
480
Thierry Reding379af672019-05-20 17:59:57 +0200481static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
482{
483#if IS_ENABLED(CONFIG_OF_CONTROL)
484 const uint8_t *p;
485
486 p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
487 if (!p)
488 p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
489
490 if (!p)
491 return false;
492
493 memcpy(mac, p, ARP_HLEN);
494
495 return true;
496#else
497 return false;
498#endif
499}
500
Simon Glassdb9391e2016-01-17 14:52:00 -0700501static int eth_post_probe(struct udevice *dev)
502{
503 struct eth_device_priv *priv = dev->uclass_priv;
504 struct eth_pdata *pdata = dev->platdata;
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100505 unsigned char env_enetaddr[ARP_HLEN];
Michal Simekd4172c92020-03-16 11:36:17 +0100506 char *source = "DT";
Simon Glassdb9391e2016-01-17 14:52:00 -0700507
508#if defined(CONFIG_NEEDS_MANUAL_RELOC)
509 struct eth_ops *ops = eth_get_ops(dev);
510 static int reloc_done;
511
512 if (!reloc_done) {
513 if (ops->start)
514 ops->start += gd->reloc_off;
515 if (ops->send)
516 ops->send += gd->reloc_off;
517 if (ops->recv)
518 ops->recv += gd->reloc_off;
519 if (ops->free_pkt)
520 ops->free_pkt += gd->reloc_off;
521 if (ops->stop)
522 ops->stop += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700523 if (ops->mcast)
524 ops->mcast += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700525 if (ops->write_hwaddr)
526 ops->write_hwaddr += gd->reloc_off;
527 if (ops->read_rom_hwaddr)
528 ops->read_rom_hwaddr += gd->reloc_off;
529
530 reloc_done++;
531 }
532#endif
533
534 priv->state = ETH_STATE_INIT;
535
Thierry Reding379af672019-05-20 17:59:57 +0200536 /* Check if the device has a valid MAC address in device tree */
537 if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
538 !is_valid_ethaddr(pdata->enetaddr)) {
Michal Simekd4172c92020-03-16 11:36:17 +0100539 source = "ROM";
Thierry Reding379af672019-05-20 17:59:57 +0200540 /* Check if the device has a MAC address in ROM */
541 if (eth_get_ops(dev)->read_rom_hwaddr)
542 eth_get_ops(dev)->read_rom_hwaddr(dev);
543 }
Simon Glassdb9391e2016-01-17 14:52:00 -0700544
Simon Glass35affd72017-08-03 12:22:14 -0600545 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700546 if (!is_zero_ethaddr(env_enetaddr)) {
547 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100548 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700549 printf("\nWarning: %s MAC addresses don't match:\n",
550 dev->name);
Michal Simekd4172c92020-03-16 11:36:17 +0100551 printf("Address in %s is\t\t%pM\n",
552 source, pdata->enetaddr);
553 printf("Address in environment is\t%pM\n",
Simon Glassdb9391e2016-01-17 14:52:00 -0700554 env_enetaddr);
555 }
556
557 /* Override the ROM MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100558 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700559 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassfd1e9592017-08-03 12:22:11 -0600560 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
Michal Simekd4172c92020-03-16 11:36:17 +0100561 printf("\nWarning: %s using MAC address from %s\n",
562 dev->name, source);
Siva Durga Prasad Paladuguaa555fe2016-11-02 12:52:13 +0100563 } else if (is_zero_ethaddr(pdata->enetaddr) ||
564 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700565#ifdef CONFIG_NET_RANDOM_ETHADDR
566 net_random_ethaddr(pdata->enetaddr);
567 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
568 dev->name, dev->seq, pdata->enetaddr);
569#else
570 printf("\nError: %s address not set.\n",
571 dev->name);
572 return -EINVAL;
573#endif
574 }
575
Thierry Redingb743bbd2019-05-20 17:59:56 +0200576 eth_write_hwaddr(dev);
577
Simon Glassdb9391e2016-01-17 14:52:00 -0700578 return 0;
579}
580
581static int eth_pre_remove(struct udevice *dev)
582{
583 struct eth_pdata *pdata = dev->platdata;
584
585 eth_get_ops(dev)->stop(dev);
586
587 /* clear the MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100588 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700589
590 return 0;
591}
592
593UCLASS_DRIVER(eth) = {
594 .name = "eth",
595 .id = UCLASS_ETH,
596 .post_bind = eth_post_bind,
597 .pre_unbind = eth_pre_unbind,
598 .post_probe = eth_post_probe,
599 .pre_remove = eth_pre_remove,
600 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
601 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
602 .flags = DM_UC_FLAG_SEQ_ALIAS,
603};