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