Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/net/eth.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
// SPDX-FileCopyrightText: 2001-2004 Wolfgang Denk <wd@denx.de>, DENX Software Engineering
3
4
#include <common.h>
5
#include <command.h>
6
#include <complete.h>
7
#include <driver.h>
8
#include <unistd.h>
9
#include <init.h>
10
#include <dhcp.h>
11
#include <net.h>
12
#include <dma.h>
13
#include <machine_id.h>
14
#include <of.h>
15
#include <of_net.h>
16
#include <linux/phy.h>
17
#include <errno.h>
18
#include <malloc.h>
19
#include <globalvar.h>
20
#include <environment.h>
21
#include <linux/ctype.h>
22
#include <linux/stat.h>
23
24
DEFINE_DEV_CLASS(eth_class, "eth");
25
EXPORT_SYMBOL(eth_class);
26
27
LIST_HEAD(ethaddr_list);
28
29
/*
30
 * Only network devices named ethX are numbered after the ethernetX device
31
 * tree aliases, so only they are what an ethid refers to.
32
 */
33
static bool eth_has_ethid(struct eth_device *edev)
34
0
{
35
0
  return !strcmp(edev->dev.name, "eth");
36
0
}
37
38
int eth_set_promisc(struct eth_device *edev, bool enable)
39
0
{
40
0
  if (!edev->set_promisc)
41
0
    return -EOPNOTSUPP;
42
43
0
  return edev->set_promisc(edev, enable);
44
0
}
45
46
int eth_set_ethaddr(struct eth_device *edev, const char *ethaddr)
47
0
{
48
0
  int ret;
49
50
0
  ret = edev->set_ethaddr(edev, ethaddr);
51
0
  if (ret)
52
0
    return ret;
53
54
0
  if (ethaddr != edev->ethaddr)
55
0
    memcpy(edev->ethaddr, ethaddr, ETH_ALEN);
56
57
0
  return 0;
58
0
}
59
60
static void register_preset_mac_address(struct eth_device *edev, const char *ethaddr)
61
0
{
62
0
  if (is_valid_ether_addr(ethaddr)) {
63
0
    dev_info(&edev->dev, "got preset MAC address: %pM\n", ethaddr);
64
0
    eth_set_ethaddr(edev, ethaddr);
65
0
  }
66
0
}
67
68
static int eth_get_registered_ethaddr(struct eth_device *edev, void *buf)
69
0
{
70
0
  struct eth_ethaddr *addr;
71
0
  struct device_node *node = NULL;
72
73
0
  if (edev->parent)
74
0
    node = edev->parent->of_node;
75
76
0
  list_for_each_entry(addr, &ethaddr_list, list) {
77
0
    if ((node && node == addr->node) ||
78
0
        (eth_has_ethid(edev) && addr->ethid == edev->dev.id)) {
79
0
      memcpy(buf, addr->ethaddr, ETH_ALEN);
80
0
      return 0;
81
0
    }
82
0
  }
83
0
  return -EINVAL;
84
0
}
85
86
static void eth_drop_ethaddr(int ethid)
87
15.6k
{
88
15.6k
  struct eth_ethaddr *addr, *tmp;
89
90
1.13M
  list_for_each_entry_safe(addr, tmp, &ethaddr_list, list) {
91
1.13M
    if (addr->ethid == ethid) {
92
15.4k
      list_del(&addr->list);
93
15.4k
      free(addr);
94
15.4k
      return;
95
15.4k
    }
96
1.13M
  }
97
15.6k
}
98
99
void eth_register_ethaddr(int ethid, const char *ethaddr)
100
15.6k
{
101
15.6k
  struct eth_ethaddr *addr;
102
15.6k
  struct eth_device *edev;
103
104
15.6k
  eth_drop_ethaddr(ethid);
105
106
15.6k
  for_each_netdev(edev) {
107
0
    if (eth_has_ethid(edev) && edev->dev.id == ethid) {
108
0
      register_preset_mac_address(edev, ethaddr);
109
0
      break;
110
0
    }
111
0
  }
112
113
15.6k
  addr = xzalloc(sizeof(*addr));
114
15.6k
  addr->ethid = ethid;
115
15.6k
  memcpy(addr->ethaddr, ethaddr, ETH_ALEN);
116
15.6k
  list_add_tail(&addr->list, &ethaddr_list);
117
15.6k
}
118
119
static struct eth_device *eth_get_by_node(struct device_node *node)
120
0
{
121
0
  struct eth_device *edev;
122
123
0
  for_each_netdev(edev) {
124
0
    if (!edev->parent)
125
0
      continue;
126
0
    if (!edev->parent->of_node)
127
0
      continue;
128
0
    if (edev->parent->of_node == node)
129
0
      return edev;
130
0
  }
131
0
  return NULL;
132
0
}
133
134
void of_eth_register_ethaddr(struct device_node *node, const char *ethaddr)
135
0
{
136
0
  struct eth_ethaddr *addr;
137
0
  struct eth_device *edev;
138
139
0
  edev = eth_get_by_node(node);
140
0
  if (edev) {
141
0
    register_preset_mac_address(edev, ethaddr);
142
0
    return;
143
0
  }
144
145
0
  addr = xzalloc(sizeof(*addr));
146
0
  addr->node = node;
147
0
  addr->ethid = -1;
148
0
  memcpy(addr->ethaddr, ethaddr, ETH_ALEN);
149
0
  list_add_tail(&addr->list, &ethaddr_list);
150
0
}
151
152
struct eth_device *eth_get_byname(const char *ethname)
153
0
{
154
0
  struct eth_device *edev;
155
156
0
  for_each_netdev(edev) {
157
0
    if (!strcmp(ethname, eth_name(edev)))
158
0
      return edev;
159
0
  }
160
0
  return NULL;
161
0
}
162
163
#ifdef CONFIG_AUTO_COMPLETE
164
int eth_complete(struct string_list *sl, char *instr)
165
0
{
166
0
  struct eth_device *edev;
167
0
  int len;
168
169
0
  len = strlen(instr);
170
171
0
  for_each_netdev(edev) {
172
0
    if (strncmp(instr, eth_name(edev), len))
173
0
      continue;
174
175
0
    string_list_add_asprintf(sl, "%s ", eth_name(edev));
176
0
  }
177
0
  return COMPLETE_CONTINUE;
178
0
}
179
#endif
180
181
int eth_carrier_poll_once(struct eth_device *edev)
182
0
{
183
0
  int ret;
184
185
0
  if (!IS_ENABLED(CONFIG_PHYLIB))
186
0
    return 0;
187
188
0
  if (!edev->phydev)
189
0
    return 0;
190
191
0
  ret = phy_update_status(edev->phydev);
192
0
  if (ret)
193
0
    return ret;
194
195
0
  edev->last_link_check = get_time_ns();
196
0
  return edev->phydev->link ? 0 : -ENETDOWN;
197
0
}
198
199
/*
200
 * Check for link if we haven't done so for longer.
201
 */
