eth.c 22.4 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>
12
#include <environment.h>
W
wdenk 已提交
13
#include <net.h>
14
#include <miiphy.h>
A
Andy Fleming 已提交
15
#include <phy.h>
16
#include <asm/errno.h>
17
#include <dm/device-internal.h>
18
#include <dm/uclass-internal.h>
W
wdenk 已提交
19

20 21
DECLARE_GLOBAL_DATA_PTR;

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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);
37
	return is_valid_ethaddr(enetaddr);
38 39 40 41 42 43 44 45 46 47
}

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

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

	return setenv(name, buf);
}
48

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

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

65 66 67 68
static int eth_mac_skip(int index)
{
	char enetvar[15];
	char *skip_state;
69

70
	sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
71 72
	skip_state = getenv(enetvar);
	return skip_state != NULL;
73 74
}

75 76
static void eth_current_changed(void);

77 78 79 80 81 82 83 84 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
/*
 * 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

	/*
	 * 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 {
110
#ifndef CONFIG_DM_ETH
111
		printf("Net Initialization Skipped\n");
112
#endif
113 114 115
	}
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#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 已提交
135 136 137
/* eth_errno - This stores the most recent failure code from DM functions */
static int eth_errno;

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
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 已提交
158 159 160 161 162 163
/*
 * 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.
 */
164 165 166 167 168 169
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 已提交
170
		eth_errno = uclass_first_device(UCLASS_ETH,
171 172 173 174
				    &uc_priv->current);
	return uc_priv->current;
}

J
Joe Hershberger 已提交
175 176 177 178 179
/*
 * 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.
 */
180 181
static void eth_set_dev(struct udevice *dev)
{
J
Joe Hershberger 已提交
182 183
	if (dev && !device_active(dev))
		eth_errno = device_probe(dev);
184 185 186
	eth_get_uclass_priv()->current = dev;
}

187 188 189 190 191 192 193 194 195 196 197
/*
 * 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;
198
	int len = strlen("eth");
199 200

	/* Must be longer than 3 to be an alias */
201 202
	if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
		startp = devname + len;
203 204 205 206 207
		seq = simple_strtoul(startp, &endp, 10);
	}

	uclass_get(UCLASS_ETH, &uc);
	uclass_foreach_dev(it, uc) {
J
Joe Hershberger 已提交
208 209 210 211 212 213 214 215
		/*
		 * 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().
		 */
216 217 218 219 220 221 222 223 224 225 226 227
		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;
}

228 229 230 231 232 233 234 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
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;
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
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;
		}

293 294 295 296
		/*
		 * Drivers are allowed to decide not to implement this at
		 * run-time. E.g. Some devices may use it and some may not.
		 */
297
		ret = eth_get_ops(dev)->write_hwaddr(dev);
298 299
		if (ret == -ENOSYS)
			ret = 0;
300 301 302 303 304 305 306 307
		if (ret)
			printf("\nWarning: %s failed to set MAC address\n",
			       dev->name);
	}

	return ret;
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static int on_ethaddr(const char *name, const char *value, enum env_op op,
	int flags)
{
	int index;
	int retval;
	struct udevice *dev;

	/* look for an index after "eth" */
	index = simple_strtoul(name + 3, NULL, 10);

	retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
	if (!retval) {
		struct eth_pdata *pdata = dev->platdata;
		switch (op) {
		case env_op_create:
		case env_op_overwrite:
			eth_parse_enetaddr(value, pdata->enetaddr);
			break;
		case env_op_delete:
			memset(pdata->enetaddr, 0, 6);
		}
	}

	return 0;
}
U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);

335 336 337 338
int eth_init(void)
{
	struct udevice *current;
	struct udevice *old_current;
J
Joe Hershberger 已提交
339
	int ret = -ENODEV;
340 341 342 343 344 345 346 347 348 349 350 351

	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)) {
J
Joe Hershberger 已提交
352 353
			ret = eth_get_ops(current)->start(current);
			if (ret >= 0) {
354 355 356 357 358 359
				struct eth_device_priv *priv =
					current->uclass_priv;

				priv->state = ETH_STATE_ACTIVE;
				return 0;
			}
360
		} else {
J
Joe Hershberger 已提交
361
			ret = eth_errno;
362
		}
J
Joe Hershberger 已提交
363

364 365
		debug("FAIL\n");

J
Joe Hershberger 已提交
366 367 368 369
		/*
		 * If ethrotate is enabled, this will change "current",
		 * otherwise we will drop out of this while loop immediately
		 */
370
		eth_try_another(0);
J
Joe Hershberger 已提交
371
		/* This will ensure the new "current" attempted to probe */
372 373 374
		current = eth_get_dev();
	} while (old_current != current);

