SPDX: Convert all of our single license tags to Linux Kernel style
[oweals/u-boot.git] / drivers / net / sandbox-raw.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015 National Instruments
4  *
5  * (C) Copyright 2015
6  * Joe Hershberger <joe.hershberger@ni.com>
7  */
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
15 DECLARE_GLOBAL_DATA_PTR;
16
17 static int reply_arp;
18 static struct in_addr arp_ip;
19
20 static 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
28         interface = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
29                                             "host-raw-interface", NULL);
30         if (interface == NULL)
31                 return -EINVAL;
32
33         if (strcmp(interface, "lo") == 0) {
34                 priv->local = 1;
35                 env_set("ipaddr", "127.0.0.1");
36                 env_set("serverip", "127.0.0.1");
37         }
38         return sandbox_eth_raw_os_start(interface, pdata->enetaddr, priv);
39 }
40
41 static 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
47         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                          */
57                         arp_ip = net_read_ip(&arp->ar_tpa);
58                         reply_arp = 1;
59                         return 0;
60                 }
61                 packet += ETHER_HDR_SIZE;
62                 length -= ETHER_HDR_SIZE;
63         }
64         return sandbox_eth_raw_os_send(packet, length, priv);
65 }
66
67 static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
68 {
69         struct eth_pdata *pdata = dev_get_platdata(dev);
70         struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
71         int retval = 0;
72         int length;
73
74         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?) */
95                 net_write_ip(&arp->ar_spa, arp_ip);
96                 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
97                 net_write_ip(&arp->ar_tpa, net_ip);
98                 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         }
106
107         if (!retval && length) {
108                 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
119                 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
127 static 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
136 static 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
143 static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
144 {
145         struct eth_pdata *pdata = dev_get_platdata(dev);
146
147         pdata->iobase = devfdt_get_addr(dev);
148         return 0;
149 }
150
151 static const struct udevice_id sb_eth_raw_ids[] = {
152         { .compatible = "sandbox,eth-raw" },
153         { }
154 };
155
156 U_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 };