1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001-2015
5 * Joe Hershberger, National Instruments
16 #include <linux/bug.h>
17 #include <linux/errno.h>
19 #include "eth_internal.h"
21 DECLARE_GLOBAL_DATA_PTR;
24 * CPU and board-specific Ethernet initializations. Aliased function
25 * signals caller to move on
27 static int __def_eth_init(struct bd_info *bis)
31 int cpu_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
32 int board_eth_init(struct bd_info *bis) __attribute__((weak, alias("__def_eth_init")));
38 } eth_rcv_bufs[PKTBUFSRX];
40 static unsigned int eth_rcv_current, eth_rcv_last;
43 static struct eth_device *eth_devices;
44 struct eth_device *eth_current;
46 void eth_set_current_to_next(void)
48 eth_current = eth_current->next;
51 void eth_set_dev(struct eth_device *dev)
56 struct eth_device *eth_get_dev_by_name(const char *devname)
58 struct eth_device *dev, *target_dev;
60 BUG_ON(devname == NULL);
68 if (strcmp(devname, dev->name) == 0) {
73 } while (dev != eth_devices);
78 struct eth_device *eth_get_dev_by_index(int index)
80 struct eth_device *dev, *target_dev;
88 if (dev->index == index) {
93 } while (dev != eth_devices);
98 int eth_get_dev_index(void)
103 return eth_current->index;
106 static int on_ethaddr(const char *name, const char *value, enum env_op op,
110 struct eth_device *dev;
115 /* look for an index after "eth" */
116 index = simple_strtoul(name + 3, NULL, 10);
120 if (dev->index == index) {
123 case env_op_overwrite:
124 string_to_enetaddr(value, dev->enetaddr);
125 eth_write_hwaddr(dev, "eth", dev->index);
128 memset(dev->enetaddr, 0, ARP_HLEN);
132 } while (dev != eth_devices);
136 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
138 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
141 unsigned char env_enetaddr[ARP_HLEN];
144 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
146 if (!is_zero_ethaddr(env_enetaddr)) {
147 if (!is_zero_ethaddr(dev->enetaddr) &&
148 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
149 printf("\nWarning: %s MAC addresses don't match:\n",
151 printf("Address in SROM is %pM\n",
153 printf("Address in environment is %pM\n",
157 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
158 } else if (is_valid_ethaddr(dev->enetaddr)) {
159 eth_env_set_enetaddr_by_index(base_name, eth_number,
161 } else if (is_zero_ethaddr(dev->enetaddr)) {
162 #ifdef CONFIG_NET_RANDOM_ETHADDR
163 net_random_ethaddr(dev->enetaddr);
164 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
165 dev->name, eth_number, dev->enetaddr);
167 printf("\nError: %s address not set.\n",
173 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
174 if (!is_valid_ethaddr(dev->enetaddr)) {
175 printf("\nError: %s address %pM illegal value\n",
176 dev->name, dev->enetaddr);
180 ret = dev->write_hwaddr(dev);
182 printf("\nWarning: %s failed to set MAC address\n",
189 int eth_register(struct eth_device *dev)
191 struct eth_device *d;
194 assert(strlen(dev->name) < sizeof(dev->name));
199 eth_current_changed();
201 for (d = eth_devices; d->next != eth_devices; d = d->next)
206 dev->state = ETH_STATE_INIT;
207 dev->next = eth_devices;
208 dev->index = index++;
213 int eth_unregister(struct eth_device *dev)
215 struct eth_device *cur;
221 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
225 /* Device not found */
226 if (cur->next != dev)
229 cur->next = dev->next;
231 if (eth_devices == dev)
232 eth_devices = dev->next == eth_devices ? NULL : dev->next;
234 if (eth_current == dev) {
235 eth_current = eth_devices;
236 eth_current_changed();
242 int eth_initialize(void)
250 * If board-specific initialization exists, call it.
251 * If not, call a CPU-specific one
253 if (board_eth_init != __def_eth_init) {
254 if (board_eth_init(gd->bd) < 0)
255 printf("Board Net Initialization Failed\n");
256 } else if (cpu_eth_init != __def_eth_init) {
257 if (cpu_eth_init(gd->bd) < 0)
258 printf("CPU Net Initialization Failed\n");
260 printf("Net Initialization Skipped\n");
264 puts("No ethernet found.\n");
265 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
267 struct eth_device *dev = eth_devices;
268 char *ethprime = env_get("ethprime");
270 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
275 printf("%s", dev->name);
277 if (ethprime && strcmp(dev->name, ethprime) == 0) {
282 if (strchr(dev->name, ' '))
283 puts("\nWarning: eth device name has a space!"
286 eth_write_hwaddr(dev, "eth", dev->index);
290 } while (dev != eth_devices);
292 eth_current_changed();
300 * mcast_addr: multicast ipaddr from which multicast Mac is made
301 * join: 1=join, 0=leave.
303 int eth_mcast_join(struct in_addr mcast_ip, int join)
305 u8 mcast_mac[ARP_HLEN];
306 if (!eth_current || !eth_current->mcast)
308 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
309 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
310 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
314 return eth_current->mcast(eth_current, mcast_mac, join);
319 struct eth_device *old_current;
322 puts("No ethernet found.\n");
326 old_current = eth_current;
328 debug("Trying %s\n", eth_current->name);
330 if (eth_current->init(eth_current, gd->bd) >= 0) {
331 eth_current->state = ETH_STATE_ACTIVE;
338 } while (old_current != eth_current);
348 eth_current->halt(eth_current);
350 eth_current->state = ETH_STATE_PASSIVE;
353 int eth_is_active(struct eth_device *dev)
355 return dev && dev->state == ETH_STATE_ACTIVE;
358 int eth_send(void *packet, int length)
365 ret = eth_current->send(eth_current, packet, length);
366 #if defined(CONFIG_CMD_PCAP)
368 pcap_post(packet, lengeth, true);
378 return eth_current->recv(eth_current);
382 static void eth_save_packet(void *packet, int length)
387 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
390 if (PKTSIZE < length)
393 for (i = 0; i < length; i++)
394 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
396 eth_rcv_bufs[eth_rcv_last].length = length;
397 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
400 int eth_receive(void *packet, int length)
403 void *pp = push_packet;
406 if (eth_rcv_current == eth_rcv_last) {
407 push_packet = eth_save_packet;
411 if (eth_rcv_current == eth_rcv_last)
415 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
417 for (i = 0; i < length; i++)
418 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
420 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
423 #endif /* CONFIG_API */