Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* Copyright (C) 2024 Linaro Ltd. */ |
| 4 | |
| 5 | #include <command.h> |
| 6 | #include <dm/device.h> |
| 7 | #include <dm/uclass.h> |
Jerome Forissier | 5b2e273 | 2025-03-06 15:32:22 +0100 | [diff] [blame] | 8 | #include <hexdump.h> |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 9 | #include <lwip/ip4_addr.h> |
| 10 | #include <lwip/err.h> |
| 11 | #include <lwip/netif.h> |
| 12 | #include <lwip/pbuf.h> |
| 13 | #include <lwip/etharp.h> |
| 14 | #include <lwip/init.h> |
| 15 | #include <lwip/prot/etharp.h> |
| 16 | #include <net.h> |
| 17 | |
| 18 | /* xx:xx:xx:xx:xx:xx\0 */ |
| 19 | #define MAC_ADDR_STRLEN 18 |
| 20 | |
| 21 | #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER) |
| 22 | void (*push_packet)(void *, int len) = 0; |
| 23 | #endif |
| 24 | int net_restart_wrap; |
| 25 | static uchar net_pkt_buf[(PKTBUFSRX) * PKTSIZE_ALIGN + PKTALIGN]; |
| 26 | uchar *net_rx_packets[PKTBUFSRX]; |
| 27 | uchar *net_rx_packet; |
| 28 | const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 29 | char *pxelinux_configfile; |
| 30 | /* Our IP addr (0 = unknown) */ |
| 31 | struct in_addr net_ip; |
Jerome Forissier | 6a78e96 | 2024-10-16 12:04:05 +0200 | [diff] [blame] | 32 | char net_boot_file_name[1024]; |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 33 | |
Jerome Forissier | 9ba2131 | 2025-03-06 15:32:21 +0100 | [diff] [blame] | 34 | static err_t net_lwip_tx(struct netif *netif, struct pbuf *p) |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 35 | { |
| 36 | struct udevice *udev = netif->state; |
| 37 | void *pp = NULL; |
| 38 | int err; |
| 39 | |
Jerome Forissier | 5b2e273 | 2025-03-06 15:32:22 +0100 | [diff] [blame] | 40 | if (CONFIG_IS_ENABLED(LWIP_DEBUG_RXTX)) { |
| 41 | printf("net_lwip_tx: %u bytes, udev %s\n", p->len, udev->name); |
| 42 | print_hex_dump("net_lwip_tx: ", 0, 16, 1, p->payload, p->len, |
| 43 | true); |
| 44 | } |
| 45 | |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 46 | if ((unsigned long)p->payload % PKTALIGN) { |
| 47 | /* |
| 48 | * Some net drivers have strict alignment requirements and may |
| 49 | * fail or output invalid data if the packet is not aligned. |
| 50 | */ |
| 51 | pp = memalign(PKTALIGN, p->len); |
| 52 | if (!pp) |
| 53 | return ERR_ABRT; |
| 54 | memcpy(pp, p->payload, p->len); |
| 55 | } |
| 56 | |
| 57 | err = eth_get_ops(udev)->send(udev, pp ? pp : p->payload, p->len); |
| 58 | free(pp); |
| 59 | if (err) { |
Ilias Apalodimas | c99a102 | 2025-03-26 10:28:58 +0200 | [diff] [blame] | 60 | debug("send error %d\n", err); |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 61 | return ERR_ABRT; |
| 62 | } |
| 63 | |
| 64 | return ERR_OK; |
| 65 | } |
| 66 | |
| 67 | static err_t net_lwip_if_init(struct netif *netif) |
| 68 | { |
| 69 | netif->output = etharp_output; |
Jerome Forissier | 9ba2131 | 2025-03-06 15:32:21 +0100 | [diff] [blame] | 70 | netif->linkoutput = net_lwip_tx; |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 71 | netif->mtu = 1500; |
| 72 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; |
| 73 | |
| 74 | return ERR_OK; |
| 75 | } |
| 76 | |
| 77 | static void eth_init_rings(void) |
| 78 | { |
| 79 | int i; |
| 80 | |
| 81 | for (i = 0; i < PKTBUFSRX; i++) |
| 82 | net_rx_packets[i] = net_pkt_buf + i * PKTSIZE_ALIGN; |
| 83 | } |
| 84 | |
| 85 | struct netif *net_lwip_get_netif(void) |
| 86 | { |
| 87 | struct netif *netif, *found = NULL; |
| 88 | |
| 89 | NETIF_FOREACH(netif) { |
| 90 | if (!found) |
| 91 | found = netif; |
| 92 | else |
| 93 | printf("Error: more than one netif in lwIP\n"); |
| 94 | } |
| 95 | return found; |
| 96 | } |
| 97 | |
| 98 | static int get_udev_ipv4_info(struct udevice *dev, ip4_addr_t *ip, |
| 99 | ip4_addr_t *mask, ip4_addr_t *gw) |
| 100 | { |
Jerome Forissier | 9159657 | 2024-11-18 15:31:25 +0100 | [diff] [blame] | 101 | char ipstr[] = "ipaddr\0\0"; |
| 102 | char maskstr[] = "netmask\0\0"; |
| 103 | char gwstr[] = "gatewayip\0\0"; |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 104 | int idx = dev_seq(dev); |
| 105 | char *env; |
| 106 | |
| 107 | if (idx < 0 || idx > 99) { |
| 108 | log_err("unexpected idx %d\n", idx); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | if (idx) { |
| 113 | sprintf(ipstr, "ipaddr%d", idx); |
| 114 | sprintf(maskstr, "netmask%d", idx); |
| 115 | sprintf(gwstr, "gatewayip%d", idx); |
| 116 | } |
| 117 | |
| 118 | ip4_addr_set_zero(ip); |
| 119 | ip4_addr_set_zero(mask); |
| 120 | ip4_addr_set_zero(gw); |
| 121 | |
| 122 | env = env_get(ipstr); |
| 123 | if (env) |
| 124 | ip4addr_aton(env, ip); |
| 125 | |
| 126 | env = env_get(maskstr); |
| 127 | if (env) |
| 128 | ip4addr_aton(env, mask); |
| 129 | |
| 130 | env = env_get(gwstr); |
| 131 | if (env) |
| 132 | ip4addr_aton(env, gw); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Jerome Forissier | 3782928 | 2025-01-30 09:22:20 +0100 | [diff] [blame] | 137 | /* Initialize the lwIP stack and the ethernet devices and set current device */ |
| 138 | void net_lwip_set_current(void) |
| 139 | { |
| 140 | static bool init_done; |
| 141 | |
| 142 | if (!init_done) { |
| 143 | eth_init_rings(); |
| 144 | eth_init(); |
| 145 | lwip_init(); |
| 146 | init_done = true; |
| 147 | } |
| 148 | eth_set_current(); |
| 149 | } |
| 150 | |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 151 | static struct netif *new_netif(struct udevice *udev, bool with_ip) |
| 152 | { |
| 153 | unsigned char enetaddr[ARP_HLEN]; |
| 154 | char hwstr[MAC_ADDR_STRLEN]; |
| 155 | ip4_addr_t ip, mask, gw; |
| 156 | struct netif *netif; |
| 157 | int ret = 0; |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 158 | |
| 159 | if (!udev) |
| 160 | return NULL; |
| 161 | |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 162 | if (eth_start_udev(udev) < 0) { |
| 163 | log_err("Could not start %s\n", udev->name); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | netif_remove(net_lwip_get_netif()); |
| 168 | |
| 169 | ip4_addr_set_zero(&ip); |
| 170 | ip4_addr_set_zero(&mask); |
| 171 | ip4_addr_set_zero(&gw); |
| 172 | |
| 173 | if (with_ip) |
| 174 | if (get_udev_ipv4_info(udev, &ip, &mask, &gw) < 0) |
| 175 | return NULL; |
| 176 | |
| 177 | eth_env_get_enetaddr_by_index("eth", dev_seq(udev), enetaddr); |
| 178 | ret = snprintf(hwstr, MAC_ADDR_STRLEN, "%pM", enetaddr); |
| 179 | if (ret < 0 || ret >= MAC_ADDR_STRLEN) |
| 180 | return NULL; |
| 181 | |
| 182 | netif = calloc(1, sizeof(struct netif)); |
| 183 | if (!netif) |
| 184 | return NULL; |
| 185 | |
| 186 | netif->name[0] = 'e'; |
| 187 | netif->name[1] = 't'; |
| 188 | |
| 189 | string_to_enetaddr(hwstr, netif->hwaddr); |
| 190 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
| 191 | debug("adding lwIP netif for %s with hwaddr:%s ip:%s ", udev->name, |
| 192 | hwstr, ip4addr_ntoa(&ip)); |
| 193 | debug("mask:%s ", ip4addr_ntoa(&mask)); |
| 194 | debug("gw:%s\n", ip4addr_ntoa(&gw)); |
| 195 | |
| 196 | if (!netif_add(netif, &ip, &mask, &gw, udev, net_lwip_if_init, |
| 197 | netif_input)) { |
| 198 | printf("error: netif_add() failed\n"); |
| 199 | free(netif); |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | netif_set_up(netif); |
| 204 | netif_set_link_up(netif); |
| 205 | /* Routing: use this interface to reach the default gateway */ |
| 206 | netif_set_default(netif); |
| 207 | |
| 208 | return netif; |
| 209 | } |
| 210 | |
| 211 | struct netif *net_lwip_new_netif(struct udevice *udev) |
| 212 | { |
| 213 | return new_netif(udev, true); |
| 214 | } |
| 215 | |
| 216 | struct netif *net_lwip_new_netif_noip(struct udevice *udev) |
| 217 | { |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 218 | return new_netif(udev, false); |
| 219 | } |
| 220 | |
| 221 | void net_lwip_remove_netif(struct netif *netif) |
| 222 | { |
| 223 | netif_remove(netif); |
| 224 | free(netif); |
| 225 | } |
| 226 | |
| 227 | int net_init(void) |
| 228 | { |
| 229 | eth_set_current(); |
| 230 | |
| 231 | net_lwip_new_netif(eth_get_dev()); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static struct pbuf *alloc_pbuf_and_copy(uchar *data, int len) |
| 237 | { |
Jerome Forissier | 9708350 | 2024-11-07 12:27:57 +0100 | [diff] [blame] | 238 | struct pbuf *p, *q; |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 239 | |
Jerome Forissier | 9708350 | 2024-11-07 12:27:57 +0100 | [diff] [blame] | 240 | /* We allocate a pbuf chain of pbufs from the pool. */ |
| 241 | p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); |
| 242 | if (!p) { |
| 243 | LINK_STATS_INC(link.memerr); |
| 244 | LINK_STATS_INC(link.drop); |
| 245 | return NULL; |
| 246 | } |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 247 | |
Jerome Forissier | 9708350 | 2024-11-07 12:27:57 +0100 | [diff] [blame] | 248 | for (q = p; q != NULL; q = q->next) { |
| 249 | memcpy(q->payload, data, q->len); |
| 250 | data += q->len; |
| 251 | } |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 252 | |
Jerome Forissier | 9708350 | 2024-11-07 12:27:57 +0100 | [diff] [blame] | 253 | LINK_STATS_INC(link.recv); |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 254 | |
Jerome Forissier | 9708350 | 2024-11-07 12:27:57 +0100 | [diff] [blame] | 255 | return p; |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | int net_lwip_rx(struct udevice *udev, struct netif *netif) |
| 259 | { |
| 260 | struct pbuf *pbuf; |
| 261 | uchar *packet; |
| 262 | int flags; |
| 263 | int len; |
| 264 | int i; |
| 265 | |
| 266 | if (!eth_is_active(udev)) |
| 267 | return -EINVAL; |
| 268 | |
| 269 | flags = ETH_RECV_CHECK_DEVICE; |
| 270 | for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) { |
| 271 | len = eth_get_ops(udev)->recv(udev, flags, &packet); |
| 272 | flags = 0; |
| 273 | |
| 274 | if (len > 0) { |
Jerome Forissier | 5b2e273 | 2025-03-06 15:32:22 +0100 | [diff] [blame] | 275 | if (CONFIG_IS_ENABLED(LWIP_DEBUG_RXTX)) { |
| 276 | printf("net_lwip_tx: %u bytes, udev %s \n", len, |
| 277 | udev->name); |
| 278 | print_hex_dump("net_lwip_rx: ", 0, 16, 1, |
| 279 | packet, len, true); |
| 280 | } |
| 281 | |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 282 | pbuf = alloc_pbuf_and_copy(packet, len); |
| 283 | if (pbuf) |
| 284 | netif->input(pbuf, netif); |
| 285 | } |
| 286 | if (len >= 0 && eth_get_ops(udev)->free_pkt) |
| 287 | eth_get_ops(udev)->free_pkt(udev, packet, len); |
| 288 | if (len <= 0) |
| 289 | break; |
| 290 | } |
| 291 | if (len == -EAGAIN) |
| 292 | len = 0; |
| 293 | |
| 294 | return len; |
| 295 | } |
| 296 | |
| 297 | void net_process_received_packet(uchar *in_packet, int len) |
| 298 | { |
| 299 | #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER) |
| 300 | if (push_packet) |
| 301 | (*push_packet)(in_packet, len); |
| 302 | #endif |
| 303 | } |
| 304 | |
Jerome Forissier | 6a78e96 | 2024-10-16 12:04:05 +0200 | [diff] [blame] | 305 | int net_loop(enum proto_t protocol) |
| 306 | { |
| 307 | char *argv[1]; |
| 308 | |
| 309 | switch (protocol) { |
| 310 | case TFTPGET: |
| 311 | argv[0] = "tftpboot"; |
| 312 | return do_tftpb(NULL, 0, 1, argv); |
| 313 | default: |
| 314 | return -EINVAL; |
| 315 | } |
| 316 | |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | |
Jerome Forissier | 1ff0036 | 2024-10-16 12:04:03 +0200 | [diff] [blame] | 320 | u32_t sys_now(void) |
| 321 | { |
| 322 | return get_timer(0); |
| 323 | } |