wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Detlev Zundel | d46bcd3 | 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 | d79de1d | 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 | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 11 | #include <miiphy.h> |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 12 | #include <phy.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | |
Mike Frysinger | c34319c | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 14 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
| 15 | { |
| 16 | char *end; |
| 17 | int i; |
| 18 | |
| 19 | for (i = 0; i < 6; ++i) { |
| 20 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; |
| 21 | if (addr) |
| 22 | addr = (*end) ? end + 1 : end; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | int eth_getenv_enetaddr(char *name, uchar *enetaddr) |
| 27 | { |
| 28 | eth_parse_enetaddr(getenv(name), enetaddr); |
| 29 | return is_valid_ether_addr(enetaddr); |
| 30 | } |
| 31 | |
| 32 | int eth_setenv_enetaddr(char *name, const uchar *enetaddr) |
| 33 | { |
| 34 | char buf[20]; |
| 35 | |
| 36 | sprintf(buf, "%pM", enetaddr); |
| 37 | |
| 38 | return setenv(name, buf); |
| 39 | } |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 40 | |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 41 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
| 42 | uchar *enetaddr) |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 43 | { |
| 44 | char enetvar[32]; |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 45 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 46 | return eth_getenv_enetaddr(enetvar, enetaddr); |
| 47 | } |
Mike Frysinger | c34319c | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 48 | |
Joe Hershberger | 6c6fe5f | 2012-07-10 16:23:22 -0500 | [diff] [blame] | 49 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
Rob Herring | c2938c0 | 2012-04-14 18:06:49 +0000 | [diff] [blame] | 50 | uchar *enetaddr) |
| 51 | { |
| 52 | char enetvar[32]; |
| 53 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
| 54 | return eth_setenv_enetaddr(enetvar, enetaddr); |
| 55 | } |
| 56 | |
| 57 | |
Ben Warren | 6db991a | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 58 | static int eth_mac_skip(int index) |
| 59 | { |
| 60 | char enetvar[15]; |
| 61 | char *skip_state; |
| 62 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
| 63 | return ((skip_state = getenv(enetvar)) != NULL); |
| 64 | } |
| 65 | |
Ben Warren | d448a49 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 66 | /* |
| 67 | * CPU and board-specific Ethernet initializations. Aliased function |
| 68 | * signals caller to move on |
| 69 | */ |
| 70 | static int __def_eth_init(bd_t *bis) |
| 71 | { |
| 72 | return -1; |
| 73 | } |
Peter Tyser | 21d2cd2 | 2009-04-20 11:08:46 -0500 | [diff] [blame] | 74 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 75 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
Ben Warren | d448a49 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 76 | |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 77 | #ifdef CONFIG_API |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 78 | static struct { |
| 79 | uchar data[PKTSIZE]; |
| 80 | int length; |
| 81 | } eth_rcv_bufs[PKTBUFSRX]; |
| 82 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 83 | static unsigned int eth_rcv_current, eth_rcv_last; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 84 | #endif |
| 85 | |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 86 | static struct eth_device *eth_devices; |
| 87 | struct eth_device *eth_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 88 | |
Ben Warren | 97824d6 | 2010-07-29 12:56:11 -0700 | [diff] [blame] | 89 | struct eth_device *eth_get_dev_by_name(const char *devname) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 90 | { |
| 91 | struct eth_device *dev, *target_dev; |
| 92 | |
Helmut Raiger | 153bf3a | 2011-08-22 00:17:17 +0000 | [diff] [blame] | 93 | BUG_ON(devname == NULL); |
| 94 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 95 | if (!eth_devices) |
| 96 | return NULL; |
| 97 | |
| 98 | dev = eth_devices; |
| 99 | target_dev = NULL; |
| 100 | do { |
| 101 | if (strcmp(devname, dev->name) == 0) { |
| 102 | target_dev = dev; |
| 103 | break; |
| 104 | } |
| 105 | dev = dev->next; |
| 106 | } while (dev != eth_devices); |
| 107 | |
| 108 | return target_dev; |
| 109 | } |
| 110 | |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 111 | struct eth_device *eth_get_dev_by_index(int index) |
| 112 | { |
| 113 | struct eth_device *dev, *target_dev; |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 114 | |
| 115 | if (!eth_devices) |
| 116 | return NULL; |
| 117 | |
| 118 | dev = eth_devices; |
| 119 | target_dev = NULL; |
| 120 | do { |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 121 | if (dev->index == index) { |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 122 | target_dev = dev; |
| 123 | break; |
| 124 | } |
| 125 | dev = dev->next; |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 126 | } while (dev != eth_devices); |
| 127 | |
| 128 | return target_dev; |
| 129 | } |
| 130 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 131 | int eth_get_dev_index(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 133 | if (!eth_current) |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 134 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 135 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 136 | return eth_current->index; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 139 | static void eth_current_changed(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 140 | { |
Mike Frysinger | a23230c | 2011-10-02 10:01:27 +0000 | [diff] [blame] | 141 | char *act = getenv("ethact"); |
| 142 | /* update current ethernet name */ |
| 143 | if (eth_current) { |
| 144 | if (act == NULL || strcmp(act, eth_current->name) != 0) |
| 145 | setenv("ethact", eth_current->name); |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 146 | } |
Mike Frysinger | a23230c | 2011-10-02 10:01:27 +0000 | [diff] [blame] | 147 | /* |
| 148 | * remove the variable completely if there is no active |
| 149 | * interface |
| 150 | */ |
| 151 | else if (act != NULL) |
| 152 | setenv("ethact", NULL); |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Simon Glass | 62b36c9 | 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 | ae97ef6 | 2012-01-18 22:56:33 +0000 | [diff] [blame] | 161 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 162 | |
| 163 | if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { |
| 164 | if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 165 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
Simon Glass | 62b36c9 | 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 | c2938c0 | 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); |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (dev->write_hwaddr && |
Benoît Thébaudeau | 050219e | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 183 | !eth_mac_skip(eth_number)) { |
| 184 | if (!is_valid_ether_addr(dev->enetaddr)) |
| 185 | return -1; |
| 186 | |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 187 | ret = dev->write_hwaddr(dev); |
Benoît Thébaudeau | 050219e | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 188 | } |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 193 | int eth_register(struct eth_device *dev) |
| 194 | { |
| 195 | struct eth_device *d; |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 196 | static int index; |
Michal Simek | 5b4945b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 197 | |
Mike Frysinger | 6b300dc | 2011-11-10 14:11:04 +0000 | [diff] [blame] | 198 | assert(strlen(dev->name) < sizeof(dev->name)); |
Michal Simek | 5b4945b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 199 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 200 | if (!eth_devices) { |
| 201 | eth_current = eth_devices = dev; |
| 202 | eth_current_changed(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 203 | } else { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 204 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
Detlev Zundel | d46bcd3 | 2010-03-31 17:56:08 +0200 | [diff] [blame] | 205 | ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | d->next = dev; |
| 207 | } |
| 208 | |
| 209 | dev->state = ETH_STATE_INIT; |
| 210 | dev->next = eth_devices; |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 211 | dev->index = index++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Vincent Palatin | 451be50 | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 216 | int eth_unregister(struct eth_device *dev) |
| 217 | { |
| 218 | struct eth_device *cur; |
| 219 | |
| 220 | /* No device */ |
| 221 | if (!eth_devices) |
| 222 | return -1; |
| 223 | |
| 224 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; |
| 225 | cur = cur->next) |
| 226 | ; |
| 227 | |
| 228 | /* Device not found */ |
| 229 | if (cur->next != dev) |
| 230 | return -1; |
| 231 | |
| 232 | cur->next = dev->next; |
| 233 | |
| 234 | if (eth_devices == dev) |
| 235 | eth_devices = dev->next == eth_devices ? NULL : dev->next; |
| 236 | |
| 237 | if (eth_current == dev) { |
| 238 | eth_current = eth_devices; |
| 239 | eth_current_changed(); |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Mike Frysinger | 36fe7db | 2012-04-04 18:53:41 +0000 | [diff] [blame] | 245 | static void eth_env_init(bd_t *bis) |
| 246 | { |
| 247 | const char *s; |
| 248 | |
| 249 | if ((s = getenv("bootfile")) != NULL) |
| 250 | copy_filename(BootFile, s, sizeof(BootFile)); |
| 251 | } |
| 252 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | int eth_initialize(bd_t *bis) |
| 254 | { |
Michael Walle | 391da4c | 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 | 0169e6b | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 259 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
Alexey Brodkin | 37ecfa1 | 2014-01-10 19:58:11 +0400 | [diff] [blame] | 260 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 261 | miiphy_init(); |
| 262 | #endif |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 263 | |
| 264 | #ifdef CONFIG_PHYLIB |
| 265 | phy_init(); |
| 266 | #endif |
| 267 | |
Mike Frysinger | 36fe7db | 2012-04-04 18:53:41 +0000 | [diff] [blame] | 268 | eth_env_init(bis); |
| 269 | |
Ben Warren | cfdc8e0 | 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 | cbdd1c8 | 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 | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 284 | puts("No ethernet found.\n"); |
Simon Glass | 0169e6b | 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 | 5956ded | 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 | 0169e6b | 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 | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 292 | if (dev->index) |
Joe Hershberger | 5956ded | 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 | 5956ded | 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 | 5956ded | 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 | 2843ed9 | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 302 | if (strchr(dev->name, ' ')) |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 303 | puts("\nWarning: eth device name has a space!" |
| 304 | "\n"); |
Mike Frysinger | 2843ed9 | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 305 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 306 | if (eth_write_hwaddr(dev, "eth", dev->index)) |
Philip Balister | dde025c | 2011-09-07 01:57:48 +0000 | [diff] [blame] | 307 | puts("\nWarning: failed to set MAC address\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 308 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 309 | dev = dev->next; |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 310 | num_devices++; |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 311 | } while (dev != eth_devices); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 312 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 313 | eth_current_changed(); |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 314 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 317 | return num_devices; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 318 | } |
| 319 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 320 | #ifdef CONFIG_MCAST_TFTP |
| 321 | /* Multicast. |
| 322 | * mcast_addr: multicast ipaddr from which multicast Mac is made |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 323 | * join: 1=join, 0=leave. |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 324 | */ |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 325 | int eth_mcast_join(IPaddr_t mcast_ip, u8 join) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 326 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 327 | u8 mcast_mac[6]; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 328 | if (!eth_current || !eth_current->mcast) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 329 | return -1; |
| 330 | mcast_mac[5] = htonl(mcast_ip) & 0xff; |
| 331 | mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff; |
| 332 | mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f; |
| 333 | mcast_mac[2] = 0x5e; |
| 334 | mcast_mac[1] = 0x0; |
| 335 | mcast_mac[0] = 0x1; |
| 336 | return eth_current->mcast(eth_current, mcast_mac, join); |
| 337 | } |
| 338 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 339 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
| 340 | * and this is the ethernet-crc method needed for TSEC -- and perhaps |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 341 | * some other adapter -- hash tables |
| 342 | */ |
| 343 | #define CRCPOLY_LE 0xedb88320 |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 344 | u32 ether_crc(size_t len, unsigned char const *p) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 345 | { |
| 346 | int i; |
| 347 | u32 crc; |
| 348 | crc = ~0; |
| 349 | while (len--) { |
| 350 | crc ^= *p++; |
| 351 | for (i = 0; i < 8; i++) |
| 352 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 353 | } |
| 354 | /* an reverse the bits, cuz of way they arrive -- last-first */ |
| 355 | crc = (crc >> 16) | (crc << 16); |
| 356 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); |
| 357 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); |
| 358 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); |
| 359 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); |
| 360 | return crc; |
| 361 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 362 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 363 | #endif |
| 364 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 365 | |
| 366 | int eth_init(bd_t *bis) |
| 367 | { |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 368 | struct eth_device *old_current, *dev; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 369 | |
Stefan Roese | 11da2be | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 370 | if (!eth_current) { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 371 | puts("No ethernet found.\n"); |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 372 | return -1; |
Stefan Roese | 11da2be | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 373 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 374 | |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 375 | /* Sync environment with network devices */ |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 376 | dev = eth_devices; |
| 377 | do { |
| 378 | uchar env_enetaddr[6]; |
| 379 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 380 | if (eth_getenv_enetaddr_by_index("eth", dev->index, |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 381 | env_enetaddr)) |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 382 | memcpy(dev->enetaddr, env_enetaddr, 6); |
| 383 | |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 384 | dev = dev->next; |
| 385 | } while (dev != eth_devices); |
| 386 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 387 | old_current = eth_current; |
| 388 | do { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 389 | debug("Trying %s\n", eth_current->name); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 390 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 391 | if (eth_current->init(eth_current, bis) >= 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 392 | eth_current->state = ETH_STATE_ACTIVE; |
| 393 | |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 394 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 395 | } |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 396 | debug("FAIL\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 397 | |
| 398 | eth_try_another(0); |
| 399 | } while (old_current != eth_current); |
| 400 | |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 401 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void eth_halt(void) |
| 405 | { |
| 406 | if (!eth_current) |
| 407 | return; |
| 408 | |
| 409 | eth_current->halt(eth_current); |
| 410 | |
| 411 | eth_current->state = ETH_STATE_PASSIVE; |
| 412 | } |
| 413 | |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 414 | int eth_send(void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 415 | { |
| 416 | if (!eth_current) |
| 417 | return -1; |
| 418 | |
| 419 | return eth_current->send(eth_current, packet, length); |
| 420 | } |
| 421 | |
| 422 | int eth_rx(void) |
| 423 | { |
| 424 | if (!eth_current) |
| 425 | return -1; |
| 426 | |
| 427 | return eth_current->recv(eth_current); |
| 428 | } |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 429 | |
| 430 | #ifdef CONFIG_API |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 431 | static void eth_save_packet(void *packet, int length) |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 432 | { |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 433 | char *p = packet; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 434 | int i; |
| 435 | |
| 436 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) |
| 437 | return; |
| 438 | |
| 439 | if (PKTSIZE < length) |
| 440 | return; |
| 441 | |
| 442 | for (i = 0; i < length; i++) |
| 443 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; |
| 444 | |
| 445 | eth_rcv_bufs[eth_rcv_last].length = length; |
| 446 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; |
| 447 | } |
| 448 | |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 449 | int eth_receive(void *packet, int length) |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 450 | { |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 451 | char *p = packet; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 452 | void *pp = push_packet; |
| 453 | int i; |
| 454 | |
| 455 | if (eth_rcv_current == eth_rcv_last) { |
| 456 | push_packet = eth_save_packet; |
| 457 | eth_rx(); |
| 458 | push_packet = pp; |
| 459 | |
| 460 | if (eth_rcv_current == eth_rcv_last) |
| 461 | return -1; |
| 462 | } |
| 463 | |
Michael Walle | df7c98a | 2012-06-22 11:24:28 +0000 | [diff] [blame] | 464 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 465 | |
| 466 | for (i = 0; i < length; i++) |
| 467 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; |
| 468 | |
| 469 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; |
| 470 | return length; |
| 471 | } |
| 472 | #endif /* CONFIG_API */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 473 | |
| 474 | void eth_try_another(int first_restart) |
| 475 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 476 | static struct eth_device *first_failed; |
Remy Bohmer | 0fce515 | 2011-02-19 20:15:14 +0100 | [diff] [blame] | 477 | char *ethrotate; |
Matthias Fuchs | 204f0ec | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Do not rotate between network interfaces when |
| 481 | * 'ethrotate' variable is set to 'no'. |
| 482 | */ |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 483 | ethrotate = getenv("ethrotate"); |
| 484 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
Matthias Fuchs | 204f0ec | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 485 | return; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 486 | |
| 487 | if (!eth_current) |
| 488 | return; |
| 489 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 490 | if (first_restart) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 491 | first_failed = eth_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 492 | |
| 493 | eth_current = eth_current->next; |
| 494 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 495 | eth_current_changed(); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 496 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 497 | if (first_failed == eth_current) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 498 | NetRestartWrap = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 499 | } |
| 500 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 501 | void eth_set_current(void) |
| 502 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 503 | static char *act; |
| 504 | static int env_changed_id; |
| 505 | struct eth_device *old_current; |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 506 | int env_id; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 507 | |
| 508 | if (!eth_current) /* XXX no current */ |
| 509 | return; |
| 510 | |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 511 | env_id = get_env_id(); |
| 512 | if ((act == NULL) || (env_changed_id != env_id)) { |
| 513 | act = getenv("ethact"); |
| 514 | env_changed_id = env_id; |
| 515 | } |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 516 | if (act != NULL) { |
| 517 | old_current = eth_current; |
| 518 | do { |
| 519 | if (strcmp(eth_current->name, act) == 0) |
| 520 | return; |
| 521 | eth_current = eth_current->next; |
| 522 | } while (old_current != eth_current); |
| 523 | } |
| 524 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 525 | eth_current_changed(); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 526 | } |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 527 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 528 | char *eth_get_name(void) |
wdenk | c6abb7e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 529 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 530 | return eth_current ? eth_current->name : "unknown"; |
wdenk | c6abb7e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 531 | } |