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