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
14 #include <linux/errno.h>
16 #include "eth_internal.h"
18 DECLARE_GLOBAL_DATA_PTR;
21 * CPU and board-specific Ethernet initializations. Aliased function
22 * signals caller to move on
24 static int __def_eth_init(bd_t *bis)
28 int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
29 int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
35 } eth_rcv_bufs[PKTBUFSRX];
37 static unsigned int eth_rcv_current, eth_rcv_last;
40 static struct eth_device *eth_devices;
41 struct eth_device *eth_current;
43 void eth_set_current_to_next(void)
45 eth_current = eth_current->next;
48 void eth_set_dev(struct eth_device *dev)
53 struct eth_device *eth_get_dev_by_name(const char *devname)
55 struct eth_device *dev, *target_dev;
57 BUG_ON(devname == NULL);
65 if (strcmp(devname, dev->name) == 0) {
70 } while (dev != eth_devices);
75 struct eth_device *eth_get_dev_by_index(int index)
77 struct eth_device *dev, *target_dev;
85 if (dev->index == index) {
90 } while (dev != eth_devices);
95 int eth_get_dev_index(void)
100 return eth_current->index;
103 static int on_ethaddr(const char *name, const char *value, enum env_op op,
107 struct eth_device *dev;
112 /* look for an index after "eth" */
113 index = simple_strtoul(name + 3, NULL, 10);
117 if (dev->index == index) {
120 case env_op_overwrite:
121 string_to_enetaddr(value, dev->enetaddr);
122 eth_write_hwaddr(dev, "eth", dev->index);
125 memset(dev->enetaddr, 0, ARP_HLEN);
129 } while (dev != eth_devices);
133 U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
135 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
138 unsigned char env_enetaddr[ARP_HLEN];
141 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
143 if (!is_zero_ethaddr(env_enetaddr)) {
144 if (!is_zero_ethaddr(dev->enetaddr) &&
145 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
146 printf("\nWarning: %s MAC addresses don't match:\n",
148 printf("Address in SROM is %pM\n",
150 printf("Address in environment is %pM\n",
154 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
155 } else if (is_valid_ethaddr(dev->enetaddr)) {
156 eth_env_set_enetaddr_by_index(base_name, eth_number,
158 } else if (is_zero_ethaddr(dev->enetaddr)) {
159 #ifdef CONFIG_NET_RANDOM_ETHADDR
160 net_random_ethaddr(dev->enetaddr);
161 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
162 dev->name, eth_number, dev->enetaddr);
164 printf("\nError: %s address not set.\n",
170 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
171 if (!is_valid_ethaddr(dev->enetaddr)) {
172 printf("\nError: %s address %pM illegal value\n",
173 dev->name, dev->enetaddr);
177 ret = dev->write_hwaddr(dev);
179 printf("\nWarning: %s failed to set MAC address\n",
186 int eth_register(struct eth_device *dev)
188 struct eth_device *d;
191 assert(strlen(dev->name) < sizeof(dev->name));
196 eth_current_changed();
198 for (d = eth_devices; d->next != eth_devices; d = d->next)
203 dev->state = ETH_STATE_INIT;
204 dev->next = eth_devices;
205 dev->index = index++;
210 int eth_unregister(struct eth_device *dev)
212 struct eth_device *cur;
218 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
222 /* Device not found */
223 if (cur->next != dev)
226 cur->next = dev->next;
228 if (eth_devices == dev)
229 eth_devices = dev->next == eth_devices ? NULL : dev->next;
231 if (eth_current == dev) {
232 eth_current = eth_devices;
233 eth_current_changed();
239 int eth_initialize(void)
247 * If board-specific initialization exists, call it.
248 * If not, call a CPU-specific one
250 if (board_eth_init != __def_eth_init) {
251 if (board_eth_init(gd->bd) < 0)
252 printf("Board Net Initialization Failed\n");
253 } else if (cpu_eth_init != __def_eth_init) {
254 if (cpu_eth_init(gd->bd) < 0)
255 printf("CPU Net Initialization Failed\n");
257 printf("Net Initialization Skipped\n");
261 puts("No ethernet found.\n");
262 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
264 struct eth_device *dev = eth_devices;
265 char *ethprime = env_get("ethprime");
267 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
272 printf("%s", dev->name);
274 if (ethprime && strcmp(dev->name, ethprime) == 0) {
279 if (strchr(dev->name, ' '))
280 puts("\nWarning: eth device name has a space!"
283 eth_write_hwaddr(dev, "eth", dev->index);
287 } while (dev != eth_devices);
289 eth_current_changed();
297 * mcast_addr: multicast ipaddr from which multicast Mac is made
298 * join: 1=join, 0=leave.
300 int eth_mcast_join(struct in_addr mcast_ip, int join)
302 u8 mcast_mac[ARP_HLEN];
303 if (!eth_current || !eth_current->mcast)
305 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
306 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
307 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
311 return eth_current->mcast(eth_current, mcast_mac, join);
316 struct eth_device *old_current;
319 puts("No ethernet found.\n");
323 old_current = eth_current;
325 debug("Trying %s\n", eth_current->name);
327 if (eth_current->init(eth_current, gd->bd) >= 0) {
328 eth_current->state = ETH_STATE_ACTIVE;
335 } while (old_current != eth_current);
345 eth_current->halt(eth_current);
347 eth_current->state = ETH_STATE_PASSIVE;
350 int eth_is_active(struct eth_device *dev)
352 return dev && dev->state == ETH_STATE_ACTIVE;
355 int eth_send(void *packet, int length)
362 ret = eth_current->send(eth_current, packet, length);
363 #if defined(CONFIG_CMD_PCAP)
365 pcap_post(packet, lengeth, true);
375 return eth_current->recv(eth_current);
379 static void eth_save_packet(void *packet, int length)
384 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
387 if (PKTSIZE < length)
390 for (i = 0; i < length; i++)
391 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
393 eth_rcv_bufs[eth_rcv_last].length = length;
394 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
397 int eth_receive(void *packet, int length)
400 void *pp = push_packet;
403 if (eth_rcv_current == eth_rcv_last) {
404 push_packet = eth_save_packet;
408 if (eth_rcv_current == eth_rcv_last)
412 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
414 for (i = 0; i < length; i++)
415 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
417 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
420 #endif /* CONFIG_API */