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