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
16 #include <asm/global_data.h>
17 #include <linux/bug.h>
18 #include <linux/errno.h>
20 #include "eth_internal.h"
22 DECLARE_GLOBAL_DATA_PTR;
25 * CPU and board-specific Ethernet initializations. Aliased function
26 * signals caller to move on
28 static int __def_eth_init(struct bd_info *bis)
32 int cpu_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
33 int board_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
39 } eth_rcv_bufs[PKTBUFSRX];
41 static unsigned int eth_rcv_current, eth_rcv_last;
44 static struct eth_device *eth_devices;
45 struct eth_device *eth_current;
47 void eth_set_current_to_next(void)
49 eth_current = eth_current->next;
52 void eth_set_dev(struct eth_device *dev)
57 struct eth_device *eth_get_dev_by_name(const char *devname)
59 struct eth_device *dev, *target_dev;
61 BUG_ON(devname == NULL);
69 if (strcmp(devname, dev->name) == 0) {
74 } while (dev != eth_devices);
79 struct eth_device *eth_get_dev_by_index(int index)
81 struct eth_device *dev, *target_dev;
89 if (dev->index == index) {
94 } while (dev != eth_devices);
99 int eth_get_dev_index(void)
104 return eth_current->index;
107 static int on_ethaddr(const char *name, const char *value, enum env_op op,
111 struct eth_device *dev;
116 /* look for an index after "eth" */
117 index = dectoul(name + 3, NULL);
121 if (dev->index == index) {
124 case env_op_overwrite:
125 string_to_enetaddr(value, dev->enetaddr);
126 eth_write_hwaddr(dev, "eth", dev->index);
129 memset(dev->enetaddr, 0, ARP_HLEN);
133 } while (dev != eth_devices);
137 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
139 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
142 unsigned char env_enetaddr[ARP_HLEN];
145 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
147 if (!is_zero_ethaddr(env_enetaddr)) {
148 if (!is_zero_ethaddr(dev->enetaddr) &&
149 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
150 printf("\nWarning: %s MAC addresses don't match:\n",
152 printf("Address in SROM is %pM\n",
154 printf("Address in environment is %pM\n",
158 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
159 } else if (is_valid_ethaddr(dev->enetaddr)) {
160 eth_env_set_enetaddr_by_index(base_name, eth_number,
162 } else if (is_zero_ethaddr(dev->enetaddr)) {
163 #ifdef CONFIG_NET_RANDOM_ETHADDR
164 net_random_ethaddr(dev->enetaddr);
165 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
166 dev->name, eth_number, dev->enetaddr);
167 eth_env_set_enetaddr_by_index("eth", eth_number,
170 printf("\nError: %s address not set.\n",
176 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
177 if (!is_valid_ethaddr(dev->enetaddr)) {
178 printf("\nError: %s address %pM illegal value\n",
179 dev->name, dev->enetaddr);
183 ret = dev->write_hwaddr(dev);
185 printf("\nWarning: %s failed to set MAC address\n",
192 int eth_register(struct eth_device *dev)
194 struct eth_device *d;
197 assert(strlen(dev->name) < sizeof(dev->name));
202 eth_current_changed();
204 for (d = eth_devices; d->next != eth_devices; d = d->next)
209 dev->state = ETH_STATE_INIT;
210 dev->next = eth_devices;
211 dev->index = index++;
216 int eth_unregister(struct eth_device *dev)
218 struct eth_device *cur;
224 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
228 /* Device not found */
229 if (cur->next != dev)
232 cur->next = dev->next;
234 if (eth_devices == dev)
235 eth_devices = dev->next == eth_devices ? NULL : dev->next;
237 if (eth_current == dev) {
238 eth_current = eth_devices;
239 eth_current_changed();
245 int eth_initialize(void)
253 * If board-specific initialization exists, call it.
254 * If not, call a CPU-specific one
256 if (board_eth_init != __def_eth_init) {
257 if (board_eth_init(gd->bd) < 0)
258 printf("Board Net Initialization Failed\n");
259 } else if (cpu_eth_init != __def_eth_init) {
260 if (cpu_eth_init(gd->bd) < 0)
261 printf("CPU Net Initialization Failed\n");
263 printf("Net Initialization Skipped\n");
267 log_err("No ethernet found.\n");
268 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
270 struct eth_device *dev = eth_devices;
271 char *ethprime = env_get("ethprime");
273 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
278 printf("%s", dev->name);
280 if (ethprime && strcmp(dev->name, ethprime) == 0) {
285 if (strchr(dev->name, ' '))
286 puts("\nWarning: eth device name has a space!"
289 eth_write_hwaddr(dev, "eth", dev->index);
293 } while (dev != eth_devices);
295 eth_current_changed();
303 * mcast_addr: multicast ipaddr from which multicast Mac is made
304 * join: 1=join, 0=leave.
306 int eth_mcast_join(struct in_addr mcast_ip, int join)
308 u8 mcast_mac[ARP_HLEN];
309 if (!eth_current || !eth_current->mcast)
311 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
312 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
313 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
317 return eth_current->mcast(eth_current, mcast_mac, join);
322 struct eth_device *old_current;
325 log_err("No ethernet found.\n");
329 old_current = eth_current;
331 debug("Trying %s\n", eth_current->name);
333 if (eth_current->init(eth_current, gd->bd) >= 0) {
334 eth_current->state = ETH_STATE_ACTIVE;
341 } while (old_current != eth_current);
351 eth_current->halt(eth_current);
353 eth_current->state = ETH_STATE_PASSIVE;
356 int eth_is_active(struct eth_device *dev)
358 return dev && dev->state == ETH_STATE_ACTIVE;
361 int eth_send(void *packet, int length)
368 ret = eth_current->send(eth_current, packet, length);
369 #if defined(CONFIG_CMD_PCAP)
371 pcap_post(packet, length, true);
381 return eth_current->recv(eth_current);
385 static void eth_save_packet(void *packet, int length)
390 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
393 if (PKTSIZE < length)
396 for (i = 0; i < length; i++)
397 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
399 eth_rcv_bufs[eth_rcv_last].length = length;
400 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
403 int eth_receive(void *packet, int length)
406 void *pp = push_packet;
409 if (eth_rcv_current == eth_rcv_last) {
410 push_packet = eth_save_packet;
414 if (eth_rcv_current == eth_rcv_last)
418 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
420 for (i = 0; i < length; i++)
421 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
423 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
426 #endif /* CONFIG_API */