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