202
static int eth_carrier_check(struct eth_device *edev, bool may_wait)
203
0
{
204
0
  if (!IS_ENABLED(CONFIG_PHYLIB))
205
0
    return 0;
206
207
0
  if (!edev->phydev)
208
0
    return 0;
209
210
0
  if (!edev->last_link_check ||
211
0
      is_timeout(edev->last_link_check, edev->phydev->polling_interval))
212
0
    eth_carrier_poll_once(edev);
213
214
0
  if (may_wait && !edev->phydev->link) {
215
0
    phy_wait_aneg_done(edev->phydev);
216
0
    edev->last_link_check = get_time_ns();
217
0
  }
218
219
0
  return edev->phydev->link ? 0 : -ENETDOWN;
220
0
}
221
222
struct eth_q {
223
  struct eth_device *edev;
224
  int length;
225
  struct list_head list;
226
  void *data;
227
};
228
229
static int eth_queue(struct eth_device *edev, void *packet, int length)
230
0
{
231
0
  struct eth_q *q;
232
233
0
  q = xzalloc(sizeof(*q));
234
0
  if (!q)
235
0
    return -ENOMEM;
236
237
0
  q->data = dma_alloc(length);
238
0
  if (!q->data) {
239
0
    free(q);
240
0
    return -ENOMEM;
241
0
  }
242
243
0
  q->length = length;
244
0
  q->edev = edev;
245
246
0
  memcpy(q->data, packet, length);
247
0
  list_add_tail(&q->list, &edev->send_queue);
248
249
0
  return 0;
250
0
}
251
252
int eth_send(struct eth_device *edev, void *packet, int length)
253
0
{
254
0
  int ret;
255
256
0
  if (!edev->active)
257
0
    return -ENETDOWN;
258
259
0
  if (slice_acquired(eth_device_slice(edev)))
260
0
    return eth_queue(edev, packet, length);
261
262
0
  ret = eth_carrier_check(edev, true);
263
0
  if (ret)
264
0
    return ret;
265
266
0
  slice_acquire(eth_device_slice(edev));
267
268
0
  led_trigger_network(LED_TRIGGER_NET_TX);
269
270
0
  ret = eth_send_raw(edev, packet, length);
271
272
0
  slice_release(eth_device_slice(edev));
273
274
0
  return ret;
275
0
}
276
277
static void eth_do_work(struct eth_device *edev)
278
0
{
279
0
  struct eth_q *q, *tmp;
280
0
  int ret;
281
282
0
  if (!phy_acquired(edev->phydev)) {
283
0
    ret = eth_carrier_check(edev, false);
284
0
    if (ret)
285
0
      return;
286
0
  }
287
288
0
  if (slice_acquired(eth_device_slice(edev)))
289
0
    return;
290
291
0
  slice_acquire(eth_device_slice(edev));
292
293
0
  edev->recv(edev);
294
295
0
  list_for_each_entry_safe(q, tmp, &edev->send_queue, list) {
296
0
    led_trigger_network(LED_TRIGGER_NET_TX);
297
0
    eth_send_raw(edev, q->data, q->length);
298
0
    list_del(&q->list);
299
0
    free(q->data);
300
0
    free(q);
301
0
  }
302
303
0
  slice_release(eth_device_slice(edev));
304
0
}
305
306
int eth_rx(void)
307
0
{
308
0
  struct eth_device *edev;
309
310
0
  for_each_netdev(edev) {
311
0
    if (edev->active)
312
0
      eth_do_work(edev);
313
0
  }
314
315
0
  return 0;
316
0
}
317
318
static int eth_param_set_ethaddr(struct param_d *param, void *priv)
319
0
{
320
0
  struct eth_device *edev = priv;
321
322
0
  return eth_set_ethaddr(edev, edev->ethaddr);
323
0
}
324
325
#ifdef CONFIG_OFTREE
326
struct device_node *eth_of_get_fixup_node(struct device_node *root,
327
            const char *node_path, int ethid)
