blob: 317e9640191723f210e59c0d83b05b5e95e4f2d2 [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
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 Hershbergera8921922015-03-22 17:09:23 -050017static int reply_arp;
Joe Hershberger5874dec2015-04-08 01:41:01 -050018static struct in_addr arp_ip;
Joe Hershberger586cbd12015-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);
Joe Hershbergere5691402018-07-02 14:47:50 -050024 int ret;
Joe Hershberger586cbd12015-03-22 17:09:21 -050025
26 debug("eth_sandbox_raw: Start\n");
27
Joe Hershbergere5691402018-07-02 14:47:50 -050028 ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
29 if (priv->local) {
Simon Glass6a38e412017-08-03 12:22:09 -060030 env_set("ipaddr", "127.0.0.1");
31 env_set("serverip", "127.0.0.1");
Joe Hershbergera8921922015-03-22 17:09:23 -050032 }
Joe Hershbergere5691402018-07-02 14:47:50 -050033 return ret;
Joe Hershberger586cbd12015-03-22 17:09:21 -050034}
35
36static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
37{
38 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
39
40 debug("eth_sandbox_raw: Send packet %d\n", length);
41
Joe Hershbergera8921922015-03-22 17:09:23 -050042 if (priv->local) {
43 struct ethernet_hdr *eth = packet;
44
45 if (ntohs(eth->et_protlen) == PROT_ARP) {
46 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
47
48 /**
49 * localhost works on a higher-level API in Linux than
50 * ARP packets, so fake it
51 */
Joe Hershberger5874dec2015-04-08 01:41:01 -050052 arp_ip = net_read_ip(&arp->ar_tpa);
Joe Hershbergera8921922015-03-22 17:09:23 -050053 reply_arp = 1;
54 return 0;
55 }
56 packet += ETHER_HDR_SIZE;
57 length -= ETHER_HDR_SIZE;
58 }
Joe Hershberger586cbd12015-03-22 17:09:21 -050059 return sandbox_eth_raw_os_send(packet, length, priv);
60}
61
Simon Glassdc6eda32015-07-06 16:47:49 -060062static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
Joe Hershberger586cbd12015-03-22 17:09:21 -050063{
Joe Hershbergera8921922015-03-22 17:09:23 -050064 struct eth_pdata *pdata = dev_get_platdata(dev);
Joe Hershberger586cbd12015-03-22 17:09:21 -050065 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
Joe Hershbergera8921922015-03-22 17:09:23 -050066 int retval = 0;
Joe Hershberger586cbd12015-03-22 17:09:21 -050067 int length;
68
Joe Hershbergera8921922015-03-22 17:09:23 -050069 if (reply_arp) {
70 struct arp_hdr *arp = (void *)net_rx_packets[0] +
71 ETHER_HDR_SIZE;
72
73 /*
74 * Fake an ARP response. The u-boot network stack is sending an
75 * ARP request (to find the MAC address to address the actual
76 * packet to) and requires an ARP response to continue. Since
77 * this is the localhost interface, there is no Etherent level
78 * traffic at all, so there is no way to send an ARP request or
79 * to get a response. For this reason we fake the response to
80 * make the u-boot network stack happy.
81 */
82 arp->ar_hrd = htons(ARP_ETHER);
83 arp->ar_pro = htons(PROT_IP);
84 arp->ar_hln = ARP_HLEN;
85 arp->ar_pln = ARP_PLEN;
86 arp->ar_op = htons(ARPOP_REPLY);
87 /* Any non-zero MAC address will work */
88 memset(&arp->ar_sha, 0x01, ARP_HLEN);
89 /* Use whatever IP we were looking for (always 127.0.0.1?) */
Joe Hershberger5874dec2015-04-08 01:41:01 -050090 net_write_ip(&arp->ar_spa, arp_ip);
Joe Hershbergera8921922015-03-22 17:09:23 -050091 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
Joe Hershberger5874dec2015-04-08 01:41:01 -050092 net_write_ip(&arp->ar_tpa, net_ip);
Joe Hershbergera8921922015-03-22 17:09:23 -050093 length = ARP_HDR_SIZE;
94 } else {
95 /* If local, the Ethernet header won't be included; skip it */
96 uchar *pktptr = priv->local ?
97 net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
98
99 retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
100 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500101
102 if (!retval && length) {
Joe Hershbergera8921922015-03-22 17:09:23 -0500103 if (priv->local) {
104 struct ethernet_hdr *eth = (void *)net_rx_packets[0];
105
106 /* Fill in enough of the missing Ethernet header */
107 memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN);
108 memset(eth->et_src, 0x01, ARP_HLEN);
109 eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP);
110 reply_arp = 0;
111 length += ETHER_HDR_SIZE;
112 }
113
Joe Hershberger586cbd12015-03-22 17:09:21 -0500114 debug("eth_sandbox_raw: received packet %d\n",
115 length);
116 *packetp = net_rx_packets[0];
117 return length;
118 }
119 return retval;
120}
121
122static void sb_eth_raw_stop(struct udevice *dev)
123{
124 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
125
126 debug("eth_sandbox_raw: Stop\n");
127
128 sandbox_eth_raw_os_stop(priv);
129}
130
131static const struct eth_ops sb_eth_raw_ops = {
132 .start = sb_eth_raw_start,
133 .send = sb_eth_raw_send,
134 .recv = sb_eth_raw_recv,
135 .stop = sb_eth_raw_stop,
136};
137
138static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
139{
140 struct eth_pdata *pdata = dev_get_platdata(dev);
Joe Hershbergere5691402018-07-02 14:47:50 -0500141 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
142 const char *ifname;
143
144 pdata->iobase = dev_read_addr(dev);
145
146 ifname = dev_read_string(dev, "host-raw-interface");
147 if (ifname) {
148 strncpy(priv->host_ifname, ifname, IFNAMSIZ);
149 printf(": Using %s from DT\n", priv->host_ifname);
150 if (strcmp(ifname, "lo") == 0)
151 priv->local = 1;
152 }
Joe Hershberger586cbd12015-03-22 17:09:21 -0500153
Joe Hershberger586cbd12015-03-22 17:09:21 -0500154 return 0;
155}
156
157static const struct udevice_id sb_eth_raw_ids[] = {
158 { .compatible = "sandbox,eth-raw" },
159 { }
160};
161
162U_BOOT_DRIVER(eth_sandbox_raw) = {
163 .name = "eth_sandbox_raw",
164 .id = UCLASS_ETH,
165 .of_match = sb_eth_raw_ids,
166 .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata,
167 .ops = &sb_eth_raw_ops,
168 .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv),
169 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
170};