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