2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
34 * Copyright (c) 2010-2013 Xilinx, Inc. All rights reserved.
37 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
38 * COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
39 * ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
40 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
41 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
42 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
43 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
44 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
45 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
46 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
47 * AND FITNESS FOR A PARTICULAR PURPOSE.
54 #include <xparameters.h>
56 #include "xlwipconfig.h"
60 #include "lwip/pbuf.h"
62 #include "lwip/stats.h"
63 #include "lwip/igmp.h"
65 #include "netif/etharp.h"
66 #include "netif/xemacpsif.h"
67 #include "netif/xadapter.h"
68 #include "netif/xpqueue.h"
69 #include "xparameters.h"
75 /* Define those to better describe your network interface. */
80 static err_t xemacpsif_mac_filter_update (struct netif *netif,
81 struct ip_addr *group, u8_t action);
83 static u8_t xemacps_mcast_entry_mask = 0;
86 XEmacPs_Config *mac_config;
88 void FreeTxPBufs(void);
90 * this function is always called with interrupts off
91 * this function also assumes that there are available BD's
93 static err_t _unbuffered_low_level_output(xemacpsif_s *xemacpsif,
99 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
101 status = emacps_sgsend(xemacpsif, p);
102 if (status != XST_SUCCESS) {
104 lwip_stats.link.drop++;
109 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
113 lwip_stats.link.xmit++;
114 #endif /* LINK_STATS */
121 * low_level_output():
123 * Should do the actual transmission of the packet. The packet is
124 * contained in the pbuf that is passed to the function. This pbuf
129 static err_t low_level_output(struct netif *netif, struct pbuf *p)
131 SYS_ARCH_DECL_PROTECT(lev);
134 struct xemac_s *xemac = (struct xemac_s *)(netif->state);
135 xemacpsif_s *xemacpsif = (xemacpsif_s *)(xemac->state);
137 SYS_ARCH_PROTECT(lev);
140 /* check if space is available to send */
141 if (is_tx_space_available(xemacpsif)) {
142 _unbuffered_low_level_output(xemacpsif, p);
146 lwip_stats.link.drop++;
148 print("pack dropped, no space\r\n");
153 SYS_ARCH_UNPROTECT(lev);
160 * Should allocate a pbuf and transfer the bytes of the incoming
161 * packet from the interface into the pbuf.
164 static struct pbuf * low_level_input(struct netif *netif)
166 struct xemac_s *xemac = (struct xemac_s *)(netif->state);
167 xemacpsif_s *xemacpsif = (xemacpsif_s *)(xemac->state);
170 /* see if there is data to process */
171 if (pq_qlength(xemacpsif->recv_q) == 0)
174 /* return one packet from receive q */
175 p = (struct pbuf *)pq_dequeue(xemacpsif->recv_q);
180 * xemacpsif_output():
182 * This function is called by the TCP/IP stack when an IP packet
183 * should be sent. It calls the function called low_level_output() to
184 * do the actual transmission of the packet.
188 static err_t xemacpsif_output(struct netif *netif, struct pbuf *p,
189 struct ip_addr *ipaddr)
191 /* resolve hardware address, then send (or queue) packet */
192 return etharp_output(netif, p, ipaddr);
198 * This function should be called when a packet is ready to be read
199 * from the interface. It uses the function low_level_input() that
200 * should handle the actual reception of bytes from the network
203 * Returns the number of packets read (max 1 packet on success,
204 * 0 if there are no packets)
208 int xemacpsif_input(struct netif *netif)
210 struct eth_hdr *ethhdr;
212 SYS_ARCH_DECL_PROTECT(lev);
214 #ifdef OS_IS_FREERTOS
218 /* move received packet into a new pbuf */
219 SYS_ARCH_PROTECT(lev);
220 p = low_level_input(netif);
221 SYS_ARCH_UNPROTECT(lev);
223 /* no packet could be read, silently ignore this */
228 /* points to packet payload, which starts with an Ethernet header */
232 lwip_stats.link.recv++;
233 #endif /* LINK_STATS */
235 switch (htons(ethhdr->type)) {
236 /* IP or ARP packet? */
241 case ETHTYPE_PPPOEDISC:
243 #endif /* PPPOE_SUPPORT */
244 /* full packet send to tcpip_thread to process */
245 if (netif->input(p, netif) != ERR_OK) {
246 LWIP_DEBUGF(NETIF_DEBUG, ("xemacpsif_input: IP input error\r\n"));
263 static err_t low_level_init(struct netif *netif)
265 unsigned mac_address = (unsigned)(netif->state);
266 struct xemac_s *xemac;
267 xemacpsif_s *xemacpsif;
270 int Status = XST_SUCCESS;
274 xemacpsif = mem_malloc(sizeof *xemacpsif);
275 if (xemacpsif == NULL) {
276 LWIP_DEBUGF(NETIF_DEBUG, ("xemacpsif_init: out of memory\r\n"));
280 xemac = mem_malloc(sizeof *xemac);
282 LWIP_DEBUGF(NETIF_DEBUG, ("xemacpsif_init: out of memory\r\n"));
286 xemac->state = (void *)xemacpsif;
287 xemac->topology_index = xtopology_find_index(mac_address);
288 xemac->type = xemac_type_emacps;
290 xemacpsif->send_q = NULL;
291 xemacpsif->recv_q = pq_create_queue();
292 if (!xemacpsif->recv_q)
295 /* maximum transfer unit */
296 netif->mtu = XEMACPS_MTU - XEMACPS_HDR_SIZE;
299 netif->igmp_mac_filter = xemacpsif_mac_filter_update;
302 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP |
306 netif->flags |= NETIF_FLAG_IGMP;
310 sys_sem_new(&xemac->sem_rx_data_available, 0);
312 /* obtain config of this emac */
313 mac_config = (XEmacPs_Config *)xemacps_lookup_config((unsigned)netif->state);
315 Status = XEmacPs_CfgInitialize(&xemacpsif->emacps, mac_config,
316 mac_config->BaseAddress);
317 if (Status != XST_SUCCESS) {
318 xil_printf("In %s:EmacPs Configuration Failed....\r\n", __func__);
321 /* initialize the mac */
322 init_emacps(xemacpsif, netif);
324 dmacrreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
325 XEMACPS_DMACR_OFFSET);
326 dmacrreg = dmacrreg | (0x00000010);
327 XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
328 XEMACPS_DMACR_OFFSET, dmacrreg);
332 start_emacps(xemacpsif);
334 /* replace the state in netif (currently the emac baseaddress)
335 * with the mac instance pointer.
337 netif->state = (void *)xemac;
342 void HandleEmacPsError(struct xemac_s *xemac)
344 xemacpsif_s *xemacpsif;
345 int Status = XST_SUCCESS;
348 SYS_ARCH_DECL_PROTECT(lev);
349 SYS_ARCH_PROTECT(lev);
352 xemacpsif = (xemacpsif_s *)(xemac->state);
353 Status = XEmacPs_CfgInitialize(&xemacpsif->emacps, mac_config,
354 mac_config->BaseAddress);
355 if (Status != XST_SUCCESS) {
356 xil_printf("In %s:EmacPs Configuration Failed....\r\n", __func__);
358 /* initialize the mac */
359 init_emacps_on_error(xemacpsif, NetIf);
360 dmacrreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
361 XEMACPS_DMACR_OFFSET);
362 dmacrreg = dmacrreg | (0x01000000);
363 XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
364 XEMACPS_DMACR_OFFSET, dmacrreg);
367 start_emacps(xemacpsif);
369 SYS_ARCH_UNPROTECT(lev);
372 void HandleTxErrors(struct xemac_s *xemac)
374 xemacpsif_s *xemacpsif;
377 SYS_ARCH_DECL_PROTECT(lev);
378 SYS_ARCH_PROTECT(lev);
379 xemacpsif = (xemacpsif_s *)(xemac->state);
380 netctrlreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
381 XEMACPS_NWCTRL_OFFSET);
382 netctrlreg = netctrlreg & (~XEMACPS_NWCTRL_TXEN_MASK);
383 XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
384 XEMACPS_NWCTRL_OFFSET, netctrlreg);
387 clean_dma_txdescs(xemac);
388 netctrlreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
389 XEMACPS_NWCTRL_OFFSET);
390 netctrlreg = netctrlreg | (XEMACPS_NWCTRL_TXEN_MASK);
391 XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
392 XEMACPS_NWCTRL_OFFSET, netctrlreg);
393 SYS_ARCH_UNPROTECT(lev);
398 static err_t xemacpsif_mac_filter_update (struct netif *netif, struct ip_addr *group,
408 * Should be called at the beginning of the program to set up the
409 * network interface. It calls the function low_level_init() to do the
410 * actual setup of the hardware.
414 err_t xemacpsif_init(struct netif *netif)
417 /* ifType ethernetCsmacd(6) @see RFC1213 */
418 netif->link_type = 6;
419 /* your link speed here */
420 netif->link_speed = ;
422 netif->ifinoctets = 0;
423 netif->ifinucastpkts = 0;
424 netif->ifinnucastpkts = 0;
425 netif->ifindiscards = 0;
426 netif->ifoutoctets = 0;
427 netif->ifoutucastpkts = 0;
428 netif->ifoutnucastpkts = 0;
429 netif->ifoutdiscards = 0;
432 netif->name[0] = IFNAME0;
433 netif->name[1] = IFNAME1;
434 netif->output = xemacpsif_output;
435 netif->linkoutput = low_level_output;
437 low_level_init(netif);
442 * xemacpsif_resetrx_on_no_rxdata():
444 * Should be called by the user at regular intervals, typically
445 * from a timer (100 msecond). This is to provide a SW workaround
446 * for the HW bug (SI #692601). Please refer to the function header
447 * for the function resetrx_on_no_rxdata in xemacpsif_dma.c to
448 * know more about the SI.
452 void xemacpsif_resetrx_on_no_rxdata(struct netif *netif)
454 struct xemac_s *xemac = (struct xemac_s *)(netif->state);
455 xemacpsif_s *xemacpsif = (xemacpsif_s *)(xemac->state);
457 resetrx_on_no_rxdata(xemacpsif);