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