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