blob: 602925d20f6d1a82da8f40909b886d2f9010e1e0 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05002 * (C) Copyright 2001-2015
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05004 * Joe Hershberger, National Instruments
wdenkc6097192002-11-03 00:24:07 +00005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00007 */
8
9#include <common.h>
10#include <command.h>
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -050011#include <dm.h>
Joe Hershberger7e0b0152015-05-20 14:27:26 -050012#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <net.h>
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +010014#include <miiphy.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050015#include <phy.h>
Pavel Machek222981f2014-07-13 10:27:02 +020016#include <asm/errno.h>
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -050017#include <dm/device-internal.h>
Joe Hershberger7e0b0152015-05-20 14:27:26 -050018#include <dm/uclass-internal.h>
Simon Glass0a9bde12016-01-17 14:51:57 -070019#include "eth_internal.h"
wdenkc6097192002-11-03 00:24:07 +000020
Joe Hershberger3dbe17e2015-03-22 17:09:06 -050021DECLARE_GLOBAL_DATA_PTR;
22
Mike Frysingerc34319c2009-01-29 19:43:44 -050023void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
24{
25 char *end;
26 int i;
27
28 for (i = 0; i < 6; ++i) {
29 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
30 if (addr)
31 addr = (*end) ? end + 1 : end;
32 }
33}
34
Josh Wu1bcee662015-09-01 18:22:55 +080035int eth_getenv_enetaddr(const char *name, uchar *enetaddr)
Mike Frysingerc34319c2009-01-29 19:43:44 -050036{
37 eth_parse_enetaddr(getenv(name), enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050038 return is_valid_ethaddr(enetaddr);
Mike Frysingerc34319c2009-01-29 19:43:44 -050039}
40
Josh Wu1bcee662015-09-01 18:22:55 +080041int eth_setenv_enetaddr(const char *name, const uchar *enetaddr)
Mike Frysingerc34319c2009-01-29 19:43:44 -050042{
43 char buf[20];
44
45 sprintf(buf, "%pM", enetaddr);
46
47 return setenv(name, buf);
48}
Mike Frysingere129d562009-07-15 21:31:28 -040049
Simon Glass62b36c92011-06-13 16:13:10 -070050int eth_getenv_enetaddr_by_index(const char *base_name, int index,
51 uchar *enetaddr)
Mike Frysingere129d562009-07-15 21:31:28 -040052{
53 char enetvar[32];
Simon Glass62b36c92011-06-13 16:13:10 -070054 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Mike Frysingere129d562009-07-15 21:31:28 -040055 return eth_getenv_enetaddr(enetvar, enetaddr);
56}
Mike Frysingerc34319c2009-01-29 19:43:44 -050057
Joe Hershberger6c6fe5f2012-07-10 16:23:22 -050058static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
Rob Herringc2938c02012-04-14 18:06:49 +000059 uchar *enetaddr)
60{
61 char enetvar[32];
62 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
63 return eth_setenv_enetaddr(enetvar, enetaddr);
64}
65
Ben Warren6db991a2010-04-26 11:11:46 -070066static int eth_mac_skip(int index)
67{
68 char enetvar[15];
69 char *skip_state;
Joe Hershbergerc10c0b12015-04-08 01:41:19 -050070
Ben Warren6db991a2010-04-26 11:11:46 -070071 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
Joe Hershbergerc10c0b12015-04-08 01:41:19 -050072 skip_state = getenv(enetvar);
73 return skip_state != NULL;
Ben Warren6db991a2010-04-26 11:11:46 -070074}
75
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -050076static void eth_current_changed(void);
77
Simon Glassb6121f22015-04-05 16:07:37 -060078/*
79 * CPU and board-specific Ethernet initializations. Aliased function
80 * signals caller to move on
81 */
82static int __def_eth_init(bd_t *bis)
83{
84 return -1;
85}
86int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
87int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
88
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -050089#ifdef CONFIG_DM_ETH
90/**
91 * struct eth_device_priv - private structure for each Ethernet device
92 *
93 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
94 */
95struct eth_device_priv {
96 enum eth_state_t state;
97};
98
99/**
100 * struct eth_uclass_priv - The structure attached to the uclass itself
101 *
102 * @current: The Ethernet device that the network functions are using
103 */
104struct eth_uclass_priv {
105 struct udevice *current;
106};
107
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500108/* eth_errno - This stores the most recent failure code from DM functions */
109static int eth_errno;
110
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500111static struct eth_uclass_priv *eth_get_uclass_priv(void)
112{
113 struct uclass *uc;
114
115 uclass_get(UCLASS_ETH, &uc);
116 assert(uc);
117 return uc->priv;
118}
119
120static void eth_set_current_to_next(void)
121{
122 struct eth_uclass_priv *uc_priv;
123
124 uc_priv = eth_get_uclass_priv();
125 if (uc_priv->current)
126 uclass_next_device(&uc_priv->current);
127 if (!uc_priv->current)
128 uclass_first_device(UCLASS_ETH, &uc_priv->current);
129}
130
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500131/*
132 * Typically this will simply return the active device.
133 * In the case where the most recent active device was unset, this will attempt
134 * to return the first device. If that device doesn't exist or fails to probe,
135 * this function will return NULL.
136 */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500137struct udevice *eth_get_dev(void)
138{
139 struct eth_uclass_priv *uc_priv;
140
141 uc_priv = eth_get_uclass_priv();
142 if (!uc_priv->current)
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500143 eth_errno = uclass_first_device(UCLASS_ETH,
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500144 &uc_priv->current);
145 return uc_priv->current;
146}
147
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500148/*
149 * Typically this will just store a device pointer.
150 * In case it was not probed, we will attempt to do so.
151 * dev may be NULL to unset the active device.
152 */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500153static void eth_set_dev(struct udevice *dev)
154{
Bin Mengcecd3942015-10-07 21:45:44 -0700155 if (dev && !device_active(dev)) {
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500156 eth_errno = device_probe(dev);
Bin Mengcecd3942015-10-07 21:45:44 -0700157 if (eth_errno)
158 dev = NULL;
159 }
160
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500161 eth_get_uclass_priv()->current = dev;
162}
163
Joe Hershberger279d2f62015-03-22 17:09:16 -0500164/*
165 * Find the udevice that either has the name passed in as devname or has an
166 * alias named devname.
167 */
168struct udevice *eth_get_dev_by_name(const char *devname)
169{
170 int seq = -1;
171 char *endp = NULL;
172 const char *startp = NULL;
173 struct udevice *it;
174 struct uclass *uc;
Bin Meng9ac7bbf2015-08-27 22:25:54 -0700175 int len = strlen("eth");
Joe Hershberger279d2f62015-03-22 17:09:16 -0500176
177 /* Must be longer than 3 to be an alias */
Bin Meng9ac7bbf2015-08-27 22:25:54 -0700178 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
179 startp = devname + len;
Joe Hershberger279d2f62015-03-22 17:09:16 -0500180 seq = simple_strtoul(startp, &endp, 10);
181 }
182
183 uclass_get(UCLASS_ETH, &uc);
184 uclass_foreach_dev(it, uc) {
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500185 /*
186 * We need the seq to be valid, so try to probe it.
187 * If the probe fails, the seq will not match since it will be
188 * -1 instead of what we are looking for.
189 * We don't care about errors from probe here. Either they won't
190 * match an alias or it will match a literal name and we'll pick
191 * up the error when we try to probe again in eth_set_dev().
192 */
Bin Mengcecd3942015-10-07 21:45:44 -0700193 if (device_probe(it))
194 continue;
195 /* Check for the name or the sequence number to match */
Joe Hershberger279d2f62015-03-22 17:09:16 -0500196 if (strcmp(it->name, devname) == 0 ||
197 (endp > startp && it->seq == seq))
198 return it;
199 }
200
201 return NULL;
202}
203
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500204unsigned char *eth_get_ethaddr(void)
205{
206 struct eth_pdata *pdata;
207
208 if (eth_get_dev()) {
209 pdata = eth_get_dev()->platdata;
210 return pdata->enetaddr;
211 }
212
213 return NULL;
214}
215
216/* Set active state without calling start on the driver */
217int eth_init_state_only(void)
218{
219 struct udevice *current;
220 struct eth_device_priv *priv;
221
222 current = eth_get_dev();
223 if (!current || !device_active(current))
224 return -EINVAL;
225
226 priv = current->uclass_priv;
227 priv->state = ETH_STATE_ACTIVE;
228
229 return 0;
230}
231
232/* Set passive state without calling stop on the driver */
233void eth_halt_state_only(void)
234{
235 struct udevice *current;
236 struct eth_device_priv *priv;
237
238 current = eth_get_dev();
239 if (!current || !device_active(current))
240 return;
241
242 priv = current->uclass_priv;
243 priv->state = ETH_STATE_PASSIVE;
244}
245
246int eth_get_dev_index(void)
247{
248 if (eth_get_dev())
249 return eth_get_dev()->seq;
250 return -1;
251}
252
Joe Hershbergere84319a2015-03-24 02:41:49 -0500253static int eth_write_hwaddr(struct udevice *dev)
254{
255 struct eth_pdata *pdata = dev->platdata;
256 int ret = 0;
257
258 if (!dev || !device_active(dev))
259 return -EINVAL;
260
261 /* seq is valid since the device is active */
262 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
263 if (!is_valid_ethaddr(pdata->enetaddr)) {
264 printf("\nError: %s address %pM illegal value\n",
265 dev->name, pdata->enetaddr);
266 return -EINVAL;
267 }
268
Simon Glassc91b2b72015-07-06 16:47:55 -0600269 /*
270 * Drivers are allowed to decide not to implement this at
271 * run-time. E.g. Some devices may use it and some may not.
272 */
Joe Hershbergere84319a2015-03-24 02:41:49 -0500273 ret = eth_get_ops(dev)->write_hwaddr(dev);
Simon Glassc91b2b72015-07-06 16:47:55 -0600274 if (ret == -ENOSYS)
275 ret = 0;
Joe Hershbergere84319a2015-03-24 02:41:49 -0500276 if (ret)
277 printf("\nWarning: %s failed to set MAC address\n",
278 dev->name);
279 }
280
281 return ret;
282}
283
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500284static int on_ethaddr(const char *name, const char *value, enum env_op op,
285 int flags)
286{
287 int index;
288 int retval;
289 struct udevice *dev;
290
291 /* look for an index after "eth" */
292 index = simple_strtoul(name + 3, NULL, 10);
293
294 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
295 if (!retval) {
296 struct eth_pdata *pdata = dev->platdata;
297 switch (op) {
298 case env_op_create:
299 case env_op_overwrite:
300 eth_parse_enetaddr(value, pdata->enetaddr);
301 break;
302 case env_op_delete:
303 memset(pdata->enetaddr, 0, 6);
304 }
305 }
306
307 return 0;
308}
309U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
310
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500311int eth_init(void)
312{
Bin Meng1e6c9fc2015-12-21 22:43:39 -0800313 char *ethact = getenv("ethact");
314 char *ethrotate = getenv("ethrotate");
315 struct udevice *current = NULL;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500316 struct udevice *old_current;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500317 int ret = -ENODEV;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500318
Bin Meng1e6c9fc2015-12-21 22:43:39 -0800319 /*
320 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
321 * is already set to an ethernet device, we should stick to 'ethact'.
322 */
323 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
324 if (ethact) {
325 current = eth_get_dev_by_name(ethact);
326 if (!current)
327 return -EINVAL;
328 }
329 }
330
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500331 if (!current) {
Bin Meng1e6c9fc2015-12-21 22:43:39 -0800332 current = eth_get_dev();
333 if (!current) {
334 printf("No ethernet found.\n");
335 return -ENODEV;
336 }
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500337 }
338
339 old_current = current;
340 do {
Bin Mengcecd3942015-10-07 21:45:44 -0700341 if (current) {
342 debug("Trying %s\n", current->name);
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500343
Bin Mengcecd3942015-10-07 21:45:44 -0700344 if (device_active(current)) {
345 ret = eth_get_ops(current)->start(current);
346 if (ret >= 0) {
347 struct eth_device_priv *priv =
348 current->uclass_priv;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500349
Bin Mengcecd3942015-10-07 21:45:44 -0700350 priv->state = ETH_STATE_ACTIVE;
351 return 0;
352 }
353 } else {
354 ret = eth_errno;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500355 }
Bin Mengcecd3942015-10-07 21:45:44 -0700356
357 debug("FAIL\n");
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500358 } else {
Bin Mengcecd3942015-10-07 21:45:44 -0700359 debug("PROBE FAIL\n");
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500360 }
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500361
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500362 /*
363 * If ethrotate is enabled, this will change "current",
364 * otherwise we will drop out of this while loop immediately
365 */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500366 eth_try_another(0);
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500367 /* This will ensure the new "current" attempted to probe */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500368 current = eth_get_dev();
369 } while (old_current != current);
370
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500371 return ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500372}
373
374void eth_halt(void)
375{
376 struct udevice *current;
377 struct eth_device_priv *priv;
378
379 current = eth_get_dev();
380 if (!current || !device_active(current))
381 return;
382
383 eth_get_ops(current)->stop(current);
384 priv = current->uclass_priv;
385 priv->state = ETH_STATE_PASSIVE;
386}
387
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200388int eth_is_active(struct udevice *dev)
389{
390 struct eth_device_priv *priv;
391
392 if (!dev || !device_active(dev))
393 return 0;
394
395 priv = dev_get_uclass_priv(dev);
396 return priv->state == ETH_STATE_ACTIVE;
397}
398
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500399int eth_send(void *packet, int length)
400{
401 struct udevice *current;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500402 int ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500403
404 current = eth_get_dev();
405 if (!current)
406 return -ENODEV;
407
408 if (!device_active(current))
409 return -EINVAL;
410
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500411 ret = eth_get_ops(current)->send(current, packet, length);
412 if (ret < 0) {
413 /* We cannot completely return the error at present */
414 debug("%s: send() returned error %d\n", __func__, ret);
415 }
416 return ret;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500417}
418
419int eth_rx(void)
420{
421 struct udevice *current;
Joe Hershberger8207c542015-03-22 17:09:12 -0500422 uchar *packet;
Simon Glassdc6eda32015-07-06 16:47:49 -0600423 int flags;
Joe Hershberger8207c542015-03-22 17:09:12 -0500424 int ret;
425 int i;
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500426
427 current = eth_get_dev();
428 if (!current)
429 return -ENODEV;
430
431 if (!device_active(current))
432 return -EINVAL;
433
Joe Hershberger8207c542015-03-22 17:09:12 -0500434 /* Process up to 32 packets at one time */
Simon Glassdc6eda32015-07-06 16:47:49 -0600435 flags = ETH_RECV_CHECK_DEVICE;
Joe Hershberger8207c542015-03-22 17:09:12 -0500436 for (i = 0; i < 32; i++) {
Simon Glassdc6eda32015-07-06 16:47:49 -0600437 ret = eth_get_ops(current)->recv(current, flags, &packet);
438 flags = 0;
Joe Hershberger8207c542015-03-22 17:09:12 -0500439 if (ret > 0)
440 net_process_received_packet(packet, ret);
Joe Hershbergera892dc12015-04-03 20:09:46 -0500441 if (ret >= 0 && eth_get_ops(current)->free_pkt)
442 eth_get_ops(current)->free_pkt(current, packet, ret);
443 if (ret <= 0)
Joe Hershberger8207c542015-03-22 17:09:12 -0500444 break;
445 }
446 if (ret == -EAGAIN)
447 ret = 0;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500448 if (ret < 0) {
449 /* We cannot completely return the error at present */
450 debug("%s: recv() returned error %d\n", __func__, ret);
451 }
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500452 return ret;
453}
454
455int eth_initialize(void)
456{
457 int num_devices = 0;
458 struct udevice *dev;
459
Simon Glassb6121f22015-04-05 16:07:37 -0600460 eth_common_init();
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500461
462 /*
463 * Devices need to write the hwaddr even if not started so that Linux
464 * will have access to the hwaddr that u-boot stored for the device.
465 * This is accomplished by attempting to probe each device and calling
466 * their write_hwaddr() operation.
467 */
468 uclass_first_device(UCLASS_ETH, &dev);
469 if (!dev) {
470 printf("No ethernet found.\n");
471 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
472 } else {
Joe Hershbergeraa52edd2015-03-22 17:09:17 -0500473 char *ethprime = getenv("ethprime");
474 struct udevice *prime_dev = NULL;
475
476 if (ethprime)
477 prime_dev = eth_get_dev_by_name(ethprime);
478 if (prime_dev) {
479 eth_set_dev(prime_dev);
480 eth_current_changed();
481 } else {
482 eth_set_dev(NULL);
483 }
484
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500485 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
486 do {
487 if (num_devices)
488 printf(", ");
489
490 printf("eth%d: %s", dev->seq, dev->name);
491
Joe Hershbergeraa52edd2015-03-22 17:09:17 -0500492 if (ethprime && dev == prime_dev)
493 printf(" [PRIME]");
494
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500495 eth_write_hwaddr(dev);
496
497 uclass_next_device(&dev);
498 num_devices++;
499 } while (dev);
500
501 putc('\n');
502 }
503
504 return num_devices;
505}
506
507static int eth_post_bind(struct udevice *dev)
508{
509 if (strchr(dev->name, ' ')) {
510 printf("\nError: eth device name \"%s\" has a space!\n",
511 dev->name);
512 return -EINVAL;
513 }
514
515 return 0;
516}
517
518static int eth_pre_unbind(struct udevice *dev)
519{
520 /* Don't hang onto a pointer that is going away */
521 if (dev == eth_get_uclass_priv()->current)
522 eth_set_dev(NULL);
523
524 return 0;
525}
526
527static int eth_post_probe(struct udevice *dev)
528{
529 struct eth_device_priv *priv = dev->uclass_priv;
530 struct eth_pdata *pdata = dev->platdata;
531 unsigned char env_enetaddr[6];
532
Michal Simekb5291092015-12-08 16:45:30 +0100533#if defined(CONFIG_NEEDS_MANUAL_RELOC)
534 struct eth_ops *ops = eth_get_ops(dev);
535 static int reloc_done;
536
537 if (!reloc_done) {
538 if (ops->start)
539 ops->start += gd->reloc_off;
540 if (ops->send)
541 ops->send += gd->reloc_off;
542 if (ops->recv)
543 ops->recv += gd->reloc_off;
544 if (ops->free_pkt)
545 ops->free_pkt += gd->reloc_off;
546 if (ops->stop)
547 ops->stop += gd->reloc_off;
548#ifdef CONFIG_MCAST_TFTP
549 if (ops->mcast)
550 ops->mcast += gd->reloc_off;
551#endif
552 if (ops->write_hwaddr)
553 ops->write_hwaddr += gd->reloc_off;
554 if (ops->read_rom_hwaddr)
555 ops->read_rom_hwaddr += gd->reloc_off;
556
557 reloc_done++;
558 }
559#endif
560
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500561 priv->state = ETH_STATE_INIT;
562
563 /* Check if the device has a MAC address in ROM */
564 if (eth_get_ops(dev)->read_rom_hwaddr)
565 eth_get_ops(dev)->read_rom_hwaddr(dev);
566
567 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500568 if (!is_zero_ethaddr(env_enetaddr)) {
569 if (!is_zero_ethaddr(pdata->enetaddr) &&
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500570 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
571 printf("\nWarning: %s MAC addresses don't match:\n",
572 dev->name);
573 printf("Address in SROM is %pM\n",
574 pdata->enetaddr);
575 printf("Address in environment is %pM\n",
576 env_enetaddr);
577 }
578
579 /* Override the ROM MAC address */
580 memcpy(pdata->enetaddr, env_enetaddr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500581 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500582 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
583 printf("\nWarning: %s using MAC address from ROM\n",
584 dev->name);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500585 } else if (is_zero_ethaddr(pdata->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500586#ifdef CONFIG_NET_RANDOM_ETHADDR
587 net_random_ethaddr(pdata->enetaddr);
588 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
589 dev->name, dev->seq, pdata->enetaddr);
590#else
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500591 printf("\nError: %s address not set.\n",
592 dev->name);
593 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500594#endif
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500595 }
596
597 return 0;
598}
599
600static int eth_pre_remove(struct udevice *dev)
601{
Bin Menge59ecbc2015-10-07 21:45:42 -0700602 struct eth_pdata *pdata = dev->platdata;
603
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500604 eth_get_ops(dev)->stop(dev);
605
Bin Menge59ecbc2015-10-07 21:45:42 -0700606 /* clear the MAC address */
607 memset(pdata->enetaddr, 0, 6);
608
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500609 return 0;
610}
611
612UCLASS_DRIVER(eth) = {
613 .name = "eth",
614 .id = UCLASS_ETH,
615 .post_bind = eth_post_bind,
616 .pre_unbind = eth_pre_unbind,
617 .post_probe = eth_post_probe,
618 .pre_remove = eth_pre_remove,
619 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
620 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershberger279d2f62015-03-22 17:09:16 -0500621 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500622};
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200623#endif /* #ifdef CONFIG_DM_ETH */
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500624
625#ifndef CONFIG_DM_ETH
Ben Warrend448a492008-06-23 22:57:27 -0700626
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100627#ifdef CONFIG_API
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100628static struct {
629 uchar data[PKTSIZE];
630 int length;
631} eth_rcv_bufs[PKTBUFSRX];
632
Joe Hershberger5956ded2012-05-15 08:59:07 +0000633static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100634#endif
635
Joe Hershberger9f374062012-08-03 10:59:08 +0000636static struct eth_device *eth_devices;
637struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000638
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -0500639static void eth_set_current_to_next(void)
640{
641 eth_current = eth_current->next;
642}
643
Joe Hershberger279d2f62015-03-22 17:09:16 -0500644static void eth_set_dev(struct eth_device *dev)
645{
646 eth_current = dev;
647}
648
Ben Warren97824d62010-07-29 12:56:11 -0700649struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200650{
651 struct eth_device *dev, *target_dev;
652
Helmut Raiger153bf3a2011-08-22 00:17:17 +0000653 BUG_ON(devname == NULL);
654
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200655 if (!eth_devices)
656 return NULL;
657
658 dev = eth_devices;
659 target_dev = NULL;
660 do {
661 if (strcmp(devname, dev->name) == 0) {
662 target_dev = dev;
663 break;
664 }
665 dev = dev->next;
666 } while (dev != eth_devices);
667
668 return target_dev;
669}
670
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600671struct eth_device *eth_get_dev_by_index(int index)
672{
673 struct eth_device *dev, *target_dev;
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600674
675 if (!eth_devices)
676 return NULL;
677
678 dev = eth_devices;
679 target_dev = NULL;
680 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000681 if (dev->index == index) {
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600682 target_dev = dev;
683 break;
684 }
685 dev = dev->next;
Andy Fleminge7fd34b2009-02-11 15:07:24 -0600686 } while (dev != eth_devices);
687
688 return target_dev;
689}
690
Joe Hershberger5956ded2012-05-15 08:59:07 +0000691int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000692{
Joe Hershberger5956ded2012-05-15 08:59:07 +0000693 if (!eth_current)
Michael Walle391da4c2011-10-27 11:31:35 +0000694 return -1;
wdenkc6097192002-11-03 00:24:07 +0000695
Michael Walle391da4c2011-10-27 11:31:35 +0000696 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000697}
698
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500699static int on_ethaddr(const char *name, const char *value, enum env_op op,
700 int flags)
701{
702 int index;
703 struct eth_device *dev;
704
705 if (!eth_devices)
706 return 0;
707
708 /* look for an index after "eth" */
709 index = simple_strtoul(name + 3, NULL, 10);
710
711 dev = eth_devices;
712 do {
713 if (dev->index == index) {
714 switch (op) {
715 case env_op_create:
716 case env_op_overwrite:
717 eth_parse_enetaddr(value, dev->enetaddr);
718 break;
719 case env_op_delete:
720 memset(dev->enetaddr, 0, 6);
721 }
722 }
Gong Qianyua4f23972015-08-31 11:34:43 +0800723 dev = dev->next;
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500724 } while (dev != eth_devices);
725
726 return 0;
727}
728U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
729
Simon Glass62b36c92011-06-13 16:13:10 -0700730int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
731 int eth_number)
732{
733 unsigned char env_enetaddr[6];
734 int ret = 0;
735
Eric Miaoae97ef62012-01-18 22:56:33 +0000736 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700737
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500738 if (!is_zero_ethaddr(env_enetaddr)) {
739 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershbergerca044ad2015-03-22 17:09:01 -0500740 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass62b36c92011-06-13 16:13:10 -0700741 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500742 dev->name);
Simon Glass62b36c92011-06-13 16:13:10 -0700743 printf("Address in SROM is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500744 dev->enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700745 printf("Address in environment is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500746 env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700747 }
748
749 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500750 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc2938c02012-04-14 18:06:49 +0000751 eth_setenv_enetaddr_by_index(base_name, eth_number,
752 dev->enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500753 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500754#ifdef CONFIG_NET_RANDOM_ETHADDR
755 net_random_ethaddr(dev->enetaddr);
756 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
757 dev->name, eth_number, dev->enetaddr);
758#else
Pavel Machek222981f2014-07-13 10:27:02 +0200759 printf("\nError: %s address not set.\n",
760 dev->name);
761 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500762#endif
Simon Glass62b36c92011-06-13 16:13:10 -0700763 }
764
Pavel Machek222981f2014-07-13 10:27:02 +0200765 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500766 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek222981f2014-07-13 10:27:02 +0200767 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500768 dev->name, dev->enetaddr);
Pavel Machek222981f2014-07-13 10:27:02 +0200769 return -EINVAL;
770 }
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000771
Simon Glass62b36c92011-06-13 16:13:10 -0700772 ret = dev->write_hwaddr(dev);
Pavel Machek222981f2014-07-13 10:27:02 +0200773 if (ret)
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500774 printf("\nWarning: %s failed to set MAC address\n",
775 dev->name);
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000776 }
Simon Glass62b36c92011-06-13 16:13:10 -0700777
778 return ret;
779}
780
Simon Glass1e9961d2011-02-16 11:14:33 -0800781int eth_register(struct eth_device *dev)
782{
783 struct eth_device *d;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000784 static int index;
Michal Simek5b4945b2011-08-29 23:30:13 +0000785
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000786 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek5b4945b2011-08-29 23:30:13 +0000787
Simon Glass1e9961d2011-02-16 11:14:33 -0800788 if (!eth_devices) {
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500789 eth_devices = dev;
790 eth_current = dev;
Simon Glass1e9961d2011-02-16 11:14:33 -0800791 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000792 } else {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000793 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundeld46bcd32010-03-31 17:56:08 +0200794 ;
wdenkc6097192002-11-03 00:24:07 +0000795 d->next = dev;
796 }
797
798 dev->state = ETH_STATE_INIT;
799 dev->next = eth_devices;
Michael Walle391da4c2011-10-27 11:31:35 +0000800 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000801
802 return 0;
803}
804
Vincent Palatin451be502012-01-09 08:32:36 +0000805int eth_unregister(struct eth_device *dev)
806{
807 struct eth_device *cur;
808
809 /* No device */
810 if (!eth_devices)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500811 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000812
813 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
814 cur = cur->next)
815 ;
816
817 /* Device not found */
818 if (cur->next != dev)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500819 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000820
821 cur->next = dev->next;
822
823 if (eth_devices == dev)
824 eth_devices = dev->next == eth_devices ? NULL : dev->next;
825
826 if (eth_current == dev) {
827 eth_current = eth_devices;
828 eth_current_changed();
829 }
830
831 return 0;
832}
833
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500834int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000835{
Michael Walle391da4c2011-10-27 11:31:35 +0000836 int num_devices = 0;
Simon Glassb6121f22015-04-05 16:07:37 -0600837
wdenkc6097192002-11-03 00:24:07 +0000838 eth_devices = NULL;
839 eth_current = NULL;
Simon Glassb6121f22015-04-05 16:07:37 -0600840 eth_common_init();
Simon Glass0a9bde12016-01-17 14:51:57 -0700841 /*
842 * If board-specific initialization exists, call it.
843 * If not, call a CPU-specific one
844 */
845 if (board_eth_init != __def_eth_init) {
846 if (board_eth_init(gd->bd) < 0)
847 printf("Board Net Initialization Failed\n");
848 } else if (cpu_eth_init != __def_eth_init) {
849 if (cpu_eth_init(gd->bd) < 0)
850 printf("CPU Net Initialization Failed\n");
851 } else {
852 printf("Net Initialization Skipped\n");
853 }
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100854
wdenkc6097192002-11-03 00:24:07 +0000855 if (!eth_devices) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000856 puts("No ethernet found.\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000857 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000858 } else {
859 struct eth_device *dev = eth_devices;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000860 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000861
Simon Glass0169e6b2012-02-13 13:51:18 +0000862 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000863 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000864 if (dev->index)
Joe Hershberger5956ded2012-05-15 08:59:07 +0000865 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000866
867 printf("%s", dev->name);
868
Joe Hershberger5956ded2012-05-15 08:59:07 +0000869 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000870 eth_current = dev;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000871 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000872 }
873
Mike Frysinger2843ed92010-06-09 22:10:48 -0400874 if (strchr(dev->name, ' '))
Joe Hershberger5956ded2012-05-15 08:59:07 +0000875 puts("\nWarning: eth device name has a space!"
876 "\n");
Mike Frysinger2843ed92010-06-09 22:10:48 -0400877
Pavel Machek222981f2014-07-13 10:27:02 +0200878 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000879
wdenkc6097192002-11-03 00:24:07 +0000880 dev = dev->next;
Michael Walle391da4c2011-10-27 11:31:35 +0000881 num_devices++;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000882 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000883
Simon Glass1e9961d2011-02-16 11:14:33 -0800884 eth_current_changed();
Joe Hershberger5956ded2012-05-15 08:59:07 +0000885 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000886 }
887
Michael Walle391da4c2011-10-27 11:31:35 +0000888 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000889}
890
David Updegraff7280da72007-06-11 10:41:07 -0500891#ifdef CONFIG_MCAST_TFTP
892/* Multicast.
893 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200894 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500895 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500896int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff7280da72007-06-11 10:41:07 -0500897{
Joe Hershberger5956ded2012-05-15 08:59:07 +0000898 u8 mcast_mac[6];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200899 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500900 return -1;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500901 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
902 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
903 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff7280da72007-06-11 10:41:07 -0500904 mcast_mac[2] = 0x5e;
905 mcast_mac[1] = 0x0;
906 mcast_mac[0] = 0x1;
907 return eth_current->mcast(eth_current, mcast_mac, join);
908}
909
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200910/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
911 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff7280da72007-06-11 10:41:07 -0500912 * some other adapter -- hash tables
913 */
914#define CRCPOLY_LE 0xedb88320
Joe Hershberger5956ded2012-05-15 08:59:07 +0000915u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff7280da72007-06-11 10:41:07 -0500916{
917 int i;
918 u32 crc;
919 crc = ~0;
920 while (len--) {
921 crc ^= *p++;
922 for (i = 0; i < 8; i++)
923 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
924 }
925 /* an reverse the bits, cuz of way they arrive -- last-first */
926 crc = (crc >> 16) | (crc << 16);
927 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
928 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
929 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
930 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
931 return crc;
932}
wdenkc6097192002-11-03 00:24:07 +0000933
David Updegraff7280da72007-06-11 10:41:07 -0500934#endif
935
wdenkc6097192002-11-03 00:24:07 +0000936
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500937int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000938{
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500939 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000940
Stefan Roese11da2be2008-03-04 17:40:41 +0100941 if (!eth_current) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000942 puts("No ethernet found.\n");
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500943 return -ENODEV;
Stefan Roese11da2be2008-03-04 17:40:41 +0100944 }
wdenkc6097192002-11-03 00:24:07 +0000945
946 old_current = eth_current;
947 do {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400948 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000949
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500950 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000951 eth_current->state = ETH_STATE_ACTIVE;
952
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530953 return 0;
wdenkc6097192002-11-03 00:24:07 +0000954 }
Robin Getz9e0a4d62009-07-23 03:01:03 -0400955 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000956
957 eth_try_another(0);
958 } while (old_current != eth_current);
959
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500960 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000961}
962
963void eth_halt(void)
964{
965 if (!eth_current)
966 return;
967
968 eth_current->halt(eth_current);
969
970 eth_current->state = ETH_STATE_PASSIVE;
971}
972
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200973int eth_is_active(struct eth_device *dev)
974{
975 return dev && dev->state == ETH_STATE_ACTIVE;
976}
977
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000978int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000979{
980 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500981 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000982
983 return eth_current->send(eth_current, packet, length);
984}
985
986int eth_rx(void)
987{
988 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500989 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000990
991 return eth_current->recv(eth_current);
992}
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -0500993#endif /* ifndef CONFIG_DM_ETH */
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100994
995#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000996static void eth_save_packet(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100997{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000998 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100999 int i;
1000
1001 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
1002 return;
1003
1004 if (PKTSIZE < length)
1005 return;
1006
1007 for (i = 0; i < length; i++)
1008 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
1009
1010 eth_rcv_bufs[eth_rcv_last].length = length;
1011 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
1012}
1013
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001014int eth_receive(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001015{
Joe Hershberger4b7747e2012-05-15 08:59:04 +00001016 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001017 void *pp = push_packet;
1018 int i;
1019
1020 if (eth_rcv_current == eth_rcv_last) {
1021 push_packet = eth_save_packet;
1022 eth_rx();
1023 push_packet = pp;
1024
1025 if (eth_rcv_current == eth_rcv_last)
1026 return -1;
1027 }
1028
Michael Walledf7c98a2012-06-22 11:24:28 +00001029 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001030
1031 for (i = 0; i < length; i++)
1032 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
1033
1034 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
1035 return length;
1036}
1037#endif /* CONFIG_API */
wdenkc6097192002-11-03 00:24:07 +00001038
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001039static void eth_current_changed(void)
1040{
1041 char *act = getenv("ethact");
Bin Meng2d5cc542015-12-21 22:43:38 -08001042 char *ethrotate;
1043
1044 /*
1045 * The call to eth_get_dev() below has a side effect of rotating
1046 * ethernet device if uc_priv->current == NULL. This is not what
1047 * we want when 'ethrotate' variable is 'no'.
1048 */
1049 ethrotate = getenv("ethrotate");
1050 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
1051 return;
1052
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001053 /* update current ethernet name */
1054 if (eth_get_dev()) {
1055 if (act == NULL || strcmp(act, eth_get_name()) != 0)
1056 setenv("ethact", eth_get_name());
1057 }
1058 /*
1059 * remove the variable completely if there is no active
1060 * interface
1061 */
1062 else if (act != NULL)
1063 setenv("ethact", NULL);
1064}
1065
wdenkc6097192002-11-03 00:24:07 +00001066void eth_try_another(int first_restart)
1067{
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05001068 static void *first_failed;
Remy Bohmer0fce5152011-02-19 20:15:14 +01001069 char *ethrotate;
Matthias Fuchs204f0ec2008-01-17 07:45:05 +01001070
1071 /*
1072 * Do not rotate between network interfaces when
1073 * 'ethrotate' variable is set to 'no'.
1074 */
Joe Hershberger5956ded2012-05-15 08:59:07 +00001075 ethrotate = getenv("ethrotate");
1076 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchs204f0ec2008-01-17 07:45:05 +01001077 return;
wdenkc6097192002-11-03 00:24:07 +00001078
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001079 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +00001080 return;
1081
Joe Hershberger5956ded2012-05-15 08:59:07 +00001082 if (first_restart)
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001083 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +00001084
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001085 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +00001086
Simon Glass1e9961d2011-02-16 11:14:33 -08001087 eth_current_changed();
wdenk145d2c12004-04-15 21:48:45 +00001088
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001089 if (first_failed == eth_get_dev())
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001090 net_restart_wrap = 1;
wdenkc6097192002-11-03 00:24:07 +00001091}
1092
wdenk145d2c12004-04-15 21:48:45 +00001093void eth_set_current(void)
1094{
Joe Hershberger5956ded2012-05-15 08:59:07 +00001095 static char *act;
1096 static int env_changed_id;
Heiko Schocher0c303fa2009-02-10 09:38:52 +01001097 int env_id;
wdenk145d2c12004-04-15 21:48:45 +00001098
Heiko Schocher0c303fa2009-02-10 09:38:52 +01001099 env_id = get_env_id();
1100 if ((act == NULL) || (env_changed_id != env_id)) {
1101 act = getenv("ethact");
1102 env_changed_id = env_id;
1103 }
Joe Hershbergeraa52edd2015-03-22 17:09:17 -05001104
1105 if (act == NULL) {
1106 char *ethprime = getenv("ethprime");
1107 void *dev = NULL;
1108
1109 if (ethprime)
1110 dev = eth_get_dev_by_name(ethprime);
1111 if (dev)
1112 eth_set_dev(dev);
1113 else
1114 eth_set_dev(NULL);
1115 } else {
Joe Hershberger279d2f62015-03-22 17:09:16 -05001116 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershbergeraa52edd2015-03-22 17:09:17 -05001117 }
wdenk145d2c12004-04-15 21:48:45 +00001118
Simon Glass1e9961d2011-02-16 11:14:33 -08001119 eth_current_changed();
wdenk145d2c12004-04-15 21:48:45 +00001120}
wdenk145d2c12004-04-15 21:48:45 +00001121
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001122const char *eth_get_name(void)
wdenkc6abb7e2003-11-17 21:14:37 +00001123{
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -05001124 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenkc6abb7e2003-11-17 21:14:37 +00001125}