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