blob: 81f0764fa6892a1a5ae9617b9c8f5226132754bf [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 Hershbergerc7eb7332018-09-26 16:48:55 -050013#include <asm/eth.h>
Joe Hershberger6f2707c2015-04-21 13:57:19 -050014#include <asm/test.h>
Joe Hershberger3ea143a2015-03-22 17:09:13 -050015
16DECLARE_GLOBAL_DATA_PTR;
17
Joe Hershberger6f2707c2015-04-21 13:57:19 -050018static bool skip_timeout;
Joe Hershberger2eede1f2015-03-22 17:09:19 -050019
20/*
21 * sandbox_eth_disable_response()
22 *
23 * index - The alias index (also DM seq number)
24 * disable - If non-zero, ignore sent packets and don't send mock response
25 */
26void sandbox_eth_disable_response(int index, bool disable)
27{
Joe Hershbergere4ab9a62018-09-26 16:48:53 -050028 struct udevice *dev;
29 struct eth_sandbox_priv *priv;
30 int ret;
31
32 ret = uclass_get_device(UCLASS_ETH, index, &dev);
33 if (ret)
34 return;
35
36 priv = dev_get_priv(dev);
37 priv->disabled = disable;
Joe Hershberger2eede1f2015-03-22 17:09:19 -050038}
39
Joe Hershberger6f2707c2015-04-21 13:57:19 -050040/*
41 * sandbox_eth_skip_timeout()
42 *
43 * When the first packet read is attempted, fast-forward time
44 */
45void sandbox_eth_skip_timeout(void)
46{
47 skip_timeout = true;
48}
49
Joe Hershbergere95bb162018-09-26 16:48:54 -050050/*
51 * sandbox_eth_arp_req_to_reply()
52 *
53 * Check for an arp request to be sent. If so, inject a reply
54 *
55 * returns 0 if injected, -EAGAIN if not
56 */
57int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
58 unsigned int len)
59{
60 struct eth_sandbox_priv *priv = dev_get_priv(dev);
61 struct ethernet_hdr *eth = packet;
62 struct arp_hdr *arp;
63 struct ethernet_hdr *eth_recv;
64 struct arp_hdr *arp_recv;
65
66 if (ntohs(eth->et_protlen) != PROT_ARP)
67 return -EAGAIN;
68
69 arp = packet + ETHER_HDR_SIZE;
70
71 if (ntohs(arp->ar_op) != ARPOP_REQUEST)
72 return -EAGAIN;
73
Joe Hershbergerc67a4202018-09-26 16:48:57 -050074 /* Don't allow the buffer to overrun */
75 if (priv->recv_packets >= PKTBUFSRX)
76 return 0;
77
Joe Hershbergere95bb162018-09-26 16:48:54 -050078 /* store this as the assumed IP of the fake host */
79 priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
80
81 /* Formulate a fake response */
Joe Hershbergerc67a4202018-09-26 16:48:57 -050082 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
Joe Hershbergere95bb162018-09-26 16:48:54 -050083 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
84 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
85 eth_recv->et_protlen = htons(PROT_ARP);
86
87 arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
88 arp_recv->ar_hrd = htons(ARP_ETHER);
89 arp_recv->ar_pro = htons(PROT_IP);
90 arp_recv->ar_hln = ARP_HLEN;
91 arp_recv->ar_pln = ARP_PLEN;
92 arp_recv->ar_op = htons(ARPOP_REPLY);
93 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
94 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
95 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
96 net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
97
Joe Hershbergerc67a4202018-09-26 16:48:57 -050098 priv->recv_packet_length[priv->recv_packets] =
99 ETHER_HDR_SIZE + ARP_HDR_SIZE;
100 ++priv->recv_packets;
Joe Hershbergere95bb162018-09-26 16:48:54 -0500101
102 return 0;
103}
104
105/*
106 * sandbox_eth_ping_req_to_reply()
107 *
108 * Check for a ping request to be sent. If so, inject a reply
109 *
110 * returns 0 if injected, -EAGAIN if not
111 */
112int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
113 unsigned int len)
114{
115 struct eth_sandbox_priv *priv = dev_get_priv(dev);
116 struct ethernet_hdr *eth = packet;
117 struct ip_udp_hdr *ip;
118 struct icmp_hdr *icmp;
119 struct ethernet_hdr *eth_recv;
120 struct ip_udp_hdr *ipr;
121 struct icmp_hdr *icmpr;
122
123 if (ntohs(eth->et_protlen) != PROT_IP)
124 return -EAGAIN;
125
126 ip = packet + ETHER_HDR_SIZE;
127
128 if (ip->ip_p != IPPROTO_ICMP)
129 return -EAGAIN;
130
131 icmp = (struct icmp_hdr *)&ip->udp_src;
132
133 if (icmp->type != ICMP_ECHO_REQUEST)
134 return -EAGAIN;
135
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500136 /* Don't allow the buffer to overrun */
137 if (priv->recv_packets >= PKTBUFSRX)
138 return 0;
139
Joe Hershbergere95bb162018-09-26 16:48:54 -0500140 /* reply to the ping */
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500141 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
Joe Hershbergere95bb162018-09-26 16:48:54 -0500142 memcpy(eth_recv, packet, len);
143 ipr = (void *)eth_recv + ETHER_HDR_SIZE;
144 icmpr = (struct icmp_hdr *)&ipr->udp_src;
145 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
146 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
147 ipr->ip_sum = 0;
148 ipr->ip_off = 0;
149 net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
150 net_write_ip((void *)&ipr->ip_src, priv->fake_host_ipaddr);
151 ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
152
153 icmpr->type = ICMP_ECHO_REPLY;
154 icmpr->checksum = 0;
155 icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
156
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500157 priv->recv_packet_length[priv->recv_packets] = len;
158 ++priv->recv_packets;
Joe Hershbergere95bb162018-09-26 16:48:54 -0500159
160 return 0;
161}
162
Joe Hershbergerc7eb7332018-09-26 16:48:55 -0500163/*
Joe Hershberger45988da2018-09-26 16:49:00 -0500164 * sandbox_eth_recv_arp_req()
165 *
166 * Inject an ARP request for this target
167 *
168 * returns 0 if injected, -EOVERFLOW if not
169 */
170int sandbox_eth_recv_arp_req(struct udevice *dev)
171{
172 struct eth_sandbox_priv *priv = dev_get_priv(dev);
173 struct ethernet_hdr *eth_recv;
174 struct arp_hdr *arp_recv;
175
176 /* Don't allow the buffer to overrun */
177 if (priv->recv_packets >= PKTBUFSRX)
178 return -EOVERFLOW;
179
180 /* Formulate a fake request */
181 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
182 memcpy(eth_recv->et_dest, net_bcast_ethaddr, ARP_HLEN);
183 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
184 eth_recv->et_protlen = htons(PROT_ARP);
185
186 arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
187 arp_recv->ar_hrd = htons(ARP_ETHER);
188 arp_recv->ar_pro = htons(PROT_IP);
189 arp_recv->ar_hln = ARP_HLEN;
190 arp_recv->ar_pln = ARP_PLEN;
191 arp_recv->ar_op = htons(ARPOP_REQUEST);
192 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
193 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
194 memcpy(&arp_recv->ar_tha, net_null_ethaddr, ARP_HLEN);
195 net_write_ip(&arp_recv->ar_tpa, net_ip);
196
197 priv->recv_packet_length[priv->recv_packets] =
198 ETHER_HDR_SIZE + ARP_HDR_SIZE;
199 ++priv->recv_packets;
200
201 return 0;
202}
203
204/*
Joe Hershbergerc7eb7332018-09-26 16:48:55 -0500205 * sb_default_handler()
206 *
207 * perform typical responses to simple ping
208 *
209 * dev - device pointer
210 * pkt - "sent" packet buffer
211 * len - length of packet
212 */
213static int sb_default_handler(struct udevice *dev, void *packet,
214 unsigned int len)
215{
216 if (!sandbox_eth_arp_req_to_reply(dev, packet, len))
217 return 0;
218 if (!sandbox_eth_ping_req_to_reply(dev, packet, len))
219 return 0;
220
221 return 0;
222}
223
224/*
225 * sandbox_eth_set_tx_handler()
226 *
227 * Set a custom response to a packet being sent through the sandbox eth test
228 * driver
229 *
230 * index - interface to set the handler for
231 * handler - The func ptr to call on send. If NULL, set to default handler
232 */
233void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler)
234{
235 struct udevice *dev;
236 struct eth_sandbox_priv *priv;
237 int ret;
238
239 ret = uclass_get_device(UCLASS_ETH, index, &dev);
240 if (ret)
241 return;
242
243 priv = dev_get_priv(dev);
244 if (handler)
245 priv->tx_handler = handler;
246 else
247 priv->tx_handler = sb_default_handler;
248}
249
Joe Hershberger9cbe5972018-09-26 16:48:59 -0500250/*
251 * Set priv ptr
252 *
253 * priv - priv void ptr to store in the device
254 */
255void sandbox_eth_set_priv(int index, void *priv)
256{
257 struct udevice *dev;
258 struct eth_sandbox_priv *dev_priv;
259 int ret;
260
261 ret = uclass_get_device(UCLASS_ETH, index, &dev);
262 if (ret)
263 return;
264
265 dev_priv = dev_get_priv(dev);
266
267 dev_priv->priv = priv;
268}
269
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500270static int sb_eth_start(struct udevice *dev)
271{
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500272 struct eth_sandbox_priv *priv = dev_get_priv(dev);
273
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500274 debug("eth_sandbox: Start\n");
275
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500276 priv->recv_packets = 0;
277 for (int i = 0; i < PKTBUFSRX; i++) {
278 priv->recv_packet_buffer[i] = net_rx_packets[i];
279 priv->recv_packet_length[i] = 0;
280 }
Joe Hershbergerb32dd182018-07-02 14:47:49 -0500281
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500282 return 0;
283}
284
285static int sb_eth_send(struct udevice *dev, void *packet, int length)
286{
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500287 struct eth_sandbox_priv *priv = dev_get_priv(dev);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500288
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500289 debug("eth_sandbox: Send packet %d\n", length);
290
Joe Hershbergere4ab9a62018-09-26 16:48:53 -0500291 if (priv->disabled)
Joe Hershberger2eede1f2015-03-22 17:09:19 -0500292 return 0;
293
Joe Hershbergerc7eb7332018-09-26 16:48:55 -0500294 return priv->tx_handler(dev, packet, length);
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500295}
296
Simon Glassa1ca92e2015-07-06 16:47:49 -0600297static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500298{
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500299 struct eth_sandbox_priv *priv = dev_get_priv(dev);
300
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500301 if (skip_timeout) {
Joe Hershbergerc5a75332015-12-21 16:31:35 -0600302 sandbox_timer_add_offset(11000UL);
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500303 skip_timeout = false;
304 }
305
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500306 if (priv->recv_packets) {
307 int lcl_recv_packet_length = priv->recv_packet_length[0];
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500308
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500309 debug("eth_sandbox: received packet[%d], %d waiting\n",
310 lcl_recv_packet_length, priv->recv_packets - 1);
311 *packetp = priv->recv_packet_buffer[0];
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500312 return lcl_recv_packet_length;
313 }
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500314 return 0;
315}
316
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500317static int sb_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
318{
319 struct eth_sandbox_priv *priv = dev_get_priv(dev);
320 int i;
321
322 if (!priv->recv_packets)
323 return 0;
324
325 --priv->recv_packets;
326 for (i = 0; i < priv->recv_packets; i++) {
327 priv->recv_packet_length[i] = priv->recv_packet_length[i + 1];
328 memcpy(priv->recv_packet_buffer[i],
329 priv->recv_packet_buffer[i + 1],
330 priv->recv_packet_length[i + 1]);
331 }
332 priv->recv_packet_length[priv->recv_packets] = 0;
333
334 return 0;
335}
336
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500337static void sb_eth_stop(struct udevice *dev)
338{
339 debug("eth_sandbox: Stop\n");
340}
341
342static int sb_eth_write_hwaddr(struct udevice *dev)
343{
344 struct eth_pdata *pdata = dev_get_platdata(dev);
345
346 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
347 pdata->enetaddr);
348 return 0;
349}
350
351static const struct eth_ops sb_eth_ops = {
352 .start = sb_eth_start,
353 .send = sb_eth_send,
354 .recv = sb_eth_recv,
Joe Hershbergerc67a4202018-09-26 16:48:57 -0500355 .free_pkt = sb_eth_free_pkt,
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500356 .stop = sb_eth_stop,
357 .write_hwaddr = sb_eth_write_hwaddr,
358};
359
360static int sb_eth_remove(struct udevice *dev)
361{
362 return 0;
363}
364
365static int sb_eth_ofdata_to_platdata(struct udevice *dev)
366{
367 struct eth_pdata *pdata = dev_get_platdata(dev);
Joe Hershbergerb32dd182018-07-02 14:47:49 -0500368 struct eth_sandbox_priv *priv = dev_get_priv(dev);
369 const u8 *mac;
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500370
Joe Hershbergerb32dd182018-07-02 14:47:49 -0500371 pdata->iobase = dev_read_addr(dev);
372
373 mac = dev_read_u8_array_ptr(dev, "fake-host-hwaddr", ARP_HLEN);
374 if (!mac) {
375 printf("'fake-host-hwaddr' is missing from the DT\n");
376 return -EINVAL;
377 }
378 memcpy(priv->fake_host_hwaddr, mac, ARP_HLEN);
Joe Hershbergere4ab9a62018-09-26 16:48:53 -0500379 priv->disabled = false;
Joe Hershbergerc7eb7332018-09-26 16:48:55 -0500380 priv->tx_handler = sb_default_handler;
Joe Hershbergerb32dd182018-07-02 14:47:49 -0500381
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500382 return 0;
383}
384
385static const struct udevice_id sb_eth_ids[] = {
386 { .compatible = "sandbox,eth" },
387 { }
388};
389
390U_BOOT_DRIVER(eth_sandbox) = {
391 .name = "eth_sandbox",
392 .id = UCLASS_ETH,
393 .of_match = sb_eth_ids,
394 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
395 .remove = sb_eth_remove,
396 .ops = &sb_eth_ops,
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500397 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500398 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
399};