blob: 7fc43feb0d67257709b9df9b762a92e7ab9135c7 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#if WITH_LWIP
2/*
3 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
26 * OF SUCH DAMAGE.
27 *
28 * This file is part of the lwIP TCP/IP stack.
29 *
30 * Author: Adam Dunkels <adam@sics.se>
31 *
32 */
33
34/*
35 * This file is a skeleton for developing Ethernet network interface
36 * drivers for lwIP. Add code to the low_level functions and do a
37 * search-and-replace for the word "ethernetif" to replace it with
38 * something that better describes your network interface.
39 */
40/*
41 * ARMEMU bits
42 * Copyright (c) 2006 Travis Geiselbrecht
43 *
44 * Permission is hereby granted, free of charge, to any person obtaining
45 * a copy of this software and associated documentation files
46 * (the "Software"), to deal in the Software without restriction,
47 * including without limitation the rights to use, copy, modify, merge,
48 * publish, distribute, sublicense, and/or sell copies of the Software,
49 * and to permit persons to whom the Software is furnished to do so,
50 * subject to the following conditions:
51 *
52 * The above copyright notice and this permission notice shall be
53 * included in all copies or substantial portions of the Software.
54 *
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
58 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
59 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
60 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
61 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62 */
63
64#include <malloc.h>
65#include <dev/ethernet.h>
66#include <err.h>
67#include <reg.h>
68#include <string.h>
69#include <platform/interrupts.h>
70#include <platform/armemu/memmap.h>
71
72#include "lwip/opt.h"
73#include "lwip/def.h"
74#include "lwip/mem.h"
75#include "lwip/pbuf.h"
76#include "lwip/sys.h"
77#include <lwip/stats.h>
78
79#include "netif/etharp.h"
80
81/* Define those to better describe your network interface. */
82#define IFNAME0 'e'
83#define IFNAME1 'n'
84
85struct ethernetif {
86 struct eth_addr *ethaddr;
87 /* Add whatever per-interface state that is needed here. */
88};
89
90static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
91
92/* Forward declarations. */
93static void ethernetif_input(struct netif *netif);
94static err_t ethernetif_output(struct netif *netif, struct pbuf *p,
95 struct ip_addr *ipaddr);
96
97static void
98low_level_init(struct netif *netif)
99{
100 struct ethernetif *ethernetif = netif->state;
101
102 /* set MAC hardware address length */
103 netif->hwaddr_len = 6;
104
105 /* set MAC hardware address */
106 netif->hwaddr[0] = 0;
107 netif->hwaddr[1] = 0x01;
108 netif->hwaddr[2] = 0x02;
109 netif->hwaddr[3] = 0x03;
110 netif->hwaddr[4] = 0x04;
111 netif->hwaddr[5] = 0x05;
112
113 /* maximum transfer unit */
114 netif->mtu = 1500;
115
116 /* broadcast capability */
117 netif->flags = NETIF_FLAG_BROADCAST;
118
119 /* Do whatever else is needed to initialize interface. */
120}
121
122/*
123 * low_level_output():
124 *
125 * Should do the actual transmission of the packet. The packet is
126 * contained in the pbuf that is passed to the function. This pbuf
127 * might be chained.
128 *
129 */
130
131static err_t
132low_level_output(struct netif *netif, struct pbuf *p)
133{
134 struct ethernetif *ethernetif = netif->state;
135 struct pbuf *q;
136 int i;
137 int j;
138
139#if ETH_PAD_SIZE
140 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
141#endif
142
143 /* XXX maybe just a mutex? */
144 enter_critical_section();
145
146 i = 0;
147 for (q = p; q != NULL; q = q->next) {
148 /* Send the data from the pbuf to the interface, one pbuf at a
149 time. The size of the data in each pbuf is kept in the ->len
150 variable. */
151// debug_dump_memory_bytes(q->payload, q->len);
152 for (j = 0; j < q->len; j++)
153 *REG8(NET_OUT_BUF + i + j) = ((unsigned char *)q->payload)[j];
154 i += q->len;
155 }
156
157 *REG(NET_SEND_LEN) = i;
158 *REG(NET_SEND) = 1;
159
160 exit_critical_section();
161
162#if ETH_PAD_SIZE
163 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
164#endif
165
166#if LINK_STATS
167 lwip_stats.link.xmit++;
168#endif /* LINK_STATS */
169
170 return ERR_OK;
171}
172
173/*
174 * low_level_input():
175 *
176 * Should allocate a pbuf and transfer the bytes of the incoming
177 * packet from the interface into the pbuf.
178 *
179 */
180
181static struct pbuf *
182low_level_input(struct netif *netif) {
183 struct ethernetif *ethernetif = netif->state;
184 struct pbuf *p, *q;
185 u16_t len;
186 int i;
187 int head, tail;
188
189 /* get the head and tail pointers from the ethernet interface */
190 head = *REG(NET_HEAD);
191 tail = *REG(NET_TAIL);
192
193 if (tail == head)
194 return NULL; // false alarm
195
196 /* Obtain the size of the packet and put it into the "len"
197 variable. */
198 len = *REG(NET_IN_BUF_LEN);
199
200#if ETH_PAD_SIZE
201 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
202#endif
203
204 /* We allocate a pbuf chain of pbufs from the pool. */
205 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
206 if (p != NULL) {
207
208#if ETH_PAD_SIZE
209 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
210#endif
211
212 /* We iterate over the pbuf chain until we have read the entire
213 * packet into the pbuf. */
214 int pos = 0;
215 for (q = p; q != NULL; q = q->next) {
216 /* Read enough bytes to fill this pbuf in the chain. The
217 * available data in the pbuf is given by the q->len
218 * variable. */
219 for (i=0; i < q->len; i++) {
220 ((unsigned char *)q->payload)[i] = *REG8(NET_IN_BUF + pos);
221 pos++;
222 }
223 }
224
225#if ETH_PAD_SIZE
226 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
227#endif
228
229#if LINK_STATS
230 lwip_stats.link.recv++;
231#endif /* LINK_STATS */
232 } else {
233#if LINK_STATS
234 lwip_stats.link.memerr++;
235 lwip_stats.link.drop++;
236#endif /* LINK_STATS */
237 }
238
239 /* push the tail pointer up by one, giving the buffer back to the hardware */
240 *REG(NET_TAIL) = (tail + 1) % NET_IN_BUF_COUNT;
241
242 return p;
243}
244
245/*
246 * ethernetif_output():
247 *
248 * This function is called by the TCP/IP stack when an IP packet
249 * should be sent. It calls the function called low_level_output() to
250 * do the actual transmission of the packet.
251 *
252 */
253
254static err_t
255ethernetif_output(struct netif *netif, struct pbuf *p,
256 struct ip_addr *ipaddr)
257{
258// dprintf("ethernetif_output: netif %p, pbuf %p, ipaddr %p\n", netif, p, ipaddr);
259
260 /* resolve hardware address, then send (or queue) packet */
261 return etharp_output(netif, ipaddr, p);
262
263}
264
265/*
266 * ethernetif_input():
267 *
268 * This function should be called when a packet is ready to be read
269 * from the interface. It uses the function low_level_input() that
270 * should handle the actual reception of bytes from the network
271 * interface.
272 *
273 */
274
275static void
276ethernetif_input(struct netif *netif)
277{
278 struct ethernetif *ethernetif;
279 struct eth_hdr *ethhdr;
280 struct pbuf *p;
281
282 ethernetif = netif->state;
283
284 /* move received packet into a new pbuf */
285 p = low_level_input(netif);
286 /* no packet could be read, silently ignore this */
287 if (p == NULL) return;
288 /* points to packet payload, which starts with an Ethernet header */
289 ethhdr = p->payload;
290
291#if LINK_STATS
292 lwip_stats.link.recv++;
293#endif /* LINK_STATS */
294
295 ethhdr = p->payload;
296
297// dprintf("ethernetif_input: type 0x%x\n", htons(ethhdr->type));
298
299 switch (htons(ethhdr->type)) {
300 /* IP packet? */
301 case ETHTYPE_IP:
302 /* update ARP table */
303 etharp_ip_input(netif, p);
304 /* skip Ethernet header */
305 pbuf_header(p, -sizeof(struct eth_hdr));
306 /* pass to network layer */
307 netif->input(p, netif);
308 break;
309
310 case ETHTYPE_ARP:
311 /* pass p to ARP module */
312 etharp_arp_input(netif, ethernetif->ethaddr, p);
313 break;
314 default:
315 pbuf_free(p);
316 p = NULL;
317 break;
318 }
319}
320
321static enum handler_return ethernet_int(void *arg)
322{
323 struct netif *netif = (struct netif *)arg;
324
325 ethernetif_input(netif);
326
327 return INT_RESCHEDULE;
328}
329
330
331
332/*
333 * ethernetif_init():
334 *
335 * Should be called at the beginning of the program to set up the
336 * network interface. It calls the function low_level_init() to do the
337 * actual setup of the hardware.
338 *
339 */
340
341static err_t
342ethernetif_init(struct netif *netif)
343{
344 struct ethernetif *ethernetif;
345
346 ethernetif = mem_malloc(sizeof(struct ethernetif));
347
348 if (ethernetif == NULL) {
349 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
350 return ERR_MEM;
351 }
352
353 netif->state = ethernetif;
354 netif->name[0] = IFNAME0;
355 netif->name[1] = IFNAME1;
356 netif->output = ethernetif_output;
357 netif->linkoutput = low_level_output;
358
359 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
360
361 low_level_init(netif);
362
363 return ERR_OK;
364}
365
366status_t ethernet_init(void)
367{
368 /* check to see if the ethernet feature is turned on */
369 if ((*REG(SYSINFO_FEATURES) & SYSINFO_FEATURE_NETWORK) == 0)
370 return ERR_NOT_FOUND;
371
372 struct netif *netif = calloc(sizeof(struct netif), 1);
373 struct ip_addr *ipaddr = calloc(sizeof(struct ip_addr), 1);
374 struct ip_addr *netmask = calloc(sizeof(struct ip_addr), 1);
375 struct ip_addr *gw = calloc(sizeof(struct ip_addr), 1);
376
377 struct netif *netifret = netif_add(netif, ipaddr, netmask, gw, NULL, &ethernetif_init, &ip_input);
378 if (netifret == NULL) {
379 free(netif);
380 free(ipaddr);
381 free(netmask);
382 free(gw);
383 return ERR_NOT_FOUND;
384 }
385
386 /* register for interrupt handlers */
387 register_int_handler(INT_NET, ethernet_int, netif);
388
389 netif_set_default(netif);
390
391 unmask_interrupt(INT_NET, NULL);
392
393 return NO_ERROR;
394}
395
396#endif // WITH_LWIP