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