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