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