]>
Commit | Line | Data |
---|---|---|
c609719b | 1 | /* |
05c3e68f | 2 | * (C) Copyright 2001-2015 |
c609719b | 3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
05c3e68f | 4 | * Joe Hershberger, National Instruments |
c609719b | 5 | * |
1a459660 | 6 | * SPDX-License-Identifier: GPL-2.0+ |
c609719b WD |
7 | */ |
8 | ||
9 | #include <common.h> | |
10 | #include <command.h> | |
05c3e68f | 11 | #include <dm.h> |
c609719b | 12 | #include <net.h> |
d9785c14 | 13 | #include <miiphy.h> |
5f184715 | 14 | #include <phy.h> |
75d9a45c | 15 | #include <asm/errno.h> |
05c3e68f | 16 | #include <dm/device-internal.h> |
c609719b | 17 | |
d2eaec60 JH |
18 | DECLARE_GLOBAL_DATA_PTR; |
19 | ||
3f6e6993 MF |
20 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
21 | { | |
22 | char *end; | |
23 | int i; | |
24 | ||
25 | for (i = 0; i < 6; ++i) { | |
26 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; | |
27 | if (addr) | |
28 | addr = (*end) ? end + 1 : end; | |
29 | } | |
30 | } | |
31 | ||
32 | int eth_getenv_enetaddr(char *name, uchar *enetaddr) | |
33 | { | |
34 | eth_parse_enetaddr(getenv(name), enetaddr); | |
0adb5b76 | 35 | return is_valid_ethaddr(enetaddr); |
3f6e6993 MF |
36 | } |
37 | ||
38 | int eth_setenv_enetaddr(char *name, const uchar *enetaddr) | |
39 | { | |
40 | char buf[20]; | |
41 | ||
42 | sprintf(buf, "%pM", enetaddr); | |
43 | ||
44 | return setenv(name, buf); | |
45 | } | |
86848a74 | 46 | |
7616e785 SG |
47 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
48 | uchar *enetaddr) | |
86848a74 MF |
49 | { |
50 | char enetvar[32]; | |
7616e785 | 51 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
86848a74 MF |
52 | return eth_getenv_enetaddr(enetvar, enetaddr); |
53 | } | |
3f6e6993 | 54 | |
154177e1 | 55 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
c88ef3c1 RH |
56 | uchar *enetaddr) |
57 | { | |
58 | char enetvar[32]; | |
59 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); | |
60 | return eth_setenv_enetaddr(enetvar, enetaddr); | |
61 | } | |
62 | ||
84eb1fba JH |
63 | static void eth_env_init(void) |
64 | { | |
65 | const char *s; | |
66 | ||
67 | s = getenv("bootfile"); | |
68 | if (s != NULL) | |
1411157d JH |
69 | copy_filename(net_boot_file_name, s, |
70 | sizeof(net_boot_file_name)); | |
84eb1fba | 71 | } |
c88ef3c1 | 72 | |
ecee9324 BW |
73 | static int eth_mac_skip(int index) |
74 | { | |
75 | char enetvar[15]; | |
76 | char *skip_state; | |
ff819a3a | 77 | |
ecee9324 | 78 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
ff819a3a JH |
79 | skip_state = getenv(enetvar); |
80 | return skip_state != NULL; | |
ecee9324 BW |
81 | } |
82 | ||
84eb1fba JH |
83 | static void eth_current_changed(void); |
84 | ||
3bc42700 SG |
85 | /* |
86 | * CPU and board-specific Ethernet initializations. Aliased function | |
87 | * signals caller to move on | |
88 | */ | |
89 | static int __def_eth_init(bd_t *bis) | |
90 | { | |
91 | return -1; | |
92 | } | |
93 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); | |
94 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); | |
95 | ||
96 | static void eth_common_init(void) | |
97 | { | |
98 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); | |
99 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) | |
100 | miiphy_init(); | |
101 | #endif | |
102 | ||
103 | #ifdef CONFIG_PHYLIB | |
104 | phy_init(); | |
105 | #endif | |
106 | ||
107 | eth_env_init(); | |
108 | ||
109 | /* | |
110 | * If board-specific initialization exists, call it. | |
111 | * If not, call a CPU-specific one | |
112 | */ | |
113 | if (board_eth_init != __def_eth_init) { | |
114 | if (board_eth_init(gd->bd) < 0) | |
115 | printf("Board Net Initialization Failed\n"); | |
116 | } else if (cpu_eth_init != __def_eth_init) { | |
117 | if (cpu_eth_init(gd->bd) < 0) | |
118 | printf("CPU Net Initialization Failed\n"); | |
119 | } else { | |
120 | printf("Net Initialization Skipped\n"); | |
121 | } | |
122 | } | |
123 | ||
05c3e68f JH |
124 | #ifdef CONFIG_DM_ETH |
125 | /** | |
126 | * struct eth_device_priv - private structure for each Ethernet device | |
127 | * | |
128 | * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t) | |
129 | */ | |
130 | struct eth_device_priv { | |
131 | enum eth_state_t state; | |
132 | }; | |
133 | ||
134 | /** | |
135 | * struct eth_uclass_priv - The structure attached to the uclass itself | |
136 | * | |
137 | * @current: The Ethernet device that the network functions are using | |
138 | */ | |
139 | struct eth_uclass_priv { | |
140 | struct udevice *current; | |
141 | }; | |
142 | ||
60304592 JH |
143 | /* eth_errno - This stores the most recent failure code from DM functions */ |
144 | static int eth_errno; | |
145 | ||
05c3e68f JH |
146 | static struct eth_uclass_priv *eth_get_uclass_priv(void) |
147 | { | |
148 | struct uclass *uc; | |
149 | ||
150 | uclass_get(UCLASS_ETH, &uc); | |
151 | assert(uc); | |
152 | return uc->priv; | |
153 | } | |
154 | ||
155 | static void eth_set_current_to_next(void) | |
156 | { | |
157 | struct eth_uclass_priv *uc_priv; | |
158 | ||
159 | uc_priv = eth_get_uclass_priv(); | |
160 | if (uc_priv->current) | |
161 | uclass_next_device(&uc_priv->current); | |
162 | if (!uc_priv->current) | |
163 | uclass_first_device(UCLASS_ETH, &uc_priv->current); | |
164 | } | |
165 | ||
60304592 JH |
166 | /* |
167 | * Typically this will simply return the active device. | |
168 | * In the case where the most recent active device was unset, this will attempt | |
169 | * to return the first device. If that device doesn't exist or fails to probe, | |
170 | * this function will return NULL. | |
171 | */ | |
05c3e68f JH |
172 | struct udevice *eth_get_dev(void) |
173 | { | |
174 | struct eth_uclass_priv *uc_priv; | |
175 | ||
176 | uc_priv = eth_get_uclass_priv(); | |
177 | if (!uc_priv->current) | |
60304592 | 178 | eth_errno = uclass_first_device(UCLASS_ETH, |
05c3e68f JH |
179 | &uc_priv->current); |
180 | return uc_priv->current; | |
181 | } | |
182 | ||
60304592 JH |
183 | /* |
184 | * Typically this will just store a device pointer. | |
185 | * In case it was not probed, we will attempt to do so. | |
186 | * dev may be NULL to unset the active device. | |
187 | */ | |
05c3e68f JH |
188 | static void eth_set_dev(struct udevice *dev) |
189 | { | |
60304592 JH |
190 | if (dev && !device_active(dev)) |
191 | eth_errno = device_probe(dev); | |
05c3e68f JH |
192 | eth_get_uclass_priv()->current = dev; |
193 | } | |
194 | ||
e58780dc JH |
195 | /* |
196 | * Find the udevice that either has the name passed in as devname or has an | |
197 | * alias named devname. | |
198 | */ | |
199 | struct udevice *eth_get_dev_by_name(const char *devname) | |
200 | { | |
201 | int seq = -1; | |
202 | char *endp = NULL; | |
203 | const char *startp = NULL; | |
204 | struct udevice *it; | |
205 | struct uclass *uc; | |
206 | ||
207 | /* Must be longer than 3 to be an alias */ | |
208 | if (strlen(devname) > strlen("eth")) { | |
209 | startp = devname + strlen("eth"); | |
210 | seq = simple_strtoul(startp, &endp, 10); | |
211 | } | |
212 | ||
213 | uclass_get(UCLASS_ETH, &uc); | |
214 | uclass_foreach_dev(it, uc) { | |
60304592 JH |
215 | /* |
216 | * We need the seq to be valid, so try to probe it. | |
217 | * If the probe fails, the seq will not match since it will be | |
218 | * -1 instead of what we are looking for. | |
219 | * We don't care about errors from probe here. Either they won't | |
220 | * match an alias or it will match a literal name and we'll pick | |
221 | * up the error when we try to probe again in eth_set_dev(). | |
222 | */ | |
e58780dc JH |
223 | device_probe(it); |
224 | /* | |
225 | * Check for the name or the sequence number to match | |
226 | */ | |
227 | if (strcmp(it->name, devname) == 0 || | |
228 | (endp > startp && it->seq == seq)) | |
229 | return it; | |
230 | } | |
231 | ||
232 | return NULL; | |
233 | } | |
234 | ||
05c3e68f JH |
235 | unsigned char *eth_get_ethaddr(void) |
236 | { | |
237 | struct eth_pdata *pdata; | |
238 | ||
239 | if (eth_get_dev()) { | |
240 | pdata = eth_get_dev()->platdata; | |
241 | return pdata->enetaddr; | |
242 | } | |
243 | ||
244 | return NULL; | |
245 | } | |
246 | ||
247 | /* Set active state without calling start on the driver */ | |
248 | int eth_init_state_only(void) | |
249 | { | |
250 | struct udevice *current; | |
251 | struct eth_device_priv *priv; | |
252 | ||
253 | current = eth_get_dev(); | |
254 | if (!current || !device_active(current)) | |
255 | return -EINVAL; | |
256 | ||
257 | priv = current->uclass_priv; | |
258 | priv->state = ETH_STATE_ACTIVE; | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | /* Set passive state without calling stop on the driver */ | |
264 | void eth_halt_state_only(void) | |
265 | { | |
266 | struct udevice *current; | |
267 | struct eth_device_priv *priv; | |
268 | ||
269 | current = eth_get_dev(); | |
270 | if (!current || !device_active(current)) | |
271 | return; | |
272 | ||
273 | priv = current->uclass_priv; | |
274 | priv->state = ETH_STATE_PASSIVE; | |
275 | } | |
276 | ||
277 | int eth_get_dev_index(void) | |
278 | { | |
279 | if (eth_get_dev()) | |
280 | return eth_get_dev()->seq; | |
281 | return -1; | |
282 | } | |
283 | ||
284 | int eth_init(void) | |
285 | { | |
286 | struct udevice *current; | |
287 | struct udevice *old_current; | |
60304592 | 288 | int ret = -ENODEV; |
05c3e68f JH |
289 | |
290 | current = eth_get_dev(); | |
291 | if (!current) { | |
292 | printf("No ethernet found.\n"); | |
293 | return -ENODEV; | |
294 | } | |
295 | ||
296 | old_current = current; | |
297 | do { | |
298 | debug("Trying %s\n", current->name); | |
299 | ||
300 | if (device_active(current)) { | |
301 | uchar env_enetaddr[6]; | |
302 | struct eth_pdata *pdata = current->platdata; | |
303 | ||
304 | /* Sync environment with network device */ | |
305 | if (eth_getenv_enetaddr_by_index("eth", current->seq, | |
306 | env_enetaddr)) | |
307 | memcpy(pdata->enetaddr, env_enetaddr, 6); | |
308 | else | |
309 | memset(pdata->enetaddr, 0, 6); | |
310 | ||
60304592 JH |
311 | ret = eth_get_ops(current)->start(current); |
312 | if (ret >= 0) { | |
05c3e68f JH |
313 | struct eth_device_priv *priv = |
314 | current->uclass_priv; | |
315 | ||
316 | priv->state = ETH_STATE_ACTIVE; | |
317 | return 0; | |
318 | } | |
ff819a3a | 319 | } else { |
60304592 | 320 | ret = eth_errno; |
ff819a3a | 321 | } |
60304592 | 322 | |
05c3e68f JH |
323 | debug("FAIL\n"); |
324 | ||
60304592 JH |
325 | /* |
326 | * If ethrotate is enabled, this will change "current", | |
327 | * otherwise we will drop out of this while loop immediately | |
328 | */ | |
05c3e68f | 329 | eth_try_another(0); |
60304592 | 330 | /* This will ensure the new "current" attempted to probe */ |
05c3e68f JH |
331 | current = eth_get_dev(); |
332 | } while (old_current != current); | |
333 | ||
60304592 | 334 | return ret; |
05c3e68f JH |
335 | } |
336 | ||
337 | void eth_halt(void) | |
338 | { | |
339 | struct udevice *current; | |
340 | struct eth_device_priv *priv; | |
341 | ||
342 | current = eth_get_dev(); | |
343 | if (!current || !device_active(current)) | |
344 | return; | |
345 | ||
346 | eth_get_ops(current)->stop(current); | |
347 | priv = current->uclass_priv; | |
348 | priv->state = ETH_STATE_PASSIVE; | |
349 | } | |
350 | ||
351 | int eth_send(void *packet, int length) | |
352 | { | |
353 | struct udevice *current; | |
60304592 | 354 | int ret; |
05c3e68f JH |
355 | |
356 | current = eth_get_dev(); | |
357 | if (!current) | |
358 | return -ENODEV; | |
359 | ||
360 | if (!device_active(current)) | |
361 | return -EINVAL; | |
362 | ||
60304592 JH |
363 | ret = eth_get_ops(current)->send(current, packet, length); |
364 | if (ret < 0) { | |
365 | /* We cannot completely return the error at present */ | |
366 | debug("%s: send() returned error %d\n", __func__, ret); | |
367 | } | |
368 | return ret; | |
05c3e68f JH |
369 | } |
370 | ||
371 | int eth_rx(void) | |
372 | { | |
373 | struct udevice *current; | |
17591405 JH |
374 | uchar *packet; |
375 | int ret; | |
376 | int i; | |
05c3e68f JH |
377 | |
378 | current = eth_get_dev(); | |
379 | if (!current) | |
380 | return -ENODEV; | |
381 | ||
382 | if (!device_active(current)) | |
383 | return -EINVAL; | |
384 | ||
17591405 JH |
385 | /* Process up to 32 packets at one time */ |
386 | for (i = 0; i < 32; i++) { | |
387 | ret = eth_get_ops(current)->recv(current, &packet); | |
388 | if (ret > 0) | |
389 | net_process_received_packet(packet, ret); | |
63c9729a JH |
390 | if (ret >= 0 && eth_get_ops(current)->free_pkt) |
391 | eth_get_ops(current)->free_pkt(current, packet, ret); | |
392 | if (ret <= 0) | |
17591405 JH |
393 | break; |
394 | } | |
395 | if (ret == -EAGAIN) | |
396 | ret = 0; | |
60304592 JH |
397 | if (ret < 0) { |
398 | /* We cannot completely return the error at present */ | |
399 | debug("%s: recv() returned error %d\n", __func__, ret); | |
400 | } | |
17591405 | 401 | return ret; |
05c3e68f JH |
402 | } |
403 | ||
404 | static int eth_write_hwaddr(struct udevice *dev) | |
405 | { | |
406 | struct eth_pdata *pdata = dev->platdata; | |
407 | int ret = 0; | |
408 | ||
409 | if (!dev || !device_active(dev)) | |
410 | return -EINVAL; | |
411 | ||
412 | /* seq is valid since the device is active */ | |
413 | if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) { | |
0adb5b76 | 414 | if (!is_valid_ethaddr(pdata->enetaddr)) { |
05c3e68f JH |
415 | printf("\nError: %s address %pM illegal value\n", |
416 | dev->name, pdata->enetaddr); | |
417 | return -EINVAL; | |
418 | } | |
419 | ||
420 | ret = eth_get_ops(dev)->write_hwaddr(dev); | |
421 | if (ret) | |
422 | printf("\nWarning: %s failed to set MAC address\n", | |
423 | dev->name); | |
424 | } | |
425 | ||
426 | return ret; | |
427 | } | |
428 | ||
429 | int eth_initialize(void) | |
430 | { | |
431 | int num_devices = 0; | |
432 | struct udevice *dev; | |
433 | ||
3bc42700 | 434 | eth_common_init(); |
05c3e68f JH |
435 | |
436 | /* | |
437 | * Devices need to write the hwaddr even if not started so that Linux | |
438 | * will have access to the hwaddr that u-boot stored for the device. | |
439 | * This is accomplished by attempting to probe each device and calling | |
440 | * their write_hwaddr() operation. | |
441 | */ | |
442 | uclass_first_device(UCLASS_ETH, &dev); | |
443 | if (!dev) { | |
444 | printf("No ethernet found.\n"); | |
445 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); | |
446 | } else { | |
6536b9bb JH |
447 | char *ethprime = getenv("ethprime"); |
448 | struct udevice *prime_dev = NULL; | |
449 | ||
450 | if (ethprime) | |
451 | prime_dev = eth_get_dev_by_name(ethprime); | |
452 | if (prime_dev) { | |
453 | eth_set_dev(prime_dev); | |
454 | eth_current_changed(); | |
455 | } else { | |
456 | eth_set_dev(NULL); | |
457 | } | |
458 | ||
05c3e68f JH |
459 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
460 | do { | |
461 | if (num_devices) | |
462 | printf(", "); | |
463 | ||
464 | printf("eth%d: %s", dev->seq, dev->name); | |
465 | ||
6536b9bb JH |
466 | if (ethprime && dev == prime_dev) |
467 | printf(" [PRIME]"); | |
468 | ||
05c3e68f JH |
469 | eth_write_hwaddr(dev); |
470 | ||
471 | uclass_next_device(&dev); | |
472 | num_devices++; | |
473 | } while (dev); | |
474 | ||
475 | putc('\n'); | |
476 | } | |
477 | ||
478 | return num_devices; | |
479 | } | |
480 | ||
481 | static int eth_post_bind(struct udevice *dev) | |
482 | { | |
483 | if (strchr(dev->name, ' ')) { | |
484 | printf("\nError: eth device name \"%s\" has a space!\n", | |
485 | dev->name); | |
486 | return -EINVAL; | |
487 | } | |
488 | ||
489 | return 0; | |
490 | } | |
491 | ||
492 | static int eth_pre_unbind(struct udevice *dev) | |
493 | { | |
494 | /* Don't hang onto a pointer that is going away */ | |
495 | if (dev == eth_get_uclass_priv()->current) | |
496 | eth_set_dev(NULL); | |
497 | ||
498 | return 0; | |
499 | } | |
500 | ||
501 | static int eth_post_probe(struct udevice *dev) | |
502 | { | |
503 | struct eth_device_priv *priv = dev->uclass_priv; | |
504 | struct eth_pdata *pdata = dev->platdata; | |
505 | unsigned char env_enetaddr[6]; | |
506 | ||
507 | priv->state = ETH_STATE_INIT; | |
508 | ||
509 | /* Check if the device has a MAC address in ROM */ | |
510 | if (eth_get_ops(dev)->read_rom_hwaddr) | |
511 | eth_get_ops(dev)->read_rom_hwaddr(dev); | |
512 | ||
513 | eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr); | |
0adb5b76 JH |
514 | if (!is_zero_ethaddr(env_enetaddr)) { |
515 | if (!is_zero_ethaddr(pdata->enetaddr) && | |
05c3e68f JH |
516 | memcmp(pdata->enetaddr, env_enetaddr, 6)) { |
517 | printf("\nWarning: %s MAC addresses don't match:\n", | |
518 | dev->name); | |
519 | printf("Address in SROM is %pM\n", | |
520 | pdata->enetaddr); | |
521 | printf("Address in environment is %pM\n", | |
522 | env_enetaddr); | |
523 | } | |
524 | ||
525 | /* Override the ROM MAC address */ | |
526 | memcpy(pdata->enetaddr, env_enetaddr, 6); | |
0adb5b76 | 527 | } else if (is_valid_ethaddr(pdata->enetaddr)) { |
05c3e68f JH |
528 | eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr); |
529 | printf("\nWarning: %s using MAC address from ROM\n", | |
530 | dev->name); | |
0adb5b76 | 531 | } else if (is_zero_ethaddr(pdata->enetaddr)) { |
05c3e68f JH |
532 | printf("\nError: %s address not set.\n", |
533 | dev->name); | |
534 | return -EINVAL; | |
535 | } | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
540 | static int eth_pre_remove(struct udevice *dev) | |
541 | { | |
542 | eth_get_ops(dev)->stop(dev); | |
543 | ||
544 | return 0; | |
545 | } | |
546 | ||
547 | UCLASS_DRIVER(eth) = { | |
548 | .name = "eth", | |
549 | .id = UCLASS_ETH, | |
550 | .post_bind = eth_post_bind, | |
551 | .pre_unbind = eth_pre_unbind, | |
552 | .post_probe = eth_post_probe, | |
553 | .pre_remove = eth_pre_remove, | |
554 | .priv_auto_alloc_size = sizeof(struct eth_uclass_priv), | |
555 | .per_device_auto_alloc_size = sizeof(struct eth_device_priv), | |
e58780dc | 556 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
05c3e68f JH |
557 | }; |
558 | #endif | |
559 | ||
560 | #ifndef CONFIG_DM_ETH | |
dd35479a | 561 | |
f85b6071 | 562 | #ifdef CONFIG_API |
f85b6071 RJ |
563 | static struct { |
564 | uchar data[PKTSIZE]; | |
565 | int length; | |
566 | } eth_rcv_bufs[PKTBUFSRX]; | |
567 | ||
66c7385a | 568 | static unsigned int eth_rcv_current, eth_rcv_last; |
f85b6071 RJ |
569 | #endif |
570 | ||
f8be7d65 JH |
571 | static struct eth_device *eth_devices; |
572 | struct eth_device *eth_current; | |
c609719b | 573 | |
84eb1fba JH |
574 | static void eth_set_current_to_next(void) |
575 | { | |
576 | eth_current = eth_current->next; | |
577 | } | |
578 | ||
e58780dc JH |
579 | static void eth_set_dev(struct eth_device *dev) |
580 | { | |
581 | eth_current = dev; | |
582 | } | |
583 | ||
d7fb9bcf | 584 | struct eth_device *eth_get_dev_by_name(const char *devname) |
63ff004c MB |
585 | { |
586 | struct eth_device *dev, *target_dev; | |
587 | ||
7e7f903f HR |
588 | BUG_ON(devname == NULL); |
589 | ||
63ff004c MB |
590 | if (!eth_devices) |
591 | return NULL; | |
592 | ||
593 | dev = eth_devices; | |
594 | target_dev = NULL; | |
595 | do { | |
596 | if (strcmp(devname, dev->name) == 0) { | |
597 | target_dev = dev; | |
598 | break; | |
599 | } | |
600 | dev = dev->next; | |
601 | } while (dev != eth_devices); | |
602 | ||
603 | return target_dev; | |
604 | } | |
605 | ||
9e56986a AF |
606 | struct eth_device *eth_get_dev_by_index(int index) |
607 | { | |
608 | struct eth_device *dev, *target_dev; | |
9e56986a AF |
609 | |
610 | if (!eth_devices) | |
611 | return NULL; | |
612 | ||
613 | dev = eth_devices; | |
614 | target_dev = NULL; | |
615 | do { | |
fea7dcae | 616 | if (dev->index == index) { |
9e56986a AF |
617 | target_dev = dev; |
618 | break; | |
619 | } | |
620 | dev = dev->next; | |
9e56986a AF |
621 | } while (dev != eth_devices); |
622 | ||
623 | return target_dev; | |
624 | } | |
625 | ||
66c7385a | 626 | int eth_get_dev_index(void) |
c609719b | 627 | { |
66c7385a | 628 | if (!eth_current) |
fea7dcae | 629 | return -1; |
c609719b | 630 | |
fea7dcae | 631 | return eth_current->index; |
c609719b WD |
632 | } |
633 | ||
7616e785 SG |
634 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
635 | int eth_number) | |
636 | { | |
637 | unsigned char env_enetaddr[6]; | |
638 | int ret = 0; | |
639 | ||
69376644 | 640 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
7616e785 | 641 | |
0adb5b76 JH |
642 | if (!is_zero_ethaddr(env_enetaddr)) { |
643 | if (!is_zero_ethaddr(dev->enetaddr) && | |
4c7c65af | 644 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
7616e785 | 645 | printf("\nWarning: %s MAC addresses don't match:\n", |
ff819a3a | 646 | dev->name); |
7616e785 | 647 | printf("Address in SROM is %pM\n", |
ff819a3a | 648 | dev->enetaddr); |
7616e785 | 649 | printf("Address in environment is %pM\n", |
ff819a3a | 650 | env_enetaddr); |
7616e785 SG |
651 | } |
652 | ||
653 | memcpy(dev->enetaddr, env_enetaddr, 6); | |
0adb5b76 | 654 | } else if (is_valid_ethaddr(dev->enetaddr)) { |
c88ef3c1 RH |
655 | eth_setenv_enetaddr_by_index(base_name, eth_number, |
656 | dev->enetaddr); | |
657 | printf("\nWarning: %s using MAC address from net device\n", | |
ff819a3a | 658 | dev->name); |
0adb5b76 | 659 | } else if (is_zero_ethaddr(dev->enetaddr)) { |
75d9a45c PM |
660 | printf("\nError: %s address not set.\n", |
661 | dev->name); | |
662 | return -EINVAL; | |
7616e785 SG |
663 | } |
664 | ||
75d9a45c | 665 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
0adb5b76 | 666 | if (!is_valid_ethaddr(dev->enetaddr)) { |
75d9a45c | 667 | printf("\nError: %s address %pM illegal value\n", |
ff819a3a | 668 | dev->name, dev->enetaddr); |
75d9a45c PM |
669 | return -EINVAL; |
670 | } | |
460f949f | 671 | |
7616e785 | 672 | ret = dev->write_hwaddr(dev); |
75d9a45c | 673 | if (ret) |
ff819a3a JH |
674 | printf("\nWarning: %s failed to set MAC address\n", |
675 | dev->name); | |
460f949f | 676 | } |
7616e785 SG |
677 | |
678 | return ret; | |
679 | } | |
680 | ||
89d48367 SG |
681 | int eth_register(struct eth_device *dev) |
682 | { | |
683 | struct eth_device *d; | |
66c7385a | 684 | static int index; |
58c583b6 | 685 | |
f6add132 | 686 | assert(strlen(dev->name) < sizeof(dev->name)); |
58c583b6 | 687 | |
89d48367 | 688 | if (!eth_devices) { |
ff819a3a JH |
689 | eth_devices = dev; |
690 | eth_current = dev; | |
89d48367 | 691 | eth_current_changed(); |
c609719b | 692 | } else { |
66c7385a | 693 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
aba4b69d | 694 | ; |
c609719b WD |
695 | d->next = dev; |
696 | } | |
697 | ||
698 | dev->state = ETH_STATE_INIT; | |
699 | dev->next = eth_devices; | |
fea7dcae | 700 | dev->index = index++; |
c609719b WD |
701 | |
702 | return 0; | |
703 | } | |
704 | ||
e7e982d6 VP |
705 | int eth_unregister(struct eth_device *dev) |
706 | { | |
707 | struct eth_device *cur; | |
708 | ||
709 | /* No device */ | |
710 | if (!eth_devices) | |
05324a48 | 711 | return -ENODEV; |
e7e982d6 VP |
712 | |
713 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; | |
714 | cur = cur->next) | |
715 | ; | |
716 | ||
717 | /* Device not found */ | |
718 | if (cur->next != dev) | |
05324a48 | 719 | return -ENODEV; |
e7e982d6 VP |
720 | |
721 | cur->next = dev->next; | |
722 | ||
723 | if (eth_devices == dev) | |
724 | eth_devices = dev->next == eth_devices ? NULL : dev->next; | |
725 | ||
726 | if (eth_current == dev) { | |
727 | eth_current = eth_devices; | |
728 | eth_current_changed(); | |
729 | } | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
d2eaec60 | 734 | int eth_initialize(void) |
c609719b | 735 | { |
fea7dcae | 736 | int num_devices = 0; |
3bc42700 | 737 | |
c609719b WD |
738 | eth_devices = NULL; |
739 | eth_current = NULL; | |
3bc42700 | 740 | eth_common_init(); |
d9785c14 | 741 | |
c609719b | 742 | if (!eth_devices) { |
66c7385a | 743 | puts("No ethernet found.\n"); |
770605e4 | 744 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
c609719b WD |
745 | } else { |
746 | struct eth_device *dev = eth_devices; | |
66c7385a | 747 | char *ethprime = getenv("ethprime"); |
c609719b | 748 | |
770605e4 | 749 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
c609719b | 750 | do { |
fea7dcae | 751 | if (dev->index) |
66c7385a | 752 | puts(", "); |
c609719b WD |
753 | |
754 | printf("%s", dev->name); | |
755 | ||
66c7385a | 756 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
c609719b | 757 | eth_current = dev; |
66c7385a | 758 | puts(" [PRIME]"); |
c609719b WD |
759 | } |
760 | ||
1384f3bb | 761 | if (strchr(dev->name, ' ')) |
66c7385a JH |
762 | puts("\nWarning: eth device name has a space!" |
763 | "\n"); | |
1384f3bb | 764 | |
75d9a45c | 765 | eth_write_hwaddr(dev, "eth", dev->index); |
c609719b | 766 | |
c609719b | 767 | dev = dev->next; |
fea7dcae | 768 | num_devices++; |
66c7385a | 769 | } while (dev != eth_devices); |
c609719b | 770 | |
89d48367 | 771 | eth_current_changed(); |
66c7385a | 772 | putc('\n'); |
c609719b WD |
773 | } |
774 | ||
fea7dcae | 775 | return num_devices; |
c609719b WD |
776 | } |
777 | ||
53a5c424 DU |
778 | #ifdef CONFIG_MCAST_TFTP |
779 | /* Multicast. | |
780 | * mcast_addr: multicast ipaddr from which multicast Mac is made | |
85eb5caf | 781 | * join: 1=join, 0=leave. |
53a5c424 | 782 | */ |
049a95a7 | 783 | int eth_mcast_join(struct in_addr mcast_ip, int join) |
53a5c424 | 784 | { |
66c7385a | 785 | u8 mcast_mac[6]; |
85eb5caf | 786 | if (!eth_current || !eth_current->mcast) |
53a5c424 | 787 | return -1; |
049a95a7 JH |
788 | mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff; |
789 | mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff; | |
790 | mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f; | |
53a5c424 DU |
791 | mcast_mac[2] = 0x5e; |
792 | mcast_mac[1] = 0x0; | |
793 | mcast_mac[0] = 0x1; | |
794 | return eth_current->mcast(eth_current, mcast_mac, join); | |
795 | } | |
796 | ||
85eb5caf WD |
797 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
798 | * and this is the ethernet-crc method needed for TSEC -- and perhaps | |
53a5c424 DU |
799 | * some other adapter -- hash tables |
800 | */ | |
801 | #define CRCPOLY_LE 0xedb88320 | |
66c7385a | 802 | u32 ether_crc(size_t len, unsigned char const *p) |
53a5c424 DU |
803 | { |
804 | int i; | |
805 | u32 crc; | |
806 | crc = ~0; | |
807 | while (len--) { | |
808 | crc ^= *p++; | |
809 | for (i = 0; i < 8; i++) | |
810 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); | |
811 | } | |
812 | /* an reverse the bits, cuz of way they arrive -- last-first */ | |
813 | crc = (crc >> 16) | (crc << 16); | |
814 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); | |
815 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); | |
816 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); | |
817 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); | |
818 | return crc; | |
819 | } | |
820 | ||
821 | #endif | |
822 | ||
c609719b | 823 | |
d2eaec60 | 824 | int eth_init(void) |
c609719b | 825 | { |
86848a74 | 826 | struct eth_device *old_current, *dev; |
c609719b | 827 | |
6bc11388 | 828 | if (!eth_current) { |
66c7385a | 829 | puts("No ethernet found.\n"); |
05324a48 | 830 | return -ENODEV; |
6bc11388 | 831 | } |
c609719b | 832 | |
86848a74 | 833 | /* Sync environment with network devices */ |
86848a74 MF |
834 | dev = eth_devices; |
835 | do { | |
836 | uchar env_enetaddr[6]; | |
837 | ||
fea7dcae | 838 | if (eth_getenv_enetaddr_by_index("eth", dev->index, |
7616e785 | 839 | env_enetaddr)) |
86848a74 MF |
840 | memcpy(dev->enetaddr, env_enetaddr, 6); |
841 | ||
86848a74 MF |
842 | dev = dev->next; |
843 | } while (dev != eth_devices); | |
844 | ||
c609719b WD |
845 | old_current = eth_current; |
846 | do { | |
0ebf04c6 | 847 | debug("Trying %s\n", eth_current->name); |
c609719b | 848 | |
d2eaec60 | 849 | if (eth_current->init(eth_current, gd->bd) >= 0) { |
c609719b WD |
850 | eth_current->state = ETH_STATE_ACTIVE; |
851 | ||
505be87a | 852 | return 0; |
c609719b | 853 | } |
0ebf04c6 | 854 | debug("FAIL\n"); |
c609719b WD |
855 | |
856 | eth_try_another(0); | |
857 | } while (old_current != eth_current); | |
858 | ||
05324a48 | 859 | return -ETIMEDOUT; |
c609719b WD |
860 | } |
861 | ||
862 | void eth_halt(void) | |
863 | { | |
864 | if (!eth_current) | |
865 | return; | |
866 | ||
867 | eth_current->halt(eth_current); | |
868 | ||
869 | eth_current->state = ETH_STATE_PASSIVE; | |
870 | } | |
871 | ||
db288a96 | 872 | int eth_send(void *packet, int length) |
c609719b WD |
873 | { |
874 | if (!eth_current) | |
05324a48 | 875 | return -ENODEV; |
c609719b WD |
876 | |
877 | return eth_current->send(eth_current, packet, length); | |
878 | } | |
879 | ||
880 | int eth_rx(void) | |
881 | { | |
882 | if (!eth_current) | |
05324a48 | 883 | return -ENODEV; |
c609719b WD |
884 | |
885 | return eth_current->recv(eth_current); | |
886 | } | |
05c3e68f | 887 | #endif /* ifndef CONFIG_DM_ETH */ |
c609719b | 888 | |
f85b6071 | 889 | #ifdef CONFIG_API |
db288a96 | 890 | static void eth_save_packet(void *packet, int length) |
f85b6071 | 891 | { |
db288a96 | 892 | char *p = packet; |
f85b6071 RJ |
893 | int i; |
894 | ||
895 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) | |
896 | return; | |
897 | ||
898 | if (PKTSIZE < length) | |
899 | return; | |
900 | ||
901 | for (i = 0; i < length; i++) | |
902 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; | |
903 | ||
904 | eth_rcv_bufs[eth_rcv_last].length = length; | |
905 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; | |
906 | } | |
907 | ||
db288a96 | 908 | int eth_receive(void *packet, int length) |
f85b6071 | 909 | { |
db288a96 | 910 | char *p = packet; |
f85b6071 RJ |
911 | void *pp = push_packet; |
912 | int i; | |
913 | ||
914 | if (eth_rcv_current == eth_rcv_last) { | |
915 | push_packet = eth_save_packet; | |
916 | eth_rx(); | |
917 | push_packet = pp; | |
918 | ||
919 | if (eth_rcv_current == eth_rcv_last) | |
920 | return -1; | |
921 | } | |
922 | ||
46c07bcf | 923 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
f85b6071 RJ |
924 | |
925 | for (i = 0; i < length; i++) | |
926 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; | |
927 | ||
928 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; | |
929 | return length; | |
930 | } | |
931 | #endif /* CONFIG_API */ | |
932 | ||
84eb1fba JH |
933 | static void eth_current_changed(void) |
934 | { | |
935 | char *act = getenv("ethact"); | |
936 | /* update current ethernet name */ | |
937 | if (eth_get_dev()) { | |
938 | if (act == NULL || strcmp(act, eth_get_name()) != 0) | |
939 | setenv("ethact", eth_get_name()); | |
940 | } | |
941 | /* | |
942 | * remove the variable completely if there is no active | |
943 | * interface | |
944 | */ | |
945 | else if (act != NULL) | |
946 | setenv("ethact", NULL); | |
947 | } | |
948 | ||
c609719b WD |
949 | void eth_try_another(int first_restart) |
950 | { | |
05c3e68f | 951 | static void *first_failed; |
c650e1be | 952 | char *ethrotate; |
e1692577 MF |
953 | |
954 | /* | |
955 | * Do not rotate between network interfaces when | |
956 | * 'ethrotate' variable is set to 'no'. | |
957 | */ | |
66c7385a JH |
958 | ethrotate = getenv("ethrotate"); |
959 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) | |
e1692577 | 960 | return; |
c609719b | 961 | |
84eb1fba | 962 | if (!eth_get_dev()) |
c609719b WD |
963 | return; |
964 | ||
66c7385a | 965 | if (first_restart) |
84eb1fba | 966 | first_failed = eth_get_dev(); |
c609719b | 967 | |
84eb1fba | 968 | eth_set_current_to_next(); |
c609719b | 969 | |
89d48367 | 970 | eth_current_changed(); |
a3d991bd | 971 | |
84eb1fba | 972 | if (first_failed == eth_get_dev()) |
bc0571fc | 973 | net_restart_wrap = 1; |
c609719b WD |
974 | } |
975 | ||
a3d991bd WD |
976 | void eth_set_current(void) |
977 | { | |
66c7385a JH |
978 | static char *act; |
979 | static int env_changed_id; | |
2f70c49e | 980 | int env_id; |
a3d991bd | 981 | |
2f70c49e HS |
982 | env_id = get_env_id(); |
983 | if ((act == NULL) || (env_changed_id != env_id)) { | |
984 | act = getenv("ethact"); | |
985 | env_changed_id = env_id; | |
986 | } | |
6536b9bb JH |
987 | |
988 | if (act == NULL) { | |
989 | char *ethprime = getenv("ethprime"); | |
990 | void *dev = NULL; | |
991 | ||
992 | if (ethprime) | |
993 | dev = eth_get_dev_by_name(ethprime); | |
994 | if (dev) | |
995 | eth_set_dev(dev); | |
996 | else | |
997 | eth_set_dev(NULL); | |
998 | } else { | |
e58780dc | 999 | eth_set_dev(eth_get_dev_by_name(act)); |
6536b9bb | 1000 | } |
a3d991bd | 1001 | |
89d48367 | 1002 | eth_current_changed(); |
a3d991bd | 1003 | } |
a3d991bd | 1004 | |
84eb1fba | 1005 | const char *eth_get_name(void) |
5bb226e8 | 1006 | { |
84eb1fba | 1007 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
5bb226e8 | 1008 | } |