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