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
15 #include <linux/bug.h>
16 #include <linux/errno.h>
18 #include "eth_internal.h"
20 DECLARE_GLOBAL_DATA_PTR;
23 * CPU and board-specific Ethernet initializations. Aliased function
24 * signals caller to move on
26 static int __def_eth_init(struct bd_info *bis)
30 int cpu_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
31 int board_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
37 } eth_rcv_bufs[PKTBUFSRX];
39 static unsigned int eth_rcv_current, eth_rcv_last;
42 static struct eth_device *eth_devices;
43 struct eth_device *eth_current;
45 void eth_set_current_to_next(void)
47 eth_current = eth_current->next;
50 void eth_set_dev(struct eth_device *dev)
55 struct eth_device *eth_get_dev_by_name(const char *devname)
57 struct eth_device *dev, *target_dev;
59 BUG_ON(devname == NULL);
67 if (strcmp(devname, dev->name) == 0) {
72 } while (dev != eth_devices);
77 struct eth_device *eth_get_dev_by_index(int index)
79 struct eth_device *dev, *target_dev;
87 if (dev->index == index) {
92 } while (dev != eth_devices);
97 int eth_get_dev_index(void)
102 return eth_current->index;
105 static int on_ethaddr(const char *name, const char *value, enum env_op op,
109 struct eth_device *dev;
114 /* look for an index after "eth" */
115 index = simple_strtoul(name + 3, NULL, 10);
119 if (dev->index == index) {
122 case env_op_overwrite:
123 string_to_enetaddr(value, dev->enetaddr);
124 eth_write_hwaddr(dev, "eth", dev->index);
127 memset(dev->enetaddr, 0, ARP_HLEN);
131 } while (dev != eth_devices);
135 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
137 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
140 unsigned char env_enetaddr[ARP_HLEN];
143 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
145 if (!is_zero_ethaddr(env_enetaddr)) {
146 if (!is_zero_ethaddr(dev->enetaddr) &&
147 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
148 printf("\nWarning: %s MAC addresses don't match:\n",
150 printf("Address in SROM is %pM\n",
152 printf("Address in environment is %pM\n",
156 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
157 } else if (is_valid_ethaddr(dev->enetaddr)) {
158 eth_env_set_enetaddr_by_index(base_name, eth_number,
160 } else if (is_zero_ethaddr(dev->enetaddr)) {
161 #ifdef CONFIG_NET_RANDOM_ETHADDR
162 net_random_ethaddr(dev->enetaddr);
163 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
164 dev->name, eth_number, dev->enetaddr);
166 printf("\nError: %s address not set.\n",
172 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
173 if (!is_valid_ethaddr(dev->enetaddr)) {
174 printf("\nError: %s address %pM illegal value\n",
175 dev->name, dev->enetaddr);
179 ret = dev->write_hwaddr(dev);
181 printf("\nWarning: %s failed to set MAC address\n",
188 int eth_register(struct eth_device *dev)
190 struct eth_device *d;
193 assert(strlen(dev->name) < sizeof(dev->name));
198 eth_current_changed();
200 for (d = eth_devices; d->next != eth_devices; d = d->next)
205 dev->state = ETH_STATE_INIT;
206 dev->next = eth_devices;
207 dev->index = index++;
212 int eth_unregister(struct eth_device *dev)
214 struct eth_device *cur;
220 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
224 /* Device not found */
225 if (cur->next != dev)
228 cur->next = dev->next;
230 if (eth_devices == dev)
231 eth_devices = dev->next == eth_devices ? NULL : dev->next;
233 if (eth_current == dev) {
234 eth_current = eth_devices;
235 eth_current_changed();
241 int eth_initialize(void)
249 * If board-specific initialization exists, call it.
250 * If not, call a CPU-specific one
252 if (board_eth_init != __def_eth_init) {
253 if (board_eth_init(gd->bd) < 0)
254 printf("Board Net Initialization Failed\n");
255 } else if (cpu_eth_init != __def_eth_init) {
256 if (cpu_eth_init(gd->bd) < 0)
257 printf("CPU Net Initialization Failed\n");
259 printf("Net Initialization Skipped\n");
263 puts("No ethernet found.\n");
264 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
266 struct eth_device *dev = eth_devices;
267 char *ethprime = env_get("ethprime");
269 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
274 printf("%s", dev->name);
276 if (ethprime && strcmp(dev->name, ethprime) == 0) {
281 if (strchr(dev->name, ' '))
282 puts("\nWarning: eth device name has a space!"
285 eth_write_hwaddr(dev, "eth", dev->index);
289 } while (dev != eth_devices);
291 eth_current_changed();
299 * mcast_addr: multicast ipaddr from which multicast Mac is made
300 * join: 1=join, 0=leave.
302 int eth_mcast_join(struct in_addr mcast_ip, int join)
304 u8 mcast_mac[ARP_HLEN];
305 if (!eth_current || !eth_current->mcast)
307 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
308 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
309 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
313 return eth_current->mcast(eth_current, mcast_mac, join);
318 struct eth_device *old_current;
321 puts("No ethernet found.\n");
325 old_current = eth_current;
327 debug("Trying %s\n", eth_current->name);
329 if (eth_current->init(eth_current, gd->bd) >= 0) {
330 eth_current->state = ETH_STATE_ACTIVE;
337 } while (old_current != eth_current);
347 eth_current->halt(eth_current);
349 eth_current->state = ETH_STATE_PASSIVE;
352 int eth_is_active(struct eth_device *dev)
354 return dev && dev->state == ETH_STATE_ACTIVE;
357 int eth_send(void *packet, int length)
364 ret = eth_current->send(eth_current, packet, length);
365 #if defined(CONFIG_CMD_PCAP)
367 pcap_post(packet, lengeth, true);
377 return eth_current->recv(eth_current);
381 static void eth_save_packet(void *packet, int length)
386 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
389 if (PKTSIZE < length)
392 for (i = 0; i < length; i++)
393 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
395 eth_rcv_bufs[eth_rcv_last].length = length;
396 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
399 int eth_receive(void *packet, int length)
402 void *pp = push_packet;
405 if (eth_rcv_current == eth_rcv_last) {
406 push_packet = eth_save_packet;
410 if (eth_rcv_current == eth_rcv_last)
414 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
416 for (i = 0; i < length; i++)
417 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
419 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
422 #endif /* CONFIG_API */