blob: b863047f5989ba03761bfc61e2a0eb4c3d576ced [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{
Jerome Forissier91596572024-11-18 15:31:25 +010094 char ipstr[] = "ipaddr\0\0";
95 char maskstr[] = "netmask\0\0";
96 char gwstr[] = "gatewayip\0\0";
Jerome Forissier1ff00362024-10-16 12:04:03 +020097 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{
Jerome Forissier1ff00362024-10-16 12:04:03 +0200206 return new_netif(udev, false);
207}
208
209void net_lwip_remove_netif(struct netif *netif)
210{
211 netif_remove(netif);
212 free(netif);
213}
214
215int net_init(void)
216{
217 eth_set_current();
218
219 net_lwip_new_netif(eth_get_dev());
220
221 return 0;
222}
223
224static struct pbuf *alloc_pbuf_and_copy(uchar *data, int len)
225{
Jerome Forissier97083502024-11-07 12:27:57 +0100226 struct pbuf *p, *q;
Jerome Forissier1ff00362024-10-16 12:04:03 +0200227
Jerome Forissier97083502024-11-07 12:27:57 +0100228 /* We allocate a pbuf chain of pbufs from the pool. */
229 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
230 if (!p) {
231 LINK_STATS_INC(link.memerr);
232 LINK_STATS_INC(link.drop);
233 return NULL;
234 }
Jerome Forissier1ff00362024-10-16 12:04:03 +0200235
Jerome Forissier97083502024-11-07 12:27:57 +0100236 for (q = p; q != NULL; q = q->next) {
237 memcpy(q->payload, data, q->len);
238 data += q->len;
239 }
Jerome Forissier1ff00362024-10-16 12:04:03 +0200240
Jerome Forissier97083502024-11-07 12:27:57 +0100241 LINK_STATS_INC(link.recv);
Jerome Forissier1ff00362024-10-16 12:04:03 +0200242
Jerome Forissier97083502024-11-07 12:27:57 +0100243 return p;
Jerome Forissier1ff00362024-10-16 12:04:03 +0200244}
245
246int net_lwip_rx(struct udevice *udev, struct netif *netif)
247{
248 struct pbuf *pbuf;
249 uchar *packet;
250 int flags;
251 int len;
252 int i;
253
254 if (!eth_is_active(udev))
255 return -EINVAL;
256
257 flags = ETH_RECV_CHECK_DEVICE;
258 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
259 len = eth_get_ops(udev)->recv(udev, flags, &packet);
260 flags = 0;
261
262 if (len > 0) {
263 pbuf = alloc_pbuf_and_copy(packet, len);
264 if (pbuf)
265 netif->input(pbuf, netif);
266 }
267 if (len >= 0 && eth_get_ops(udev)->free_pkt)
268 eth_get_ops(udev)->free_pkt(udev, packet, len);
269 if (len <= 0)
270 break;
271 }
272 if (len == -EAGAIN)
273 len = 0;
274
275 return len;
276}
277
278void net_process_received_packet(uchar *in_packet, int len)
279{
280#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
281 if (push_packet)
282 (*push_packet)(in_packet, len);
283#endif
284}
285
Jerome Forissier6a78e962024-10-16 12:04:05 +0200286int net_loop(enum proto_t protocol)
287{
288 char *argv[1];
289
290 switch (protocol) {
291 case TFTPGET:
292 argv[0] = "tftpboot";
293 return do_tftpb(NULL, 0, 1, argv);
294 default:
295 return -EINVAL;
296 }
297
298 return -EINVAL;
299}
300
Jerome Forissier1ff00362024-10-16 12:04:03 +0200301u32_t sys_now(void)
302{
303 return get_timer(0);
304}