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