blob: 45c3b18bdf33ccfe8d4f224e5d6e10f6c2e385ab [file] [log] [blame]
Joe Hershbergera346ca72015-03-22 17:09:21 -05001/*
2 * Copyright (c) 2015 National Instruments
3 *
4 * (C) Copyright 2015
5 * Joe Hershberger <joe.hershberger@ni.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10#include <asm/eth-raw-os.h>
11#include <common.h>
12#include <dm.h>
13#include <malloc.h>
14#include <net.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
Joe Hershberger22f68522015-03-22 17:09:23 -050018static int reply_arp;
Joe Hershberger049a95a2015-04-08 01:41:01 -050019static struct in_addr arp_ip;
Joe Hershbergera346ca72015-03-22 17:09:21 -050020
21static int sb_eth_raw_start(struct udevice *dev)
22{
23 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
24 struct eth_pdata *pdata = dev_get_platdata(dev);
25 const char *interface;
26
27 debug("eth_sandbox_raw: Start\n");
28
29 interface = fdt_getprop(gd->fdt_blob, dev->of_offset,
30 "host-raw-interface", NULL);
31 if (interface == NULL)
32 return -EINVAL;
33
Joe Hershberger22f68522015-03-22 17:09:23 -050034 if (strcmp(interface, "lo") == 0) {
35 priv->local = 1;
36 setenv("ipaddr", "127.0.0.1");
37 setenv("serverip", "127.0.0.1");
38 }
Joe Hershbergera346ca72015-03-22 17:09:21 -050039 return sandbox_eth_raw_os_start(interface, pdata->enetaddr, priv);
40}
41
42static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
43{
44 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
45
46 debug("eth_sandbox_raw: Send packet %d\n", length);
47
Joe Hershberger22f68522015-03-22 17:09:23 -050048 if (priv->local) {
49 struct ethernet_hdr *eth = packet;
50
51 if (ntohs(eth->et_protlen) == PROT_ARP) {
52 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
53
54 /**
55 * localhost works on a higher-level API in Linux than
56 * ARP packets, so fake it
57 */
Joe Hershberger049a95a2015-04-08 01:41:01 -050058 arp_ip = net_read_ip(&arp->ar_tpa);
Joe Hershberger22f68522015-03-22 17:09:23 -050059 reply_arp = 1;
60 return 0;
61 }
62 packet += ETHER_HDR_SIZE;
63 length -= ETHER_HDR_SIZE;
64 }
Joe Hershbergera346ca72015-03-22 17:09:21 -050065 return sandbox_eth_raw_os_send(packet, length, priv);
66}
67
68static int sb_eth_raw_recv(struct udevice *dev, uchar **packetp)
69{
Joe Hershberger22f68522015-03-22 17:09:23 -050070 struct eth_pdata *pdata = dev_get_platdata(dev);
Joe Hershbergera346ca72015-03-22 17:09:21 -050071 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
Joe Hershberger22f68522015-03-22 17:09:23 -050072 int retval = 0;
Joe Hershbergera346ca72015-03-22 17:09:21 -050073 int length;
74
Joe Hershberger22f68522015-03-22 17:09:23 -050075 if (reply_arp) {
76 struct arp_hdr *arp = (void *)net_rx_packets[0] +
77 ETHER_HDR_SIZE;
78
79 /*
80 * Fake an ARP response. The u-boot network stack is sending an
81 * ARP request (to find the MAC address to address the actual
82 * packet to) and requires an ARP response to continue. Since
83 * this is the localhost interface, there is no Etherent level
84 * traffic at all, so there is no way to send an ARP request or
85 * to get a response. For this reason we fake the response to
86 * make the u-boot network stack happy.
87 */
88 arp->ar_hrd = htons(ARP_ETHER);
89 arp->ar_pro = htons(PROT_IP);
90 arp->ar_hln = ARP_HLEN;
91 arp->ar_pln = ARP_PLEN;
92 arp->ar_op = htons(ARPOP_REPLY);
93 /* Any non-zero MAC address will work */
94 memset(&arp->ar_sha, 0x01, ARP_HLEN);
95 /* Use whatever IP we were looking for (always 127.0.0.1?) */
Joe Hershberger049a95a2015-04-08 01:41:01 -050096 net_write_ip(&arp->ar_spa, arp_ip);
Joe Hershberger22f68522015-03-22 17:09:23 -050097 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -050098 net_write_ip(&arp->ar_tpa, net_ip);
Joe Hershberger22f68522015-03-22 17:09:23 -050099 length = ARP_HDR_SIZE;
100 } else {
101 /* If local, the Ethernet header won't be included; skip it */
102 uchar *pktptr = priv->local ?
103 net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
104
105 retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
106 }
Joe Hershbergera346ca72015-03-22 17:09:21 -0500107
108 if (!retval && length) {
Joe Hershberger22f68522015-03-22 17:09:23 -0500109 if (priv->local) {
110 struct ethernet_hdr *eth = (void *)net_rx_packets[0];
111
112 /* Fill in enough of the missing Ethernet header */
113 memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN);
114 memset(eth->et_src, 0x01, ARP_HLEN);
115 eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP);
116 reply_arp = 0;
117 length += ETHER_HDR_SIZE;
118 }
119
Joe Hershbergera346ca72015-03-22 17:09:21 -0500120 debug("eth_sandbox_raw: received packet %d\n",
121 length);
122 *packetp = net_rx_packets[0];
123 return length;
124 }
125 return retval;
126}
127
128static void sb_eth_raw_stop(struct udevice *dev)
129{
130 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
131
132 debug("eth_sandbox_raw: Stop\n");
133
134 sandbox_eth_raw_os_stop(priv);
135}
136
137static const struct eth_ops sb_eth_raw_ops = {
138 .start = sb_eth_raw_start,
139 .send = sb_eth_raw_send,
140 .recv = sb_eth_raw_recv,
141 .stop = sb_eth_raw_stop,
142};
143
144static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
145{
146 struct eth_pdata *pdata = dev_get_platdata(dev);
147
148 pdata->iobase = dev_get_addr(dev);
149 return 0;
150}
151
152static const struct udevice_id sb_eth_raw_ids[] = {
153 { .compatible = "sandbox,eth-raw" },
154 { }
155};
156
157U_BOOT_DRIVER(eth_sandbox_raw) = {
158 .name = "eth_sandbox_raw",
159 .id = UCLASS_ETH,
160 .of_match = sb_eth_raw_ids,
161 .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata,
162 .ops = &sb_eth_raw_ops,
163 .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv),
164 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
165};