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