eth.c 21.5 KB
Newer Older
W
wdenk 已提交
1
/*
2
 * (C) Copyright 2001-2015
W
wdenk 已提交
3
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4
 * Joe Hershberger, National Instruments
W
wdenk 已提交
5
 *
6
 * SPDX-License-Identifier:	GPL-2.0+
W
wdenk 已提交
7 8 9 10
 */

#include <common.h>
#include <command.h>
11
#include <dm.h>
W
wdenk 已提交
12
#include <net.h>
13
#include <miiphy.h>
A
Andy Fleming 已提交
14
#include <phy.h>
15
#include <asm/errno.h>
16
#include <dm/device-internal.h>
W
wdenk 已提交
17

18 19
DECLARE_GLOBAL_DATA_PTR;

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
{
	char *end;
	int i;

	for (i = 0; i < 6; ++i) {
		enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
		if (addr)
			addr = (*end) ? end + 1 : end;
	}
}

int eth_getenv_enetaddr(char *name, uchar *enetaddr)
{
	eth_parse_enetaddr(getenv(name), enetaddr);
35
	return is_valid_ethaddr(enetaddr);
36 37 38 39 40 41 42 43 44 45
}

int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
{
	char buf[20];

	sprintf(buf, "%pM", enetaddr);

	return setenv(name, buf);
}
46

47 48
int eth_getenv_enetaddr_by_index(const char *base_name, int index,
				 uchar *enetaddr)
49 50
{
	char enetvar[32];
51
	sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
52 53
	return eth_getenv_enetaddr(enetvar, enetaddr);
}
54

55
static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
56 57 58 59 60 61 62
				 uchar *enetaddr)
{
	char enetvar[32];
	sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
	return eth_setenv_enetaddr(enetvar, enetaddr);
}

63 64 65 66 67 68
static void eth_env_init(void)
{
	const char *s;

	s = getenv("bootfile");
	if (s != NULL)
69 70
		copy_filename(net_boot_file_name, s,
			      sizeof(net_boot_file_name));
71
}
72

73 74 75 76
static int eth_mac_skip(int index)
{
	char enetvar[15];
	char *skip_state;
77

78
	sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
79 80
	skip_state = getenv(enetvar);
	return skip_state != NULL;
81 82
}

83 84
static void eth_current_changed(void);

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/*
 * CPU and board-specific Ethernet initializations.  Aliased function
 * signals caller to move on
 */
static int __def_eth_init(bd_t *bis)
{
	return -1;
}
int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));

