Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 2 | /* |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 3 | * (C) Copyright 2001-2015 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Joe Hershberger | 05c3e68 | 2015-03-22 17:09:10 -0500 | [diff] [blame] | 5 | * Joe Hershberger, National Instruments |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 10 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 11 | #include <net.h> |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 12 | #include <phy.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 14 | #include "eth_internal.h" |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 15 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 18 | /* |
| 19 | * CPU and board-specific Ethernet initializations. Aliased function |
| 20 | * signals caller to move on |
| 21 | */ |
| 22 | static int __def_eth_init(bd_t *bis) |
| 23 | { |
| 24 | return -1; |
| 25 | } |
| 26 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 27 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 28 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 29 | #ifdef CONFIG_API |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 30 | static struct { |
| 31 | uchar data[PKTSIZE]; |
| 32 | int length; |
| 33 | } eth_rcv_bufs[PKTBUFSRX]; |
| 34 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 35 | static unsigned int eth_rcv_current, eth_rcv_last; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 36 | #endif |
| 37 | |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 38 | static struct eth_device *eth_devices; |
| 39 | struct eth_device *eth_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 40 | |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 41 | void eth_set_current_to_next(void) |
Joe Hershberger | 84eb1fb | 2015-03-22 17:09:03 -0500 | [diff] [blame] | 42 | { |
| 43 | eth_current = eth_current->next; |
| 44 | } |
| 45 | |
Simon Glass | 8607a6b | 2016-01-17 14:51:59 -0700 | [diff] [blame] | 46 | void eth_set_dev(struct eth_device *dev) |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 47 | { |
| 48 | eth_current = dev; |
| 49 | } |
| 50 | |
Ben Warren | d7fb9bc | 2010-07-29 12:56:11 -0700 | [diff] [blame] | 51 | struct eth_device *eth_get_dev_by_name(const char *devname) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 52 | { |
| 53 | struct eth_device *dev, *target_dev; |
| 54 | |
Helmut Raiger | 7e7f903 | 2011-08-22 00:17:17 +0000 | [diff] [blame] | 55 | BUG_ON(devname == NULL); |
| 56 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 57 | if (!eth_devices) |
| 58 | return NULL; |
| 59 | |
| 60 | dev = eth_devices; |
| 61 | target_dev = NULL; |
| 62 | do { |
| 63 | if (strcmp(devname, dev->name) == 0) { |
| 64 | target_dev = dev; |
| 65 | break; |
| 66 | } |
| 67 | dev = dev->next; |
| 68 | } while (dev != eth_devices); |
| 69 | |
| 70 | return target_dev; |
| 71 | } |
| 72 | |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 73 | struct eth_device *eth_get_dev_by_index(int index) |
| 74 | { |
| 75 | struct eth_device *dev, *target_dev; |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 76 | |
| 77 | if (!eth_devices) |
| 78 | return NULL; |
| 79 | |
| 80 | dev = eth_devices; |
| 81 | target_dev = NULL; |
| 82 | do { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 83 | if (dev->index == index) { |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 84 | target_dev = dev; |
| 85 | break; |
| 86 | } |
| 87 | dev = dev->next; |
Andy Fleming | 9e56986 | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 88 | } while (dev != eth_devices); |
| 89 | |
| 90 | return target_dev; |
| 91 | } |
| 92 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 93 | int eth_get_dev_index(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 94 | { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 95 | if (!eth_current) |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 96 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 97 | |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 98 | return eth_current->index; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 101 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
| 102 | int flags) |
| 103 | { |
| 104 | int index; |
| 105 | struct eth_device *dev; |
| 106 | |
| 107 | if (!eth_devices) |
| 108 | return 0; |
| 109 | |
| 110 | /* look for an index after "eth" */ |
| 111 | index = simple_strtoul(name + 3, NULL, 10); |
| 112 | |
| 113 | dev = eth_devices; |
| 114 | do { |
| 115 | if (dev->index == index) { |
| 116 | switch (op) { |
| 117 | case env_op_create: |
| 118 | case env_op_overwrite: |
| 119 | eth_parse_enetaddr(value, dev->enetaddr); |
Marek Vasut | 73d570a7 | 2016-11-12 16:28:40 +0100 | [diff] [blame] | 120 | eth_write_hwaddr(dev, "eth", dev->index); |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 121 | break; |
| 122 | case env_op_delete: |
oliver@schinagl.nl | a40db6d | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 123 | memset(dev->enetaddr, 0, ARP_HLEN); |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 124 | } |
| 125 | } |
Gong Qianyu | 7aba0f2 | 2015-08-31 11:34:43 +0800 | [diff] [blame] | 126 | dev = dev->next; |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 127 | } while (dev != eth_devices); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); |
| 132 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 133 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
| 134 | int eth_number) |
| 135 | { |
oliver@schinagl.nl | a40db6d | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 136 | unsigned char env_enetaddr[ARP_HLEN]; |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 137 | int ret = 0; |
| 138 | |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 139 | eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 140 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 141 | if (!is_zero_ethaddr(env_enetaddr)) { |
| 142 | if (!is_zero_ethaddr(dev->enetaddr) && |
oliver@schinagl.nl | a40db6d | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 143 | memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) { |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 144 | printf("\nWarning: %s MAC addresses don't match:\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 145 | dev->name); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 146 | printf("Address in SROM is %pM\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 147 | dev->enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 148 | printf("Address in environment is %pM\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 149 | env_enetaddr); |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 150 | } |
| 151 | |
oliver@schinagl.nl | a40db6d | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 152 | memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 153 | } else if (is_valid_ethaddr(dev->enetaddr)) { |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 154 | eth_env_set_enetaddr_by_index(base_name, eth_number, |
| 155 | dev->enetaddr); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 156 | } else if (is_zero_ethaddr(dev->enetaddr)) { |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 157 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
| 158 | net_random_ethaddr(dev->enetaddr); |
| 159 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", |
| 160 | dev->name, eth_number, dev->enetaddr); |
| 161 | #else |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 162 | printf("\nError: %s address not set.\n", |
| 163 | dev->name); |
| 164 | return -EINVAL; |
Joe Hershberger | bef1014 | 2015-05-04 14:55:13 -0500 | [diff] [blame] | 165 | #endif |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 168 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 169 | if (!is_valid_ethaddr(dev->enetaddr)) { |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 170 | printf("\nError: %s address %pM illegal value\n", |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 171 | dev->name, dev->enetaddr); |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 172 | return -EINVAL; |
| 173 | } |
Benoît Thébaudeau | 460f949 | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 174 | |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 175 | ret = dev->write_hwaddr(dev); |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 176 | if (ret) |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 177 | printf("\nWarning: %s failed to set MAC address\n", |
| 178 | dev->name); |
Benoît Thébaudeau | 460f949 | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 179 | } |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 184 | int eth_register(struct eth_device *dev) |
| 185 | { |
| 186 | struct eth_device *d; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 187 | static int index; |
Michal Simek | 58c583b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 188 | |
Mike Frysinger | f6add13 | 2011-11-10 14:11:04 +0000 | [diff] [blame] | 189 | assert(strlen(dev->name) < sizeof(dev->name)); |
Michal Simek | 58c583b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 190 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 191 | if (!eth_devices) { |
Joe Hershberger | ff819a3 | 2015-04-08 01:41:19 -0500 | [diff] [blame] | 192 | eth_devices = dev; |
| 193 | eth_current = dev; |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 194 | eth_current_changed(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 195 | } else { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 196 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
Detlev Zundel | aba4b69 | 2010-03-31 17:56:08 +0200 | [diff] [blame] | 197 | ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 198 | d->next = dev; |
| 199 | } |
| 200 | |
| 201 | dev->state = ETH_STATE_INIT; |
| 202 | dev->next = eth_devices; |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 203 | dev->index = index++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 208 | int eth_unregister(struct eth_device *dev) |
| 209 | { |
| 210 | struct eth_device *cur; |
| 211 | |
| 212 | /* No device */ |
| 213 | if (!eth_devices) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 214 | return -ENODEV; |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 215 | |
| 216 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; |
| 217 | cur = cur->next) |
| 218 | ; |
| 219 | |
| 220 | /* Device not found */ |
| 221 | if (cur->next != dev) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 222 | return -ENODEV; |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 223 | |
| 224 | cur->next = dev->next; |
| 225 | |
| 226 | if (eth_devices == dev) |
| 227 | eth_devices = dev->next == eth_devices ? NULL : dev->next; |
| 228 | |
| 229 | if (eth_current == dev) { |
| 230 | eth_current = eth_devices; |
| 231 | eth_current_changed(); |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 237 | int eth_initialize(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 239 | int num_devices = 0; |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 240 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | eth_devices = NULL; |
| 242 | eth_current = NULL; |
Simon Glass | 3bc4270 | 2015-04-05 16:07:37 -0600 | [diff] [blame] | 243 | eth_common_init(); |
Simon Glass | 818f91e | 2016-01-17 14:51:57 -0700 | [diff] [blame] | 244 | /* |
| 245 | * If board-specific initialization exists, call it. |
| 246 | * If not, call a CPU-specific one |
| 247 | */ |
| 248 | if (board_eth_init != __def_eth_init) { |
| 249 | if (board_eth_init(gd->bd) < 0) |
| 250 | printf("Board Net Initialization Failed\n"); |
| 251 | } else if (cpu_eth_init != __def_eth_init) { |
| 252 | if (cpu_eth_init(gd->bd) < 0) |
| 253 | printf("CPU Net Initialization Failed\n"); |
| 254 | } else { |
| 255 | printf("Net Initialization Skipped\n"); |
| 256 | } |
Marian Balakowicz | d9785c1 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 257 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 258 | if (!eth_devices) { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 259 | puts("No ethernet found.\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 260 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 261 | } else { |
| 262 | struct eth_device *dev = eth_devices; |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 263 | char *ethprime = env_get("ethprime"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 264 | |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 265 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 266 | do { |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 267 | if (dev->index) |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 268 | puts(", "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 269 | |
| 270 | printf("%s", dev->name); |
| 271 | |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 272 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 273 | eth_current = dev; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 274 | puts(" [PRIME]"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Mike Frysinger | 1384f3b | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 277 | if (strchr(dev->name, ' ')) |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 278 | puts("\nWarning: eth device name has a space!" |
| 279 | "\n"); |
Mike Frysinger | 1384f3b | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 280 | |
Pavel Machek | 75d9a45 | 2014-07-13 10:27:02 +0200 | [diff] [blame] | 281 | eth_write_hwaddr(dev, "eth", dev->index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 282 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 283 | dev = dev->next; |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 284 | num_devices++; |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 285 | } while (dev != eth_devices); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 286 | |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 287 | eth_current_changed(); |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 288 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Michael Walle | fea7dca | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 291 | return num_devices; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 292 | } |
| 293 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 294 | #ifdef CONFIG_MCAST_TFTP |
| 295 | /* Multicast. |
| 296 | * mcast_addr: multicast ipaddr from which multicast Mac is made |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 297 | * join: 1=join, 0=leave. |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 298 | */ |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 299 | int eth_mcast_join(struct in_addr mcast_ip, int join) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 300 | { |
oliver@schinagl.nl | a40db6d | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 301 | u8 mcast_mac[ARP_HLEN]; |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 302 | if (!eth_current || !eth_current->mcast) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 303 | return -1; |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 304 | mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff; |
| 305 | mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff; |
| 306 | mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f; |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 307 | mcast_mac[2] = 0x5e; |
| 308 | mcast_mac[1] = 0x0; |
| 309 | mcast_mac[0] = 0x1; |
| 310 | return eth_current->mcast(eth_current, mcast_mac, join); |
| 311 | } |
| 312 | |
Wolfgang Denk | 85eb5ca | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 313 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
| 314 | * and this is the ethernet-crc method needed for TSEC -- and perhaps |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 315 | * some other adapter -- hash tables |
| 316 | */ |
| 317 | #define CRCPOLY_LE 0xedb88320 |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 318 | u32 ether_crc(size_t len, unsigned char const *p) |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 319 | { |
| 320 | int i; |
| 321 | u32 crc; |
| 322 | crc = ~0; |
| 323 | while (len--) { |
| 324 | crc ^= *p++; |
| 325 | for (i = 0; i < 8; i++) |
| 326 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 327 | } |
| 328 | /* an reverse the bits, cuz of way they arrive -- last-first */ |
| 329 | crc = (crc >> 16) | (crc << 16); |
| 330 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); |
| 331 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); |
| 332 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); |
| 333 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); |
| 334 | return crc; |
| 335 | } |
| 336 | |
| 337 | #endif |
| 338 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 339 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 340 | int eth_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 341 | { |
Joe Hershberger | 6e0d26c | 2015-05-20 14:27:26 -0500 | [diff] [blame] | 342 | struct eth_device *old_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 343 | |
Stefan Roese | 6bc1138 | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 344 | if (!eth_current) { |
Joe Hershberger | 66c7385 | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 345 | puts("No ethernet found.\n"); |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 346 | return -ENODEV; |
Stefan Roese | 6bc1138 | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 347 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 348 | |
| 349 | old_current = eth_current; |
| 350 | do { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 351 | debug("Trying %s\n", eth_current->name); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 352 | |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 353 | if (eth_current->init(eth_current, gd->bd) >= 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 354 | eth_current->state = ETH_STATE_ACTIVE; |
| 355 | |
Upakul Barkakaty | 505be87 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 356 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 357 | } |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 358 | debug("FAIL\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 359 | |
| 360 | eth_try_another(0); |
| 361 | } while (old_current != eth_current); |
| 362 | |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 363 | return -ETIMEDOUT; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void eth_halt(void) |
| 367 | { |
| 368 | if (!eth_current) |
| 369 | return; |
| 370 | |
| 371 | eth_current->halt(eth_current); |
| 372 | |
| 373 | eth_current->state = ETH_STATE_PASSIVE; |
| 374 | } |
| 375 | |
Bernhard Nortmann | eaa8a19 | 2015-09-14 15:29:43 +0200 | [diff] [blame] | 376 | int eth_is_active(struct eth_device *dev) |
| 377 | { |
| 378 | return dev && dev->state == ETH_STATE_ACTIVE; |
| 379 | } |
| 380 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 381 | int eth_send(void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 382 | { |
| 383 | if (!eth_current) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 384 | return -ENODEV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 385 | |
| 386 | return eth_current->send(eth_current, packet, length); |
| 387 | } |
| 388 | |
| 389 | int eth_rx(void) |
| 390 | { |
| 391 | if (!eth_current) |
Joe Hershberger | 05324a4 | 2015-03-22 17:09:04 -0500 | [diff] [blame] | 392 | return -ENODEV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 393 | |
| 394 | return eth_current->recv(eth_current); |
| 395 | } |
| 396 | |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 397 | #ifdef CONFIG_API |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 398 | static void eth_save_packet(void *packet, int length) |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 399 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 400 | char *p = packet; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 401 | int i; |
| 402 | |
| 403 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) |
| 404 | return; |
| 405 | |
| 406 | if (PKTSIZE < length) |
| 407 | return; |
| 408 | |
| 409 | for (i = 0; i < length; i++) |
| 410 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; |
| 411 | |
| 412 | eth_rcv_bufs[eth_rcv_last].length = length; |
| 413 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; |
| 414 | } |
| 415 | |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 416 | int eth_receive(void *packet, int length) |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 417 | { |
Joe Hershberger | db288a9 | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 418 | char *p = packet; |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 419 | void *pp = push_packet; |
| 420 | int i; |
| 421 | |
| 422 | if (eth_rcv_current == eth_rcv_last) { |
| 423 | push_packet = eth_save_packet; |
| 424 | eth_rx(); |
| 425 | push_packet = pp; |
| 426 | |
| 427 | if (eth_rcv_current == eth_rcv_last) |
| 428 | return -1; |
| 429 | } |
| 430 | |
Michael Walle | 46c07bc | 2012-06-22 11:24:28 +0000 | [diff] [blame] | 431 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
Rafal Jaworowski | f85b607 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 432 | |
| 433 | for (i = 0; i < length; i++) |
| 434 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; |
| 435 | |
| 436 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; |
| 437 | return length; |
| 438 | } |
| 439 | #endif /* CONFIG_API */ |