blob: ce66ff781ffe833a630e7e8d51f07d1dc9e704b2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershberger586cbd12015-03-22 17:09:21 -05002/*
3 * Copyright (c) 2015 National Instruments
4 *
5 * (C) Copyright 2015
6 * Joe Hershberger <joe.hershberger@ni.com>
Joe Hershberger586cbd12015-03-22 17:09:21 -05007 */
8
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Joe Hershberger586cbd12015-03-22 17:09:21 -050010#include <asm/eth-raw-os.h>
11#include <common.h>
12#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Joe Hershberger586cbd12015-03-22 17:09:21 -050014#include <malloc.h>
15#include <net.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Joe Hershberger586cbd12015-03-22 17:09:21 -050017
18DECLARE_GLOBAL_DATA_PTR;
19
Joe Hershbergera8921922015-03-22 17:09:23 -050020static int reply_arp;
Joe Hershberger5874dec2015-04-08 01:41:01 -050021static struct in_addr arp_ip;
Joe Hershberger586cbd12015-03-22 17:09:21 -050022
23static int sb_eth_raw_start(struct udevice *dev)
24{
25 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070026 struct eth_pdata *pdata = dev_get_plat(dev);
Joe Hershbergere5691402018-07-02 14:47:50 -050027 int ret;
Joe Hershberger586cbd12015-03-22 17:09:21 -050028
29 debug("eth_sandbox_raw: Start\n");
30
Joe Hershbergere5691402018-07-02 14:47:50 -050031 ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
32 if (priv->local) {
Simon Glass6a38e412017-08-03 12:22:09 -060033 env_set("ipaddr", "127.0.0.1");
34 env_set("serverip", "127.0.0.1");
Joe Hershberger1b312cb2018-07-02 14:47:51 -050035 net_ip = string_to_ip("127.0.0.1");
36 net_server_ip = net_ip;
Joe Hershbergera8921922015-03-22 17:09:23 -050037 }
Joe Hershbergere5691402018-07-02 14:47:50 -050038 return ret;
Joe Hershberger586cbd12015-03-22 17:09:21 -050039}
40
41static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
42{
43 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
44
45 debug("eth_sandbox_raw: Send packet %d\n", length);
46
Joe Hershbergera8921922015-03-22 17:09:23 -050047 if (priv->local) {
48 struct ethernet_hdr *eth = packet;
49
50 if (ntohs(eth->et_protlen) == PROT_ARP) {
51 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
52
53 /**
54 * localhost works on a higher-level API in Linux than
55 * ARP packets, so fake it
56 */
Joe Hershberger5874dec2015-04-08 01:41:01 -050057 arp_ip = net_read_ip(&arp->ar_tpa);
Joe Hershbergera8921922015-03-22 17:09:23 -050058 reply_arp = 1;
59 return 0;
60 }
61 packet += ETHER_HDR_SIZE;
62 length -= ETHER_HDR_SIZE;
63 }
Joe Hershberger586cbd12015-03-22 17:09:21 -050064 return sandbox_eth_raw_os_send(packet, length, priv);
65}
66
Simon Glassdc6eda32015-07-06 16:47:49 -060067static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
Joe Hershberger586cbd12015-03-22 17:09:21 -050068{
Simon Glassfa20e932020-12-03 16:55:20 -070069 struct eth_pdata *pdata = dev_get_plat(dev);
Joe Hershberger586cbd12015-03-22 17:09:21 -050070 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
Joe Hershbergera8921922015-03-22 17:09:23 -050071 int retval = 0;
Joe Hershberger586cbd12015-03-22 17:09:21 -050072 int length;
73
Joe Hershbergera8921922015-03-22 17:09:23 -050074 if (reply_arp) {
75 struct arp_hdr *arp = (void *)net_rx_packets[0] +
76 ETHER_HDR_SIZE;
77
78 /*
79 * Fake an ARP response. The u-boot network stack is sending an
80 * ARP request (to find the MAC address to address the actual
81 * packet to) and requires an ARP response to continue. Since
82 * this is the localhost interface, there is no Etherent level
83 * traffic at all, so there is no way to send an ARP request or
84 * to get a response. For this reason we fake the response to
85 * make the u-boot network stack happy.
86 */
87 arp->ar_hrd = htons(ARP_ETHER);
88 arp->ar_pro = htons(PROT_IP);
89 arp->ar_hln = ARP_HLEN;
90 arp->ar_pln = ARP_PLEN;
91 arp->ar_op = htons(ARPOP_REPLY);
92 /* Any non-zero MAC address will work */
93 memset(&arp->ar_sha, 0x01, ARP_HLEN);
94 /* Use whatever IP we were looking for (always 127.0.0.1?) */
Joe Hershberger5874dec2015-04-08 01:41:01 -050095 net_write_ip(&arp->ar_spa, arp_ip);
Joe Hershbergera8921922015-03-22 17:09:23 -050096 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
Joe Hershberger5874dec2015-04-08 01:41:01 -050097 net_write_ip(&arp->ar_tpa, net_ip);
Joe Hershbergera8921922015-03-22 17:09:23 -050098 length = ARP_HDR_SIZE;
99 } else {
100 /* If local, the Ethernet header won't be included; skip it */
101 uchar *pktptr = priv->local ?
102 net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
103
104 retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
105 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500106
107 if (!retval && length) {
Joe Hershbergera8921922015-03-22 17:09:23 -0500108 if (priv->local) {
109 struct ethernet_hdr *eth = (void *)net_rx_packets[0];
110
111 /* Fill in enough of the missing Ethernet header */
112 memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN);
113 memset(eth->et_src, 0x01, ARP_HLEN);
114 eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP);
115 reply_arp = 0;
116 length += ETHER_HDR_SIZE;
117 }
118
Joe Hershberger586cbd12015-03-22 17:09:21 -0500119 debug("eth_sandbox_raw: received packet %d\n",
120 length);
121 *packetp = net_rx_packets[0];
122 return length;
123 }
124 return retval;
125}
126
127static void sb_eth_raw_stop(struct udevice *dev)
128{
129 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
130
131 debug("eth_sandbox_raw: Stop\n");
132
133 sandbox_eth_raw_os_stop(priv);
134}
135
Joe Hershbergerf6258b82018-07-02 14:47:53 -0500136static int sb_eth_raw_read_rom_hwaddr(struct udevice *dev)
137{
Simon Glassfa20e932020-12-03 16:55:20 -0700138 struct eth_pdata *pdata = dev_get_plat(dev);
Joe Hershbergerf6258b82018-07-02 14:47:53 -0500139
140 net_random_ethaddr(pdata->enetaddr);
141
142 return 0;
143}
144
Joe Hershberger586cbd12015-03-22 17:09:21 -0500145static const struct eth_ops sb_eth_raw_ops = {
146 .start = sb_eth_raw_start,
147 .send = sb_eth_raw_send,
148 .recv = sb_eth_raw_recv,
149 .stop = sb_eth_raw_stop,
Joe Hershbergerf6258b82018-07-02 14:47:53 -0500150 .read_rom_hwaddr = sb_eth_raw_read_rom_hwaddr,
Joe Hershberger586cbd12015-03-22 17:09:21 -0500151};
152
Simon Glassaad29ae2020-12-03 16:55:21 -0700153static int sb_eth_raw_of_to_plat(struct udevice *dev)
Joe Hershberger586cbd12015-03-22 17:09:21 -0500154{
Simon Glassfa20e932020-12-03 16:55:20 -0700155 struct eth_pdata *pdata = dev_get_plat(dev);
Joe Hershbergere5691402018-07-02 14:47:50 -0500156 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
157 const char *ifname;
Joe Hershberger144e14b2018-07-02 14:47:52 -0500158 int ret;
Joe Hershbergere5691402018-07-02 14:47:50 -0500159
160 pdata->iobase = dev_read_addr(dev);
161
162 ifname = dev_read_string(dev, "host-raw-interface");
163 if (ifname) {
164 strncpy(priv->host_ifname, ifname, IFNAMSIZ);
165 printf(": Using %s from DT\n", priv->host_ifname);
Joe Hershbergere5691402018-07-02 14:47:50 -0500166 }
Joe Hershberger144e14b2018-07-02 14:47:52 -0500167 if (dev_read_u32(dev, "host-raw-interface-idx",
168 &priv->host_ifindex) < 0) {
169 priv->host_ifindex = 0;
170 } else {
171 ret = sandbox_eth_raw_os_idx_to_name(priv);
172 if (ret < 0)
173 return ret;
174 printf(": Using interface index %d from DT (%s)\n",
175 priv->host_ifindex, priv->host_ifname);
176 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500177
Simon Glass1c1bf9f2019-01-07 16:44:22 -0700178 ret = sandbox_eth_raw_os_is_local(priv->host_ifname);
179 if (ret < 0)
180 return ret;
181 priv->local = ret;
Joe Hershberger1b312cb2018-07-02 14:47:51 -0500182
Joe Hershberger586cbd12015-03-22 17:09:21 -0500183 return 0;
184}
185
186static const struct udevice_id sb_eth_raw_ids[] = {
187 { .compatible = "sandbox,eth-raw" },
188 { }
189};
190
191U_BOOT_DRIVER(eth_sandbox_raw) = {
192 .name = "eth_sandbox_raw",
193 .id = UCLASS_ETH,
194 .of_match = sb_eth_raw_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700195 .of_to_plat = sb_eth_raw_of_to_plat,
Joe Hershberger586cbd12015-03-22 17:09:21 -0500196 .ops = &sb_eth_raw_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700197 .priv_auto = sizeof(struct eth_sandbox_raw_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700198 .plat_auto = sizeof(struct eth_pdata),
Joe Hershberger586cbd12015-03-22 17:09:21 -0500199};