static void eth_common_init(void)
{
	bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
	miiphy_init();
#endif

#ifdef CONFIG_PHYLIB
	phy_init();
#endif

	eth_env_init();

	/*
	 * If board-specific initialization exists, call it.
	 * If not, call a CPU-specific one
	 */
	if (board_eth_init != __def_eth_init) {
		if (board_eth_init(gd->bd) < 0)
			printf("Board Net Initialization Failed\n");
	} else if (cpu_eth_init != __def_eth_init) {
		if (cpu_eth_init(gd->bd) < 0)
			printf("CPU Net Initialization Failed\n");
	} else {
		printf("Net Initialization Skipped\n");
	}
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#ifdef CONFIG_DM_ETH
/**
 * struct eth_device_priv - private structure for each Ethernet device
 *
 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
 */
struct eth_device_priv {
	enum eth_state_t state;
};

/**
 * struct eth_uclass_priv - The structure attached to the uclass itself
 *
 * @current: The Ethernet device that the network functions are using
 */
struct eth_uclass_priv {
	struct udevice *current;
};

J
Joe Hershberger 已提交
143 144 145
/* eth_errno - This stores the most recent failure code from DM functions */
static int eth_errno;

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static struct eth_uclass_priv *eth_get_uclass_priv(void)
{
	struct uclass *uc;

	uclass_get(UCLASS_ETH, &uc);
	assert(uc);
	return uc->priv;
}

static void eth_set_current_to_next(void)
{
	struct eth_uclass_priv *uc_priv;

	uc_priv = eth_get_uclass_priv();
	if (uc_priv->current)
		uclass_next_device(&uc_priv->current);
	if (!uc_priv->current)
		uclass_first_device(UCLASS_ETH, &uc_priv->current);
}

J
Joe Hershberger 已提交
166 167 168 169 170 171
/*
 * Typically this will simply return the active device.
 * In the case where the most recent active device was unset, this will attempt
 * to return the first device. If that device doesn't exist or fails to probe,
 * this function will return NULL.
 */
172 173 174 175 176 177
struct udevice *eth_get_dev(void)
{
	struct eth_uclass_priv *uc_priv;

	uc_priv = eth_get_uclass_priv();
	if (!uc_priv->current)
J
Joe Hershberger 已提交
178
		eth_errno = uclass_first_device(UCLASS_ETH,
179 180 181 182
				    &uc_priv->current);
	return uc_priv->current;
}

J
Joe Hershberger 已提交
183 184 185 186 187
/*
 * Typically this will just store a device pointer.
 * In case it was not probed, we will attempt to do so.
 * dev may be NULL to unset the active device.
 */
188 189
static void eth_set_dev(struct udevice *dev)
{
J
Joe Hershberger 已提交
190 191
	if (dev && !device_active(dev))
		eth_errno = device_probe(dev);
192 193 194
	eth_get_uclass_priv()->current = dev;
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/*
 * Find the udevice that either has the name passed in as devname or has an
 * alias named devname.
 */
struct udevice *eth_get_dev_by_name(const char *devname)
{
	int seq = -1;
	char *endp = NULL;
	const char *startp = NULL;
	struct udevice *it;
	struct uclass *uc;

	/* Must be longer than 3 to be an alias */
	if (strlen(devname) > strlen("eth")) {
		startp = devname + strlen("eth");
		seq = simple_strtoul(startp, &endp, 10);
	}

	uclass_get(UCLASS_ETH, &uc);
	uclass_foreach_dev(it, uc) {
J
Joe Hershberger 已提交
215 216 217 218 219 220 221 222
		/*
		 * We need the seq to be valid, so try to probe it.
		 * If the probe fails, the seq will not match since it will be
		 * -1 instead of what we are looking for.
		 * We don't care about errors from probe here. Either they won't
		 * match an alias or it will match a literal name and we'll pick
		 * up the error when we try to probe again in eth_set_dev().
		 */
223 224 225 226 227 228 229 230 231 232 233 234
		device_probe(it);
		/*
		 * Check for the name or the sequence number to match
		 */
		if (strcmp(it->name, devname) == 0 ||
		    (endp > startp && it->seq == seq))
			return it;
	}

	return NULL;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
unsigned char *eth_get_ethaddr(void)
{
	struct eth_pdata *pdata;

	if (eth_get_dev()) {
		pdata = eth_get_dev()->platdata;
		return pdata->enetaddr;
	}

	return NULL;
}

/* Set active state without calling start on the driver */
int eth_init_state_only(void)
{
	struct udevice *current;
	struct eth_device_priv *priv;

	current = eth_get_dev();
	if (!current || !device_active(current))
		return -EINVAL;

	priv = current->uclass_priv;
	priv->state = ETH_STATE_ACTIVE;

	return 0;
}

/* Set passive state without calling stop on the driver */
void eth_halt_state_only(void)
{
	struct udevice *current;
	struct eth_device_priv *priv;

	current = eth_get_dev();
	if (!current || !device_active(current))
		return;

	priv = current->uclass_priv;
	priv->state = ETH_STATE_PASSIVE;
}

int eth_get_dev_index(void)
{
	if (eth_get_dev())
		return eth_get_dev()->seq;
	return -1;
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
static int eth_write_hwaddr(struct udevice *dev)
{
	struct eth_pdata *pdata = dev->platdata;
	int ret = 0;

	if (!dev || !device_active(dev))
		return -EINVAL;

	/* seq is valid since the device is active */
	if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
		if (!is_valid_ethaddr(pdata->enetaddr)) {
			printf("\nError: %s address %pM illegal value\n",
			       dev->name, pdata->enetaddr);
			return -EINVAL;
		}

		ret = eth_get_ops(dev)->write_hwaddr(dev);
		if (ret)
			printf("\nWarning: %s failed to set MAC address\n",
			       dev->name);
	}

	return ret;
}

309 310 311 312
int eth_init(void)
{
	struct udevice *current;
	struct udevice *old_current;
J
Joe Hershberger 已提交
313
	int ret = -ENODEV;
314 315 316 317 318 319 320 321 322 323 324 325 326 327

	current = eth_get_dev();
	if (!current) {
		printf("No ethernet found.\n");
		return -ENODEV;
	}

	old_current = current;
	do {
		debug("Trying %s\n", current->name);

		if (device_active(current)) {
			uchar env_enetaddr[6];
			struct eth_pdata *pdata = current->platdata;
328
			int enetaddr_changed = 0;
329 330 331

			/* Sync environment with network device */
			if (eth_getenv_enetaddr_by_index("eth", current->seq,
332 333 334
							 env_enetaddr)) {
				enetaddr_changed = memcmp(pdata->enetaddr,
					env_enetaddr, 6);
335
				memcpy(pdata->enetaddr, env_enetaddr, 6);
336 337 338 339
			} else {
				memset(env_enetaddr, 0, 6);
				enetaddr_changed = memcmp(pdata->enetaddr,
					env_enetaddr, 6);
340
				memset(pdata->enetaddr, 0, 6);
341 342 343
			}
			if (enetaddr_changed)
				eth_write_hwaddr(current);
344

J
Joe Hershberger 已提交
345 346
			ret = eth_get_ops(current)->start(current);
			if (ret >= 0) {
347 348 349 350 351 352
				struct eth_device_priv *priv =
					current->uclass_priv;

				priv->state = ETH_STATE_ACTIVE;
				return 0;
			}
353
		} else {
J
Joe Hershberger 已提交
354
			ret = eth_errno;
355
		}
J
Joe Hershberger 已提交
356

357 358
		debug("FAIL\n");

J
Joe Hershberger 已提交
359 360 361 362
		/*
		 * If ethrotate is enabled, this will change "current",
		 * otherwise we will drop out of this while loop immediately
		 */
363
		eth_try_another(0);
J
Joe Hershberger 已提交
364
		/* This will ensure the new "current" attempted to probe */
365 366 367
		current = eth_get_dev();
	} while (old_current != current);

J
Joe Hershberger 已提交
368
	return ret;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
}

void eth_halt(void)
{
	struct udevice *current;
	struct eth_device_priv *priv;

	current = eth_get_dev();
	if (!current || !device_active(current))
		return;

	eth_get_ops(current)->stop(current);
	priv = current->uclass_priv;
	priv->state = ETH_STATE_PASSIVE;
}

int eth_send(void *packet, int length)
{
	struct udevice *current;
J
Joe Hershberger 已提交
388
	int ret;
389 390 391 392 393 394 395 396

	current = eth_get_dev();
	if (!current)
		return -ENODEV;

	if (!device_active(current))
		return -EINVAL;

J
Joe Hershberger 已提交
397 398 399 400 401 402
	ret = eth_get_ops(current)->send(current, packet, length);
	if (ret < 0) {
		/* We cannot completely return the error at present */
		debug("%s: send() returned error %d\n", __func__, ret);
	}
	return ret;
403 404 405 406 407
}

int eth_rx(void)
{
	struct udevice *current;
408 409 410
	uchar *packet;
	int ret;
	int i;
411 412 413 414 415 416 417 418

	current = eth_get_dev();
	if (!current)
		return -ENODEV;

	if (!device_active(current))
		return -EINVAL;

419 420 421 422 423
	/* Process up to 32 packets at one time */
	for (i = 0; i < 32; i++) {
		ret = eth_get_ops(current)->recv(current, &packet);
		if (ret > 0)
			net_process_received_packet(packet, ret);
424 425 426
		if (ret >= 0 && eth_get_ops(current)->free_pkt)
			eth_get_ops(current)->free_pkt(current, packet, ret);
		if (ret <= 0)
427 428 429 430
			break;
	}
	if (ret == -EAGAIN)
		ret = 0;
J
Joe Hershberger 已提交
431 432 433 434
	if (ret < 0) {
		/* We cannot completely return the error at present */
		debug("%s: recv() returned error %d\n", __func__, ret);
	}
435
	return ret;
436 437 438 439 440 441 442
}

int eth_initialize(void)
{
	int num_devices = 0;
	struct udevice *dev;

443
	eth_common_init();
444 445 446 447 448 449 450 451 452 453 454 455

	/*
	 * Devices need to write the hwaddr even if not started so that Linux
	 * will have access to the hwaddr that u-boot stored for the device.
	 * This is accomplished by attempting to probe each device and calling
	 * their write_hwaddr() operation.
	 */
	uclass_first_device(UCLASS_ETH, &dev);
	if (!dev) {
		printf("No ethernet found.\n");
		bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
	} else {
456 457 458 459 460 461 462 463 464 465 466 467
		char *ethprime = getenv("ethprime");
		struct udevice *prime_dev = NULL;

		if (ethprime)
			prime_dev = eth_get_dev_by_name(ethprime);
		if (prime_dev) {
			eth_set_dev(prime_dev);
			eth_current_changed();
		} else {
			eth_set_dev(NULL);
		}

468 469 470 471 472 473 474
		bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
		do {
			if (num_devices)
				printf(", ");

			printf("eth%d: %s", dev->seq, dev->name);

475 476 477
			if (ethprime && dev == prime_dev)
				printf(" [PRIME]");

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
			eth_write_hwaddr(dev);

			uclass_next_device(&dev);
			num_devices++;
		} while (dev);

		putc('\n');
	}

	return num_devices;
}

static int eth_post_bind(struct udevice *dev)
{
	if (strchr(dev->name, ' ')) {
		printf("\nError: eth device name \"%s\" has a space!\n",
		       dev->name);
		return -EINVAL;
	}

	return 0;
}

static int eth_pre_unbind(struct udevice *dev)
{
	/* Don't hang onto a pointer that is going away */
	if (dev == eth_get_uclass_priv()->current)
		eth_set_dev(NULL);

	return 0;
}

static int eth_post_probe(struct udevice *dev)
{
	struct eth_device_priv *priv = dev->uclass_priv;
	struct eth_pdata *pdata = dev->platdata;
	unsigned char env_enetaddr[6];

	priv->state = ETH_STATE_INIT;

	/* Check if the device has a MAC address in ROM */
	if (eth_get_ops(dev)->read_rom_hwaddr)
		eth_get_ops(dev)->read_rom_hwaddr(dev);

	eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
523 524
	if (!is_zero_ethaddr(env_enetaddr)) {
		if (!is_zero_ethaddr(pdata->enetaddr) &&
525 526 527 528 529 530 531 532 533 534 535
		    memcmp(pdata->enetaddr, env_enetaddr, 6)) {
			printf("\nWarning: %s MAC addresses don't match:\n",
			       dev->name);
			printf("Address in SROM is         %pM\n",
			       pdata->enetaddr);
			printf("Address in environment is  %pM\n",
			       env_enetaddr);
		}

		/* Override the ROM MAC address */
		memcpy(pdata->enetaddr, env_enetaddr, 6);
536
	} else if (is_valid_ethaddr(pdata->enetaddr)) {
537 538 539
		eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
		printf("\nWarning: %s using MAC address from ROM\n",
		       dev->name);
540
	} else if (is_zero_ethaddr(pdata->enetaddr)) {
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
		printf("\nError: %s address not set.\n",
		       dev->name);
		return -EINVAL;
	}

	return 0;
}

static int eth_pre_remove(struct udevice *dev)
{
	eth_get_ops(dev)->stop(dev);

	return 0;
}

UCLASS_DRIVER(eth) = {
	.name		= "eth",
	.id		= UCLASS_ETH,
	.post_bind	= eth_post_bind,
	.pre_unbind	= eth_pre_unbind,
	.post_probe	= eth_post_probe,
	.pre_remove	= eth_pre_remove,
	.priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
	.per_device_auto_alloc_size = sizeof(struct eth_device_priv),
565
	.flags		= DM_UC_FLAG_SEQ_ALIAS,
566 567 568 569
};
#endif

#ifndef CONFIG_DM_ETH
570

571 572 573 574 575 576
#ifdef CONFIG_API
static struct {
	uchar data[PKTSIZE];
	int length;
} eth_rcv_bufs[PKTBUFSRX];

577
static unsigned int eth_rcv_current, eth_rcv_last;
578 579
#endif

580 581
static struct eth_device *eth_devices;
struct eth_device *eth_current;
W
wdenk 已提交
582

583 584 585 586 587
static void eth_set_current_to_next(void)
{
	eth_current = eth_current->next;
}

588 589 590 591 592
static void eth_set_dev(struct eth_device *dev)
{
	eth_current = dev;
}

593
struct eth_device *eth_get_dev_by_name(const char *devname)
M
Marian Balakowicz 已提交
594 595 596
{
	struct eth_device *dev, *target_dev;

597 598
	BUG_ON(devname == NULL);

M
Marian Balakowicz 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	if (!eth_devices)
		return NULL;

	dev = eth_devices;
	target_dev = NULL;
	do {
		if (strcmp(devname, dev->name) == 0) {
			target_dev = dev;
			break;
		}
		dev = dev->next;
	} while (dev != eth_devices);

	return target_dev;
}

A
Andy Fleming 已提交
615 616 617 618 619 620 621 622 623 624
struct eth_device *eth_get_dev_by_index(int index)
{
	struct eth_device *dev, *target_dev;

	if (!eth_devices)
		return NULL;

	dev = eth_devices;
	target_dev = NULL;
	do {
M
Michael Walle 已提交
625
		if (dev->index == index) {
A
Andy Fleming 已提交
626 627 628 629 630 631 632 633 634
			target_dev = dev;
			break;
		}
		dev = dev->next;
	} while (dev != eth_devices);

	return target_dev;
}

635
int eth_get_dev_index(void)
W
wdenk 已提交
636
{
637
	if (!eth_current)
M
Michael Walle 已提交
638
		return -1;
W
wdenk 已提交
639

M
Michael Walle 已提交
640
	return eth_current->index;
W
wdenk 已提交
641 642
}

643 644 645 646 647 648
int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
		   int eth_number)
{
	unsigned char env_enetaddr[6];
	int ret = 0;

649
	eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
650

651 652
	if (!is_zero_ethaddr(env_enetaddr)) {
		if (!is_zero_ethaddr(dev->enetaddr) &&
653
		    memcmp(dev->enetaddr, env_enetaddr, 6)) {
654
			printf("\nWarning: %s MAC addresses don't match:\n",
655
			       dev->name);
656
			printf("Address in SROM is         %pM\n",
657
			       dev->enetaddr);
658
			printf("Address in environment is  %pM\n",
659
			       env_enetaddr);
660 661 662
		}

		memcpy(dev->enetaddr, env_enetaddr, 6);
663
	} else if (is_valid_ethaddr(dev->enetaddr)) {
664 665 666
		eth_setenv_enetaddr_by_index(base_name, eth_number,
					     dev->enetaddr);
		printf("\nWarning: %s using MAC address from net device\n",
667
		       dev->name);
668
	} else if (is_zero_ethaddr(dev->enetaddr)) {
669 670 671
		printf("\nError: %s address not set.\n",
		       dev->name);
		return -EINVAL;
672 673
	}

674
	if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
675
		if (!is_valid_ethaddr(dev->enetaddr)) {
676
			printf("\nError: %s address %pM illegal value\n",
677
			       dev->name, dev->enetaddr);
678 679
			return -EINVAL;
		}
680

681
		ret = dev->write_hwaddr(dev);
682
		if (ret)
683 684
			printf("\nWarning: %s failed to set MAC address\n",
			       dev->name);
685
	}
686 687 688 689

	return ret;
}

690 691 692
int eth_register(struct eth_device *dev)
{
	struct eth_device *d;
693
	static int index;
694

695
	assert(strlen(dev->name) < sizeof(dev->name));
696

697
	if (!eth_devices) {
698 699
		eth_devices = dev;
		eth_current = dev;
700
		eth_current_changed();
W
wdenk 已提交
701
	} else {
702
		for (d = eth_devices; d->next != eth_devices; d = d->next)
703
			;
W
wdenk 已提交
704 705 706 707 708
		d->next = dev;
	}

	dev->state = ETH_STATE_INIT;
	dev->next  = eth_devices;
M
Michael Walle 已提交
709
	dev->index = index++;
W
wdenk 已提交
710 711 712 713

	return 0;
}

714 715 716 717 718 719
int eth_unregister(struct eth_device *dev)
{
	struct eth_device *cur;

	/* No device */
	if (!eth_devices)
720
		return -ENODEV;
721 722 723 724 725 726 727

	for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
	     cur = cur->next)
		;

	/* Device not found */
	if (cur->next != dev)
728
		return -ENODEV;
729 730 731 732 733 734 735 736 737 738 739 740 741 742

	cur->next = dev->next;

	if (eth_devices == dev)
		eth_devices = dev->next == eth_devices ? NULL : dev->next;

	if (eth_current == dev) {
		eth_current = eth_devices;
		eth_current_changed();
	}

	return 0;
}

