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 | |
| 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 | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 17 | static int reply_arp; |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 18 | static struct in_addr arp_ip; |
Joe Hershberger | 586cbd1 | 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 | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 24 | int ret; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 25 | |
| 26 | debug("eth_sandbox_raw: Start\n"); |
| 27 | |
Joe Hershberger | e569140 | 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 | 6a38e41 | 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 | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 32 | } |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 33 | return ret; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 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 | |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 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 | */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 52 | arp_ip = net_read_ip(&arp->ar_tpa); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 53 | reply_arp = 1; |
| 54 | return 0; |
| 55 | } |
| 56 | packet += ETHER_HDR_SIZE; |
| 57 | length -= ETHER_HDR_SIZE; |
| 58 | } |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 59 | return sandbox_eth_raw_os_send(packet, length, priv); |
| 60 | } |
| 61 | |
Simon Glass | dc6eda3 | 2015-07-06 16:47:49 -0600 | [diff] [blame] | 62 | 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] | 63 | { |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 64 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 65 | struct eth_sandbox_raw_priv *priv = dev_get_priv(dev); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 66 | int retval = 0; |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 67 | int length; |
| 68 | |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 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?) */ |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 90 | net_write_ip(&arp->ar_spa, arp_ip); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 91 | memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 92 | net_write_ip(&arp->ar_tpa, net_ip); |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 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 | } |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 101 | |
| 102 | if (!retval && length) { |
Joe Hershberger | a892192 | 2015-03-22 17:09:23 -0500 | [diff] [blame] | 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 | |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 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); |
Joe Hershberger | e569140 | 2018-07-02 14:47:50 -0500 | [diff] [blame^] | 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 | } |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 153 | |
Joe Hershberger | 586cbd1 | 2015-03-22 17:09:21 -0500 | [diff] [blame] | 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 | }; |