328
0
{
329
0
  struct device_node *bb_node, *fixup_node;
330
0
  char *name;
331
332
0
  if (node_path) {
333
0
    bb_node = of_find_node_by_path_from(0, node_path);
334
0
    name = of_get_reproducible_name(bb_node);
335
0
    fixup_node = of_find_node_by_reproducible_name(root, name);
336
0
    free(name);
337
0
  } else {
338
0
    char eth[12];
339
0
    sprintf(eth, "ethernet%d", ethid);
340
0
    fixup_node = of_find_node_by_alias(root, eth);
341
0
  }
342
343
0
  return fixup_node;
344
0
}
345
346
static void eth_of_fixup_node(struct device_node *root,
347
            const char *node_path, int ethid,
348
            const u8 ethaddr[ETH_ALEN])
349
0
{
350
0
  struct device_node *fixup_node;
351
0
  int ret;
352
353
0
  if (!is_valid_ether_addr(ethaddr)) {
354
0
    pr_debug("%s: no valid mac address, cannot fixup\n",
355
0
       __func__);
356
0
    return;
357
0
  }
358
359
0
  fixup_node = eth_of_get_fixup_node(root, node_path, ethid);
360
0
  if (!fixup_node) {
361
0
    pr_debug("%s: no node to fixup\n", __func__);
362
0
    return;
363
0
  }
364
365
0
  ret = of_set_property(fixup_node, "mac-address", ethaddr, ETH_ALEN, 1);
366
0
  if (ret)
367
0
    pr_err("Setting mac-address property of %s failed with: %pe\n",
368
0
           fixup_node->full_name, ERR_PTR(ret));
369
0
}
370
371
static int eth_of_fixup(struct device_node *root, void *unused)
372
0
{
373
0
  struct eth_ethaddr *addr;
374
0
  struct eth_device *edev;
375
376
  /*
377
   * Add the mac-address property for each ethaddr and then each network
378
   * device we find a node path for and which has a valid mac address.
379
   * This will find both network devices barebox was told about as well as
380
   * addresses registered by boards but for which no network device was
381
   * ever loaded.
382
   */
383
0
  list_for_each_entry(addr, &ethaddr_list, list)
384
0
    eth_of_fixup_node(root, addr->node ? addr->node->full_name : NULL,
385
0
          addr->ethid, addr->ethaddr);
386
387
0
  for_each_netdev(edev) {
388
0
    if (!edev->nodepath && !eth_has_ethid(edev))
389
0
      continue;
390
391
0
    eth_of_fixup_node(root, edev->nodepath, edev->dev.id, edev->ethaddr);
392
0
  }
393
394
0
  return 0;
395
0
}
396
397
static int eth_register_of_fixup(void)
398
30
{
399
30
  return of_register_fixup(eth_of_fixup, NULL);
400
30
}
401
late_initcall(eth_register_of_fixup);
402
#endif
403
404
extern char *net_server;
405
extern IPaddr_t net_gateway;
406
407
static const char * const eth_mode_names[] = {
408
  [ETH_MODE_DHCP] = "dhcp",
409
  [ETH_MODE_STATIC] = "static",
410
  [ETH_MODE_DISABLED] = "disabled",
411
};
412
413
static int eth_param_set_ip(struct param_d *p, void *priv)
414
0
{
415
0
  struct eth_device *edev = priv;
416
417
0
  net_set_ip(edev, edev->ipaddr);
418
419
0
  return 0;
420
0
}
421
422
static int eth_param_set_gw(struct param_d *p, void *priv)
423
0
{
424
0
  struct eth_device *edev = priv;
425
426
0
  net_set_gateway(edev, net_get_gateway());
427
428
0
  return 0;
429
0
}
430
431
static int eth_param_set_nm(struct param_d *p, void *priv)
432
0
{
433
0
  struct eth_device *edev = priv;
434
435
0
  net_set_netmask(edev, edev->netmask);
436
437
0
  return 0;
438
0
}
439
440
int eth_register(struct eth_device *edev)
441
0
{
442
0
  struct device *dev = &edev->dev;
443
0
  unsigned char ethaddr[ETH_ALEN];
444
0
  int ret, found = 0;
445
446
0
  if (!edev->get_ethaddr) {
447
0
    dev_err(dev, "no get_mac_address found for current eth device\n");
448
0
    return -1;
449
0
  }
450
451
  /* Drivers may name the device, e.g. like its Linux counterpart */
452
0
  if (!dev->name)
453
0
    dev_set_name(dev, "eth");
454
455
0
  if (edev->parent)
456
0
    edev->dev.parent = edev->parent;
457
458
0
  if (eth_has_ethid(edev) && edev->dev.parent &&
459
0
      edev->dev.parent->of_node) {
460
0
    edev->dev.id = of_alias_get_id(edev->dev.parent->of_node,
461
0
                 "ethernet");
462
0
    if (edev->dev.id < 0)
463
0
      edev->dev.id = DEVICE_ID_DYNAMIC;
464
0
  } else {
465
0
    edev->dev.id = DEVICE_ID_DYNAMIC;
466
0
  }
467
468
0
  INIT_LIST_HEAD(&edev->send_queue);
469
470
0
  ret = register_device(&edev->dev);
471
0
  if (ret)
472
0
    return ret;
473
474
0
  slice_init(&edev->slice, dev_name(dev));
475
476
0
  edev->devname = xstrdup(dev_name(&edev->dev));
477
478
0
  dev_add_param_ip(dev, "ipaddr", eth_param_set_ip, NULL, &edev->ipaddr, edev);
479
0
  dev_add_param_string(dev, "serverip", NULL, NULL, &net_server, edev);
480
0
  dev_add_param_ip(dev, "gateway", eth_param_set_gw, NULL, &net_gateway, edev);
481
0
  dev_add_param_ip(dev, "netmask", eth_param_set_nm, NULL, &edev->netmask, edev);
482
0
  dev_add_param_mac(dev, "ethaddr", eth_param_set_ethaddr, NULL,
483
0
      edev->ethaddr, edev);
484
0
  edev->bootarg = xstrdup("");
485
0
  dev_add_param_string(dev, "linux.bootargs", NULL, NULL, &edev->bootarg, NULL);
486
0
  edev->linuxdevname = xstrdup("");
487
0
  dev_add_param_string(dev, "linux.devname", NULL, NULL, &edev->linuxdevname, NULL);
488
0
  dev_add_param_enum(dev, "mode", NULL, NULL, &edev->global_mode,
489
0
          eth_mode_names, ARRAY_SIZE(eth_mode_names),
490
0
          NULL);
491
492
0
  if (edev->init)
493
0
    edev->init(edev);
494
495
0
  class_add_device(&eth_class, &edev->dev);
496
497
0
  ret = eth_get_registered_ethaddr(edev, ethaddr);
498
0
  if (!ret)
499
0
    found = 1;
500
501
0
  if (!found) {
502
0
    ret = edev->get_ethaddr(edev, ethaddr);
503
0
    if (!ret)
504
0
      found = 1;
505
0
  }
506
507
0
  if (found)
508
0
    register_preset_mac_address(edev, ethaddr);
509
510
0
  if (IS_ENABLED(CONFIG_OFDEVICE) && edev->parent &&
511
0
      edev->parent->of_node)
512
0
    edev->nodepath = xstrdup_const(edev->parent->of_node->full_name);
513
514
0
  return 0;
515
0
}
516
517
int eth_open(struct eth_device *edev)
518
0
{
519
0
  int ret;
520
521
0
  if (edev->active)
522
0
    return 0;
523
524
0
  edev->last_link_check = 0;
525
526
0
  ret = edev->open(edev);
527
0
  if (!ret)
528
0
    edev->active = 1;
529
530
0
  return ret;
531
0
}
532
533
void eth_close(struct eth_device *edev)
534
0
{
535
0
  if (!edev->active)
536
0
    return;
537
538
0
  edev->halt(edev);
539
540
0
  edev->active = 0;
541
0
}
542
543
void eth_unregister(struct eth_device *edev)
544
0
{
545
0
  struct eth_q *q, *tmp;
546
547
0
  if (edev->active)
548
0
    edev->halt(edev);
549
550
0
  list_for_each_entry_safe(q, tmp, &edev->send_queue, list) {
551
0
    if (q->edev != edev)
552
0
      continue;
553
554
0
    list_del(&q->list);
555
0
    free(q->data);
556
0
    free(q);
557
0
  }
558
559
0
  if (IS_ENABLED(CONFIG_OFDEVICE))
560
0
    free_const(edev->nodepath);
561
562
0
  free(edev->devname);
563
564
0
  unregister_device(&edev->dev);
565
0
  slice_exit(&edev->slice);
566
0
}
567
568
void led_trigger_network(enum led_trigger trigger)
569
0
{
570
0
  led_trigger(trigger, TRIGGER_FLASH);
571
0
  led_trigger(LED_TRIGGER_NET_TXRX, TRIGGER_FLASH);
572
0
}
573
574
struct eth_device *of_find_eth_device_by_node(struct device_node *np)
575
0
{
576
0
  struct eth_device *edev;
577
578
  /* There may multiple devices with the same device_node, e.g.
579
   * MII bus and Ethernet controller. Thus ignore errors and
580
   * walk the full list of registered network devices below
581
   */
582
0
  (void)of_device_ensure_probed(np);
583
584
0
  for_each_netdev(edev)
585
0
    if (edev->parent->of_node == np)
586
0
      return edev;
587
0
  return NULL;
588
0
}
589
EXPORT_SYMBOL(of_find_eth_device_by_node);
590
591
void eth_open_all(void)
592
0
{
593
0
  struct eth_device *edev;
594
595
  /*
596
   * PHY registration is deferred until an MDIO bus is first used. Flush
597
   * any pending registrations now so the PHY devices are present when
598
   * entering interactive mode, even for buses no netdev connects to.
599
   */
600
0
  mdiobus_flush_deferred_all();
601
602
0
  for_each_netdev(edev) {
603
0
    if (edev->global_mode == ETH_MODE_DISABLED)
604
0
      continue;
605
0
    eth_open(edev);
606
0
  }
607
0
}
608
609
static int populate_ethaddr(void)
610
30
{
611
30
  struct eth_device *edev;
612
30
  int ret;
613
614
30
  for_each_netdev(edev) {
615
0
    bool generated = false;
616
617
0
    if (!edev->parent || is_valid_ether_addr(edev->ethaddr))
618
0
      continue;
619
620
0
    ret = of_get_mac_addr_nvmem(edev->parent->of_node,
621
0
              edev->ethaddr);
622
0
    if (IS_ENABLED(CONFIG_NET_ETHADDR_FROM_MACHINE_ID) && ret) {
623
0
      ret = generate_ether_addr(edev, edev->ethaddr);
624
0
      generated = true;
625
0
    }
626
0
    if (ret)
627
0
      continue;
628
629
0
    if (generated)
630
0
      dev_notice(&edev->dev, "Generated MAC address from unique id: %pM\n",
631
0
           edev->ethaddr);
632
0
    else
633
0
      dev_info(&edev->dev, "Got preset MAC address from NVMEM: %pM\n",
634
0
         edev->ethaddr);
635
0
    eth_set_ethaddr(edev, edev->ethaddr);
636
0
  }
637
638
30
  return 0;
639
30
}
640
postenvironment_initcall(populate_ethaddr);