blob: 3f8020f629d6359f68632c644d23b053b3216d56 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershbergera346ca72015-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 Hershbergera346ca72015-03-22 17:09:21 -05007 */
8
9#include <asm/eth-raw-os.h>
10#include <common.h>
11#include <dm.h>
12#include <malloc.h>
13#include <net.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
Joe Hershberger22f68522015-03-22 17:09:23 -050017static int reply_arp;
Joe Hershberger049a95a2015-04-08 01:41:01 -050018static struct in_addr arp_ip;
Joe Hershbergera346ca72015-03-22 17:09:21 -050019
20static int sb_eth_raw_start(struct udevice *dev)
21{
22 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
23 struct eth_pdata *pdata = dev_get_platdata(dev);
24 const char *interface;
25
26 debug("eth_sandbox_raw: Start\n");
27
Simon Glasse160f7d2017-01-17 16:52:55 -070028 interface = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
Joe Hershbergera346ca72015-03-22 17:09:21 -050029 "host-raw-interface", NULL);
30 if (interface == NULL)
31 return -EINVAL;
32
Joe Hershberger22f68522015-03-22 17:09:23 -050033 if (strcmp(interface, "lo") == 0) {
34 priv->local = 1;
Simon Glass382bee52017-08-03 12:22:09 -060035 env_set("ipaddr", "127.0.0.1");
36 env_set("serverip", "127.0.0.1");
Joe Hershberger22f68522015-03-22 17:09:23 -050037 }
Joe Hershbergera346ca72015-03-22 17:09:21 -050038 return sandbox_eth_raw_os_start(interface, pdata->enetaddr, priv);
39}
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 Hershberger22f68522015-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 Hershberger049a95a2015-04-08 01:41:01 -050057 arp_ip = net_read_ip(&arp->ar_tpa);
Joe Hershberger22f68522015-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 Hershbergera346ca72015-03-22 17:09:21 -050064 return sandbox_eth_raw_os_send(packet, length, priv);
65}
66
Simon Glassa1ca92e2015-07-06 16:47:49 -060067static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
Joe Hershbergera346ca72015-03-22 17:09:21 -050068{
Joe Hershberger22f68522015-03-22 17:09:23 -050069 struct eth_pdata *pdata = dev_get_platdata(dev);
Joe Hershbergera346ca72015-03-22 17:09:21 -050070 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
Joe Hershberger22f68522015-03-22 17:09:23 -050071 int retval = 0;
Joe Hershbergera346ca72015-03-22 17:09:21 -050072 int length;
73
Joe Hershberger22f68522015-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 Hershberger049a95a2015-04-08 01:41:01 -050095 net_write_ip(&arp->ar_spa, arp_ip);
Joe Hershberger22f68522015-03-22 17:09:23 -050096 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -050097 net_write_ip(&arp->ar_tpa, net_ip);
Joe Hershberger22f68522015-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 Hershbergera346ca72015-03-22 17:09:21 -0500106
107 if (!retval && length) {
Joe Hershberger22f68522015-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 Hershbergera346ca72015-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
136static const struct eth_ops sb_eth_raw_ops = {
137 .start = sb_eth_raw_start,
138 .send = sb_eth_raw_send,
139 .recv = sb_eth_raw_recv,
140 .stop = sb_eth_raw_stop,
141};
142
143static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
144{
145 struct eth_pdata *pdata = dev_get_platdata(dev);
146
Simon Glassa821c4a2017-05-17 17:18:05 -0600147 pdata->iobase = devfdt_get_addr(dev);
Joe Hershbergera346ca72015-03-22 17:09:21 -0500148 return 0;
149}
150
151static const struct udevice_id sb_eth_raw_ids[] = {
152 { .compatible = "sandbox,eth-raw" },
153 { }
154};
155
156U_BOOT_DRIVER(eth_sandbox_raw) = {
157 .name = "eth_sandbox_raw",
158 .id = UCLASS_ETH,
159 .of_match = sb_eth_raw_ids,
160 .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata,
161 .ops = &sb_eth_raw_ops,
162 .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv),
163 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
164};