Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001-2015 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * Joe Hershberger, National Instruments |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Patrick Delaunay | a8f2ca2 | 2021-07-20 20:15:30 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_ETH |
| 9 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 11 | #include <bootdev.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 12 | #include <bootstage.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 16 | #include <net.h> |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 17 | #include <nvmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
| 20 | #include <dm/uclass-internal.h> |
Ramon Fried | ac598c1 | 2019-07-18 21:43:30 +0300 | [diff] [blame] | 21 | #include <net/pcap.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 22 | #include "eth_internal.h" |
Ye Li | cd5bb77 | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 23 | #include <eth_phy.h> |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 24 | |
Simon Glass | 004b891 | 2016-01-30 15:45:14 -0700 | [diff] [blame] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 27 | /** |
| 28 | * struct eth_device_priv - private structure for each Ethernet device |
| 29 | * |
| 30 | * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t) |
| 31 | */ |
| 32 | struct eth_device_priv { |
| 33 | enum eth_state_t state; |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 34 | bool running; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * struct eth_uclass_priv - The structure attached to the uclass itself |
| 39 | * |
| 40 | * @current: The Ethernet device that the network functions are using |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 41 | * @no_bootdevs: true to skip binding Ethernet bootdevs (this is a negative flag |
| 42 | * so that the default value enables it) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 43 | */ |
| 44 | struct eth_uclass_priv { |
| 45 | struct udevice *current; |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 46 | bool no_bootdevs; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* eth_errno - This stores the most recent failure code from DM functions */ |
| 50 | static int eth_errno; |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 51 | /* Are we currently in eth_init() or eth_halt()? */ |
| 52 | static bool in_init_halt; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 53 | |
Marek Vasut | b506e7d | 2023-03-06 15:53:42 +0100 | [diff] [blame] | 54 | /* board-specific Ethernet Interface initializations. */ |
| 55 | __weak int board_interface_eth_init(struct udevice *dev, |
| 56 | phy_interface_t interface_type) |
| 57 | { |
| 58 | return 0; |
| 59 | } |
| 60 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 61 | static struct eth_uclass_priv *eth_get_uclass_priv(void) |
| 62 | { |
| 63 | struct uclass *uc; |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 64 | int ret; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 65 | |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 66 | ret = uclass_get(UCLASS_ETH, &uc); |
| 67 | if (ret) |
| 68 | return NULL; |
| 69 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 70 | assert(uc); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 71 | return uclass_get_priv(uc); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 74 | void eth_set_enable_bootdevs(bool enable) |
| 75 | { |
| 76 | struct eth_uclass_priv *priv = eth_get_uclass_priv(); |
| 77 | |
| 78 | if (priv) |
| 79 | priv->no_bootdevs = !enable; |
| 80 | } |
| 81 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 82 | void eth_set_current_to_next(void) |
| 83 | { |
| 84 | struct eth_uclass_priv *uc_priv; |
| 85 | |
| 86 | uc_priv = eth_get_uclass_priv(); |
| 87 | if (uc_priv->current) |
| 88 | uclass_next_device(&uc_priv->current); |
| 89 | if (!uc_priv->current) |
| 90 | uclass_first_device(UCLASS_ETH, &uc_priv->current); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Typically this will simply return the active device. |
| 95 | * In the case where the most recent active device was unset, this will attempt |
Michael Walle | 384023e | 2021-02-24 17:30:44 +0100 | [diff] [blame] | 96 | * to return the device with sequence id 0 (which can be configured by the |
| 97 | * device tree). If this fails, fall back to just getting the first device. |
| 98 | * The latter is non-deterministic and depends on the order of the probing. |
| 99 | * If that device doesn't exist or fails to probe, this function will return |
| 100 | * NULL. |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 101 | */ |
| 102 | struct udevice *eth_get_dev(void) |
| 103 | { |
| 104 | struct eth_uclass_priv *uc_priv; |
| 105 | |
| 106 | uc_priv = eth_get_uclass_priv(); |
Sean Anderson | 35e8298 | 2020-09-12 17:45:43 -0400 | [diff] [blame] | 107 | if (!uc_priv) |
| 108 | return NULL; |
| 109 | |
Michael Walle | 384023e | 2021-02-24 17:30:44 +0100 | [diff] [blame] | 110 | if (!uc_priv->current) { |
| 111 | eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0, |
| 112 | &uc_priv->current); |
| 113 | if (eth_errno) |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 114 | eth_errno = uclass_first_device_err(UCLASS_ETH, |
| 115 | &uc_priv->current); |
Michal Suchanek | 050aa36 | 2022-10-12 21:58:02 +0200 | [diff] [blame] | 116 | if (eth_errno) |
| 117 | uc_priv->current = NULL; |
Michael Walle | 384023e | 2021-02-24 17:30:44 +0100 | [diff] [blame] | 118 | } |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 119 | return uc_priv->current; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Typically this will just store a device pointer. |
| 124 | * In case it was not probed, we will attempt to do so. |
| 125 | * dev may be NULL to unset the active device. |
| 126 | */ |
| 127 | void eth_set_dev(struct udevice *dev) |
| 128 | { |
| 129 | if (dev && !device_active(dev)) { |
| 130 | eth_errno = device_probe(dev); |
| 131 | if (eth_errno) |
| 132 | dev = NULL; |
| 133 | } |
| 134 | |
| 135 | eth_get_uclass_priv()->current = dev; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Find the udevice that either has the name passed in as devname or has an |
| 140 | * alias named devname. |
| 141 | */ |
| 142 | struct udevice *eth_get_dev_by_name(const char *devname) |
| 143 | { |
| 144 | int seq = -1; |
| 145 | char *endp = NULL; |
| 146 | const char *startp = NULL; |
| 147 | struct udevice *it; |
| 148 | struct uclass *uc; |
| 149 | int len = strlen("eth"); |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 150 | int ret; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 151 | |
| 152 | /* Must be longer than 3 to be an alias */ |
| 153 | if (!strncmp(devname, "eth", len) && strlen(devname) > len) { |
| 154 | startp = devname + len; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 155 | seq = dectoul(startp, &endp); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Peng Fan | 46ec104 | 2020-05-03 22:41:13 +0800 | [diff] [blame] | 158 | ret = uclass_get(UCLASS_ETH, &uc); |
| 159 | if (ret) |
| 160 | return NULL; |
| 161 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 162 | uclass_foreach_dev(it, uc) { |
| 163 | /* |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 164 | * We don't care about errors from probe here. Either they won't |
| 165 | * match an alias or it will match a literal name and we'll pick |
| 166 | * up the error when we try to probe again in eth_set_dev(). |
| 167 | */ |
| 168 | if (device_probe(it)) |
| 169 | continue; |
| 170 | /* Check for the name or the sequence number to match */ |
| 171 | if (strcmp(it->name, devname) == 0 || |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 172 | (endp > startp && dev_seq(it) == seq)) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 173 | return it; |
| 174 | } |
| 175 | |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | unsigned char *eth_get_ethaddr(void) |
| 180 | { |
| 181 | struct eth_pdata *pdata; |
| 182 | |
| 183 | if (eth_get_dev()) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 184 | pdata = dev_get_plat(eth_get_dev()); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 185 | return pdata->enetaddr; |
| 186 | } |
| 187 | |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | /* Set active state without calling start on the driver */ |
| 192 | int eth_init_state_only(void) |
| 193 | { |
| 194 | struct udevice *current; |
| 195 | struct eth_device_priv *priv; |
| 196 | |
| 197 | current = eth_get_dev(); |
| 198 | if (!current || !device_active(current)) |
| 199 | return -EINVAL; |
| 200 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 201 | priv = dev_get_uclass_priv(current); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 202 | priv->state = ETH_STATE_ACTIVE; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* Set passive state without calling stop on the driver */ |
| 208 | void eth_halt_state_only(void) |
| 209 | { |
| 210 | struct udevice *current; |
| 211 | struct eth_device_priv *priv; |
| 212 | |
| 213 | current = eth_get_dev(); |
| 214 | if (!current || !device_active(current)) |
| 215 | return; |
| 216 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 217 | priv = dev_get_uclass_priv(current); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 218 | priv->state = ETH_STATE_PASSIVE; |
| 219 | } |
| 220 | |
| 221 | int eth_get_dev_index(void) |
| 222 | { |
| 223 | if (eth_get_dev()) |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 224 | return dev_seq(eth_get_dev()); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | static int eth_write_hwaddr(struct udevice *dev) |
| 229 | { |
xypron.glpk@gmx.de | 0211e39 | 2017-05-16 05:07:01 +0200 | [diff] [blame] | 230 | struct eth_pdata *pdata; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 231 | int ret = 0; |
| 232 | |
| 233 | if (!dev || !device_active(dev)) |
| 234 | return -EINVAL; |
| 235 | |
| 236 | /* seq is valid since the device is active */ |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 237 | if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 238 | pdata = dev_get_plat(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 239 | if (!is_valid_ethaddr(pdata->enetaddr)) { |
| 240 | printf("\nError: %s address %pM illegal value\n", |
| 241 | dev->name, pdata->enetaddr); |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Drivers are allowed to decide not to implement this at |
| 247 | * run-time. E.g. Some devices may use it and some may not. |
| 248 | */ |
| 249 | ret = eth_get_ops(dev)->write_hwaddr(dev); |
| 250 | if (ret == -ENOSYS) |
| 251 | ret = 0; |
| 252 | if (ret) |
| 253 | printf("\nWarning: %s failed to set MAC address\n", |
| 254 | dev->name); |
| 255 | } |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
| 261 | int flags) |
| 262 | { |
| 263 | int index; |
| 264 | int retval; |
| 265 | struct udevice *dev; |
| 266 | |
| 267 | /* look for an index after "eth" */ |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 268 | index = dectoul(name + 3, NULL); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 269 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 270 | retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 271 | if (!retval) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 272 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 273 | switch (op) { |
| 274 | case env_op_create: |
| 275 | case env_op_overwrite: |
Joe Hershberger | 8e7545e | 2019-09-13 19:21:16 -0500 | [diff] [blame] | 276 | string_to_enetaddr(value, pdata->enetaddr); |
Hannes Schmelzer | 3071f07 | 2016-09-02 14:48:17 +0200 | [diff] [blame] | 277 | eth_write_hwaddr(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 278 | break; |
| 279 | case env_op_delete: |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 280 | memset(pdata->enetaddr, 0, ARP_HLEN); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); |
| 287 | |
| 288 | int eth_init(void) |
| 289 | { |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 290 | struct udevice *current = NULL; |
| 291 | struct udevice *old_current; |
| 292 | int ret = -ENODEV; |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 293 | char *ethrotate; |
| 294 | char *ethact; |
| 295 | |
| 296 | if (in_init_halt) |
| 297 | return -EBUSY; |
| 298 | |
| 299 | in_init_halt = true; |
| 300 | |
| 301 | ethact = env_get("ethact"); |
| 302 | ethrotate = env_get("ethrotate"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * When 'ethrotate' variable is set to 'no' and 'ethact' variable |
| 306 | * is already set to an ethernet device, we should stick to 'ethact'. |
| 307 | */ |
| 308 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) { |
| 309 | if (ethact) { |
| 310 | current = eth_get_dev_by_name(ethact); |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 311 | if (!current) { |
| 312 | ret = -EINVAL; |
| 313 | goto end; |
| 314 | } |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | if (!current) { |
| 319 | current = eth_get_dev(); |
| 320 | if (!current) { |
Heinrich Schuchardt | 653f3ee | 2020-09-14 11:00:18 +0200 | [diff] [blame] | 321 | log_err("No ethernet found.\n"); |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 322 | ret = -ENODEV; |
| 323 | goto end; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | old_current = current; |
| 328 | do { |
| 329 | if (current) { |
| 330 | debug("Trying %s\n", current->name); |
| 331 | |
| 332 | if (device_active(current)) { |
| 333 | ret = eth_get_ops(current)->start(current); |
| 334 | if (ret >= 0) { |
| 335 | struct eth_device_priv *priv = |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 336 | dev_get_uclass_priv(current); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 337 | |
| 338 | priv->state = ETH_STATE_ACTIVE; |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 339 | priv->running = true; |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 340 | ret = 0; |
| 341 | goto end; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 342 | } |
| 343 | } else { |
| 344 | ret = eth_errno; |
| 345 | } |
| 346 | |
| 347 | debug("FAIL\n"); |
| 348 | } else { |
| 349 | debug("PROBE FAIL\n"); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * If ethrotate is enabled, this will change "current", |
| 354 | * otherwise we will drop out of this while loop immediately |
| 355 | */ |
| 356 | eth_try_another(0); |
| 357 | /* This will ensure the new "current" attempted to probe */ |
| 358 | current = eth_get_dev(); |
| 359 | } while (old_current != current); |
| 360 | |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 361 | end: |
| 362 | in_init_halt = false; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | void eth_halt(void) |
| 367 | { |
| 368 | struct udevice *current; |
| 369 | struct eth_device_priv *priv; |
| 370 | |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 371 | if (in_init_halt) |
| 372 | return; |
| 373 | |
| 374 | in_init_halt = true; |
| 375 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 376 | current = eth_get_dev(); |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 377 | if (!current) |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 378 | goto end; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 379 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 380 | priv = dev_get_uclass_priv(current); |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 381 | if (!priv || !priv->running) |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 382 | goto end; |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 383 | |
| 384 | eth_get_ops(current)->stop(current); |
| 385 | priv->state = ETH_STATE_PASSIVE; |
| 386 | priv->running = false; |
Matthias Schiffer | 2469b13 | 2024-04-26 10:02:24 +0200 | [diff] [blame] | 387 | |
| 388 | end: |
| 389 | in_init_halt = false; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | int eth_is_active(struct udevice *dev) |
| 393 | { |
| 394 | struct eth_device_priv *priv; |
| 395 | |
| 396 | if (!dev || !device_active(dev)) |
| 397 | return 0; |
| 398 | |
| 399 | priv = dev_get_uclass_priv(dev); |
| 400 | return priv->state == ETH_STATE_ACTIVE; |
| 401 | } |
| 402 | |
| 403 | int eth_send(void *packet, int length) |
| 404 | { |
| 405 | struct udevice *current; |
| 406 | int ret; |
| 407 | |
| 408 | current = eth_get_dev(); |
| 409 | if (!current) |
| 410 | return -ENODEV; |
| 411 | |
Alexander Graf | 7151149 | 2018-03-15 15:07:09 +0100 | [diff] [blame] | 412 | if (!eth_is_active(current)) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 413 | return -EINVAL; |
| 414 | |
| 415 | ret = eth_get_ops(current)->send(current, packet, length); |
| 416 | if (ret < 0) { |
| 417 | /* We cannot completely return the error at present */ |
| 418 | debug("%s: send() returned error %d\n", __func__, ret); |
| 419 | } |
Ramon Fried | ac598c1 | 2019-07-18 21:43:30 +0300 | [diff] [blame] | 420 | #if defined(CONFIG_CMD_PCAP) |
| 421 | if (ret >= 0) |
| 422 | pcap_post(packet, length, true); |
| 423 | #endif |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | int eth_rx(void) |
| 428 | { |
| 429 | struct udevice *current; |
| 430 | uchar *packet; |
| 431 | int flags; |
| 432 | int ret; |
| 433 | int i; |
| 434 | |
| 435 | current = eth_get_dev(); |
| 436 | if (!current) |
| 437 | return -ENODEV; |
| 438 | |
Alexander Graf | 7151149 | 2018-03-15 15:07:09 +0100 | [diff] [blame] | 439 | if (!eth_is_active(current)) |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 440 | return -EINVAL; |
| 441 | |
| 442 | /* Process up to 32 packets at one time */ |
| 443 | flags = ETH_RECV_CHECK_DEVICE; |
Patrick Wildt | d47430a | 2020-10-07 11:03:30 +0200 | [diff] [blame] | 444 | for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) { |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 445 | ret = eth_get_ops(current)->recv(current, flags, &packet); |
| 446 | flags = 0; |
| 447 | if (ret > 0) |
| 448 | net_process_received_packet(packet, ret); |
| 449 | if (ret >= 0 && eth_get_ops(current)->free_pkt) |
| 450 | eth_get_ops(current)->free_pkt(current, packet, ret); |
| 451 | if (ret <= 0) |
| 452 | break; |
| 453 | } |
| 454 | if (ret == -EAGAIN) |
| 455 | ret = 0; |
| 456 | if (ret < 0) { |
| 457 | /* We cannot completely return the error at present */ |
| 458 | debug("%s: recv() returned error %d\n", __func__, ret); |
| 459 | } |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | int eth_initialize(void) |
| 464 | { |
| 465 | int num_devices = 0; |
| 466 | struct udevice *dev; |
| 467 | |
| 468 | eth_common_init(); |
| 469 | |
| 470 | /* |
| 471 | * Devices need to write the hwaddr even if not started so that Linux |
| 472 | * will have access to the hwaddr that u-boot stored for the device. |
| 473 | * This is accomplished by attempting to probe each device and calling |
| 474 | * their write_hwaddr() operation. |
| 475 | */ |
Mario Six | 865c387 | 2018-04-27 14:52:56 +0200 | [diff] [blame] | 476 | uclass_first_device_check(UCLASS_ETH, &dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 477 | if (!dev) { |
Heinrich Schuchardt | 653f3ee | 2020-09-14 11:00:18 +0200 | [diff] [blame] | 478 | log_err("No ethernet found.\n"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 479 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
| 480 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 481 | char *ethprime = env_get("ethprime"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 482 | struct udevice *prime_dev = NULL; |
| 483 | |
| 484 | if (ethprime) |
| 485 | prime_dev = eth_get_dev_by_name(ethprime); |
| 486 | if (prime_dev) { |
| 487 | eth_set_dev(prime_dev); |
| 488 | eth_current_changed(); |
| 489 | } else { |
| 490 | eth_set_dev(NULL); |
| 491 | } |
| 492 | |
| 493 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
| 494 | do { |
Simon Glass | 3e14a22 | 2020-12-16 21:20:16 -0700 | [diff] [blame] | 495 | if (device_active(dev)) { |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 496 | if (num_devices) |
| 497 | printf(", "); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 498 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 499 | printf("eth%d: %s", dev_seq(dev), dev->name); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 500 | |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 501 | if (ethprime && dev == prime_dev) |
| 502 | printf(" [PRIME]"); |
| 503 | } |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 504 | |
| 505 | eth_write_hwaddr(dev); |
| 506 | |
Simon Glass | 3e14a22 | 2020-12-16 21:20:16 -0700 | [diff] [blame] | 507 | if (device_active(dev)) |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 508 | num_devices++; |
Mario Six | 865c387 | 2018-04-27 14:52:56 +0200 | [diff] [blame] | 509 | uclass_next_device_check(&dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 510 | } while (dev); |
| 511 | |
Michael Walle | 902c796 | 2019-10-22 01:03:10 +0200 | [diff] [blame] | 512 | if (!num_devices) |
Heinrich Schuchardt | 653f3ee | 2020-09-14 11:00:18 +0200 | [diff] [blame] | 513 | log_err("No ethernet found.\n"); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 514 | putc('\n'); |
| 515 | } |
| 516 | |
| 517 | return num_devices; |
| 518 | } |
| 519 | |
| 520 | static int eth_post_bind(struct udevice *dev) |
| 521 | { |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 522 | struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass); |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 523 | int ret; |
| 524 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 525 | if (strchr(dev->name, ' ')) { |
| 526 | printf("\nError: eth device name \"%s\" has a space!\n", |
| 527 | dev->name); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
Ye Li | cd5bb77 | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 531 | #ifdef CONFIG_DM_ETH_PHY |
| 532 | eth_phy_binds_nodes(dev); |
| 533 | #endif |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 534 | if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) { |
Simon Glass | 4f840a2 | 2022-04-24 23:31:15 -0600 | [diff] [blame] | 535 | ret = bootdev_setup_for_dev(dev, "eth_bootdev"); |
| 536 | if (ret) |
| 537 | return log_msg_ret("bootdev", ret); |
| 538 | } |
Ye Li | cd5bb77 | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 539 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int eth_pre_unbind(struct udevice *dev) |
| 544 | { |
| 545 | /* Don't hang onto a pointer that is going away */ |
| 546 | if (dev == eth_get_uclass_priv()->current) |
| 547 | eth_set_dev(NULL); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 552 | static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN]) |
| 553 | { |
Simon Glass | 1eb7294 | 2021-01-13 20:29:47 -0700 | [diff] [blame] | 554 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 555 | const uint8_t *p; |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 556 | struct nvmem_cell mac_cell; |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 557 | |
| 558 | p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN); |
| 559 | if (!p) |
| 560 | p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN); |
| 561 | |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 562 | if (p) { |
| 563 | memcpy(mac, p, ARP_HLEN); |
| 564 | return true; |
| 565 | } |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 566 | |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 567 | if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell)) |
| 568 | return false; |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 569 | |
Sean Anderson | dfdda78 | 2022-05-05 13:11:41 -0400 | [diff] [blame] | 570 | return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN); |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 571 | #else |
| 572 | return false; |
| 573 | #endif |
| 574 | } |
| 575 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 576 | static int eth_post_probe(struct udevice *dev) |
| 577 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 578 | struct eth_device_priv *priv = dev_get_uclass_priv(dev); |
| 579 | struct eth_pdata *pdata = dev_get_plat(dev); |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 580 | unsigned char env_enetaddr[ARP_HLEN]; |
Michal Simek | 3141113 | 2020-03-16 11:36:17 +0100 | [diff] [blame] | 581 | char *source = "DT"; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 582 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 583 | priv->state = ETH_STATE_INIT; |
Matthias Schiffer | 9f5c1a5 | 2020-11-04 14:45:14 +0100 | [diff] [blame] | 584 | priv->running = false; |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 585 | |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 586 | /* Check if the device has a valid MAC address in device tree */ |
| 587 | if (!eth_dev_get_mac_address(dev, pdata->enetaddr) || |
| 588 | !is_valid_ethaddr(pdata->enetaddr)) { |
| 589 | /* Check if the device has a MAC address in ROM */ |
Michal Simek | 58e609b | 2023-09-15 16:10:06 +0200 | [diff] [blame] | 590 | if (eth_get_ops(dev)->read_rom_hwaddr) { |
| 591 | int ret; |
| 592 | |
| 593 | ret = eth_get_ops(dev)->read_rom_hwaddr(dev); |
| 594 | if (!ret) |
| 595 | source = "ROM"; |
| 596 | } |
Thierry Reding | 8fe0853 | 2019-05-20 17:59:57 +0200 | [diff] [blame] | 597 | } |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 598 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 599 | eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 600 | if (!is_zero_ethaddr(env_enetaddr)) { |
| 601 | if (!is_zero_ethaddr(pdata->enetaddr) && |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 602 | memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) { |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 603 | printf("\nWarning: %s MAC addresses don't match:\n", |
| 604 | dev->name); |
Michal Simek | 3141113 | 2020-03-16 11:36:17 +0100 | [diff] [blame] | 605 | printf("Address in %s is\t\t%pM\n", |
| 606 | source, pdata->enetaddr); |
| 607 | printf("Address in environment is\t%pM\n", |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 608 | env_enetaddr); |
| 609 | } |
| 610 | |
| 611 | /* Override the ROM MAC address */ |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 612 | memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 613 | } else if (is_valid_ethaddr(pdata->enetaddr)) { |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 614 | eth_env_set_enetaddr_by_index("eth", dev_seq(dev), |
| 615 | pdata->enetaddr); |
Siva Durga Prasad Paladugu | e0b7bc7 | 2016-11-02 12:52:13 +0100 | [diff] [blame] | 616 | } else if (is_zero_ethaddr(pdata->enetaddr) || |
| 617 | !is_valid_ethaddr(pdata->enetaddr)) { |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 618 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
| 619 | net_random_ethaddr(pdata->enetaddr); |
| 620 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 621 | dev->name, dev_seq(dev), pdata->enetaddr); |
Michal Simek | 2b25402 | 2022-01-11 10:28:09 +0100 | [diff] [blame] | 622 | eth_env_set_enetaddr_by_index("eth", dev_seq(dev), |
| 623 | pdata->enetaddr); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 624 | #else |
Fabio Estevam | 3cc8444 | 2023-10-20 09:41:51 -0300 | [diff] [blame] | 625 | printf("\nError: %s No valid MAC address found.\n", |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 626 | dev->name); |
| 627 | return -EINVAL; |
| 628 | #endif |
| 629 | } |
| 630 | |
Thierry Reding | 1306487 | 2019-05-20 17:59:56 +0200 | [diff] [blame] | 631 | eth_write_hwaddr(dev); |
| 632 | |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static int eth_pre_remove(struct udevice *dev) |
| 637 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 638 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 639 | |
| 640 | eth_get_ops(dev)->stop(dev); |
| 641 | |
| 642 | /* clear the MAC address */ |
oliver@schinagl.nl | 496904b | 2016-11-25 16:30:19 +0100 | [diff] [blame] | 643 | memset(pdata->enetaddr, 0, ARP_HLEN); |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
Michael Walle | 7efcdfd | 2021-02-25 16:51:11 +0100 | [diff] [blame] | 648 | UCLASS_DRIVER(ethernet) = { |
| 649 | .name = "ethernet", |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 650 | .id = UCLASS_ETH, |
| 651 | .post_bind = eth_post_bind, |
| 652 | .pre_unbind = eth_pre_unbind, |
| 653 | .post_probe = eth_post_probe, |
| 654 | .pre_remove = eth_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 655 | .priv_auto = sizeof(struct eth_uclass_priv), |
| 656 | .per_device_auto = sizeof(struct eth_device_priv), |
Simon Glass | c6011d2 | 2016-01-17 14:52:00 -0700 | [diff] [blame] | 657 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 658 | }; |