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