743
int eth_initialize(void)
W
wdenk 已提交
744
{
M
Michael Walle 已提交
745
	int num_devices = 0;
746

W
wdenk 已提交
747 748
	eth_devices = NULL;
	eth_current = NULL;
749
	eth_common_init();
750

W
wdenk 已提交
751
	if (!eth_devices) {
752
		puts("No ethernet found.\n");
753
		bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
W
wdenk 已提交
754 755
	} else {
		struct eth_device *dev = eth_devices;
756
		char *ethprime = getenv("ethprime");
W
wdenk 已提交
757

758
		bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
W
wdenk 已提交
759
		do {
M
Michael Walle 已提交
760
			if (dev->index)
761
				puts(", ");
W
wdenk 已提交
762 763 764

			printf("%s", dev->name);

765
			if (ethprime && strcmp(dev->name, ethprime) == 0) {
W
wdenk 已提交
766
				eth_current = dev;
767
				puts(" [PRIME]");
W
wdenk 已提交
768 769
			}

770
			if (strchr(dev->name, ' '))
771 772
				puts("\nWarning: eth device name has a space!"
					"\n");
773

774
			eth_write_hwaddr(dev, "eth", dev->index);
W
wdenk 已提交
775 776

			dev = dev->next;
M
Michael Walle 已提交
777
			num_devices++;
778
		} while (dev != eth_devices);
W
wdenk 已提交
779

780
		eth_current_changed();
781
		putc('\n');
W
wdenk 已提交
782 783
	}

M
Michael Walle 已提交
784
	return num_devices;
W
wdenk 已提交
785 786
}

