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