]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c609719b | 2 | /* |
05c3e68f | 3 | * (C) Copyright 2001-2015 |
c609719b | 4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
05c3e68f | 5 | * Joe Hershberger, National Instruments |
c609719b WD |
6 | */ |
7 | ||
8 | #include <common.h> | |
52f24238 | 9 | #include <bootstage.h> |
c609719b | 10 | #include <command.h> |
2a64ada7 | 11 | #include <dm.h> |
9fb625ce | 12 | #include <env.h> |
f7ae49fc | 13 | #include <log.h> |
c609719b | 14 | #include <net.h> |
5f184715 | 15 | #include <phy.h> |
401d1c4f | 16 | #include <asm/global_data.h> |
eb41d8a1 | 17 | #include <linux/bug.h> |
1221ce45 | 18 | #include <linux/errno.h> |
3eaac630 | 19 | #include <net/pcap.h> |
818f91eb | 20 | #include "eth_internal.h" |
c609719b | 21 | |
d2eaec60 JH |
22 | DECLARE_GLOBAL_DATA_PTR; |
23 | ||
3bc42700 SG |
24 | /* |
25 | * CPU and board-specific Ethernet initializations. Aliased function | |
26 | * signals caller to move on | |
27 | */ | |
b75d8dc5 | 28 | static int __def_eth_init(struct bd_info *bis) |
3bc42700 SG |
29 | { |
30 | return -1; | |
31 | } | |
b75d8dc5 MY |
32 | int cpu_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init"))); |
33 | int board_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init"))); | |
3bc42700 | 34 | |
f85b6071 | 35 | #ifdef CONFIG_API |
f85b6071 RJ |
36 | static struct { |
37 | uchar data[PKTSIZE]; | |
38 | int length; | |
39 | } eth_rcv_bufs[PKTBUFSRX]; | |
40 | ||
66c7385a | 41 | static unsigned int eth_rcv_current, eth_rcv_last; |
f85b6071 RJ |
42 | #endif |
43 | ||
f8be7d65 JH |
44 | static struct eth_device *eth_devices; |
45 | struct eth_device *eth_current; | |
c609719b | 46 | |
8607a6bf | 47 | void eth_set_current_to_next(void) |
84eb1fba JH |
48 | { |
49 | eth_current = eth_current->next; | |
50 | } | |
51 | ||
8607a6bf | 52 | void eth_set_dev(struct eth_device *dev) |
e58780dc JH |
53 | { |
54 | eth_current = dev; | |
55 | } | |
56 | ||
d7fb9bcf | 57 | struct eth_device *eth_get_dev_by_name(const char *devname) |
63ff004c MB |
58 | { |
59 | struct eth_device *dev, *target_dev; | |
60 | ||
7e7f903f HR |
61 | BUG_ON(devname == NULL); |
62 | ||
63ff004c MB |
63 | if (!eth_devices) |
64 | return NULL; | |
65 | ||
66 | dev = eth_devices; | |
67 | target_dev = NULL; | |
68 | do { | |
69 | if (strcmp(devname, dev->name) == 0) { | |
70 | target_dev = dev; | |
71 | break; | |
72 | } | |
73 | dev = dev->next; | |
74 | } while (dev != eth_devices); | |
75 | ||
76 | return target_dev; | |
77 | } | |
78 | ||
9e56986a AF |
79 | struct eth_device *eth_get_dev_by_index(int index) |
80 | { | |
81 | struct eth_device *dev, *target_dev; | |
9e56986a AF |
82 | |
83 | if (!eth_devices) | |
84 | return NULL; | |
85 | ||
86 | dev = eth_devices; | |
87 | target_dev = NULL; | |
88 | do { | |
fea7dcae | 89 | if (dev->index == index) { |
9e56986a AF |
90 | target_dev = dev; |
91 | break; | |
92 | } | |
93 | dev = dev->next; | |
9e56986a AF |
94 | } while (dev != eth_devices); |
95 | ||
96 | return target_dev; | |
97 | } | |
98 | ||
66c7385a | 99 | int eth_get_dev_index(void) |
c609719b | 100 | { |
66c7385a | 101 | if (!eth_current) |
fea7dcae | 102 | return -1; |
c609719b | 103 | |
fea7dcae | 104 | return eth_current->index; |
c609719b WD |
105 | } |
106 | ||
6e0d26c0 JH |
107 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
108 | int flags) | |
109 | { | |
110 | int index; | |
111 | struct eth_device *dev; | |
112 | ||
113 | if (!eth_devices) | |
114 | return 0; | |
115 | ||
116 | /* look for an index after "eth" */ | |
0b1284eb | 117 | index = dectoul(name + 3, NULL); |
6e0d26c0 JH |
118 | |
119 | dev = eth_devices; | |
120 | do { | |
121 | if (dev->index == index) { | |
122 | switch (op) { | |
123 | case env_op_create: | |
124 | case env_op_overwrite: | |
fb8977c5 | 125 | string_to_enetaddr(value, dev->enetaddr); |
73d570a7 | 126 | eth_write_hwaddr(dev, "eth", dev->index); |
6e0d26c0 JH |
127 | break; |
128 | case env_op_delete: | |
a40db6d5 | 129 | memset(dev->enetaddr, 0, ARP_HLEN); |
6e0d26c0 JH |
130 | } |
131 | } | |
7aba0f2c | 132 | dev = dev->next; |
6e0d26c0 JH |
133 | } while (dev != eth_devices); |
134 | ||
135 | return 0; | |
136 | } | |
137 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); | |
138 | ||
7616e785 SG |
139 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
140 | int eth_number) | |
141 | { | |
a40db6d5 | 142 | unsigned char env_enetaddr[ARP_HLEN]; |
7616e785 SG |
143 | int ret = 0; |
144 | ||
35affd7a | 145 | eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
7616e785 | 146 | |
0adb5b76 JH |
147 | if (!is_zero_ethaddr(env_enetaddr)) { |
148 | if (!is_zero_ethaddr(dev->enetaddr) && | |
a40db6d5 | 149 | memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) { |
7616e785 | 150 | printf("\nWarning: %s MAC addresses don't match:\n", |
ff819a3a | 151 | dev->name); |
7616e785 | 152 | printf("Address in SROM is %pM\n", |
ff819a3a | 153 | dev->enetaddr); |
7616e785 | 154 | printf("Address in environment is %pM\n", |
ff819a3a | 155 | env_enetaddr); |
7616e785 SG |
156 | } |
157 | ||
a40db6d5 | 158 | memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN); |
0adb5b76 | 159 | } else if (is_valid_ethaddr(dev->enetaddr)) { |
fd1e959e SG |
160 | eth_env_set_enetaddr_by_index(base_name, eth_number, |
161 | dev->enetaddr); | |
0adb5b76 | 162 | } else if (is_zero_ethaddr(dev->enetaddr)) { |
bef1014b JH |
163 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
164 | net_random_ethaddr(dev->enetaddr); | |
165 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", | |
166 | dev->name, eth_number, dev->enetaddr); | |
167 | #else | |
75d9a45c PM |
168 | printf("\nError: %s address not set.\n", |
169 | dev->name); | |
170 | return -EINVAL; | |
bef1014b | 171 | #endif |
7616e785 SG |
172 | } |
173 | ||
75d9a45c | 174 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
0adb5b76 | 175 | if (!is_valid_ethaddr(dev->enetaddr)) { |
75d9a45c | 176 | printf("\nError: %s address %pM illegal value\n", |
ff819a3a | 177 | dev->name, dev->enetaddr); |
75d9a45c PM |
178 | return -EINVAL; |
179 | } | |
460f949f | 180 | |
7616e785 | 181 | ret = dev->write_hwaddr(dev); |
75d9a45c | 182 | if (ret) |
ff819a3a JH |
183 | printf("\nWarning: %s failed to set MAC address\n", |
184 | dev->name); | |
460f949f | 185 | } |
7616e785 SG |
186 | |
187 | return ret; | |
188 | } | |
189 | ||
89d48367 SG |
190 | int eth_register(struct eth_device *dev) |
191 | { | |
192 | struct eth_device *d; | |
66c7385a | 193 | static int index; |
58c583b6 | 194 | |
f6add132 | 195 | assert(strlen(dev->name) < sizeof(dev->name)); |
58c583b6 | 196 | |
89d48367 | 197 | if (!eth_devices) { |
ff819a3a JH |
198 | eth_devices = dev; |
199 | eth_current = dev; | |
89d48367 | 200 | eth_current_changed(); |
c609719b | 201 | } else { |
66c7385a | 202 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
aba4b69d | 203 | ; |
c609719b WD |
204 | d->next = dev; |
205 | } | |
206 | ||
207 | dev->state = ETH_STATE_INIT; | |
208 | dev->next = eth_devices; | |
fea7dcae | 209 | dev->index = index++; |
c609719b WD |
210 | |
211 | return 0; | |
212 | } | |
213 | ||
e7e982d6 VP |
214 | int eth_unregister(struct eth_device *dev) |
215 | { | |
216 | struct eth_device *cur; | |
217 | ||
218 | /* No device */ | |
219 | if (!eth_devices) | |
05324a48 | 220 | return -ENODEV; |
e7e982d6 VP |
221 | |
222 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; | |
223 | cur = cur->next) | |
224 | ; | |
225 | ||
226 | /* Device not found */ | |
227 | if (cur->next != dev) | |
05324a48 | 228 | return -ENODEV; |
e7e982d6 VP |
229 | |
230 | cur->next = dev->next; | |
231 | ||
232 | if (eth_devices == dev) | |
233 | eth_devices = dev->next == eth_devices ? NULL : dev->next; | |
234 | ||
235 | if (eth_current == dev) { | |
236 | eth_current = eth_devices; | |
237 | eth_current_changed(); | |
238 | } | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
d2eaec60 | 243 | int eth_initialize(void) |
c609719b | 244 | { |
fea7dcae | 245 | int num_devices = 0; |
3bc42700 | 246 | |
c609719b WD |
247 | eth_devices = NULL; |
248 | eth_current = NULL; | |
3bc42700 | 249 | eth_common_init(); |
818f91eb SG |
250 | /* |
251 | * If board-specific initialization exists, call it. | |
252 | * If not, call a CPU-specific one | |
253 | */ | |
254 | if (board_eth_init != __def_eth_init) { | |
255 | if (board_eth_init(gd->bd) < 0) | |
256 | printf("Board Net Initialization Failed\n"); | |
257 | } else if (cpu_eth_init != __def_eth_init) { | |
258 | if (cpu_eth_init(gd->bd) < 0) | |
259 | printf("CPU Net Initialization Failed\n"); | |
260 | } else { | |
261 | printf("Net Initialization Skipped\n"); | |
262 | } | |
d9785c14 | 263 | |
c609719b | 264 | if (!eth_devices) { |
ad895913 | 265 | log_err("No ethernet found.\n"); |
770605e4 | 266 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
c609719b WD |
267 | } else { |
268 | struct eth_device *dev = eth_devices; | |
00caae6d | 269 | char *ethprime = env_get("ethprime"); |
c609719b | 270 | |
770605e4 | 271 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
c609719b | 272 | do { |
fea7dcae | 273 | if (dev->index) |
66c7385a | 274 | puts(", "); |
c609719b WD |
275 | |
276 | printf("%s", dev->name); | |
277 | ||
66c7385a | 278 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
c609719b | 279 | eth_current = dev; |
66c7385a | 280 | puts(" [PRIME]"); |
c609719b WD |
281 | } |
282 | ||
1384f3bb | 283 | if (strchr(dev->name, ' ')) |
66c7385a JH |
284 | puts("\nWarning: eth device name has a space!" |
285 | "\n"); | |
1384f3bb | 286 | |
75d9a45c | 287 | eth_write_hwaddr(dev, "eth", dev->index); |
c609719b | 288 | |
c609719b | 289 | dev = dev->next; |
fea7dcae | 290 | num_devices++; |
66c7385a | 291 | } while (dev != eth_devices); |
c609719b | 292 | |
89d48367 | 293 | eth_current_changed(); |
66c7385a | 294 | putc('\n'); |
c609719b WD |
295 | } |
296 | ||
fea7dcae | 297 | return num_devices; |
c609719b WD |
298 | } |
299 | ||
53a5c424 DU |
300 | /* Multicast. |
301 | * mcast_addr: multicast ipaddr from which multicast Mac is made | |
85eb5caf | 302 | * join: 1=join, 0=leave. |
53a5c424 | 303 | */ |
049a95a7 | 304 | int eth_mcast_join(struct in_addr mcast_ip, int join) |
53a5c424 | 305 | { |
a40db6d5 | 306 | u8 mcast_mac[ARP_HLEN]; |
85eb5caf | 307 | if (!eth_current || !eth_current->mcast) |
53a5c424 | 308 | return -1; |
049a95a7 JH |
309 | mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff; |
310 | mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff; | |
311 | mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f; | |
53a5c424 DU |
312 | mcast_mac[2] = 0x5e; |
313 | mcast_mac[1] = 0x0; | |
314 | mcast_mac[0] = 0x1; | |
315 | return eth_current->mcast(eth_current, mcast_mac, join); | |
316 | } | |
317 | ||
d2eaec60 | 318 | int eth_init(void) |
c609719b | 319 | { |
6e0d26c0 | 320 | struct eth_device *old_current; |
c609719b | 321 | |
6bc11388 | 322 | if (!eth_current) { |
ad895913 | 323 | log_err("No ethernet found.\n"); |
05324a48 | 324 | return -ENODEV; |
6bc11388 | 325 | } |
c609719b WD |
326 | |
327 | old_current = eth_current; | |
328 | do { | |
0ebf04c6 | 329 | debug("Trying %s\n", eth_current->name); |
c609719b | 330 | |
d2eaec60 | 331 | if (eth_current->init(eth_current, gd->bd) >= 0) { |
c609719b WD |
332 | eth_current->state = ETH_STATE_ACTIVE; |
333 | ||
505be87a | 334 | return 0; |
c609719b | 335 | } |
0ebf04c6 | 336 | debug("FAIL\n"); |
c609719b WD |
337 | |
338 | eth_try_another(0); | |
339 | } while (old_current != eth_current); | |
340 | ||
05324a48 | 341 | return -ETIMEDOUT; |
c609719b WD |
342 | } |
343 | ||
344 | void eth_halt(void) | |
345 | { | |
346 | if (!eth_current) | |
347 | return; | |
348 | ||
349 | eth_current->halt(eth_current); | |
350 | ||
351 | eth_current->state = ETH_STATE_PASSIVE; | |
352 | } | |
353 | ||
eaa8a195 BN |
354 | int eth_is_active(struct eth_device *dev) |
355 | { | |
356 | return dev && dev->state == ETH_STATE_ACTIVE; | |
357 | } | |
358 | ||
db288a96 | 359 | int eth_send(void *packet, int length) |
c609719b | 360 | { |
3eaac630 RF |
361 | int ret; |
362 | ||
c609719b | 363 | if (!eth_current) |
05324a48 | 364 | return -ENODEV; |
c609719b | 365 | |
3eaac630 RF |
366 | ret = eth_current->send(eth_current, packet, length); |
367 | #if defined(CONFIG_CMD_PCAP) | |
368 | if (ret >= 0) | |
c1ab7381 | 369 | pcap_post(packet, length, true); |
3eaac630 RF |
370 | #endif |
371 | return ret; | |
c609719b WD |
372 | } |
373 | ||
374 | int eth_rx(void) | |
375 | { | |
376 | if (!eth_current) | |
05324a48 | 377 | return -ENODEV; |
c609719b WD |
378 | |
379 | return eth_current->recv(eth_current); | |
380 | } | |
381 | ||
f85b6071 | 382 | #ifdef CONFIG_API |
db288a96 | 383 | static void eth_save_packet(void *packet, int length) |
f85b6071 | 384 | { |
db288a96 | 385 | char *p = packet; |
f85b6071 RJ |
386 | int i; |
387 | ||
388 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) | |
389 | return; | |
390 | ||
391 | if (PKTSIZE < length) | |
392 | return; | |
393 | ||
394 | for (i = 0; i < length; i++) | |
395 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; | |
396 | ||
397 | eth_rcv_bufs[eth_rcv_last].length = length; | |
398 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; | |
399 | } | |
400 | ||
db288a96 | 401 | int eth_receive(void *packet, int length) |
f85b6071 | 402 | { |
db288a96 | 403 | char *p = packet; |
f85b6071 RJ |
404 | void *pp = push_packet; |
405 | int i; | |
406 | ||
407 | if (eth_rcv_current == eth_rcv_last) { | |
408 | push_packet = eth_save_packet; | |
409 | eth_rx(); | |
410 | push_packet = pp; | |
411 | ||
412 | if (eth_rcv_current == eth_rcv_last) | |
413 | return -1; | |
414 | } | |
415 | ||
46c07bcf | 416 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
f85b6071 RJ |
417 | |
418 | for (i = 0; i < length; i++) | |
419 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; | |
420 | ||
421 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; | |
422 | return length; | |
423 | } | |
424 | #endif /* CONFIG_API */ |