]>
Commit | Line | Data |
---|---|---|
c609719b | 1 | /* |
aba4b69d | 2 | * (C) Copyright 2001-2010 |
c609719b WD |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
c609719b WD |
6 | */ |
7 | ||
8 | #include <common.h> | |
9 | #include <command.h> | |
10 | #include <net.h> | |
d9785c14 | 11 | #include <miiphy.h> |
5f184715 | 12 | #include <phy.h> |
75d9a45c | 13 | #include <asm/errno.h> |
c609719b | 14 | |
3f6e6993 MF |
15 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
16 | { | |
17 | char *end; | |
18 | int i; | |
19 | ||
20 | for (i = 0; i < 6; ++i) { | |
21 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; | |
22 | if (addr) | |
23 | addr = (*end) ? end + 1 : end; | |
24 | } | |
25 | } | |
26 | ||
27 | int eth_getenv_enetaddr(char *name, uchar *enetaddr) | |
28 | { | |
29 | eth_parse_enetaddr(getenv(name), enetaddr); | |
30 | return is_valid_ether_addr(enetaddr); | |
31 | } | |
32 | ||
33 | int eth_setenv_enetaddr(char *name, const uchar *enetaddr) | |
34 | { | |
35 | char buf[20]; | |
36 | ||
37 | sprintf(buf, "%pM", enetaddr); | |
38 | ||
39 | return setenv(name, buf); | |
40 | } | |
86848a74 | 41 | |
7616e785 SG |
42 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
43 | uchar *enetaddr) | |
86848a74 MF |
44 | { |
45 | char enetvar[32]; | |
7616e785 | 46 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
86848a74 MF |
47 | return eth_getenv_enetaddr(enetvar, enetaddr); |
48 | } | |
3f6e6993 | 49 | |
154177e1 | 50 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
c88ef3c1 RH |
51 | uchar *enetaddr) |
52 | { | |
53 | char enetvar[32]; | |
54 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); | |
55 | return eth_setenv_enetaddr(enetvar, enetaddr); | |
56 | } | |
57 | ||
58 | ||
ecee9324 BW |
59 | static int eth_mac_skip(int index) |
60 | { | |
61 | char enetvar[15]; | |
62 | char *skip_state; | |
63 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); | |
64 | return ((skip_state = getenv(enetvar)) != NULL); | |
65 | } | |
66 | ||
dd35479a BW |
67 | /* |
68 | * CPU and board-specific Ethernet initializations. Aliased function | |
69 | * signals caller to move on | |
70 | */ | |
71 | static int __def_eth_init(bd_t *bis) | |
72 | { | |
73 | return -1; | |
74 | } | |
f9a109b3 PT |
75 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
76 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); | |
dd35479a | 77 | |
f85b6071 | 78 | #ifdef CONFIG_API |
f85b6071 RJ |
79 | static struct { |
80 | uchar data[PKTSIZE]; | |
81 | int length; | |
82 | } eth_rcv_bufs[PKTBUFSRX]; | |
83 | ||
66c7385a | 84 | static unsigned int eth_rcv_current, eth_rcv_last; |
f85b6071 RJ |
85 | #endif |
86 | ||
f8be7d65 JH |
87 | static struct eth_device *eth_devices; |
88 | struct eth_device *eth_current; | |
c609719b | 89 | |
d7fb9bcf | 90 | struct eth_device *eth_get_dev_by_name(const char *devname) |
63ff004c MB |
91 | { |
92 | struct eth_device *dev, *target_dev; | |
93 | ||
7e7f903f HR |
94 | BUG_ON(devname == NULL); |
95 | ||
63ff004c MB |
96 | if (!eth_devices) |
97 | return NULL; | |
98 | ||
99 | dev = eth_devices; | |
100 | target_dev = NULL; | |
101 | do { | |
102 | if (strcmp(devname, dev->name) == 0) { | |
103 | target_dev = dev; | |
104 | break; | |
105 | } | |
106 | dev = dev->next; | |
107 | } while (dev != eth_devices); | |
108 | ||
109 | return target_dev; | |
110 | } | |
111 | ||
9e56986a AF |
112 | struct eth_device *eth_get_dev_by_index(int index) |
113 | { | |
114 | struct eth_device *dev, *target_dev; | |
9e56986a AF |
115 | |
116 | if (!eth_devices) | |
117 | return NULL; | |
118 | ||
119 | dev = eth_devices; | |
120 | target_dev = NULL; | |
121 | do { | |
fea7dcae | 122 | if (dev->index == index) { |
9e56986a AF |
123 | target_dev = dev; |
124 | break; | |
125 | } | |
126 | dev = dev->next; | |
9e56986a AF |
127 | } while (dev != eth_devices); |
128 | ||
129 | return target_dev; | |
130 | } | |
131 | ||
66c7385a | 132 | int eth_get_dev_index(void) |
c609719b | 133 | { |
66c7385a | 134 | if (!eth_current) |
fea7dcae | 135 | return -1; |
c609719b | 136 | |
fea7dcae | 137 | return eth_current->index; |
c609719b WD |
138 | } |
139 | ||
89d48367 | 140 | static void eth_current_changed(void) |
c609719b | 141 | { |
e2a53458 MF |
142 | char *act = getenv("ethact"); |
143 | /* update current ethernet name */ | |
144 | if (eth_current) { | |
145 | if (act == NULL || strcmp(act, eth_current->name) != 0) | |
146 | setenv("ethact", eth_current->name); | |
89d48367 | 147 | } |
e2a53458 MF |
148 | /* |
149 | * remove the variable completely if there is no active | |
150 | * interface | |
151 | */ | |
152 | else if (act != NULL) | |
153 | setenv("ethact", NULL); | |
89d48367 SG |
154 | } |
155 | ||
75d9a45c PM |
156 | int eth_address_set(unsigned char *addr) |
157 | { | |
158 | return memcmp(addr, "\0\0\0\0\0\0", 6); | |
159 | } | |
160 | ||
7616e785 SG |
161 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
162 | int eth_number) | |
163 | { | |
164 | unsigned char env_enetaddr[6]; | |
165 | int ret = 0; | |
166 | ||
69376644 | 167 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
7616e785 | 168 | |
75d9a45c PM |
169 | if (eth_address_set(env_enetaddr)) { |
170 | if (eth_address_set(dev->enetaddr) && | |
66c7385a | 171 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
7616e785 SG |
172 | printf("\nWarning: %s MAC addresses don't match:\n", |
173 | dev->name); | |
174 | printf("Address in SROM is %pM\n", | |
175 | dev->enetaddr); | |
176 | printf("Address in environment is %pM\n", | |
177 | env_enetaddr); | |
178 | } | |
179 | ||
180 | memcpy(dev->enetaddr, env_enetaddr, 6); | |
c88ef3c1 RH |
181 | } else if (is_valid_ether_addr(dev->enetaddr)) { |
182 | eth_setenv_enetaddr_by_index(base_name, eth_number, | |
183 | dev->enetaddr); | |
184 | printf("\nWarning: %s using MAC address from net device\n", | |
185 | dev->name); | |
75d9a45c PM |
186 | } else if (!(eth_address_set(dev->enetaddr))) { |
187 | printf("\nError: %s address not set.\n", | |
188 | dev->name); | |
189 | return -EINVAL; | |
7616e785 SG |
190 | } |
191 | ||
75d9a45c PM |
192 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
193 | if (!is_valid_ether_addr(dev->enetaddr)) { | |
194 | printf("\nError: %s address %pM illegal value\n", | |
195 | dev->name, dev->enetaddr); | |
196 | return -EINVAL; | |
197 | } | |
460f949f | 198 | |
7616e785 | 199 | ret = dev->write_hwaddr(dev); |
75d9a45c PM |
200 | if (ret) |
201 | printf("\nWarning: %s failed to set MAC address\n", dev->name); | |
460f949f | 202 | } |
7616e785 SG |
203 | |
204 | return ret; | |
205 | } | |
206 | ||
89d48367 SG |
207 | int eth_register(struct eth_device *dev) |
208 | { | |
209 | struct eth_device *d; | |
66c7385a | 210 | static int index; |
58c583b6 | 211 | |
f6add132 | 212 | assert(strlen(dev->name) < sizeof(dev->name)); |
58c583b6 | 213 | |
89d48367 SG |
214 | if (!eth_devices) { |
215 | eth_current = eth_devices = dev; | |
216 | eth_current_changed(); | |
c609719b | 217 | } else { |
66c7385a | 218 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
aba4b69d | 219 | ; |
c609719b WD |
220 | d->next = dev; |
221 | } | |
222 | ||
223 | dev->state = ETH_STATE_INIT; | |
224 | dev->next = eth_devices; | |
fea7dcae | 225 | dev->index = index++; |
c609719b WD |
226 | |
227 | return 0; | |
228 | } | |
229 | ||
e7e982d6 VP |
230 | int eth_unregister(struct eth_device *dev) |
231 | { | |
232 | struct eth_device *cur; | |
233 | ||
234 | /* No device */ | |
235 | if (!eth_devices) | |
236 | return -1; | |
237 | ||
238 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; | |
239 | cur = cur->next) | |
240 | ; | |
241 | ||
242 | /* Device not found */ | |
243 | if (cur->next != dev) | |
244 | return -1; | |
245 | ||
246 | cur->next = dev->next; | |
247 | ||
248 | if (eth_devices == dev) | |
249 | eth_devices = dev->next == eth_devices ? NULL : dev->next; | |
250 | ||
251 | if (eth_current == dev) { | |
252 | eth_current = eth_devices; | |
253 | eth_current_changed(); | |
254 | } | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
de30122b MF |
259 | static void eth_env_init(bd_t *bis) |
260 | { | |
261 | const char *s; | |
262 | ||
263 | if ((s = getenv("bootfile")) != NULL) | |
264 | copy_filename(BootFile, s, sizeof(BootFile)); | |
265 | } | |
266 | ||
c609719b WD |
267 | int eth_initialize(bd_t *bis) |
268 | { | |
fea7dcae | 269 | int num_devices = 0; |
c609719b WD |
270 | eth_devices = NULL; |
271 | eth_current = NULL; | |
272 | ||
770605e4 | 273 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
27ee59af | 274 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
d9785c14 MB |
275 | miiphy_init(); |
276 | #endif | |
5f184715 AF |
277 | |
278 | #ifdef CONFIG_PHYLIB | |
279 | phy_init(); | |
280 | #endif | |
281 | ||
de30122b MF |
282 | eth_env_init(bis); |
283 | ||
8ad25bf8 BW |
284 | /* |
285 | * If board-specific initialization exists, call it. | |
286 | * If not, call a CPU-specific one | |
287 | */ | |
288 | if (board_eth_init != __def_eth_init) { | |
289 | if (board_eth_init(bis) < 0) | |
290 | printf("Board Net Initialization Failed\n"); | |
291 | } else if (cpu_eth_init != __def_eth_init) { | |
292 | if (cpu_eth_init(bis) < 0) | |
293 | printf("CPU Net Initialization Failed\n"); | |
294 | } else | |
295 | printf("Net Initialization Skipped\n"); | |
d9785c14 | 296 | |
c609719b | 297 | if (!eth_devices) { |
66c7385a | 298 | puts("No ethernet found.\n"); |
770605e4 | 299 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
c609719b WD |
300 | } else { |
301 | struct eth_device *dev = eth_devices; | |
66c7385a | 302 | char *ethprime = getenv("ethprime"); |
c609719b | 303 | |
770605e4 | 304 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
c609719b | 305 | do { |
fea7dcae | 306 | if (dev->index) |
66c7385a | 307 | puts(", "); |
c609719b WD |
308 | |
309 | printf("%s", dev->name); | |
310 | ||
66c7385a | 311 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
c609719b | 312 | eth_current = dev; |
66c7385a | 313 | puts(" [PRIME]"); |
c609719b WD |
314 | } |
315 | ||
1384f3bb | 316 | if (strchr(dev->name, ' ')) |
66c7385a JH |
317 | puts("\nWarning: eth device name has a space!" |
318 | "\n"); | |
1384f3bb | 319 | |
75d9a45c | 320 | eth_write_hwaddr(dev, "eth", dev->index); |
c609719b | 321 | |
c609719b | 322 | dev = dev->next; |
fea7dcae | 323 | num_devices++; |
66c7385a | 324 | } while (dev != eth_devices); |
c609719b | 325 | |
89d48367 | 326 | eth_current_changed(); |
66c7385a | 327 | putc('\n'); |
c609719b WD |
328 | } |
329 | ||
fea7dcae | 330 | return num_devices; |
c609719b WD |
331 | } |
332 | ||
53a5c424 DU |
333 | #ifdef CONFIG_MCAST_TFTP |
334 | /* Multicast. | |
335 | * mcast_addr: multicast ipaddr from which multicast Mac is made | |
85eb5caf | 336 | * join: 1=join, 0=leave. |
53a5c424 | 337 | */ |
66c7385a | 338 | int eth_mcast_join(IPaddr_t mcast_ip, u8 join) |
53a5c424 | 339 | { |
66c7385a | 340 | u8 mcast_mac[6]; |
85eb5caf | 341 | if (!eth_current || !eth_current->mcast) |
53a5c424 DU |
342 | return -1; |
343 | mcast_mac[5] = htonl(mcast_ip) & 0xff; | |
344 | mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff; | |
345 | mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f; | |
346 | mcast_mac[2] = 0x5e; | |
347 | mcast_mac[1] = 0x0; | |
348 | mcast_mac[0] = 0x1; | |
349 | return eth_current->mcast(eth_current, mcast_mac, join); | |
350 | } | |
351 | ||
85eb5caf WD |
352 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
353 | * and this is the ethernet-crc method needed for TSEC -- and perhaps | |
53a5c424 DU |
354 | * some other adapter -- hash tables |
355 | */ | |
356 | #define CRCPOLY_LE 0xedb88320 | |
66c7385a | 357 | u32 ether_crc(size_t len, unsigned char const *p) |
53a5c424 DU |
358 | { |
359 | int i; | |
360 | u32 crc; | |
361 | crc = ~0; | |
362 | while (len--) { | |
363 | crc ^= *p++; | |
364 | for (i = 0; i < 8; i++) | |
365 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); | |
366 | } | |
367 | /* an reverse the bits, cuz of way they arrive -- last-first */ | |
368 | crc = (crc >> 16) | (crc << 16); | |
369 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); | |
370 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); | |
371 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); | |
372 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); | |
373 | return crc; | |
374 | } | |
375 | ||
376 | #endif | |
377 | ||
c609719b WD |
378 | |
379 | int eth_init(bd_t *bis) | |
380 | { | |
86848a74 | 381 | struct eth_device *old_current, *dev; |
c609719b | 382 | |
6bc11388 | 383 | if (!eth_current) { |
66c7385a | 384 | puts("No ethernet found.\n"); |
505be87a | 385 | return -1; |
6bc11388 | 386 | } |
c609719b | 387 | |
86848a74 | 388 | /* Sync environment with network devices */ |
86848a74 MF |
389 | dev = eth_devices; |
390 | do { | |
391 | uchar env_enetaddr[6]; | |
392 | ||
fea7dcae | 393 | if (eth_getenv_enetaddr_by_index("eth", dev->index, |
7616e785 | 394 | env_enetaddr)) |
86848a74 MF |
395 | memcpy(dev->enetaddr, env_enetaddr, 6); |
396 | ||
86848a74 MF |
397 | dev = dev->next; |
398 | } while (dev != eth_devices); | |
399 | ||
c609719b WD |
400 | old_current = eth_current; |
401 | do { | |
0ebf04c6 | 402 | debug("Trying %s\n", eth_current->name); |
c609719b | 403 | |
66c7385a | 404 | if (eth_current->init(eth_current, bis) >= 0) { |
c609719b WD |
405 | eth_current->state = ETH_STATE_ACTIVE; |
406 | ||
505be87a | 407 | return 0; |
c609719b | 408 | } |
0ebf04c6 | 409 | debug("FAIL\n"); |
c609719b WD |
410 | |
411 | eth_try_another(0); | |
412 | } while (old_current != eth_current); | |
413 | ||
505be87a | 414 | return -1; |
c609719b WD |
415 | } |
416 | ||
417 | void eth_halt(void) | |
418 | { | |
419 | if (!eth_current) | |
420 | return; | |
421 | ||
422 | eth_current->halt(eth_current); | |
423 | ||
424 | eth_current->state = ETH_STATE_PASSIVE; | |
425 | } | |
426 | ||
db288a96 | 427 | int eth_send(void *packet, int length) |
c609719b WD |
428 | { |
429 | if (!eth_current) | |
430 | return -1; | |
431 | ||
432 | return eth_current->send(eth_current, packet, length); | |
433 | } | |
434 | ||
435 | int eth_rx(void) | |
436 | { | |
437 | if (!eth_current) | |
438 | return -1; | |
439 | ||
440 | return eth_current->recv(eth_current); | |
441 | } | |
442 | ||
f85b6071 | 443 | #ifdef CONFIG_API |
db288a96 | 444 | static void eth_save_packet(void *packet, int length) |
f85b6071 | 445 | { |
db288a96 | 446 | char *p = packet; |
f85b6071 RJ |
447 | int i; |
448 | ||
449 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) | |
450 | return; | |
451 | ||
452 | if (PKTSIZE < length) | |
453 | return; | |
454 | ||
455 | for (i = 0; i < length; i++) | |
456 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; | |
457 | ||
458 | eth_rcv_bufs[eth_rcv_last].length = length; | |
459 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; | |
460 | } | |
461 | ||
db288a96 | 462 | int eth_receive(void *packet, int length) |
f85b6071 | 463 | { |
db288a96 | 464 | char *p = packet; |
f85b6071 RJ |
465 | void *pp = push_packet; |
466 | int i; | |
467 | ||
468 | if (eth_rcv_current == eth_rcv_last) { | |
469 | push_packet = eth_save_packet; | |
470 | eth_rx(); | |
471 | push_packet = pp; | |
472 | ||
473 | if (eth_rcv_current == eth_rcv_last) | |
474 | return -1; | |
475 | } | |
476 | ||
46c07bcf | 477 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
f85b6071 RJ |
478 | |
479 | for (i = 0; i < length; i++) | |
480 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; | |
481 | ||
482 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; | |
483 | return length; | |
484 | } | |
485 | #endif /* CONFIG_API */ | |
486 | ||
c609719b WD |
487 | void eth_try_another(int first_restart) |
488 | { | |
66c7385a | 489 | static struct eth_device *first_failed; |
c650e1be | 490 | char *ethrotate; |
e1692577 MF |
491 | |
492 | /* | |
493 | * Do not rotate between network interfaces when | |
494 | * 'ethrotate' variable is set to 'no'. | |
495 | */ | |
66c7385a JH |
496 | ethrotate = getenv("ethrotate"); |
497 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) | |
e1692577 | 498 | return; |
c609719b WD |
499 | |
500 | if (!eth_current) | |
501 | return; | |
502 | ||
66c7385a | 503 | if (first_restart) |
c609719b | 504 | first_failed = eth_current; |
c609719b WD |
505 | |
506 | eth_current = eth_current->next; | |
507 | ||
89d48367 | 508 | eth_current_changed(); |
a3d991bd | 509 | |
66c7385a | 510 | if (first_failed == eth_current) |
c609719b | 511 | NetRestartWrap = 1; |
c609719b WD |
512 | } |
513 | ||
a3d991bd WD |
514 | void eth_set_current(void) |
515 | { | |
66c7385a JH |
516 | static char *act; |
517 | static int env_changed_id; | |
518 | struct eth_device *old_current; | |
2f70c49e | 519 | int env_id; |
a3d991bd WD |
520 | |
521 | if (!eth_current) /* XXX no current */ | |
522 | return; | |
523 | ||
2f70c49e HS |
524 | env_id = get_env_id(); |
525 | if ((act == NULL) || (env_changed_id != env_id)) { | |
526 | act = getenv("ethact"); | |
527 | env_changed_id = env_id; | |
528 | } | |
a3d991bd WD |
529 | if (act != NULL) { |
530 | old_current = eth_current; | |
531 | do { | |
532 | if (strcmp(eth_current->name, act) == 0) | |
533 | return; | |
534 | eth_current = eth_current->next; | |
535 | } while (old_current != eth_current); | |
536 | } | |
537 | ||
89d48367 | 538 | eth_current_changed(); |
a3d991bd | 539 | } |
a3d991bd | 540 | |
66c7385a | 541 | char *eth_get_name(void) |
5bb226e8 | 542 | { |
66c7385a | 543 | return eth_current ? eth_current->name : "unknown"; |
5bb226e8 | 544 | } |