Blame view

net/eth-uclass.c 13 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
db9391e16   Simon Glass   net: Move driver-...
2
3
4
5
  /*
   * (C) Copyright 2001-2015
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   * Joe Hershberger, National Instruments
db9391e16   Simon Glass   net: Move driver-...
6
7
8
9
   */
  
  #include <common.h>
  #include <dm.h>
9fb625ce0   Simon Glass   env: Move env_set...
10
  #include <env.h>
db9391e16   Simon Glass   net: Move driver-...
11
12
13
  #include <net.h>
  #include <dm/device-internal.h>
  #include <dm/uclass-internal.h>
3eaac6307   Ramon Fried   net: introduce pa...
14
  #include <net/pcap.h>
db9391e16   Simon Glass   net: Move driver-...
15
  #include "eth_internal.h"
a7c45ec4d   Simon Glass   microblaze: Corre...
16
  DECLARE_GLOBAL_DATA_PTR;
db9391e16   Simon Glass   net: Move driver-...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  /**
   * struct eth_device_priv - private structure for each Ethernet device
   *
   * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
   */
  struct eth_device_priv {
  	enum eth_state_t state;
  };
  
  /**
   * struct eth_uclass_priv - The structure attached to the uclass itself
   *
   * @current: The Ethernet device that the network functions are using
   */
  struct eth_uclass_priv {
  	struct udevice *current;
  };
  
  /* eth_errno - This stores the most recent failure code from DM functions */
  static int eth_errno;
  
  static struct eth_uclass_priv *eth_get_uclass_priv(void)
  {
  	struct uclass *uc;
  
  	uclass_get(UCLASS_ETH, &uc);
  	assert(uc);
  	return uc->priv;
  }
  
  void eth_set_current_to_next(void)
  {
  	struct eth_uclass_priv *uc_priv;
  
  	uc_priv = eth_get_uclass_priv();
  	if (uc_priv->current)
  		uclass_next_device(&uc_priv->current);
  	if (!uc_priv->current)
  		uclass_first_device(UCLASS_ETH, &uc_priv->current);
  }
  
  /*
   * Typically this will simply return the active device.
   * In the case where the most recent active device was unset, this will attempt
   * to return the first device. If that device doesn't exist or fails to probe,
   * this function will return NULL.
   */
  struct udevice *eth_get_dev(void)
  {
  	struct eth_uclass_priv *uc_priv;
  
  	uc_priv = eth_get_uclass_priv();
  	if (!uc_priv->current)
  		eth_errno = uclass_first_device(UCLASS_ETH,
  				    &uc_priv->current);
  	return uc_priv->current;
  }
  
  /*
   * Typically this will just store a device pointer.
   * In case it was not probed, we will attempt to do so.
   * dev may be NULL to unset the active device.
   */
  void eth_set_dev(struct udevice *dev)
  {
  	if (dev && !device_active(dev)) {
  		eth_errno = device_probe(dev);
  		if (eth_errno)
  			dev = NULL;
  	}
  
  	eth_get_uclass_priv()->current = dev;
  }
  
  /*
   * Find the udevice that either has the name passed in as devname or has an
   * alias named devname.
   */
  struct udevice *eth_get_dev_by_name(const char *devname)
  {
  	int seq = -1;
  	char *endp = NULL;
  	const char *startp = NULL;
  	struct udevice *it;
  	struct uclass *uc;
  	int len = strlen("eth");
  
  	/* Must be longer than 3 to be an alias */
  	if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
  		startp = devname + len;
  		seq = simple_strtoul(startp, &endp, 10);
  	}
  
  	uclass_get(UCLASS_ETH, &uc);
  	uclass_foreach_dev(it, uc) {
  		/*
  		 * We need the seq to be valid, so try to probe it.
  		 * If the probe fails, the seq will not match since it will be
  		 * -1 instead of what we are looking for.
  		 * We don't care about errors from probe here. Either they won't
  		 * match an alias or it will match a literal name and we'll pick
  		 * up the error when we try to probe again in eth_set_dev().
  		 */
  		if (device_probe(it))
  			continue;
  		/* Check for the name or the sequence number to match */
  		if (strcmp(it->name, devname) == 0 ||
  		    (endp > startp && it->seq == seq))
  			return it;
  	}
  
  	return NULL;
  }
  
  unsigned char *eth_get_ethaddr(void)
  {
  	struct eth_pdata *pdata;
  
  	if (eth_get_dev()) {
  		pdata = eth_get_dev()->platdata;
  		return pdata->enetaddr;
  	}
  
  	return NULL;
  }
  
  /* Set active state without calling start on the driver */
  int eth_init_state_only(void)
  {
  	struct udevice *current;
  	struct eth_device_priv *priv;
  
  	current = eth_get_dev();
  	if (!current || !device_active(current))
  		return -EINVAL;
  
  	priv = current->uclass_priv;
  	priv->state = ETH_STATE_ACTIVE;
  
  	return 0;
  }
  
  /* Set passive state without calling stop on the driver */
  void eth_halt_state_only(void)
  {
  	struct udevice *current;
  	struct eth_device_priv *priv;
  
  	current = eth_get_dev();
  	if (!current || !device_active(current))
  		return;
  
  	priv = current->uclass_priv;
  	priv->state = ETH_STATE_PASSIVE;
  }
  
  int eth_get_dev_index(void)
  {
  	if (eth_get_dev())
  		return eth_get_dev()->seq;
  	return -1;
  }
  
  static int eth_write_hwaddr(struct udevice *dev)
  {
c08248d60   xypron.glpk@gmx.de   net: core: avoid ...
182
  	struct eth_pdata *pdata;
db9391e16   Simon Glass   net: Move driver-...
183
184
185
186
187
188
189
  	int ret = 0;
  
  	if (!dev || !device_active(dev))
  		return -EINVAL;
  
  	/* seq is valid since the device is active */
  	if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
c08248d60   xypron.glpk@gmx.de   net: core: avoid ...
190
  		pdata = dev->platdata;
db9391e16   Simon Glass   net: Move driver-...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  		if (!is_valid_ethaddr(pdata->enetaddr)) {
  			printf("
  Error: %s address %pM illegal value
  ",
  			       dev->name, pdata->enetaddr);
  			return -EINVAL;
  		}
  
  		/*
  		 * Drivers are allowed to decide not to implement this at
  		 * run-time. E.g. Some devices may use it and some may not.
  		 */
  		ret = eth_get_ops(dev)->write_hwaddr(dev);
  		if (ret == -ENOSYS)
  			ret = 0;
  		if (ret)
  			printf("
  Warning: %s failed to set MAC address
  ",
  			       dev->name);
  	}
  
  	return ret;
  }
  
  static int on_ethaddr(const char *name, const char *value, enum env_op op,
  	int flags)
  {
  	int index;
  	int retval;
  	struct udevice *dev;
  
  	/* look for an index after "eth" */
  	index = simple_strtoul(name + 3, NULL, 10);
  
  	retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
  	if (!retval) {
  		struct eth_pdata *pdata = dev->platdata;
  		switch (op) {
  		case env_op_create:
  		case env_op_overwrite:
fb8977c5b   Joe Hershberger   net: Always build...
232
  			string_to_enetaddr(value, pdata->enetaddr);
c86ff7fdb   Hannes Schmelzer   net: write enetad...
233
  			eth_write_hwaddr(dev);
db9391e16   Simon Glass   net: Move driver-...
234
235
  			break;
  		case env_op_delete:
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
236
  			memset(pdata->enetaddr, 0, ARP_HLEN);
db9391e16   Simon Glass   net: Move driver-...
237
238
239
240
241
242
243
244
245
  		}
  	}
  
  	return 0;
  }
  U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
  
  int eth_init(void)
  {
00caae6d4   Simon Glass   env: Rename geten...
246
247
  	char *ethact = env_get("ethact");
  	char *ethrotate = env_get("ethrotate");
db9391e16   Simon Glass   net: Move driver-...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  	struct udevice *current = NULL;
  	struct udevice *old_current;
  	int ret = -ENODEV;
  
  	/*
  	 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
  	 * is already set to an ethernet device, we should stick to 'ethact'.
  	 */
  	if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
  		if (ethact) {
  			current = eth_get_dev_by_name(ethact);
  			if (!current)
  				return -EINVAL;
  		}
  	}
  
  	if (!current) {
  		current = eth_get_dev();
  		if (!current) {
  			printf("No ethernet found.
  ");
  			return -ENODEV;
  		}
  	}
  
  	old_current = current;
  	do {
  		if (current) {
  			debug("Trying %s
  ", current->name);
  
  			if (device_active(current)) {
  				ret = eth_get_ops(current)->start(current);
  				if (ret >= 0) {
  					struct eth_device_priv *priv =
  						current->uclass_priv;
  
  					priv->state = ETH_STATE_ACTIVE;
  					return 0;
  				}
  			} else {
  				ret = eth_errno;
  			}
  
  			debug("FAIL
  ");
  		} else {
  			debug("PROBE FAIL
  ");
  		}
  
  		/*
  		 * If ethrotate is enabled, this will change "current",
  		 * otherwise we will drop out of this while loop immediately
  		 */
  		eth_try_another(0);
  		/* This will ensure the new "current" attempted to probe */
  		current = eth_get_dev();
  	} while (old_current != current);
  
  	return ret;
  }
  
  void eth_halt(void)
  {
  	struct udevice *current;
  	struct eth_device_priv *priv;
  
  	current = eth_get_dev();
68acb51f4   Joe Hershberger   net: Only call ha...
317
  	if (!current || !eth_is_active(current))
db9391e16   Simon Glass   net: Move driver-...
318
319
320
321
  		return;
  
  	eth_get_ops(current)->stop(current);
  	priv = current->uclass_priv;
c3211708c   Jean-Jacques Hiblot   net: eth-uclass: ...
322
323
  	if (priv)
  		priv->state = ETH_STATE_PASSIVE;
db9391e16   Simon Glass   net: Move driver-...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  }
  
  int eth_is_active(struct udevice *dev)
  {
  	struct eth_device_priv *priv;
  
  	if (!dev || !device_active(dev))
  		return 0;
  
  	priv = dev_get_uclass_priv(dev);
  	return priv->state == ETH_STATE_ACTIVE;
  }
  
  int eth_send(void *packet, int length)
  {
  	struct udevice *current;
  	int ret;
  
  	current = eth_get_dev();
  	if (!current)
  		return -ENODEV;
a532e2f2e   Alexander Graf   net: Only access ...
345
  	if (!eth_is_active(current))
db9391e16   Simon Glass   net: Move driver-...
346
347
348
349
350
351
352
353
  		return -EINVAL;
  
  	ret = eth_get_ops(current)->send(current, packet, length);
  	if (ret < 0) {
  		/* We cannot completely return the error at present */
  		debug("%s: send() returned error %d
  ", __func__, ret);
  	}
3eaac6307   Ramon Fried   net: introduce pa...
354
355
356
357
  #if defined(CONFIG_CMD_PCAP)
  	if (ret >= 0)
  		pcap_post(packet, length, true);
  #endif
db9391e16   Simon Glass   net: Move driver-...
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  	return ret;
  }
  
  int eth_rx(void)
  {
  	struct udevice *current;
  	uchar *packet;
  	int flags;
  	int ret;
  	int i;
  
  	current = eth_get_dev();
  	if (!current)
  		return -ENODEV;
a532e2f2e   Alexander Graf   net: Only access ...
372
  	if (!eth_is_active(current))
db9391e16   Simon Glass   net: Move driver-...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  		return -EINVAL;
  
  	/* Process up to 32 packets at one time */
  	flags = ETH_RECV_CHECK_DEVICE;
  	for (i = 0; i < 32; i++) {
  		ret = eth_get_ops(current)->recv(current, flags, &packet);
  		flags = 0;
  		if (ret > 0)
  			net_process_received_packet(packet, ret);
  		if (ret >= 0 && eth_get_ops(current)->free_pkt)
  			eth_get_ops(current)->free_pkt(current, packet, ret);
  		if (ret <= 0)
  			break;
  	}
  	if (ret == -EAGAIN)
  		ret = 0;
  	if (ret < 0) {
  		/* We cannot completely return the error at present */
  		debug("%s: recv() returned error %d
  ", __func__, ret);
  	}
  	return ret;
  }
  
  int eth_initialize(void)
  {
  	int num_devices = 0;
  	struct udevice *dev;
  
  	eth_common_init();
  
  	/*
  	 * Devices need to write the hwaddr even if not started so that Linux
  	 * will have access to the hwaddr that u-boot stored for the device.
  	 * This is accomplished by attempting to probe each device and calling
  	 * their write_hwaddr() operation.
  	 */
3ce430420   Mario Six   net: Initialize a...
410
  	uclass_first_device_check(UCLASS_ETH, &dev);
db9391e16   Simon Glass   net: Move driver-...
411
412
413
414
415
  	if (!dev) {
  		printf("No ethernet found.
  ");
  		bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  	} else {
00caae6d4   Simon Glass   env: Rename geten...
416
  		char *ethprime = env_get("ethprime");
db9391e16   Simon Glass   net: Move driver-...
417
418
419
420
421
422
423
424
425
426
427
428
429
  		struct udevice *prime_dev = NULL;
  
  		if (ethprime)
  			prime_dev = eth_get_dev_by_name(ethprime);
  		if (prime_dev) {
  			eth_set_dev(prime_dev);
  			eth_current_changed();
  		} else {
  			eth_set_dev(NULL);
  		}
  
  		bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
  		do {
19820db0b   Michael Walle   net: eth-uclass: ...
430
431
432
  			if (dev->seq != -1) {
  				if (num_devices)
  					printf(", ");
db9391e16   Simon Glass   net: Move driver-...
433

19820db0b   Michael Walle   net: eth-uclass: ...
434
  				printf("eth%d: %s", dev->seq, dev->name);
db9391e16   Simon Glass   net: Move driver-...
435

19820db0b   Michael Walle   net: eth-uclass: ...
436
437
438
  				if (ethprime && dev == prime_dev)
  					printf(" [PRIME]");
  			}
db9391e16   Simon Glass   net: Move driver-...
439
440
  
  			eth_write_hwaddr(dev);
19820db0b   Michael Walle   net: eth-uclass: ...
441
442
  			if (dev->seq != -1)
  				num_devices++;
3ce430420   Mario Six   net: Initialize a...
443
  			uclass_next_device_check(&dev);
db9391e16   Simon Glass   net: Move driver-...
444
  		} while (dev);
19820db0b   Michael Walle   net: eth-uclass: ...
445
446
447
  		if (!num_devices)
  			printf("No ethernet found.
  ");
db9391e16   Simon Glass   net: Move driver-...
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  		putc('
  ');
  	}
  
  	return num_devices;
  }
  
  static int eth_post_bind(struct udevice *dev)
  {
  	if (strchr(dev->name, ' ')) {
  		printf("
  Error: eth device name \"%s\" has a space!
  ",
  		       dev->name);
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int eth_pre_unbind(struct udevice *dev)
  {
  	/* Don't hang onto a pointer that is going away */
  	if (dev == eth_get_uclass_priv()->current)
  		eth_set_dev(NULL);
  
  	return 0;
  }
379af67ab   Thierry Reding   net: eth-uclass: ...
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
  static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
  {
  #if IS_ENABLED(CONFIG_OF_CONTROL)
  	const uint8_t *p;
  
  	p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
  	if (!p)
  		p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
  
  	if (!p)
  		return false;
  
  	memcpy(mac, p, ARP_HLEN);
  
  	return true;
  #else
  	return false;
  #endif
  }
db9391e16   Simon Glass   net: Move driver-...
495
496
497
498
  static int eth_post_probe(struct udevice *dev)
  {
  	struct eth_device_priv *priv = dev->uclass_priv;
  	struct eth_pdata *pdata = dev->platdata;
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
499
  	unsigned char env_enetaddr[ARP_HLEN];
db9391e16   Simon Glass   net: Move driver-...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  
  #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  	struct eth_ops *ops = eth_get_ops(dev);
  	static int reloc_done;
  
  	if (!reloc_done) {
  		if (ops->start)
  			ops->start += gd->reloc_off;
  		if (ops->send)
  			ops->send += gd->reloc_off;
  		if (ops->recv)
  			ops->recv += gd->reloc_off;
  		if (ops->free_pkt)
  			ops->free_pkt += gd->reloc_off;
  		if (ops->stop)
  			ops->stop += gd->reloc_off;
db9391e16   Simon Glass   net: Move driver-...
516
517
  		if (ops->mcast)
  			ops->mcast += gd->reloc_off;
db9391e16   Simon Glass   net: Move driver-...
518
519
520
521
522
523
524
525
526
527
  		if (ops->write_hwaddr)
  			ops->write_hwaddr += gd->reloc_off;
  		if (ops->read_rom_hwaddr)
  			ops->read_rom_hwaddr += gd->reloc_off;
  
  		reloc_done++;
  	}
  #endif
  
  	priv->state = ETH_STATE_INIT;
379af67ab   Thierry Reding   net: eth-uclass: ...
528
529
530
531
532
533
534
  	/* Check if the device has a valid MAC address in device tree */
  	if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
  	    !is_valid_ethaddr(pdata->enetaddr)) {
  		/* Check if the device has a MAC address in ROM */
  		if (eth_get_ops(dev)->read_rom_hwaddr)
  			eth_get_ops(dev)->read_rom_hwaddr(dev);
  	}
db9391e16   Simon Glass   net: Move driver-...
535

35affd7a2   Simon Glass   env: Rename eth_g...
536
  	eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
db9391e16   Simon Glass   net: Move driver-...
537
538
  	if (!is_zero_ethaddr(env_enetaddr)) {
  		if (!is_zero_ethaddr(pdata->enetaddr) &&
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
539
  		    memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
db9391e16   Simon Glass   net: Move driver-...
540
541
542
543
  			printf("
  Warning: %s MAC addresses don't match:
  ",
  			       dev->name);
26d40b0a1   oliver@schinagl.nl   net: core: cosmet...
544
545
  			printf("Address in ROM is          %pM
  ",
db9391e16   Simon Glass   net: Move driver-...
546
547
548
549
550
551
552
  			       pdata->enetaddr);
  			printf("Address in environment is  %pM
  ",
  			       env_enetaddr);
  		}
  
  		/* Override the ROM MAC address */
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
553
  		memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
db9391e16   Simon Glass   net: Move driver-...
554
  	} else if (is_valid_ethaddr(pdata->enetaddr)) {
fd1e959e9   Simon Glass   env: Rename eth_s...
555
  		eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
db9391e16   Simon Glass   net: Move driver-...
556
557
558
559
  		printf("
  Warning: %s using MAC address from ROM
  ",
  		       dev->name);
aa555fe9f   Siva Durga Prasad Paladugu   net: use random e...
560
561
  	} else if (is_zero_ethaddr(pdata->enetaddr) ||
  		   !is_valid_ethaddr(pdata->enetaddr)) {
db9391e16   Simon Glass   net: Move driver-...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  #ifdef CONFIG_NET_RANDOM_ETHADDR
  		net_random_ethaddr(pdata->enetaddr);
  		printf("
  Warning: %s (eth%d) using random MAC address - %pM
  ",
  		       dev->name, dev->seq, pdata->enetaddr);
  #else
  		printf("
  Error: %s address not set.
  ",
  		       dev->name);
  		return -EINVAL;
  #endif
  	}
b743bbd2e   Thierry Reding   net: eth-uclass: ...
576
  	eth_write_hwaddr(dev);
db9391e16   Simon Glass   net: Move driver-...
577
578
579
580
581
582
583
584
585
586
  	return 0;
  }
  
  static int eth_pre_remove(struct udevice *dev)
  {
  	struct eth_pdata *pdata = dev->platdata;
  
  	eth_get_ops(dev)->stop(dev);
  
  	/* clear the MAC address */
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
587
  	memset(pdata->enetaddr, 0, ARP_HLEN);
db9391e16   Simon Glass   net: Move driver-...
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
  
  	return 0;
  }
  
  UCLASS_DRIVER(eth) = {
  	.name		= "eth",
  	.id		= UCLASS_ETH,
  	.post_bind	= eth_post_bind,
  	.pre_unbind	= eth_pre_unbind,
  	.post_probe	= eth_post_probe,
  	.pre_remove	= eth_pre_remove,
  	.priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
  	.per_device_auto_alloc_size = sizeof(struct eth_device_priv),
  	.flags		= DM_UC_FLAG_SEQ_ALIAS,
  };