blob: 60fe065ee530b3e3d0099c77c179e746d9c6da22 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershberger3ea143a2015-03-22 17:09:13 -05002/*
3 * Copyright (c) 2015 National Instruments
4 *
5 * (C) Copyright 2015
6 * Joe Hershberger <joe.hershberger@ni.com>
Joe Hershberger3ea143a2015-03-22 17:09:13 -05007 */
8
9#include <common.h>
10#include <dm.h>
11#include <malloc.h>
12#include <net.h>
Joe Hershberger6f2707c2015-04-21 13:57:19 -050013#include <asm/test.h>
Joe Hershberger3ea143a2015-03-22 17:09:13 -050014
15DECLARE_GLOBAL_DATA_PTR;
16
Joe Hershbergerd87a4572015-03-22 17:09:14 -050017/**
18 * struct eth_sandbox_priv - memory for sandbox mock driver
19 *
20 * fake_host_hwaddr: MAC address of mocked machine
21 * fake_host_ipaddr: IP address of mocked machine
Joe Hershbergere4ab9a62018-09-26 16:48:53 -050022 * disabled: Will not respond
Joe Hershbergerd87a4572015-03-22 17:09:14 -050023 * recv_packet_buffer: buffer of the packet returned as received
24 * recv_packet_length: length of the packet returned as received
25 */
26struct eth_sandbox_priv {
27 uchar fake_host_hwaddr[ARP_HLEN];
Joe Hershberger049a95a2015-04-08 01:41:01 -050028 struct in_addr fake_host_ipaddr;
Joe Hershbergere4ab9a62018-09-26 16:48:53 -050029 bool disabled;
Joe Hershbergerd87a4572015-03-22 17:09:14 -050030 uchar *recv_packet_buffer;
31 int recv_packet_length;
32};
33
Joe Hershberger6f2707c2015-04-21 13:57:19 -050034static bool skip_timeout;
Joe Hershberger2eede1f2015-03-22 17:09:19 -050035
36/*
37 * sandbox_eth_disable_response()
38 *
39 * index - The alias index (also DM seq number)
40 * disable - If non-zero, ignore sent packets and don't send mock response
41 */
42void sandbox_eth_disable_response(int index, bool disable)
43{
Joe Hershbergere4ab9a62018-09-26 16:48:53 -050044 struct udevice *dev;
45 struct eth_sandbox_priv *priv;
46 int ret;
47
48 ret = uclass_get_device(UCLASS_ETH, index, &dev);
49 if (ret)
50 return;
51
52 priv = dev_get_priv(dev);
53 priv->disabled = disable;
Joe Hershberger2eede1f2015-03-22 17:09:19 -050054}
55
Joe Hershberger6f2707c2015-04-21 13:57:19 -050056/*
57 * sandbox_eth_skip_timeout()
58 *
59 * When the first packet read is attempted, fast-forward time
60 */
61void sandbox_eth_skip_timeout(void)
62{
63 skip_timeout = true;
64}
65
Joe Hershberger3ea143a2015-03-22 17:09:13 -050066static int sb_eth_start(struct udevice *dev)
67{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050068 struct eth_sandbox_priv *priv = dev_get_priv(dev);
69
Joe Hershberger3ea143a2015-03-22 17:09:13 -050070 debug("eth_sandbox: Start\n");
71
Joe Hershbergerd87a4572015-03-22 17:09:14 -050072 priv->recv_packet_buffer = net_rx_packets[0];
Joe Hershbergerb32dd182018-07-02 14:47:49 -050073
Joe Hershberger3ea143a2015-03-22 17:09:13 -050074 return 0;
75}
76
77static int sb_eth_send(struct udevice *dev, void *packet, int length)
78{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050079 struct eth_sandbox_priv *priv = dev_get_priv(dev);
80 struct ethernet_hdr *eth = packet;
81
Joe Hershberger3ea143a2015-03-22 17:09:13 -050082 debug("eth_sandbox: Send packet %d\n", length);
83
Joe Hershbergere4ab9a62018-09-26 16:48:53 -050084 if (priv->disabled)
Joe Hershberger2eede1f2015-03-22 17:09:19 -050085 return 0;
86
Joe Hershbergerd87a4572015-03-22 17:09:14 -050087 if (ntohs(eth->et_protlen) == PROT_ARP) {
88 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
89
90 if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
91 struct ethernet_hdr *eth_recv;
92 struct arp_hdr *arp_recv;
93
94 /* store this as the assumed IP of the fake host */
Joe Hershberger049a95a2015-04-08 01:41:01 -050095 priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
Joe Hershbergerd87a4572015-03-22 17:09:14 -050096 /* Formulate a fake response */
97 eth_recv = (void *)priv->recv_packet_buffer;
98 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
99 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
100 ARP_HLEN);
101 eth_recv->et_protlen = htons(PROT_ARP);
102
103 arp_recv = (void *)priv->recv_packet_buffer +
104 ETHER_HDR_SIZE;
105 arp_recv->ar_hrd = htons(ARP_ETHER);
106 arp_recv->ar_pro = htons(PROT_IP);
107 arp_recv->ar_hln = ARP_HLEN;
108 arp_recv->ar_pln = ARP_PLEN;
109 arp_recv->ar_op = htons(ARPOP_REPLY);
110 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr,
111 ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500112 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500113 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500114 net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500115
116 priv->recv_packet_length = ETHER_HDR_SIZE +
117 ARP_HDR_SIZE;
118 }
119 } else if (ntohs(eth->et_protlen) == PROT_IP) {
120 struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
121
122 if (ip->ip_p == IPPROTO_ICMP) {
123 struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
124
125 if (icmp->type == ICMP_ECHO_REQUEST) {
126 struct ethernet_hdr *eth_recv;
127 struct ip_udp_hdr *ipr;
128 struct icmp_hdr *icmpr;
129
130 /* reply to the ping */
131 memcpy(priv->recv_packet_buffer, packet,
132 length);
133 eth_recv = (void *)priv->recv_packet_buffer;
134 ipr = (void *)priv->recv_packet_buffer +
135 ETHER_HDR_SIZE;
136 icmpr = (struct icmp_hdr *)&ipr->udp_src;
137 memcpy(eth_recv->et_dest, eth->et_src,
138 ARP_HLEN);
139 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
140 ARP_HLEN);
141 ipr->ip_sum = 0;
142 ipr->ip_off = 0;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500143 net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
144 net_write_ip((void *)&ipr->ip_src,
145 priv->fake_host_ipaddr);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500146 ipr->ip_sum = compute_ip_checksum(ipr,
147 IP_HDR_SIZE);
148
149 icmpr->type = ICMP_ECHO_REPLY;
150 icmpr->checksum = 0;
151 icmpr->checksum = compute_ip_checksum(icmpr,
152 ICMP_HDR_SIZE);
153
154 priv->recv_packet_length = length;
155 }
156 }
157 }
158
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500159 return 0;
160}
161
Simon Glassa1ca92e2015-07-06 16:47:49 -0600162static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500163{
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500164 struct eth_sandbox_priv *priv = dev_get_priv(dev);
165
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500166 if (skip_timeout) {
Joe Hershbergerc5a75332015-12-21 16:31:35 -0600167 sandbox_timer_add_offset(11000UL);
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500168 skip_timeout = false;
169 }
170
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500171 if (priv->recv_packet_length) {
172 int lcl_recv_packet_length = priv->recv_packet_length;
173
174 debug("eth_sandbox: received packet %d\n",
175 priv->recv_packet_length);
176 priv->recv_packet_length = 0;
177 *packetp = priv->recv_packet_buffer;
178 return lcl_recv_packet_length;
179 }
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500180 return 0;
181}
182
183static void sb_eth_stop(struct udevice *dev)
184{
185 debug("eth_sandbox: Stop\n");
186}
187
188static int sb_eth_write_hwaddr(struct udevice *dev)
189{
190 struct eth_pdata *pdata = dev_get_platdata(dev);
191
192 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
193 pdata->enetaddr);
194 return 0;
195}
196
197static const struct eth_ops sb_eth_ops = {
198 .start = sb_eth_start,
199 .send = sb_eth_send,
200 .recv = sb_eth_recv,
201 .stop = sb_eth_stop,
202 .write_hwaddr = sb_eth_write_hwaddr,
203};
204
205static int sb_eth_remove(struct udevice *dev)
206{
207 return 0;
208}
209
210static int sb_eth_ofdata_to_platdata(struct udevice *dev)
211{
212 struct eth_pdata *pdata = dev_get_platdata(dev);
Joe Hershbergerb32dd182018-07-02 14:47:49 -0500213 struct eth_sandbox_priv *priv = dev_get_priv(dev);
214 const u8 *mac;
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500215
Joe Hershbergerb32dd182018-07-02 14:47:49 -0500216 pdata->iobase = dev_read_addr(dev);
217
218 mac = dev_read_u8_array_ptr(dev, "fake-host-hwaddr", ARP_HLEN);
219 if (!mac) {
220 printf("'fake-host-hwaddr' is missing from the DT\n");
221 return -EINVAL;
222 }
223 memcpy(priv->fake_host_hwaddr, mac, ARP_HLEN);
Joe Hershbergere4ab9a62018-09-26 16:48:53 -0500224 priv->disabled = false;
Joe Hershbergerb32dd182018-07-02 14:47:49 -0500225
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500226 return 0;
227}
228
229static const struct udevice_id sb_eth_ids[] = {
230 { .compatible = "sandbox,eth" },
231 { }
232};
233
234U_BOOT_DRIVER(eth_sandbox) = {
235 .name = "eth_sandbox",
236 .id = UCLASS_ETH,
237 .of_match = sb_eth_ids,
238 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
239 .remove = sb_eth_remove,
240 .ops = &sb_eth_ops,
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500241 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500242 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
243};