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

#include <common.h>
#include <command.h>
#include <net.h>
11
#include <miiphy.h>
A
Andy Fleming 已提交
12
#include <phy.h>
13
#include <asm/errno.h>
W
wdenk 已提交
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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);
	return is_valid_ether_addr(enetaddr);
}

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

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

	return setenv(name, buf);
}
41

42 43
int eth_getenv_enetaddr_by_index(const char *base_name, int index,
				 uchar *enetaddr)
44 45
{
	char enetvar[32];
46
	sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
47 48
	return eth_getenv_enetaddr(enetvar, enetaddr);
}
49

50
static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
51 52 53 54 55 56 57 58
				 uchar *enetaddr)
{
	char enetvar[32];
	sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
	return eth_setenv_enetaddr(enetvar, enetaddr);
}


59 60 61 62 63 64 65 66
static int eth_mac_skip(int index)
{
	char enetvar[15];
	char *skip_state;
	sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
	return ((skip_state = getenv(enetvar)) != NULL);
}

67 68 69 70 71 72 73 74
/*
 * CPU and board-specific Ethernet initializations.  Aliased function
 * signals caller to move on
 */
static int __def_eth_init(bd_t *bis)
{
	return -1;
}
75 76
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")));
77

78 79 80 81 82 83
#ifdef CONFIG_API
static struct {
	uchar data[PKTSIZE];
	int length;
} eth_rcv_bufs[PKTBUFSRX];

84
static unsigned int eth_rcv_current, eth_rcv_last;
85 86
#endif

87 88
static struct eth_device *eth_devices;
struct eth_device *eth_current;
W
wdenk 已提交
89

90
struct eth_device *eth_get_dev_by_name(const char *devname)
M
Marian Balakowicz 已提交
91 92 93
{
	struct eth_device *dev, *target_dev;

94 95
	BUG_ON(devname == NULL);

M
Marian Balakowicz 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	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 已提交
112 113 114 115 116 117 118 119 120 121
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 已提交
122
		if (dev->index == index) {
A
Andy Fleming 已提交
123 124 125 126 127 128 129 130 131
			target_dev = dev;
			break;
		}
		dev = dev->next;
	} while (dev != eth_devices);

	return target_dev;
}

132
int eth_get_dev_index(void)
W
wdenk 已提交
133
{
134
	if (!eth_current)
M
Michael Walle 已提交
135
		return -1;
W
wdenk 已提交
136

M
Michael Walle 已提交
137
	return eth_current->index;
W
wdenk 已提交
138 139
}

140
static void eth_current_changed(void)
W
wdenk 已提交
141
{
M
Mike Frysinger 已提交
142 143 144 145 146
	char *act = getenv("ethact");
	/* update current ethernet name */
	if (eth_current) {
		if (act == NULL || strcmp(act, eth_current->name) != 0)
			setenv("ethact", eth_current->name);
147
	}
M
Mike Frysinger 已提交
148 149 150 151 152 153
	/*
	 * remove the variable completely if there is no active
	 * interface
	 */
	else if (act != NULL)
		setenv("ethact", NULL);
154 155
}

156 157 158 159 160
int eth_address_set(unsigned char *addr)
{
	return memcmp(addr, "\0\0\0\0\0\0", 6);
}

161 162 163 164 165 166
int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
		   int eth_number)
{
	unsigned char env_enetaddr[6];
	int ret = 0;

167
	eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
168

169 170
	if (eth_address_set(env_enetaddr)) {
		if (eth_address_set(dev->enetaddr) &&
171
				memcmp(dev->enetaddr, env_enetaddr, 6)) {
172 173 174 175 176 177 178 179 180
			printf("\nWarning: %s MAC addresses don't match:\n",
				dev->name);
			printf("Address in SROM is         %pM\n",
				dev->enetaddr);
			printf("Address in environment is  %pM\n",
				env_enetaddr);
		}

		memcpy(dev->enetaddr, env_enetaddr, 6);
181 182 183 184 185
	} else if (is_valid_ether_addr(dev->enetaddr)) {
		eth_setenv_enetaddr_by_index(base_name, eth_number,
					     dev->enetaddr);
		printf("\nWarning: %s using MAC address from net device\n",
			dev->name);
186 187 188 189
	} else if (!(eth_address_set(dev->enetaddr))) {
		printf("\nError: %s address not set.\n",
		       dev->name);
		return -EINVAL;
190 191
	}

192 193 194 195 196 197
	if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
		if (!is_valid_ether_addr(dev->enetaddr)) {
			printf("\nError: %s address %pM illegal value\n",
				 dev->name, dev->enetaddr);
			return -EINVAL;
		}
198

199
		ret = dev->write_hwaddr(dev);
200 201
		if (ret)
			printf("\nWarning: %s failed to set MAC address\n", dev->name);
202
	}
