sandbox: Allow ethernet bootdevs to be disabled for tests
[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 #define LOG_CATEGORY UCLASS_ETH
9
10 #include <common.h>
11 #include <bootdev.h>
12 #include <bootstage.h>
13 #include <dm.h>
14 #include <env.h>
15 #include <log.h>
16 #include <net.h>
17 #include <nvmem.h>
18 #include <asm/global_data.h>
19 #include <dm/device-internal.h>
20 #include <dm/uclass-internal.h>
21 #include <net/pcap.h>
22 #include "eth_internal.h"
23 #include <eth_phy.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
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;
34         bool running;
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
41  * @no_bootdevs: true to skip binding Ethernet bootdevs (this is a negative flag
42  * so that the default value enables it)
43  */
44 struct eth_uclass_priv {
45         struct udevice *current;
46         bool no_bootdevs;
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;
55         int ret;
56
57         ret = uclass_get(UCLASS_ETH, &uc);
58         if (ret)
59                 return NULL;
60
61         assert(uc);
62         return uclass_get_priv(uc);
63 }
64
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
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
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.
92  */
93 struct udevice *eth_get_dev(void)
94 {
95         struct eth_uclass_priv *uc_priv;
96
97         uc_priv = eth_get_uclass_priv();
98         if (!uc_priv)
99                 return NULL;
100
101         if (!uc_priv->current) {
102                 eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
103                                                      &uc_priv->current);
104                 if (eth_errno)
105                         eth_errno = uclass_first_device_err(UCLASS_ETH,
106                                                             &uc_priv->current);
107                 if (eth_errno)
108                         uc_priv->current = NULL;
109         }
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");
141         int ret;
142
143         /* Must be longer than 3 to be an alias */
144         if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
145                 startp = devname + len;
146                 seq = dectoul(startp, &endp);
147         }
148
149         ret = uclass_get(UCLASS_ETH, &uc);
150         if (ret)
151                 return NULL;
152
153         uclass_foreach_dev(it, uc) {
154                 /*
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 ||
163                     (endp > startp && dev_seq(it) == seq))
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()) {
175                 pdata = dev_get_plat(eth_get_dev());
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
192         priv = dev_get_uclass_priv(current);
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
208         priv = dev_get_uclass_priv(current);
209         priv->state = ETH_STATE_PASSIVE;
210 }
211
212 int eth_get_dev_index(void)
213 {
214         if (eth_get_dev())
215                 return dev_seq(eth_get_dev());
216         return -1;
217 }
218
219 static int eth_write_hwaddr(struct udevice *dev)
220 {
221         struct eth_pdata *pdata;
222         int ret = 0;
223
224         if (!dev || !device_active(dev))
225                 return -EINVAL;
226
227         /* seq is valid since the device is active */
228         if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
229                 pdata = dev_get_plat(dev);
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" */
259         index = dectoul(name + 3, NULL);
260
261         retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
262         if (!retval) {
263                 struct eth_pdata *pdata = dev_get_plat(dev);
264                 switch (op) {
265                 case env_op_create:
266                 case env_op_overwrite:
267                         string_to_enetaddr(value, pdata->enetaddr);
268                         eth_write_hwaddr(dev);
269                         break;
270                 case env_op_delete:
271                         memset(pdata->enetaddr, 0, ARP_HLEN);
272                 }
273         }
274
275         return 0;
276 }
277 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
278
279 int eth_init(void)
280 {
281         char *ethact = env_get("ethact");
282         char *ethrotate = env_get("ethrotate");
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) {
302                         log_err("No ethernet found.\n");
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 =
316                                                 dev_get_uclass_priv(current);
317
318                                         priv->state = ETH_STATE_ACTIVE;
319                                         priv->running = true;
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();
349         if (!current)
350                 return;
351
352         priv = dev_get_uclass_priv(current);
353         if (!priv || !priv->running)
354                 return;
355
356         eth_get_ops(current)->stop(current);
357         priv->state = ETH_STATE_PASSIVE;
358         priv->running = false;
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
381         if (!eth_is_active(current))
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         }
389 #if defined(CONFIG_CMD_PCAP)
390         if (ret >= 0)
391                 pcap_post(packet, length, true);
392 #endif
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
408         if (!eth_is_active(current))
409                 return -EINVAL;
410
411         /* Process up to 32 packets at one time */
412         flags = ETH_RECV_CHECK_DEVICE;
413         for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
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          */
445         uclass_first_device_check(UCLASS_ETH, &dev);
446         if (!dev) {
447                 log_err("No ethernet found.\n");
448                 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
449         } else {
450                 char *ethprime = env_get("ethprime");
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 {
464                         if (device_active(dev)) {
465                                 if (num_devices)
466                                         printf(", ");
467
468                                 printf("eth%d: %s", dev_seq(dev), dev->name);
469
470                                 if (ethprime && dev == prime_dev)
471                                         printf(" [PRIME]");
472                         }
473
474                         eth_write_hwaddr(dev);
475
476                         if (device_active(dev))
477                                 num_devices++;
478                         uclass_next_device_check(&dev);
479                 } while (dev);
480
481                 if (!num_devices)
482                         log_err("No ethernet found.\n");
483                 putc('\n');
484         }
485
486         return num_devices;
487 }
488
489 static int eth_post_bind(struct udevice *dev)
490 {
491         struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
492         int ret;
493
494         if (strchr(dev->name, ' ')) {
495                 printf("\nError: eth device name \"%s\" has a space!\n",
496                        dev->name);
497                 return -EINVAL;
498         }
499
500 #ifdef CONFIG_DM_ETH_PHY
501         eth_phy_binds_nodes(dev);
502 #endif
503         if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
504                 ret = bootdev_setup_for_dev(dev, "eth_bootdev");
505                 if (ret)
506                         return log_msg_ret("bootdev", ret);
507         }
508
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
521 static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
522 {
523 #if CONFIG_IS_ENABLED(OF_CONTROL)
524         const uint8_t *p;
525         struct nvmem_cell mac_cell;
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
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))
537                 return false;
538
539         return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
540 #else
541         return false;
542 #endif
543 }
544
545 static int eth_post_probe(struct udevice *dev)
546 {
547         struct eth_device_priv *priv = dev_get_uclass_priv(dev);
548         struct eth_pdata *pdata = dev_get_plat(dev);
549         unsigned char env_enetaddr[ARP_HLEN];
550         char *source = "DT";
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;
567                 if (ops->mcast)
568                         ops->mcast += gd->reloc_off;
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;
579         priv->running = false;
580
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)) {
584                 source = "ROM";
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         }
589
590         eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
591         if (!is_zero_ethaddr(env_enetaddr)) {
592                 if (!is_zero_ethaddr(pdata->enetaddr) &&
593                     memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
594                         printf("\nWarning: %s MAC addresses don't match:\n",
595                                dev->name);
596                         printf("Address in %s is\t\t%pM\n",
597                                source, pdata->enetaddr);
598                         printf("Address in environment is\t%pM\n",
599                                env_enetaddr);
600                 }
601
602                 /* Override the ROM MAC address */
603                 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
604         } else if (is_valid_ethaddr(pdata->enetaddr)) {
605                 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
606                                               pdata->enetaddr);
607         } else if (is_zero_ethaddr(pdata->enetaddr) ||
608                    !is_valid_ethaddr(pdata->enetaddr)) {
609 #ifdef CONFIG_NET_RANDOM_ETHADDR
610                 net_random_ethaddr(pdata->enetaddr);
611                 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
612                        dev->name, dev_seq(dev), pdata->enetaddr);
613                 eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
614                                               pdata->enetaddr);
615 #else
616                 printf("\nError: %s address not set.\n",
617                        dev->name);
618                 return -EINVAL;
619 #endif
620         }
621
622         eth_write_hwaddr(dev);
623
624         return 0;
625 }
626
627 static int eth_pre_remove(struct udevice *dev)
628 {
629         struct eth_pdata *pdata = dev_get_plat(dev);
630
631         eth_get_ops(dev)->stop(dev);
632
633         /* clear the MAC address */
634         memset(pdata->enetaddr, 0, ARP_HLEN);
635
636         return 0;
637 }
638
639 UCLASS_DRIVER(ethernet) = {
640         .name           = "ethernet",
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,
646         .priv_auto      = sizeof(struct eth_uclass_priv),
647         .per_device_auto        = sizeof(struct eth_device_priv),
648         .flags          = DM_UC_FLAG_SEQ_ALIAS,
649 };