1 /**
2  * @file
3  * Ethernet Interface Skeleton
4  *
5  */
6 
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38 
39 #include "lwip/opt.h"
40 
41 #include "lwip/def.h"
42 #include "lwip/mem.h"
43 #include "lwip/pbuf.h"
44 #include "lwip/stats.h"
45 #include "lwip/snmp.h"
46 #include "lwip/ethip6.h"
47 #include "netif/etharp.h"
48 #include <stdio.h>
49 #include <string.h>
50 
51 #include "esp_eth.h"
52 #include "esp_netif.h"
53 #include "esp_netif_net_stack.h"
54 #include "esp_compiler.h"
55 
56 /* Define those to better describe your network interface. */
57 #define IFNAME0 'e'
58 #define IFNAME1 'n'
59 
60 /**
61  * @brief Free resources allocated in L2 layer
62  *
63  * @param buf memory alloc in L2 layer
64  * @note this function is also the callback when invoke pbuf_free
65  */
ethernet_free_rx_buf_l2(struct netif * netif,void * buf)66 static void ethernet_free_rx_buf_l2(struct netif *netif, void *buf)
67 {
68     free(buf);
69 }
70 
71 /**
72  * In this function, the hardware should be initialized.
73  * Invoked by ethernetif_init().
74  *
75  * @param netif lwip network interface which has already been initialized
76  */
ethernet_low_level_init(struct netif * netif)77 static void ethernet_low_level_init(struct netif *netif)
78 {
79     /* set MAC hardware address length */
80     netif->hwaddr_len = ETHARP_HWADDR_LEN;
81 
82     /* maximum transfer unit */
83     netif->mtu = 1500;
84 
85     /* device capabilities */
86     /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
87     netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
88 
89 #if ESP_LWIP
90 #if LWIP_IGMP
91     netif->flags |= NETIF_FLAG_IGMP;
92 #endif
93 #endif
94 }
95 
96 /**
97  * @brief This function should do the actual transmission of the packet. The packet is
98  * contained in the pbuf that is passed to the function. This pbuf might be chained.
99  *
100  * @param netif lwip network interface structure for this ethernetif
101  * @param p MAC packet to send (e.g. IP packet including MAC addresses and type)
102  * @return err_t ERR_OK if the packet has been sent to Ethernet DMA buffer successfully
103  *               ERR_MEM if private data couldn't be allocated
104  *               ERR_IF if netif is not supported
105  *               ERR_ABORT if there's some wrong when send pbuf payload to DMA buffer
106  */
ethernet_low_level_output(struct netif * netif,struct pbuf * p)107 static err_t ethernet_low_level_output(struct netif *netif, struct pbuf *p)
108 {
109     struct pbuf *q = p;
110     esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
111     esp_err_t ret = ESP_FAIL;
112     if (!esp_netif) {
113         LWIP_DEBUGF(NETIF_DEBUG, ("corresponding esp-netif is NULL: netif=%p pbuf=%p len=%d\n", netif, p, p->len));
114         return ERR_IF;
115     }
116 
117     if (q->next == NULL) {
118         ret = esp_netif_transmit(esp_netif, q->payload, q->len);
119     } else {
120         LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug"));
121         q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM);
122         if (q != NULL) {
123 #if ESP_LWIP
124             /* This pbuf RAM was not allocated on layer2, no extra free operation needed in pbuf_free */
125             q->l2_owner = NULL;
126             q->l2_buf = NULL;
127 #endif
128             pbuf_copy(q, p);
129         } else {
130             return ERR_MEM;
131         }
132         ret = esp_netif_transmit(esp_netif, q->payload, q->len);
133         /* content in payload has been copied to DMA buffer, it's safe to free pbuf now */
134         pbuf_free(q);
135     }
136     /* Check error */
137     if (likely(ret == ESP_OK)) {
138         return ERR_OK;
139     } else if (ret == ESP_ERR_NO_MEM) {
140         return ERR_MEM;
141     }
142     return ERR_IF;
143 }
144 
145 /**
146  * @brief This function should be called when a packet is ready to be read
147  * from the interface. It uses the function low_level_input() that
148  * should handle the actual reception of bytes from the network
149  * interface. Then the type of the received packet is determined and
150  * the appropriate input function is called.
151  *
152  * @param netif lwip network interface structure for this ethernetif
153  * @param buffer ethernet buffer
154  * @param len length of buffer
155  */
ethernetif_input(void * h,void * buffer,size_t len,void * eb)156 void ethernetif_input(void *h, void *buffer, size_t len, void *eb)
157 {
158     struct netif *netif = h;
159     struct pbuf *p;
160 
161     if (unlikely(buffer == NULL || !netif_is_up(netif))) {
162         if (buffer) {
163             ethernet_free_rx_buf_l2(netif, buffer);
164         }
165         return;
166     }
167 
168     /* acquire new pbuf, type: PBUF_REF */
169     p = pbuf_alloc(PBUF_RAW, len, PBUF_REF);
170     if (p == NULL) {
171         ethernet_free_rx_buf_l2(netif, buffer);
172         return;
173     }
174     p->payload = buffer;
175 #if ESP_LWIP
176     p->l2_owner = netif;
177     p->l2_buf = buffer;
178 #endif
179     /* full packet send to tcpip_thread to process */
180     if (unlikely(netif->input(p, netif) != ERR_OK)) {
181         LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
182         pbuf_free(p);
183     }
184     /* the pbuf will be free in upper layer, eg: ethernet_input */
185 }
186 
187 /**
188  * Set up the network interface. It calls the function low_level_init() to do the
189  * actual init work of the hardware.
190  *
191  * This function should be passed as a parameter to netif_add().
192  *
193  * @param netif the lwip network interface structure for this ethernetif
194  * @return ERR_OK if the ethernetif is initialized
195  */
ethernetif_init(struct netif * netif)196 err_t ethernetif_init(struct netif *netif)
197 {
198     LWIP_ASSERT("netif != NULL", (netif != NULL));
199     /* Have to get the esp-netif handle from netif first and then driver==ethernet handle from there */
200     esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
201     esp_eth_handle_t eth_handle = esp_netif_get_io_driver(esp_netif);
202     /* Initialize interface hostname */
203 #if LWIP_NETIF_HOSTNAME
204 #if ESP_LWIP
205     if (esp_netif_get_hostname(esp_netif, &netif->hostname) != ESP_OK) {
206         netif->hostname = CONFIG_LWIP_LOCAL_HOSTNAME;
207     }
208 #else
209     netif->hostname = "lwip";
210 #endif
211 
212 #endif /* LWIP_NETIF_HOSTNAME */
213 
214     /* Initialize the snmp variables and counters inside the struct netif. */
215     eth_speed_t speed;
216 
217     esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed);
218     if (speed == ETH_SPEED_100M) {
219         NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
220     } else {
221         NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 10000000);
222     }
223 
224     netif->name[0] = IFNAME0;
225     netif->name[1] = IFNAME1;
226     netif->output = etharp_output;
227 #if LWIP_IPV6
228     netif->output_ip6 = ethip6_output;
229 #endif /* LWIP_IPV6 */
230     netif->linkoutput = ethernet_low_level_output;
231     netif->l2_buffer_free_notify = ethernet_free_rx_buf_l2;
232 
233     ethernet_low_level_init(netif);
234 
235     return ERR_OK;
236 }
237