1
2
3
4
5
6
7
8#include <common.h>
9#include <bootstage.h>
10#include <command.h>
11#include <dm.h>
12#include <env.h>
13#include <log.h>
14#include <net.h>
15#include <phy.h>
16#include <asm/global_data.h>
17#include <linux/bug.h>
18#include <linux/errno.h>
19#include <net/pcap.h>
20#include "eth_internal.h"
21
22DECLARE_GLOBAL_DATA_PTR;
23
24
25
26
27
28static int __def_eth_init(struct bd_info *bis)
29{
30 return -1;
31}
32int cpu_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
33int board_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
34
35#ifdef CONFIG_API
36static struct {
37 uchar data[PKTSIZE];
38 int length;
39} eth_rcv_bufs[PKTBUFSRX];
40
41static unsigned int eth_rcv_current, eth_rcv_last;
42#endif
43
44static struct eth_device *eth_devices;
45struct eth_device *eth_current;
46
47void eth_set_current_to_next(void)
48{
49 eth_current = eth_current->next;
50}
51
52void eth_set_dev(struct eth_device *dev)
53{
54 eth_current = dev;
55}
56
57struct eth_device *eth_get_dev_by_name(const char *devname)
58{
59 struct eth_device *dev, *target_dev;
60
61 BUG_ON(devname == NULL);
62
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
79struct eth_device *eth_get_dev_by_index(int index)
80{
81 struct eth_device *dev, *target_dev;
82
83 if (!eth_devices)
84 return NULL;
85
86 dev = eth_devices;
87 target_dev = NULL;
88 do {
89 if (dev->index == index) {
90 target_dev = dev;
91 break;
92 }
93 dev = dev->next;
94 } while (dev != eth_devices);
95
96 return target_dev;
97}
98
99int eth_get_dev_index(void)
100{
101 if (!eth_current)
102 return -1;
103
104 return eth_current->index;
105}
106
107static 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
117 index = dectoul(name + 3, NULL);
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:
125 string_to_enetaddr(value, dev->enetaddr);
126 eth_write_hwaddr(dev, "eth", dev->index);
127 break;
128 case env_op_delete:
129 memset(dev->enetaddr, 0, ARP_HLEN);
130 }
131 }
132 dev = dev->next;
133 } while (dev != eth_devices);
134
135 return 0;
136}
137U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
138
139int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
140 int eth_number)
141{
142 unsigned char env_enetaddr[ARP_HLEN];
143 int ret = 0;
144
145 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
146
147 if (!is_zero_ethaddr(env_enetaddr)) {
148 if (!is_zero_ethaddr(dev->enetaddr) &&
149 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
150 printf("\nWarning: %s MAC addresses don't match:\n",
151 dev->name);
152 printf("Address in SROM is %pM\n",
153 dev->enetaddr);
154 printf("Address in environment is %pM\n",
155 env_enetaddr);
156 }
157
158 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
159 } else if (is_valid_ethaddr(dev->enetaddr)) {
160 eth_env_set_enetaddr_by_index(base_name, eth_number,
161 dev->enetaddr);
162 } else if (is_zero_ethaddr(dev->enetaddr)) {
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
168 printf("\nError: %s address not set.\n",
169 dev->name);
170 return -EINVAL;
171#endif
172 }
173
174 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
175 if (!is_valid_ethaddr(dev->enetaddr)) {
176 printf("\nError: %s address %pM illegal value\n",
177 dev->name, dev->enetaddr);
178 return -EINVAL;
179 }
180
181 ret = dev->write_hwaddr(dev);
182 if (ret)
183 printf("\nWarning: %s failed to set MAC address\n",
184 dev->name);
185 }
186
187 return ret;
188}
189
190int eth_register(struct eth_device *dev)
191{
192 struct eth_device *d;
193 static int index;
194
195 assert(strlen(dev->name) < sizeof(dev->name));
196
197 if (!eth_devices) {
198 eth_devices = dev;
199 eth_current = dev;
200 eth_current_changed();
201 } else {
202 for (d = eth_devices; d->next != eth_devices; d = d->next)
203 ;
204 d->next = dev;
205 }
206
207 dev->state = ETH_STATE_INIT;
208 dev->next = eth_devices;
209 dev->index = index++;
210
211 return 0;
212}
213
214int eth_unregister(struct eth_device *dev)
215{
216 struct eth_device *cur;
217
218
219 if (!eth_devices)
220 return -ENODEV;
221
222 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
223 cur = cur->next)
224 ;
225
226
227 if (cur->next != dev)
228 return -ENODEV;
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
243int eth_initialize(void)
244{
245 int num_devices = 0;
246
247 eth_devices = NULL;
248 eth_current = NULL;
249 eth_common_init();
250
251
252
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 }
263
264 if (!eth_devices) {
265 log_err("No ethernet found.\n");
266 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
267 } else {
268 struct eth_device *dev = eth_devices;
269 char *ethprime = env_get("ethprime");
270
271 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
272 do {
273 if (dev->index)
274 puts(", ");
275
276 printf("%s", dev->name);
277
278 if (ethprime && strcmp(dev->name, ethprime) == 0) {
279 eth_current = dev;
280 puts(" [PRIME]");
281 }
282
283 if (strchr(dev->name, ' '))
284 puts("\nWarning: eth device name has a space!"
285 "\n");
286
287 eth_write_hwaddr(dev, "eth", dev->index);
288
289 dev = dev->next;
290 num_devices++;
291 } while (dev != eth_devices);
292
293 eth_current_changed();
294 putc('\n');
295 }
296
297 return num_devices;
298}
299
300
301
302
303
304int eth_mcast_join(struct in_addr mcast_ip, int join)
305{
306 u8 mcast_mac[ARP_HLEN];
307 if (!eth_current || !eth_current->mcast)
308 return -1;
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;
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
318int eth_init(void)
319{
320 struct eth_device *old_current;
321
322 if (!eth_current) {
323 log_err("No ethernet found.\n");
324 return -ENODEV;
325 }
326
327 old_current = eth_current;
328 do {
329 debug("Trying %s\n", eth_current->name);
330
331 if (eth_current->init(eth_current, gd->bd) >= 0) {
332 eth_current->state = ETH_STATE_ACTIVE;
333
334 return 0;
335 }
336 debug("FAIL\n");
337
338 eth_try_another(0);
339 } while (old_current != eth_current);
340
341 return -ETIMEDOUT;
342}
343
344void 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
354int eth_is_active(struct eth_device *dev)
355{
356 return dev && dev->state == ETH_STATE_ACTIVE;
357}
358
359int eth_send(void *packet, int length)
360{
361 int ret;
362
363 if (!eth_current)
364 return -ENODEV;
365
366 ret = eth_current->send(eth_current, packet, length);
367#if defined(CONFIG_CMD_PCAP)
368 if (ret >= 0)
369 pcap_post(packet, length, true);
370#endif
371 return ret;
372}
373
374int eth_rx(void)
375{
376 if (!eth_current)
377 return -ENODEV;
378
379 return eth_current->recv(eth_current);
380}
381
382#ifdef CONFIG_API
383static void eth_save_packet(void *packet, int length)
384{
385 char *p = packet;
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
401int eth_receive(void *packet, int length)
402{
403 char *p = packet;
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
416 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
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
425