D
David Updegraff 已提交
787 788 789
#ifdef CONFIG_MCAST_TFTP
/* Multicast.
 * mcast_addr: multicast ipaddr from which multicast Mac is made
790
 * join: 1=join, 0=leave.
D
David Updegraff 已提交
791
 */
792
int eth_mcast_join(struct in_addr mcast_ip, int join)
D
David Updegraff 已提交
793
{
794
	u8 mcast_mac[6];
795
	if (!eth_current || !eth_current->mcast)
D
David Updegraff 已提交
796
		return -1;
797 798 799
	mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
	mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
	mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
D
David Updegraff 已提交
800 801 802 803 804 805
	mcast_mac[2] = 0x5e;
	mcast_mac[1] = 0x0;
	mcast_mac[0] = 0x1;
	return eth_current->mcast(eth_current, mcast_mac, join);
}

806 807
/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
 * and this is the ethernet-crc method needed for TSEC -- and perhaps
D
David Updegraff 已提交
808 809 810
 * some other adapter -- hash tables
 */
#define CRCPOLY_LE 0xedb88320
811
u32 ether_crc(size_t len, unsigned char const *p)
D
David Updegraff 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
{
	int i;
	u32 crc;
	crc = ~0;
	while (len--) {
		crc ^= *p++;
		for (i = 0; i < 8; i++)
			crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
	}
	/* an reverse the bits, cuz of way they arrive -- last-first */
	crc = (crc >> 16) | (crc << 16);
	crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
	crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
	crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
	crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
	return crc;
}

