Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 National Instruments |
| 4 | * |
| 5 | * (C) Copyright 2015 |
| 6 | * Joe Hershberger <joe.hershberger@ni.com> |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 10 | #include <asm/eth-raw-os.h> |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 13 | #include <env.h> |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <net.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 19 | static int reply_arp; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 20 | static struct in_addr arp_ip; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 21 | |
| 22 | static int sb_eth_raw_start(struct udevice *dev) |
| 23 | { |
| 24 | struct eth_sandbox_raw_priv *priv = dev_get_priv(dev); |
| 25 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame] | 26 | int ret; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 27 | |
| 28 | debug("eth_sandbox_raw: Start\n"); |
| 29 | |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame] | 30 | ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr); |
| 31 | if (priv->local) { |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 32 | env_set("ipaddr", "127.0.0.1"); |
| 33 | env_set("serverip", "127.0.0.1"); |
Joe Hershberger | 1b312cb | 2018-07-02 14:47:51 -0500 | [diff] [blame] | 34 | net_ip = string_to_ip("127.0.0.1"); |
| 35 | net_server_ip = net_ip; |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 36 | } |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame] | 37 | return ret; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static int sb_eth_raw_send(struct udevice *dev, void *packet, int length) |
| 41 | { |
| 42 | struct eth_sandbox_raw_priv *priv = dev_get_priv(dev); |
| 43 | |
| 44 | debug("eth_sandbox_raw: Send packet %d\n", length); |
| 45 | |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 46 | if (priv->local) { |
| 47 | struct ethernet_hdr *eth = packet; |
| 48 | |
| 49 | if (ntohs(eth->et_protlen) == PROT_ARP) { |
| 50 | struct arp_hdr *arp = packet + ETHER_HDR_SIZE; |
| 51 | |
| 52 | /** |
| 53 | * localhost works on a higher-level API in Linux than |
| 54 | * ARP packets, so fake it |
| 55 | */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 56 | arp_ip = net_read_ip(&arp->ar_tpa); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 57 | reply_arp = 1; |
| 58 | return 0; |
| 59 | } |
| 60 | packet += ETHER_HDR_SIZE; |
| 61 | length -= ETHER_HDR_SIZE; |
| 62 | } |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 63 | return sandbox_eth_raw_os_send(packet, length, priv); |
| 64 | } |
| 65 | |
Simon Glass | dc6eda3 | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 66 | static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp) |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 67 | { |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 68 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 69 | struct eth_sandbox_raw_priv *priv = dev_get_priv(dev); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 70 | int retval = 0; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 71 | int length; |
| 72 | |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 73 | if (reply_arp) { |
| 74 | struct arp_hdr *arp = (void *)net_rx_packets[0] + |
| 75 | ETHER_HDR_SIZE; |
| 76 | |
| 77 | /* |
| 78 | * Fake an ARP response. The u-boot network stack is sending an |
| 79 | * ARP request (to find the MAC address to address the actual |
| 80 | * packet to) and requires an ARP response to continue. Since |
| 81 | * this is the localhost interface, there is no Etherent level |
| 82 | * traffic at all, so there is no way to send an ARP request or |
| 83 | * to get a response. For this reason we fake the response to |
| 84 | * make the u-boot network stack happy. |
| 85 | */ |
| 86 | arp->ar_hrd = htons(ARP_ETHER); |
| 87 | arp->ar_pro = htons(PROT_IP); |
| 88 | arp->ar_hln = ARP_HLEN; |
| 89 | arp->ar_pln = ARP_PLEN; |
| 90 | arp->ar_op = htons(ARPOP_REPLY); |
| 91 | /* Any non-zero MAC address will work */ |
| 92 | memset(&arp->ar_sha, 0x01, ARP_HLEN); |
| 93 | /* Use whatever IP we were looking for (always 127.0.0.1?) */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 94 | net_write_ip(&arp->ar_spa, arp_ip); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 95 | memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 96 | net_write_ip(&arp->ar_tpa, net_ip); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 97 | length = ARP_HDR_SIZE; |
| 98 | } else { |
| 99 | /* If local, the Ethernet header won't be included; skip it */ |
| 100 | uchar *pktptr = priv->local ? |
| 101 | net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0]; |
| 102 | |
| 103 | retval = sandbox_eth_raw_os_recv(pktptr, &length, priv); |
| 104 | } |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 105 | |
| 106 | if (!retval && length) { |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 107 | if (priv->local) { |
| 108 | struct ethernet_hdr *eth = (void *)net_rx_packets[0]; |
| 109 | |
| 110 | /* Fill in enough of the missing Ethernet header */ |
| 111 | memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN); |
| 112 | memset(eth->et_src, 0x01, ARP_HLEN); |
| 113 | eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP); |
| 114 | reply_arp = 0; |
| 115 | length += ETHER_HDR_SIZE; |
| 116 | } |
| 117 | |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 118 | debug("eth_sandbox_raw: received packet %d\n", |
| 119 | length); |
| 120 | *packetp = net_rx_packets[0]; |
| 121 | return length; |
| 122 | } |
| 123 | return retval; |
| 124 | } |
| 125 | |
| 126 | static void sb_eth_raw_stop(struct udevice *dev) |
| 127 | { |
| 128 | struct eth_sandbox_raw_priv *priv = dev_get_priv(dev); |
| 129 | |
| 130 | debug("eth_sandbox_raw: Stop\n"); |
| 131 | |
| 132 | sandbox_eth_raw_os_stop(priv); |
| 133 | } |
| 134 | |
Joe Hershberger | f6258b8 | 2018-07-02 14:47:53 -0500 | [diff] [blame] | 135 | static int sb_eth_raw_read_rom_hwaddr(struct udevice *dev) |
| 136 | { |
| 137 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 138 | |
| 139 | net_random_ethaddr(pdata->enetaddr); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 144 | static const struct eth_ops sb_eth_raw_ops = { |
| 145 | .start = sb_eth_raw_start, |
| 146 | .send = sb_eth_raw_send, |
| 147 | .recv = sb_eth_raw_recv, |
| 148 | .stop = sb_eth_raw_stop, |
Joe Hershberger | f6258b8 | 2018-07-02 14:47:53 -0500 | [diff] [blame] | 149 | .read_rom_hwaddr = sb_eth_raw_read_rom_hwaddr, |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev) |
| 153 | { |
| 154 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame] | 155 | struct eth_sandbox_raw_priv *priv = dev_get_priv(dev); |
| 156 | const char *ifname; |
Joe Hershberger | 144e14b | 2018-07-02 14:47:52 -0500 | [diff] [blame] | 157 | int ret; |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame] | 158 | |
| 159 | pdata->iobase = dev_read_addr(dev); |
| 160 | |
| 161 | ifname = dev_read_string(dev, "host-raw-interface"); |
| 162 | if (ifname) { |
| 163 | strncpy(priv->host_ifname, ifname, IFNAMSIZ); |
| 164 | printf(": Using %s from DT\n", priv->host_ifname); |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame] | 165 | } |
Joe Hershberger | 144e14b | 2018-07-02 14:47:52 -0500 | [diff] [blame] | 166 | if (dev_read_u32(dev, "host-raw-interface-idx", |
| 167 | &priv->host_ifindex) < 0) { |
| 168 | priv->host_ifindex = 0; |
| 169 | } else { |
| 170 | ret = sandbox_eth_raw_os_idx_to_name(priv); |
| 171 | if (ret < 0) |
| 172 | return ret; |
| 173 | printf(": Using interface index %d from DT (%s)\n", |
| 174 | priv->host_ifindex, priv->host_ifname); |
| 175 | } |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 176 | |
Simon Glass | 1c1bf9f | 2019-01-07 16:44:22 -0700 | [diff] [blame] | 177 | ret = sandbox_eth_raw_os_is_local(priv->host_ifname); |
| 178 | if (ret < 0) |
| 179 | return ret; |
| 180 | priv->local = ret; |
Joe Hershberger | 1b312cb | 2018-07-02 14:47:51 -0500 | [diff] [blame] | 181 | |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static const struct udevice_id sb_eth_raw_ids[] = { |
| 186 | { .compatible = "sandbox,eth-raw" }, |
| 187 | { } |
| 188 | }; |
| 189 | |
| 190 | U_BOOT_DRIVER(eth_sandbox_raw) = { |
| 191 | .name = "eth_sandbox_raw", |
| 192 | .id = UCLASS_ETH, |
| 193 | .of_match = sb_eth_raw_ids, |
| 194 | .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata, |
| 195 | .ops = &sb_eth_raw_ops, |
| 196 | .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv), |
| 197 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 198 | }; |