1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 National Instruments
6 * Joe Hershberger <joe.hershberger@ni.com>
9 #include <asm/eth-raw-os.h>
15 DECLARE_GLOBAL_DATA_PTR;
18 static struct in_addr arp_ip;
20 static int sb_eth_raw_start(struct udevice *dev)
22 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
23 struct eth_pdata *pdata = dev_get_platdata(dev);
26 debug("eth_sandbox_raw: Start\n");
28 ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
30 env_set("ipaddr", "127.0.0.1");
31 env_set("serverip", "127.0.0.1");
32 net_ip = string_to_ip("127.0.0.1");
33 net_server_ip = net_ip;
38 static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
40 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
42 debug("eth_sandbox_raw: Send packet %d\n", length);
45 struct ethernet_hdr *eth = packet;
47 if (ntohs(eth->et_protlen) == PROT_ARP) {
48 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
51 * localhost works on a higher-level API in Linux than
52 * ARP packets, so fake it
54 arp_ip = net_read_ip(&arp->ar_tpa);
58 packet += ETHER_HDR_SIZE;
59 length -= ETHER_HDR_SIZE;
61 return sandbox_eth_raw_os_send(packet, length, priv);
64 static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
66 struct eth_pdata *pdata = dev_get_platdata(dev);
67 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
72 struct arp_hdr *arp = (void *)net_rx_packets[0] +
76 * Fake an ARP response. The u-boot network stack is sending an
77 * ARP request (to find the MAC address to address the actual
78 * packet to) and requires an ARP response to continue. Since
79 * this is the localhost interface, there is no Etherent level
80 * traffic at all, so there is no way to send an ARP request or
81 * to get a response. For this reason we fake the response to
82 * make the u-boot network stack happy.
84 arp->ar_hrd = htons(ARP_ETHER);
85 arp->ar_pro = htons(PROT_IP);
86 arp->ar_hln = ARP_HLEN;
87 arp->ar_pln = ARP_PLEN;
88 arp->ar_op = htons(ARPOP_REPLY);
89 /* Any non-zero MAC address will work */
90 memset(&arp->ar_sha, 0x01, ARP_HLEN);
91 /* Use whatever IP we were looking for (always 127.0.0.1?) */
92 net_write_ip(&arp->ar_spa, arp_ip);
93 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
94 net_write_ip(&arp->ar_tpa, net_ip);
95 length = ARP_HDR_SIZE;
97 /* If local, the Ethernet header won't be included; skip it */
98 uchar *pktptr = priv->local ?
99 net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
101 retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
104 if (!retval && length) {
106 struct ethernet_hdr *eth = (void *)net_rx_packets[0];
108 /* Fill in enough of the missing Ethernet header */
109 memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN);
110 memset(eth->et_src, 0x01, ARP_HLEN);
111 eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP);
113 length += ETHER_HDR_SIZE;
116 debug("eth_sandbox_raw: received packet %d\n",
118 *packetp = net_rx_packets[0];
124 static void sb_eth_raw_stop(struct udevice *dev)
126 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
128 debug("eth_sandbox_raw: Stop\n");
130 sandbox_eth_raw_os_stop(priv);
133 static int sb_eth_raw_read_rom_hwaddr(struct udevice *dev)
135 struct eth_pdata *pdata = dev_get_platdata(dev);
137 net_random_ethaddr(pdata->enetaddr);
142 static const struct eth_ops sb_eth_raw_ops = {
143 .start = sb_eth_raw_start,
144 .send = sb_eth_raw_send,
145 .recv = sb_eth_raw_recv,
146 .stop = sb_eth_raw_stop,
147 .read_rom_hwaddr = sb_eth_raw_read_rom_hwaddr,
150 static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
152 struct eth_pdata *pdata = dev_get_platdata(dev);
153 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
157 pdata->iobase = dev_read_addr(dev);
159 ifname = dev_read_string(dev, "host-raw-interface");
161 strncpy(priv->host_ifname, ifname, IFNAMSIZ);
162 printf(": Using %s from DT\n", priv->host_ifname);
164 if (dev_read_u32(dev, "host-raw-interface-idx",
165 &priv->host_ifindex) < 0) {
166 priv->host_ifindex = 0;
168 ret = sandbox_eth_raw_os_idx_to_name(priv);
171 printf(": Using interface index %d from DT (%s)\n",
172 priv->host_ifindex, priv->host_ifname);
175 ret = sandbox_eth_raw_os_is_local(priv->host_ifname);
183 static const struct udevice_id sb_eth_raw_ids[] = {
184 { .compatible = "sandbox,eth-raw" },
188 U_BOOT_DRIVER(eth_sandbox_raw) = {
189 .name = "eth_sandbox_raw",
191 .of_match = sb_eth_raw_ids,
192 .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata,
193 .ops = &sb_eth_raw_ops,
194 .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv),
195 .platdata_auto_alloc_size = sizeof(struct eth_pdata),