Blame view

net/eth-uclass.c 12.3 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
d537b7077   Peng Fan   MLK-14862 net: et...
6
7
   * Copyright 2017 NXP
   *
db9391e16   Simon Glass   net: Move driver-...
8
9
10
11
12
13
14
15
16
   */
  
  #include <common.h>
  #include <dm.h>
  #include <environment.h>
  #include <net.h>
  #include <dm/device-internal.h>
  #include <dm/uclass-internal.h>
  #include "eth_internal.h"
a7c45ec4d   Simon Glass   microblaze: Corre...
17
  DECLARE_GLOBAL_DATA_PTR;
db9391e16   Simon Glass   net: Move driver-...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  /**
   * 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;
d537b7077   Peng Fan   MLK-14862 net: et...
42
43
44
45
46
  	int ret;
  
  	ret = uclass_get(UCLASS_ETH, &uc);
  	if (ret)
  		return NULL;
db9391e16   Simon Glass   net: Move driver-...
47

db9391e16   Simon Glass   net: Move driver-...
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
  	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");
d537b7077   Peng Fan   MLK-14862 net: et...
108
  	int ret;
db9391e16   Simon Glass   net: Move driver-...
109
110
111
112
113
114
  
  	/* 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);
  	}
d537b7077   Peng Fan   MLK-14862 net: et...
115
116
117
  	ret = uclass_get(UCLASS_ETH, &uc);
  	if (ret)
  		return NULL;
db9391e16   Simon Glass   net: Move driver-...
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
182
183
184
185
186
187
188
  	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 ...
189
  	struct eth_pdata *pdata;
db9391e16   Simon Glass   net: Move driver-...
190
191
192
193
194
195
196
  	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 ...
197
  		pdata = dev->platdata;
db9391e16   Simon Glass   net: Move driver-...
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
232
233
234
235
236
237
238
239
  		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:
  			eth_parse_enetaddr(value, pdata->enetaddr);
c86ff7fdb   Hannes Schmelzer   net: write enetad...
240
  			eth_write_hwaddr(dev);
db9391e16   Simon Glass   net: Move driver-...
241
242
  			break;
  		case env_op_delete:
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
243
  			memset(pdata->enetaddr, 0, ARP_HLEN);
db9391e16   Simon Glass   net: Move driver-...
244
245
246
247
248
249
250
251
252
  		}
  	}
  
  	return 0;
  }
  U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
  
  int eth_init(void)
  {
00caae6d4   Simon Glass   env: Rename geten...
253
254
  	char *ethact = env_get("ethact");
  	char *ethrotate = env_get("ethrotate");
db9391e16   Simon Glass   net: Move driver-...
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
317
318
319
320
321
322
323
  	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...
324
  	if (!current || !eth_is_active(current))
db9391e16   Simon Glass   net: Move driver-...
325
326
327
328
  		return;
  
  	eth_get_ops(current)->stop(current);
  	priv = current->uclass_priv;
c3211708c   Jean-Jacques Hiblot   net: eth-uclass: ...
329
330
  	if (priv)
  		priv->state = ETH_STATE_PASSIVE;
db9391e16   Simon Glass   net: Move driver-...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  }
  
  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 ...
352
  	if (!eth_is_active(current))
db9391e16   Simon Glass   net: Move driver-...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  		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);
  	}
  	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 ...
375
  	if (!eth_is_active(current))
db9391e16   Simon Glass   net: Move driver-...
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
410
411
412
  		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...
413
  	uclass_first_device_check(UCLASS_ETH, &dev);
db9391e16   Simon Glass   net: Move driver-...
414
415
416
417
418
  	if (!dev) {
  		printf("No ethernet found.
  ");
  		bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
  	} else {
00caae6d4   Simon Glass   env: Rename geten...
419
  		char *ethprime = env_get("ethprime");
db9391e16   Simon Glass   net: Move driver-...
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  		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 {
  			if (num_devices)
  				printf(", ");
  
  			printf("eth%d: %s", dev->seq, dev->name);
  
  			if (ethprime && dev == prime_dev)
  				printf(" [PRIME]");
  
  			eth_write_hwaddr(dev);
3ce430420   Mario Six   net: Initialize a...
442
  			uclass_next_device_check(&dev);
db9391e16   Simon Glass   net: Move driver-...
443
444
445
446
447
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
476
477
478
  			num_devices++;
  		} while (dev);
  
  		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;
  }
  
  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...
479
  	unsigned char env_enetaddr[ARP_HLEN];
db9391e16   Simon Glass   net: Move driver-...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  
  #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-...
496
497
  		if (ops->mcast)
  			ops->mcast += gd->reloc_off;
db9391e16   Simon Glass   net: Move driver-...
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  		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;
  
  	/* 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);
35affd7a2   Simon Glass   env: Rename eth_g...
512
  	eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
db9391e16   Simon Glass   net: Move driver-...
513
514
  	if (!is_zero_ethaddr(env_enetaddr)) {
  		if (!is_zero_ethaddr(pdata->enetaddr) &&
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
515
  		    memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
db9391e16   Simon Glass   net: Move driver-...
516
517
518
519
  			printf("
  Warning: %s MAC addresses don't match:
  ",
  			       dev->name);
26d40b0a1   oliver@schinagl.nl   net: core: cosmet...
520
521
  			printf("Address in ROM is          %pM
  ",
db9391e16   Simon Glass   net: Move driver-...
522
523
524
525
526
527
528
  			       pdata->enetaddr);
  			printf("Address in environment is  %pM
  ",
  			       env_enetaddr);
  		}
  
  		/* Override the ROM MAC address */
a40db6d51   oliver@schinagl.nl   net: cosmetic: Do...
529
  		memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
db9391e16   Simon Glass   net: Move driver-...
530
  	} else if (is_valid_ethaddr(pdata->enetaddr)) {
fd1e959e9   Simon Glass   env: Rename eth_s...
531
  		eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
db9391e16   Simon Glass   net: Move driver-...
532
533
534
535
  		printf("
  Warning: %s using MAC address from ROM
  ",
  		       dev->name);
aa555fe9f   Siva Durga Prasad Paladugu   net: use random e...
536
537
  	} else if (is_zero_ethaddr(pdata->enetaddr) ||
  		   !is_valid_ethaddr(pdata->enetaddr)) {
db9391e16   Simon Glass   net: Move driver-...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
  #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
  	}
  
  	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...
563
  	memset(pdata->enetaddr, 0, ARP_HLEN);
db9391e16   Simon Glass   net: Move driver-...
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
  
  	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,
  };