J
Joe Hershberger 已提交
375
	return ret;
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
}

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;
}

392 393 394 395 396 397 398 399 400 401 402
int eth_is_active(struct udevice *dev)
{
	struct eth_device_priv *priv;

	if (!dev || !device_active(dev))
		return 0;

	priv = dev_get_uclass_priv(dev);
	return priv->state == ETH_STATE_ACTIVE;
}

403 404 405
int eth_send(void *packet, int length)
{
	struct udevice *current;
J
Joe Hershberger 已提交
406
	int ret;
407 408 409 410 411 412 413 414

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

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

J
Joe Hershberger 已提交
415 416 417 418 419 420
	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;
421 422 423 424 425
}

int eth_rx(void)
{
	struct udevice *current;
426
	uchar *packet;
427
	int flags;
428 429
	int ret;
	int i;
430 431 432 433 434 435 436 437

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

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

438
	/* Process up to 32 packets at one time */
439
	flags = ETH_RECV_CHECK_DEVICE;
440
	for (i = 0; i < 32; i++) {
441 442
		ret = eth_get_ops(current)->recv(current, flags, &packet);
		flags = 0;
443 444
		if (ret > 0)
			net_process_received_packet(packet, ret);
445 446 447
		if (ret >= 0 && eth_get_ops(current)->free_pkt)
			eth_get_ops(current)->free_pkt(current, packet, ret);
		if (ret <= 0)
448 449 450 451
			break;
	}
	if (ret == -EAGAIN)
		ret = 0;
J
Joe Hershberger 已提交
452 453 454 455
	if (ret < 0) {
		/* We cannot completely return the error at present */
		debug("%s: recv() returned error %d\n", __func__, ret);
	}
456
	return ret;
457 458 459 460 461 462 463
}

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

464
	eth_common_init();
465 466 467 468 469 470 471 472 473 474 475 476

	/*
	 * 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 {
477 478 479 480 481 482 483 484 485 486 487 488
		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);
		}

489 490 491 492 493 494 495
		bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
		do {
			if (num_devices)
				printf(", ");

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

496 497 498
			if (ethprime && dev == prime_dev)
				printf(" [PRIME]");

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
			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);
544 545
	if (!is_zero_ethaddr(env_enetaddr)) {
		if (!is_zero_ethaddr(pdata->enetaddr) &&
546 547 548 549 550 551 552 553 554 555 556
		    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);
557
	} else if (is_valid_ethaddr(pdata->enetaddr)) {
558 559 560
		eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
		printf("\nWarning: %s using MAC address from ROM\n",
		       dev->name);
561
	} else if (is_zero_ethaddr(pdata->enetaddr)) {
562 563 564 565 566
#ifdef CONFIG_NET_RANDOM_ETHADDR
		net_random_ethaddr(pdata->enetaddr);
		printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
		       dev->name, dev->seq, pdata->enetaddr);
#else
567 568 569
		printf("\nError: %s address not set.\n",
		       dev->name);
		return -EINVAL;
570
#endif
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	}

	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),
592
	.flags		= DM_UC_FLAG_SEQ_ALIAS,
593
};
594
#endif /* #ifdef CONFIG_DM_ETH */
595 596

#ifndef CONFIG_DM_ETH
597

598 599 600 601 602 603
#ifdef CONFIG_API
static struct {
	uchar data[PKTSIZE];
	int length;
} eth_rcv_bufs[PKTBUFSRX];

604
static unsigned int eth_rcv_current, eth_rcv_last;
605 606
#endif

607 608
static struct eth_device *eth_devices;
struct eth_device *eth_current;
W
wdenk 已提交
609

610 611 612 613 614
static void eth_set_current_to_next(void)
{
	eth_current = eth_current->next;
}

615 616 617 618 619
static void eth_set_dev(struct eth_device *dev)
{
	eth_current = dev;
}

620
struct eth_device *eth_get_dev_by_name(const char *devname)
M
Marian Balakowicz 已提交
621 622 623
{
	struct eth_device *dev, *target_dev;

624 625
	BUG_ON(devname == NULL);

M
Marian Balakowicz 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
	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 已提交
642 643 644 645 646 647 648 649 650 651
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 已提交
652
		if (dev->index == index) {
A
Andy Fleming 已提交
653 654 655 656 657 658 659 660 661
			target_dev = dev;
			break;
		}
		dev = dev->next;
	} while (dev != eth_devices);

	return target_dev;
}

