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