#endif

W
wdenk 已提交
832

833
int eth_init(void)
W
wdenk 已提交
834
{
835
	struct eth_device *old_current, *dev;
W
wdenk 已提交
836

837
	if (!eth_current) {
838
		puts("No ethernet found.\n");
839
		return -ENODEV;
840
	}
W
wdenk 已提交
841

842 843 844 845
	/* Sync environment with network devices */
	dev = eth_devices;
	do {
		uchar env_enetaddr[6];
846
		int enetaddr_changed = 0;
847

M
Michael Walle 已提交
848
		if (eth_getenv_enetaddr_by_index("eth", dev->index,
849 850 851
						 env_enetaddr)) {
			enetaddr_changed = memcmp(dev->enetaddr,
				env_enetaddr, 6);
852
			memcpy(dev->enetaddr, env_enetaddr, 6);
853 854 855 856 857 858 859 860
		} else {
			memset(env_enetaddr, 0, 6);
			enetaddr_changed = memcmp(dev->enetaddr,
				env_enetaddr, 6);
			memset(dev->enetaddr, 0, 6);
		}
		if (enetaddr_changed)
			eth_write_hwaddr(dev, "eth", dev->index);
861 862 863 864

		dev = dev->next;
	} while (dev != eth_devices);

W
wdenk 已提交
865 866
	old_current = eth_current;
	do {
R
Robin Getz 已提交
867
		debug("Trying %s\n", eth_current->name);
W
wdenk 已提交
868

869
		if (eth_current->init(eth_current, gd->bd) >= 0) {
W
wdenk 已提交
870 871
			eth_current->state = ETH_STATE_ACTIVE;

872
			return 0;
W
wdenk 已提交
873
		}
R
Robin Getz 已提交
874
		debug("FAIL\n");
W
wdenk 已提交
875 876 877 878

		eth_try_another(0);
	} while (old_current != eth_current);

879
	return -ETIMEDOUT;
W
wdenk 已提交
880 881 882 883 884 885 886 887 888 889 890 891
}

