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