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