void eth_halt(void)
{
	if (!eth_current)
		return;

	eth_current->halt(eth_current);

	eth_current->state = ETH_STATE_PASSIVE;
}

892
int eth_send(void *packet, int length)
W
wdenk 已提交
893 894
{
	if (!eth_current)
895
		return -ENODEV;
W
wdenk 已提交
896 897 898 899 900 901 902

	return eth_current->send(eth_current, packet, length);
}

int eth_rx(void)
{
	if (!eth_current)
903
		return -ENODEV;
W
wdenk 已提交
904 905 906

	return eth_current->recv(eth_current);
}
907
#endif /* ifndef CONFIG_DM_ETH */
W
wdenk 已提交
908

909
#ifdef CONFIG_API
910
static void eth_save_packet(void *packet, int length)
911
{
912
	char *p = packet;
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	int i;

	if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
		return;

	if (PKTSIZE < length)
		return;

	for (i = 0; i < length; i++)
		eth_rcv_bufs[eth_rcv_last].data[i] = p[i];

	eth_rcv_bufs[eth_rcv_last].length = length;
	eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
}

928
int eth_receive(void *packet, int length)
929
{
930
	char *p = packet;
931 932 933 934 935 936 937 938 939 940 941 942
	void *pp = push_packet;
	int i;

	if (eth_rcv_current == eth_rcv_last) {
		push_packet = eth_save_packet;
		eth_rx();
		push_packet = pp;

		if (eth_rcv_current == eth_rcv_last)
			return -1;
	}

943
	length = min(eth_rcv_bufs[eth_rcv_current].length, length);
944 945 946 947 948 949 950 951 952

	for (i = 0; i < length; i++)
		p[i] = eth_rcv_bufs[eth_rcv_current].data[i];

	eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
	return length;
}
#endif /* CONFIG_API */

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
static void eth_current_changed(void)
{
	char *act = getenv("ethact");
	/* update current ethernet name */
	if (eth_get_dev()) {
		if (act == NULL || strcmp(act, eth_get_name()) != 0)
			setenv("ethact", eth_get_name());
	}
	/*
	 * remove the variable completely if there is no active
	 * interface
	 */
	else if (act != NULL)
		setenv("ethact", NULL);
}