203 204 205 206

	return ret;
}

207 208 209
int eth_register(struct eth_device *dev)
{
	struct eth_device *d;
210
	static int index;
211

212
	assert(strlen(dev->name) < sizeof(dev->name));
213

214 215 216
	if (!eth_devices) {
		eth_current = eth_devices = dev;
		eth_current_changed();
W
wdenk 已提交
217
	} else {
218
		for (d = eth_devices; d->next != eth_devices; d = d->next)
219
			;
W
wdenk 已提交
220 221 222 223 224
		d->next = dev;
	}

	dev->state = ETH_STATE_INIT;
	dev->next  = eth_devices;
M
Michael Walle 已提交
225
	dev->index = index++;
W
wdenk 已提交
226 227 228 229

	return 0;
}

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
int eth_unregister(struct eth_device *dev)
{
	struct eth_device *cur;

	/* No device */
	if (!eth_devices)
		return -1;

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

	/* Device not found */
	if (cur->next != dev)
		return -1;

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

259 260 261 262 263 264 265 266
static void eth_env_init(bd_t *bis)
{
	const char *s;

	if ((s = getenv("bootfile")) != NULL)
		copy_filename(BootFile, s, sizeof(BootFile));
}

W
wdenk 已提交
267 268
int eth_initialize(bd_t *bis)
{
M
Michael Walle 已提交
269
	int num_devices = 0;
W
wdenk 已提交
270 271 272
	eth_devices = NULL;
	eth_current = NULL;

273
	bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
274
#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
275 276
	miiphy_init();
#endif
A
Andy Fleming 已提交
277 278 279 280 281

#ifdef CONFIG_PHYLIB
	phy_init();
#endif

282 283
	eth_env_init(bis);

B
Ben Warren 已提交
284 285 286 287 288 289 290 291 292 293 294 295
	/*
	 * 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(bis) < 0)
			printf("Board Net Initialization Failed\n");
	} else if (cpu_eth_init != __def_eth_init) {
		if (cpu_eth_init(bis) < 0)
			printf("CPU Net Initialization Failed\n");
	} else
		printf("Net Initialization Skipped\n");
296

W
wdenk 已提交
297
	if (!eth_devices) {
298
		puts("No ethernet found.\n");
299
		bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
W
wdenk 已提交
300 301
	} else {
		struct eth_device *dev = eth_devices;
302
		char *ethprime = getenv("ethprime");
W
wdenk 已提交
303

304
		bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
W
wdenk 已提交
305
		do {
M
Michael Walle 已提交
306
			if (dev->index)
307
				puts(", ");
W
wdenk 已提交
308 309 310

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

311
			if (ethprime && strcmp(dev->name, ethprime) == 0) {
W
wdenk 已提交
312
				eth_current = dev;
313
				puts(" [PRIME]");
W
wdenk 已提交
314 315
			}

316
			if (strchr(dev->name, ' '))
317 318
				puts("\nWarning: eth device name has a space!"
					"\n");
319

320
			eth_write_hwaddr(dev, "eth", dev->index);
W
wdenk 已提交
321 322

			dev = dev->next;
M
Michael Walle 已提交
323
			num_devices++;
324
		} while (dev != eth_devices);
W
wdenk 已提交
325

326
		eth_current_changed();
327
		putc('\n');
W
wdenk 已提交
328 329
	}

M
Michael Walle 已提交
330
	return num_devices;
W
wdenk 已提交
331 332
}

D
David Updegraff 已提交
333 334 335
#ifdef CONFIG_MCAST_TFTP
/* Multicast.
 * mcast_addr: multicast ipaddr from which multicast Mac is made
336
 * join: 1=join, 0=leave.
D
David Updegraff 已提交
337
 */
338
int eth_mcast_join(IPaddr_t mcast_ip, u8 join)
D
David Updegraff 已提交
339
{
340
	u8 mcast_mac[6];
341
	if (!eth_current || !eth_current->mcast)
D
David Updegraff 已提交
342 343 344 345 346 347 348 349 350 351
		return -1;
	mcast_mac[5] = htonl(mcast_ip) & 0xff;
	mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
	mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
	mcast_mac[2] = 0x5e;
	mcast_mac[1] = 0x0;
	mcast_mac[0] = 0x1;
	return eth_current->mcast(eth_current, mcast_mac, join);
}

352 353
/* 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 已提交
354 355 356
 * some other adapter -- hash tables
 */
#define CRCPOLY_LE 0xedb88320
357
u32 ether_crc(size_t len, unsigned char const *p)
D
David Updegraff 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
{
	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 已提交
378 379 380

int eth_init(bd_t *bis)
{
381
	struct eth_device *old_current, *dev;
W
wdenk 已提交
382

383
	if (!eth_current) {
384
		puts("No ethernet found.\n");
385
		return -1;
386
	}
W
wdenk 已提交
387

388 389 390 391 392
	/* Sync environment with network devices */
	dev = eth_devices;
	do {
		uchar env_enetaddr[6];

M
Michael Walle 已提交
393
		if (eth_getenv_enetaddr_by_index("eth", dev->index,
394
						 env_enetaddr))
395 396 397 398 399
			memcpy(dev->enetaddr, env_enetaddr, 6);

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

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

404
		if (eth_current->init(eth_current, bis) >= 0) {
W
wdenk 已提交
405 406
			eth_current->state = ETH_STATE_ACTIVE;

407
			return 0;
W
wdenk 已提交
408
		}
R
Robin Getz 已提交
409
		debug("FAIL\n");
W
wdenk 已提交
410 411 412 413

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

414
	return -1;
W
wdenk 已提交
415 416 417 418 419 420 421 422 423 424 425 426
}

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

	eth_current->halt(eth_current);

	eth_current->state = ETH_STATE_PASSIVE;
}

427
int eth_send(void *packet, int length)
W
wdenk 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
{
	if (!eth_current)
		return -1;

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

int eth_rx(void)
{
	if (!eth_current)
		return -1;

	return eth_current->recv(eth_current);
}

443
#ifdef CONFIG_API
444
static void eth_save_packet(void *packet, int length)
445
{
446
	char *p = packet;
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	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;
}

462
int eth_receive(void *packet, int length)
463
{
464
	char *p = packet;
465 466 467 468 469 470 471 472 473 474 475 476
	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;
	}

477
	length = min(eth_rcv_bufs[eth_rcv_current].length, length);
478 479 480 481 482 483 484 485 486

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

W
wdenk 已提交
487 488
void eth_try_another(int first_restart)
{
489
	static struct eth_device *first_failed;
R
Remy Bohmer 已提交
490
	char *ethrotate;
491 492 493 494 495

	/*
	 * Do not rotate between network interfaces when
	 * 'ethrotate' variable is set to 'no'.
	 */
496 497
	ethrotate = getenv("ethrotate");
	if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
498
		return;
W
wdenk 已提交
499 500 501 502

	if (!eth_current)
		return;

503
	if (first_restart)
W
wdenk 已提交
504 505 506 507
		first_failed = eth_current;

	eth_current = eth_current->next;

508
	eth_current_changed();
509

510
	if (first_failed == eth_current)
W
wdenk 已提交
511 512 513
		NetRestartWrap = 1;
}

514 515
void eth_set_current(void)
{
516 517 518
	static char *act;
	static int  env_changed_id;
	struct eth_device *old_current;
H
Heiko Schocher 已提交
519
	int	env_id;
520 521 522 523

	if (!eth_current)	/* XXX no current */
		return;

H
Heiko Schocher 已提交
524 525 526 527 528
	env_id = get_env_id();
	if ((act == NULL) || (env_changed_id != env_id)) {
		act = getenv("ethact");
		env_changed_id = env_id;
	}
529 530 531 532 533 534 535 536 537
	if (act != NULL) {
		old_current = eth_current;
		do {
			if (strcmp(eth_current->name, act) == 0)
				return;
			eth_current = eth_current->next;
		} while (old_current != eth_current);
	}

538
	eth_current_changed();
539 540
}

541
char *eth_get_name(void)
542
{
543
	return eth_current ? eth_current->name : "unknown";
544
}