1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
13 #include <linux/errno.h>
15 #include "eth_internal.h"
17 DECLARE_GLOBAL_DATA_PTR;
20 * CPU and board-specific Ethernet initializations. Aliased function
21 * signals caller to move on
23 static int __def_eth_init(bd_t *bis)
27 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
28 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
34 } eth_rcv_bufs[PKTBUFSRX];
36 static unsigned int eth_rcv_current, eth_rcv_last;
39 static struct eth_device *eth_devices;
40 struct eth_device *eth_current;
42 void eth_set_current_to_next(void)
44 eth_current = eth_current->next;
47 void eth_set_dev(struct eth_device *dev)
52 struct eth_device *eth_get_dev_by_name(const char *devname)
54 struct eth_device *dev, *target_dev;
56 BUG_ON(devname == NULL);
64 if (strcmp(devname, dev->name) == 0) {
69 } while (dev != eth_devices);
74 struct eth_device *eth_get_dev_by_index(int index)
76 struct eth_device *dev, *target_dev;
84 if (dev->index == index) {
89 } while (dev != eth_devices);
94 int eth_get_dev_index(void)
99 return eth_current->index;
102 static int on_ethaddr(const char *name, const char *value, enum env_op op,
106 struct eth_device *dev;
111 /* look for an index after "eth" */
112 index = simple_strtoul(name + 3, NULL, 10);
116 if (dev->index == index) {
119 case env_op_overwrite:
120 string_to_enetaddr(value, dev->enetaddr);
121 eth_write_hwaddr(dev, "eth", dev->index);
124 memset(dev->enetaddr, 0, ARP_HLEN);
128 } while (dev != eth_devices);
132 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
134 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
137 unsigned char env_enetaddr[ARP_HLEN];
140 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
142 if (!is_zero_ethaddr(env_enetaddr)) {
143 if (!is_zero_ethaddr(dev->enetaddr) &&
144 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
145 printf("\nWarning: %s MAC addresses don't match:\n",
147 printf("Address in SROM is %pM\n",
149 printf("Address in environment is %pM\n",
153 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
154 } else if (is_valid_ethaddr(dev->enetaddr)) {
155 eth_env_set_enetaddr_by_index(base_name, eth_number,
157 } else if (is_zero_ethaddr(dev->enetaddr)) {
158 #ifdef CONFIG_NET_RANDOM_ETHADDR
159 net_random_ethaddr(dev->enetaddr);
160 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
161 dev->name, eth_number, dev->enetaddr);
163 printf("\nError: %s address not set.\n",
169 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
170 if (!is_valid_ethaddr(dev->enetaddr)) {
171 printf("\nError: %s address %pM illegal value\n",
172 dev->name, dev->enetaddr);
176 ret = dev->write_hwaddr(dev);
178 printf("\nWarning: %s failed to set MAC address\n",
185 int eth_register(struct eth_device *dev)
187 struct eth_device *d;
190 assert(strlen(dev->name) < sizeof(dev->name));
195 eth_current_changed();
197 for (d = eth_devices; d->next != eth_devices; d = d->next)
202 dev->state = ETH_STATE_INIT;
203 dev->next = eth_devices;
204 dev->index = index++;
209 int eth_unregister(struct eth_device *dev)
211 struct eth_device *cur;
217 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
221 /* Device not found */
222 if (cur->next != dev)
225 cur->next = dev->next;
227 if (eth_devices == dev)
228 eth_devices = dev->next == eth_devices ? NULL : dev->next;
230 if (eth_current == dev) {
231 eth_current = eth_devices;
232 eth_current_changed();
238 int eth_initialize(void)
246 * If board-specific initialization exists, call it.
247 * If not, call a CPU-specific one
249 if (board_eth_init != __def_eth_init) {
250 if (board_eth_init(gd->bd) < 0)
251 printf("Board Net Initialization Failed\n");
252 } else if (cpu_eth_init != __def_eth_init) {
253 if (cpu_eth_init(gd->bd) < 0)
254 printf("CPU Net Initialization Failed\n");
256 printf("Net Initialization Skipped\n");
260 puts("No ethernet found.\n");
261 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
263 struct eth_device *dev = eth_devices;
264 char *ethprime = env_get("ethprime");
266 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
271 printf("%s", dev->name);
273 if (ethprime && strcmp(dev->name, ethprime) == 0) {
278 if (strchr(dev->name, ' '))
279 puts("\nWarning: eth device name has a space!"
282 eth_write_hwaddr(dev, "eth", dev->index);
286 } while (dev != eth_devices);
288 eth_current_changed();
296 * mcast_addr: multicast ipaddr from which multicast Mac is made
297 * join: 1=join, 0=leave.
299 int eth_mcast_join(struct in_addr mcast_ip, int join)
301 u8 mcast_mac[ARP_HLEN];
302 if (!eth_current || !eth_current->mcast)
304 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
305 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
306 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
310 return eth_current->mcast(eth_current, mcast_mac, join);
315 struct eth_device *old_current;
318 puts("No ethernet found.\n");
322 old_current = eth_current;
324 debug("Trying %s\n", eth_current->name);
326 if (eth_current->init(eth_current, gd->bd) >= 0) {
327 eth_current->state = ETH_STATE_ACTIVE;
334 } while (old_current != eth_current);
344 eth_current->halt(eth_current);
346 eth_current->state = ETH_STATE_PASSIVE;
349 int eth_is_active(struct eth_device *dev)
351 return dev && dev->state == ETH_STATE_ACTIVE;
354 int eth_send(void *packet, int length)
361 ret = eth_current->send(eth_current, packet, length);
362 #if defined(CONFIG_CMD_PCAP)
364 pcap_post(packet, lengeth, true);
374 return eth_current->recv(eth_current);
378 static void eth_save_packet(void *packet, int length)
383 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
386 if (PKTSIZE < length)
389 for (i = 0; i < length; i++)
390 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
392 eth_rcv_bufs[eth_rcv_last].length = length;
393 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
396 int eth_receive(void *packet, int length)
399 void *pp = push_packet;
402 if (eth_rcv_current == eth_rcv_last) {
403 push_packet = eth_save_packet;
407 if (eth_rcv_current == eth_rcv_last)
411 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
413 for (i = 0; i < length; i++)
414 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
416 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
419 #endif /* CONFIG_API */