W
wdenk 已提交
969 970
void eth_try_another(int first_restart)
{
971
	static void *first_failed;
R
Remy Bohmer 已提交
972
	char *ethrotate;
973 974 975 976 977

	/*
	 * Do not rotate between network interfaces when
	 * 'ethrotate' variable is set to 'no'.
	 */
978 979
	ethrotate = getenv("ethrotate");
	if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
980
		return;
W
wdenk 已提交
981

982
	if (!eth_get_dev())
W
wdenk 已提交
983 984
		return;

985
	if (first_restart)
986
		first_failed = eth_get_dev();
W
wdenk 已提交
987

988
	eth_set_current_to_next();
W
wdenk 已提交
989

990
	eth_current_changed();
991

992
	if (first_failed == eth_get_dev())
993
		net_restart_wrap = 1;
W
wdenk 已提交
994 995
}

996 997
void eth_set_current(void)
{
998 999
	static char *act;
	static int  env_changed_id;
H
Heiko Schocher 已提交
1000
	int	env_id;
1001

H
Heiko Schocher 已提交
1002 1003 1004 1005 1006
	env_id = get_env_id();
	if ((act == NULL) || (env_changed_id != env_id)) {
		act = getenv("ethact");
		env_changed_id = env_id;
	}
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

	if (act == NULL) {
		char *ethprime = getenv("ethprime");
		void *dev = NULL;

		if (ethprime)
			dev = eth_get_dev_by_name(ethprime);
		if (dev)
			eth_set_dev(dev);
		else
			eth_set_dev(NULL);
	} else {
1019
		eth_set_dev(eth_get_dev_by_name(act));
1020
	}
1021

1022
	eth_current_changed();
1023 1024
}

1025
const char *eth_get_name(void)
1026
{
1027
	return eth_get_dev() ? eth_get_dev()->name : "unknown";
1028
}