662
int eth_get_dev_index(void)
W
wdenk 已提交
663
{
664
	if (!eth_current)
M
Michael Walle 已提交
665
		return -1;
W
wdenk 已提交
666

M
Michael Walle 已提交
667
	return eth_current->index;
W
wdenk 已提交
668 669
}

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
static int on_ethaddr(const char *name, const char *value, enum env_op op,
	int flags)
{
	int index;
	struct eth_device *dev;

	if (!eth_devices)
		return 0;

	/* look for an index after "eth" */
	index = simple_strtoul(name + 3, NULL, 10);

	dev = eth_devices;
	do {
		if (dev->index == index) {
			switch (op) {
			case env_op_create:
			case env_op_overwrite:
				eth_parse_enetaddr(value, dev->enetaddr);
				break;
			case env_op_delete:
				memset(dev->enetaddr, 0, 6);
			}
		}
	} while (dev != eth_devices);

	return 0;
}
U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);

700 701 702 703 704 705
int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
		   int eth_number)
{
	unsigned char env_enetaddr[6];
	int ret = 0;

706
	eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
707

708 709
	if (!is_zero_ethaddr(env_enetaddr)) {
		if (!is_zero_ethaddr(dev->enetaddr) &&
710
		    memcmp(dev->enetaddr, env_enetaddr, 6)) {
711
			printf("\nWarning: %s MAC addresses don't match:\n",
712
			       dev->name);
713
			printf("Address in SROM is         %pM\n",
714
			       dev->enetaddr);
715
			printf("Address in environment is  %pM\n",
716
			       env_enetaddr);
717 718 719
		}

		memcpy(dev->enetaddr, env_enetaddr, 6);
720
	} else if (is_valid_ethaddr(dev->enetaddr)) {
721 722 723
		eth_setenv_enetaddr_by_index(base_name, eth_number,
					     dev->enetaddr);
		printf("\nWarning: %s using MAC address from net device\n",
724
		       dev->name);
725
	} else if (is_zero_ethaddr(dev->enetaddr)) {
726 727 728 729 730
#ifdef CONFIG_NET_RANDOM_ETHADDR
		net_random_ethaddr(dev->enetaddr);
		printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
		       dev->name, eth_number, dev->enetaddr);
#else
731 732 733
		printf("\nError: %s address not set.\n",
		       dev->name);
		return -EINVAL;
734
#endif
735 736
	}

737
	if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
738
		if (!is_valid_ethaddr(dev->enetaddr)) {
739
			printf("\nError: %s address %pM illegal value\n",
740
			       dev->name, dev->enetaddr);
741 742
			return -EINVAL;
		}
743

744
		ret = dev->write_hwaddr(dev);
745
		if (ret)
746 747
			printf("\nWarning: %s failed to set MAC address\n",
			       dev->name);
748
	}
749 750 751 752

	return ret;
}

753 754 755
int eth_register(struct eth_device *dev)
{
	struct eth_device *d;
756
	static int index;
757

758
	assert(strlen(dev->name) < sizeof(dev->name));
759

760
	if (!eth_devices) {
761 762
		eth_devices = dev;
		eth_current = dev;
763
		eth_current_changed();
W
wdenk 已提交
764
	} else {
765
		for (d = eth_devices; d->next != eth_devices; d = d->next)
766
			;
W
wdenk 已提交
767 768 769 770 771
		d->next = dev;
	}

	dev->state = ETH_STATE_INIT;
	dev->next  = eth_devices;
M
Michael Walle 已提交
772
	dev->index = index++;
W
wdenk 已提交
773 774 775 776

	return 0;
}

777 778 779 780 781 782
int eth_unregister(struct eth_device *dev)
{
	struct eth_device *cur;

	/* No device */
	if (!eth_devices)
783
		return -ENODEV;
784 785 786 787 788 789 790

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

	/* Device not found */
	if (cur->next != dev)
791
		return -ENODEV;
792 793 794 795 796 797 798 799 800 801 802 803 804 805

	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;
}

806
int eth_initialize(void)
W
wdenk 已提交
807
{
M
Michael Walle 已提交
808
	int num_devices = 0;
809

W
wdenk 已提交
810 811
	eth_devices = NULL;
	eth_current = NULL;
812
	eth_common_init();
813

W
wdenk 已提交
814
	if (!eth_devices) {
815
		puts("No ethernet found.\n");
816
		bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
W
wdenk 已提交
817 818
	} else {
		struct eth_device *dev = eth_devices;
819
		char *ethprime = getenv("ethprime");
W
wdenk 已提交
820

821
		bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
W
wdenk 已提交
822
		do {
M
Michael Walle 已提交
823
			if (dev->index)
824
				puts(", ");
W
wdenk 已提交
825 826 827

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

828
			if (ethprime && strcmp(dev->name, ethprime) == 0) {
W
wdenk 已提交
829
				eth_current = dev;
830
				puts(" [PRIME]");
W
wdenk 已提交
831 832
			}

833
			if (strchr(dev->name, ' '))
834 835
				puts("\nWarning: eth device name has a space!"
					"\n");
836

837
			eth_write_hwaddr(dev, "eth", dev->index);
W
wdenk 已提交
838 839

			dev = dev->next;
M
Michael Walle 已提交
840
			num_devices++;
841
		} while (dev != eth_devices);
W
wdenk 已提交
842

843
		eth_current_changed();
844
		putc('\n');
W
wdenk 已提交
845 846
	}

M
Michael Walle 已提交
847
	return num_devices;
W
wdenk 已提交
848 849
}

