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