net: sandbox-raw: Convert raw eth driver to livetree
[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         int ret;
25
26         debug("eth_sandbox_raw: Start\n");
27
28         ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
29         if (priv->local) {
30                 env_set("ipaddr", "127.0.0.1");
31                 env_set("serverip", "127.0.0.1");
32         }
33         return ret;
34 }
35
36 static 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
42         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                          */
52                         arp_ip = net_read_ip(&arp->ar_tpa);
53                         reply_arp = 1;
54                         return 0;
55                 }
56                 packet += ETHER_HDR_SIZE;
57                 length -= ETHER_HDR_SIZE;
58         }
59         return sandbox_eth_raw_os_send(packet, length, priv);
60 }
61
62 static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
63 {
64         struct eth_pdata *pdata = dev_get_platdata(dev);
65         struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
66         int retval = 0;
67         int length;
68
69         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?) */
90                 net_write_ip(&arp->ar_spa, arp_ip);
91                 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
92                 net_write_ip(&arp->ar_tpa, net_ip);
93                 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         }
101
102         if (!retval && length) {
103                 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
114                 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
122 static 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
131 static 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
138 static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
139 {
140         struct eth_pdata *pdata = dev_get_platdata(dev);
141         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         }
153
154         return 0;
155 }
156
157 static const struct udevice_id sb_eth_raw_ids[] = {
158         { .compatible = "sandbox,eth-raw" },
159         { }
160 };
161
162 U_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 };