D
David Updegraff 已提交
850 851 852
#ifdef CONFIG_MCAST_TFTP
/* Multicast.
 * mcast_addr: multicast ipaddr from which multicast Mac is made
853
 * join: 1=join, 0=leave.
D
David Updegraff 已提交
854
 */
855
int eth_mcast_join(struct in_addr mcast_ip, int join)
D
David Updegraff 已提交
856
{
857
	u8 mcast_mac[6];
858
	if (!eth_current || !eth_current->mcast)
D
David Updegraff 已提交
859
		return -1;
860 861 862
	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 已提交
863 864 865 866 867 868
	mcast_mac[2] = 0x5e;
	mcast_mac[1] = 0x0;
	mcast_mac[0] = 0x1;
	return eth_current->mcast(eth_current, mcast_mac, join);
}

869 870
/* 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 已提交
871 872 873
 * some other adapter -- hash tables
 */
#define CRCPOLY_LE 0xedb88320
874
u32 ether_crc(size_t len, unsigned char const *p)
D
David Updegraff 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
{
	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 已提交
895

896
int eth_init(void)
W
wdenk 已提交
897
{
898
	struct eth_device *old_current;
W
wdenk 已提交
899

900
	if (!eth_current) {
901
		puts("No ethernet found.\n");
902
		return -ENODEV;
903
	}
W
wdenk 已提交
904 905 906

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

909
		if (eth_current->init(eth_current, gd->bd) >= 0) {
W
wdenk 已提交
910 911
			eth_current->state = ETH_STATE_ACTIVE;

912
			return 0;
W
wdenk 已提交
913
		}
R
Robin Getz 已提交
914
		debug("FAIL\n");
W
wdenk 已提交
915 916 917 918

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

919
	return -ETIMEDOUT;
W
wdenk 已提交
920 921 922 923 924 925 926 927 928 929 930 931
}

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

	eth_current->halt(eth_current);

	eth_current->state = ETH_STATE_PASSIVE;
}

932 933 934 935 936
int eth_is_active(struct eth_device *dev)
{
	return dev && dev->state == ETH_STATE_ACTIVE;
}

937
int eth_send(void *packet, int length)
W
wdenk 已提交
938 939
{
	if (!eth_current)
940
		return -ENODEV;
W
wdenk 已提交
941 942 943 944 945 946 947

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

int eth_rx(void)
{
	if (!eth_current)
948
		return -ENODEV;
W
wdenk 已提交
949 950 951

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

954
#ifdef CONFIG_API
955
static void eth_save_packet(void *packet, int length)
956
{
957
	char *p = packet;
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
	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;
}

973
int eth_receive(void *packet, int length)
974
{
975
	char *p = packet;
976 977 978 979 980 981 982 983 984 985 986 987
	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;
	}

988
	length = min(eth_rcv_bufs[eth_rcv_current].length, length);
989 990 991 992 993 994 995 996 997

	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 */

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
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 已提交
1014 1015
void eth_try_another(int first_restart)
{
1016
	static void *first_failed;
R
Remy Bohmer 已提交
1017
	char *ethrotate;
1018 1019 1020 1021 1022

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

1027
	if (!eth_get_dev())
W
wdenk 已提交
1028 1029
		return;

1030
	if (first_restart)
1031
		first_failed = eth_get_dev();
W
wdenk 已提交
1032

1033
	eth_set_current_to_next();
W
wdenk 已提交
1034

1035
	eth_current_changed();
1036

1037
	if (first_failed == eth_get_dev())
1038
		net_restart_wrap = 1;
W
wdenk 已提交
1039 1040
}

1041 1042
void eth_set_current(void)
{
1043 1044
	static char *act;
	static int  env_changed_id;
H
Heiko Schocher 已提交
1045
	int	env_id;
1046

H
Heiko Schocher 已提交
1047 1048 1049 1050 1051
	env_id = get_env_id();
	if ((act == NULL) || (env_changed_id != env_id)) {
		act = getenv("ethact");
		env_changed_id = env_id;
	}
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

	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 {
1064
		eth_set_dev(eth_get_dev_by_name(act));
1065
	}
1066

1067
	eth_current_changed();
1068 1069
}

1070
const char *eth_get_name(void)
1071
{
1072
	return eth_get_dev() ? eth_get_dev()->name : "unknown";
1073
}