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