1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
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 = simple_strtoul(name + 3, NULL, 10);
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);
168 printf("\nError: %s address not set.\n",
174 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
175 if (!is_valid_ethaddr(dev->enetaddr)) {
176 printf("\nError: %s address %pM illegal value\n",
177 dev->name, dev->enetaddr);
181 ret = dev->write_hwaddr(dev);
183 printf("\nWarning: %s failed to set MAC address\n",
190 int eth_register(struct eth_device *dev)
192 struct eth_device *d;
195 assert(strlen(dev->name) < sizeof(dev->name));
200 eth_current_changed();
202 for (d = eth_devices; d->next != eth_devices; d = d->next)
207 dev->state = ETH_STATE_INIT;
208 dev->next = eth_devices;
209 dev->index = index++;
214 int eth_unregister(struct eth_device *dev)
216 struct eth_device *cur;
222 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
226 /* Device not found */
227 if (cur->next != dev)
230 cur->next = dev->next;
232 if (eth_devices == dev)
233 eth_devices = dev->next == eth_devices ? NULL : dev->next;
235 if (eth_current == dev) {
236 eth_current = eth_devices;
237 eth_current_changed();
243 int eth_initialize(void)
251 * If board-specific initialization exists, call it.
252 * If not, call a CPU-specific one
254 if (board_eth_init != __def_eth_init) {
255 if (board_eth_init(gd->bd) < 0)
256 printf("Board Net Initialization Failed\n");
257 } else if (cpu_eth_init != __def_eth_init) {
258 if (cpu_eth_init(gd->bd) < 0)
259 printf("CPU Net Initialization Failed\n");
261 printf("Net Initialization Skipped\n");
265 log_err("No ethernet found.\n");
266 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
268 struct eth_device *dev = eth_devices;
269 char *ethprime = env_get("ethprime");
271 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
276 printf("%s", dev->name);
278 if (ethprime && strcmp(dev->name, ethprime) == 0) {
283 if (strchr(dev->name, ' '))
284 puts("\nWarning: eth device name has a space!"
287 eth_write_hwaddr(dev, "eth", dev->index);
291 } while (dev != eth_devices);
293 eth_current_changed();
301 * mcast_addr: multicast ipaddr from which multicast Mac is made
302 * join: 1=join, 0=leave.
304 int eth_mcast_join(struct in_addr mcast_ip, int join)
306 u8 mcast_mac[ARP_HLEN];
307 if (!eth_current || !eth_current->mcast)
309 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
310 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
311 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
315 return eth_current->mcast(eth_current, mcast_mac, join);
320 struct eth_device *old_current;
323 log_err("No ethernet found.\n");
327 old_current = eth_current;
329 debug("Trying %s\n", eth_current->name);
331 if (eth_current->init(eth_current, gd->bd) >= 0) {
332 eth_current->state = ETH_STATE_ACTIVE;
339 } while (old_current != eth_current);
349 eth_current->halt(eth_current);
351 eth_current->state = ETH_STATE_PASSIVE;
354 int eth_is_active(struct eth_device *dev)
356 return dev && dev->state == ETH_STATE_ACTIVE;
359 int eth_send(void *packet, int length)
366 ret = eth_current->send(eth_current, packet, length);
367 #if defined(CONFIG_CMD_PCAP)
369 pcap_post(packet, length, true);
379 return eth_current->recv(eth_current);
383 static void eth_save_packet(void *packet, int length)
388 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
391 if (PKTSIZE < length)
394 for (i = 0; i < length; i++)
395 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
397 eth_rcv_bufs[eth_rcv_last].length = length;
398 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
401 int eth_receive(void *packet, int length)
404 void *pp = push_packet;
407 if (eth_rcv_current == eth_rcv_last) {
408 push_packet = eth_save_packet;
412 if (eth_rcv_current == eth_rcv_last)
416 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
418 for (i = 0; i < length; i++)
419 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
421 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
424 #endif /* CONFIG_API */