xemacpsif.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * Copyright (C) 2010 - 2019 Xilinx, Inc.
  3. * Copyright (C) 2021 WangHuachen.
  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. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <xparameters.h>
  34. #include "lwipopts.h"
  35. #include "xlwipconfig.h"
  36. #include "lwip/opt.h"
  37. #include "lwip/def.h"
  38. #include "lwip/mem.h"
  39. #include "lwip/pbuf.h"
  40. #include "lwip/sys.h"
  41. #include "lwip/stats.h"
  42. #include "lwip/igmp.h"
  43. #include "netif/etharp.h"
  44. #include "netif/xemacpsif.h"
  45. #include "netif/xadapter.h"
  46. #include "netif/xpqueue.h"
  47. #include "xparameters.h"
  48. // #include "xscugic.h"
  49. #include "xemacps.h"
  50. #if LWIP_IPV6
  51. #include "lwip/ethip6.h"
  52. #endif
  53. #ifdef SYS_ARCH_DECL_PROTECT
  54. #undef SYS_ARCH_DECL_PROTECT
  55. #endif
  56. #define SYS_ARCH_DECL_PROTECT(lev) rt_base_t lev;
  57. #ifdef SYS_ARCH_PROTECT
  58. #undef SYS_ARCH_PROTECT
  59. #endif
  60. #define SYS_ARCH_PROTECT(lev) lev = rt_hw_interrupt_disable();
  61. #ifdef SYS_ARCH_UNPROTECT
  62. #undef SYS_ARCH_UNPROTECT
  63. #endif
  64. #define SYS_ARCH_UNPROTECT(lev) rt_hw_interrupt_enable(lev);
  65. /* Define those to better describe your network interface. */
  66. #define IFNAME0 't'
  67. #define IFNAME1 'e'
  68. #if LWIP_IGMP
  69. static err_t xemacpsif_mac_filter_update (struct netif *netif,
  70. ip_addr_t *group, u8_t action);
  71. static u8_t xemacps_mcast_entry_mask = 0;
  72. #endif
  73. #if LWIP_IPV6 && LWIP_IPV6_MLD
  74. static err_t xemacpsif_mld6_mac_filter_update (struct netif *netif,
  75. ip_addr_t *group, u8_t action);
  76. static u8_t xemacps_mld6_mcast_entry_mask;
  77. #endif
  78. XEmacPs_Config *mac_config;
  79. struct netif *NetIf;
  80. #if defined(OS_IS_FREERTOS) && defined(__arm__) && !defined(ARMR5)
  81. int32_t lExpireCounter = 0;
  82. #define RESETRXTIMEOUT 10
  83. #endif
  84. /*
  85. * this function is always called with interrupts off
  86. * this function also assumes that there are available BD's
  87. */
  88. err_t _unbuffered_low_level_output(xemacpsif_s *xemacpsif,
  89. struct pbuf *p)
  90. {
  91. XStatus status = 0;
  92. #if ETH_PAD_SIZE
  93. pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
  94. #endif
  95. status = emacps_sgsend(xemacpsif, p);
  96. if (status != XST_SUCCESS) {
  97. #if LINK_STATS
  98. lwip_stats.link.drop++;
  99. #endif
  100. }
  101. #if ETH_PAD_SIZE
  102. pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
  103. #endif
  104. #if LINK_STATS
  105. lwip_stats.link.xmit++;
  106. #endif /* LINK_STATS */
  107. return ERR_OK;
  108. }
  109. /*
  110. * low_level_output():
  111. *
  112. * Should do the actual transmission of the packet. The packet is
  113. * contained in the pbuf that is passed to the function. This pbuf
  114. * might be chained.
  115. *
  116. */
  117. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  118. {
  119. SYS_ARCH_DECL_PROTECT(lev);
  120. err_t err;
  121. s32_t freecnt;
  122. XEmacPs_BdRing *txring;
  123. struct xemac_s *xemac = (struct xemac_s *)(netif->state);
  124. xemacpsif_s *xemacpsif = (xemacpsif_s *)(xemac->state);
  125. SYS_ARCH_PROTECT(lev);
  126. /* check if space is available to send */
  127. freecnt = is_tx_space_available(xemacpsif);
  128. if (freecnt <= 5) {
  129. txring = &(XEmacPs_GetTxRing(&xemacpsif->emacps));
  130. process_sent_bds(xemacpsif, txring);
  131. }
  132. if (is_tx_space_available(xemacpsif)) {
  133. _unbuffered_low_level_output(xemacpsif, p);
  134. err = ERR_OK;
  135. } else {
  136. #if LINK_STATS
  137. lwip_stats.link.drop++;
  138. #endif
  139. rt_kprintf("pack dropped, no space\r\n");
  140. err = ERR_MEM;
  141. }
  142. SYS_ARCH_UNPROTECT(lev);
  143. return err;
  144. }
  145. /*
  146. * low_level_input():
  147. *
  148. * Should allocate a pbuf and transfer the bytes of the incoming
  149. * packet from the interface into the pbuf.
  150. *
  151. */
  152. static struct pbuf * low_level_input(struct netif *netif)
  153. {
  154. struct xemac_s *xemac = (struct xemac_s *)(netif->state);
  155. xemacpsif_s *xemacpsif = (xemacpsif_s *)(xemac->state);
  156. struct pbuf *p;
  157. /* see if there is data to process */
  158. if (pq_qlength(xemacpsif->recv_q) == 0)
  159. return NULL;
  160. /* return one packet from receive q */
  161. p = (struct pbuf *)pq_dequeue(xemacpsif->recv_q);
  162. return p;
  163. }
  164. /*
  165. * xemacpsif_output():
  166. *
  167. * This function is called by the TCP/IP stack when an IP packet
  168. * should be sent. It calls the function called low_level_output() to
  169. * do the actual transmission of the packet.
  170. *
  171. */
  172. static err_t xemacpsif_output(struct netif *netif, struct pbuf *p,
  173. const ip_addr_t *ipaddr)
  174. {
  175. /* resolve hardware address, then send (or queue) packet */
  176. return etharp_output(netif, p, ipaddr);
  177. }
  178. /*
  179. * xemacpsif_input():
  180. *
  181. * This function should be called when a packet is ready to be read
  182. * from the interface. It uses the function low_level_input() that
  183. * should handle the actual reception of bytes from the network
  184. * interface.
  185. *
  186. * Returns the number of packets read (max 1 packet on success,
  187. * 0 if there are no packets)
  188. *
  189. */
  190. s32_t xemacpsif_input(struct netif *netif)
  191. {
  192. struct eth_hdr *ethhdr;
  193. struct pbuf *p;
  194. SYS_ARCH_DECL_PROTECT(lev);
  195. #ifdef OS_IS_FREERTOS
  196. while (1)
  197. #endif
  198. {
  199. /* move received packet into a new pbuf */
  200. SYS_ARCH_PROTECT(lev);
  201. p = low_level_input(netif);
  202. SYS_ARCH_UNPROTECT(lev);
  203. /* no packet could be read, silently ignore this */
  204. if (p == NULL) {
  205. return 0;
  206. }
  207. /* points to packet payload, which starts with an Ethernet header */
  208. ethhdr = p->payload;
  209. #if LINK_STATS
  210. lwip_stats.link.recv++;
  211. #endif /* LINK_STATS */
  212. switch (htons(ethhdr->type)) {
  213. /* IP or ARP packet? */
  214. case ETHTYPE_IP:
  215. case ETHTYPE_ARP:
  216. #if LWIP_IPV6
  217. /*IPv6 Packet?*/
  218. case ETHTYPE_IPV6:
  219. #endif
  220. #if PPPOE_SUPPORT
  221. /* PPPoE packet? */
  222. case ETHTYPE_PPPOEDISC:
  223. case ETHTYPE_PPPOE:
  224. #endif /* PPPOE_SUPPORT */
  225. /* full packet send to tcpip_thread to process */
  226. if (netif->input(p, netif) != ERR_OK) {
  227. LWIP_DEBUGF(NETIF_DEBUG, ("xemacpsif_input: IP input error\r\n"));
  228. pbuf_free(p);
  229. p = NULL;
  230. }
  231. break;
  232. default:
  233. pbuf_free(p);
  234. p = NULL;
  235. break;
  236. }
  237. }
  238. return 1;
  239. }
  240. #if defined(OS_IS_FREERTOS) && defined(__arm__) && !defined(ARMR5)
  241. void vTimerCallback( TimerHandle_t pxTimer )
  242. {
  243. /* Do something if the pxTimer parameter is NULL */
  244. configASSERT(pxTimer);
  245. lExpireCounter++;
  246. /* If the timer has expired 100 times then reset RX */
  247. if(lExpireCounter >= RESETRXTIMEOUT) {
  248. lExpireCounter = 0;
  249. xemacpsif_resetrx_on_no_rxdata(NetIf);
  250. }
  251. }
  252. #endif
  253. static err_t low_level_init(struct netif *netif)
  254. {
  255. UINTPTR mac_address = (UINTPTR)(netif->state);
  256. struct xemac_s *xemac;
  257. xemacpsif_s *xemacpsif;
  258. u32 dmacrreg;
  259. s32_t status = XST_SUCCESS;
  260. NetIf = netif;
  261. xemacpsif = mem_malloc(sizeof *xemacpsif);
  262. if (xemacpsif == NULL) {
  263. LWIP_DEBUGF(NETIF_DEBUG, ("xemacpsif_init: out of memory\r\n"));
  264. return ERR_MEM;
  265. }
  266. xemac = mem_malloc(sizeof *xemac);
  267. if (xemac == NULL) {
  268. LWIP_DEBUGF(NETIF_DEBUG, ("xemacpsif_init: out of memory\r\n"));
  269. return ERR_MEM;
  270. }
  271. xemac->state = (void *)xemacpsif;
  272. xemac->topology_index = xtopology_find_index(mac_address);
  273. xemac->type = xemac_type_emacps;
  274. xemacpsif->send_q = NULL;
  275. xemacpsif->recv_q = pq_create_queue();
  276. if (!xemacpsif->recv_q)
  277. return ERR_MEM;
  278. /* maximum transfer unit */
  279. #ifdef ZYNQMP_USE_JUMBO
  280. netif->mtu = XEMACPS_MTU_JUMBO - XEMACPS_HDR_SIZE;
  281. #else
  282. netif->mtu = XEMACPS_MTU - XEMACPS_HDR_SIZE;
  283. #endif
  284. #if LWIP_IGMP
  285. netif->igmp_mac_filter = xemacpsif_mac_filter_update;
  286. #endif
  287. #if LWIP_IPV6 && LWIP_IPV6_MLD
  288. netif->mld_mac_filter = xemacpsif_mld6_mac_filter_update;
  289. #endif
  290. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP |
  291. NETIF_FLAG_LINK_UP;
  292. #if LWIP_IPV6 && LWIP_IPV6_MLD
  293. netif->flags |= NETIF_FLAG_MLD6;
  294. #endif
  295. #if LWIP_IGMP
  296. netif->flags |= NETIF_FLAG_IGMP;
  297. #endif
  298. /* obtain config of this emac */
  299. mac_config = (XEmacPs_Config *)xemacps_lookup_config((unsigned)(UINTPTR)netif->state);
  300. #if EL1_NONSECURE
  301. /* Request device to indicate that this library is using it */
  302. if (mac_config->BaseAddress == VERSAL_EMACPS_0_BASEADDR) {
  303. Xil_Smc(PM_REQUEST_DEVICE_SMC_FID, DEV_GEM_0, 1, 0, 100, 1, 0, 0);
  304. }
  305. if (mac_config->BaseAddress == VERSAL_EMACPS_0_BASEADDR) {
  306. Xil_Smc(PM_REQUEST_DEVICE_SMC_FID, DEV_GEM_1, 1, 0, 100, 1, 0, 0);
  307. }
  308. #endif
  309. status = XEmacPs_CfgInitialize(&xemacpsif->emacps, mac_config,
  310. mac_config->BaseAddress);
  311. if (status != XST_SUCCESS) {
  312. xil_printf("In %s:EmacPs Configuration Failed....\r\n", __func__);
  313. }
  314. /* initialize the mac */
  315. init_emacps(xemacpsif, netif);
  316. dmacrreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
  317. XEMACPS_DMACR_OFFSET);
  318. dmacrreg = dmacrreg | (0x00000010);
  319. XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
  320. XEMACPS_DMACR_OFFSET, dmacrreg);
  321. #if defined(OS_IS_FREERTOS) && defined(__arm__) && !defined(ARMR5)
  322. /* Freertos tick is 10ms by default; set period to the same */
  323. xemac->xTimer = xTimerCreate("Timer", 10, pdTRUE, ( void * ) 1, vTimerCallback);
  324. if (xemac->xTimer == NULL) {
  325. xil_printf("In %s:Timer creation failed....\r\n", __func__);
  326. } else {
  327. if(xTimerStart(xemac->xTimer, 0) != pdPASS) {
  328. xil_printf("In %s:Timer start failed....\r\n", __func__);
  329. }
  330. }
  331. #endif
  332. setup_isr(xemac);
  333. init_dma(xemac);
  334. start_emacps(xemacpsif);
  335. /* replace the state in netif (currently the emac baseaddress)
  336. * with the mac instance pointer.
  337. */
  338. netif->state = (void *)xemac;
  339. return ERR_OK;
  340. }
  341. void HandleEmacPsError(struct xemac_s *xemac)
  342. {
  343. xemacpsif_s *xemacpsif;
  344. s32_t status = XST_SUCCESS;
  345. u32 dmacrreg;
  346. SYS_ARCH_DECL_PROTECT(lev);
  347. SYS_ARCH_PROTECT(lev);
  348. xemacpsif = (xemacpsif_s *)(xemac->state);
  349. free_txrx_pbufs(xemacpsif);
  350. status = XEmacPs_CfgInitialize(&xemacpsif->emacps, mac_config,
  351. mac_config->BaseAddress);
  352. if (status != XST_SUCCESS) {
  353. xil_printf("In %s:EmacPs Configuration Failed....\r\n", __func__);
  354. }
  355. /* initialize the mac */
  356. init_emacps_on_error(xemacpsif, NetIf);
  357. dmacrreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
  358. XEMACPS_DMACR_OFFSET);
  359. dmacrreg = dmacrreg | (0x01000000);
  360. XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
  361. XEMACPS_DMACR_OFFSET, dmacrreg);
  362. setup_isr(xemac);
  363. init_dma(xemac);
  364. start_emacps(xemacpsif);
  365. SYS_ARCH_UNPROTECT(lev);
  366. }
  367. void HandleTxErrors(struct xemac_s *xemac)
  368. {
  369. xemacpsif_s *xemacpsif;
  370. u32 netctrlreg;
  371. SYS_ARCH_DECL_PROTECT(lev);
  372. SYS_ARCH_PROTECT(lev);
  373. xemacpsif = (xemacpsif_s *)(xemac->state);
  374. netctrlreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
  375. XEMACPS_NWCTRL_OFFSET);
  376. netctrlreg = netctrlreg & (~XEMACPS_NWCTRL_TXEN_MASK);
  377. XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
  378. XEMACPS_NWCTRL_OFFSET, netctrlreg);
  379. free_onlytx_pbufs(xemacpsif);
  380. clean_dma_txdescs(xemac);
  381. netctrlreg = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress,
  382. XEMACPS_NWCTRL_OFFSET);
  383. netctrlreg = netctrlreg | (XEMACPS_NWCTRL_TXEN_MASK);
  384. XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress,
  385. XEMACPS_NWCTRL_OFFSET, netctrlreg);
  386. SYS_ARCH_UNPROTECT(lev);
  387. }
  388. #if LWIP_IPV6 && LWIP_IPV6_MLD
  389. static u8_t xemacpsif_ip6_addr_ismulticast(ip6_addr_t* ip_addr)
  390. {
  391. if(ip6_addr_ismulticast_linklocal(ip_addr)||
  392. ip6_addr_ismulticast_iflocal(ip_addr) ||
  393. ip6_addr_ismulticast_adminlocal(ip_addr)||
  394. ip6_addr_ismulticast_sitelocal(ip_addr) ||
  395. ip6_addr_ismulticast_orglocal(ip_addr) ||
  396. ip6_addr_ismulticast_global(ip_addr)) {
  397. /*Return TRUE if IPv6 is Multicast type*/
  398. return TRUE;
  399. } else {
  400. return FALSE;
  401. }
  402. }
  403. static void xemacpsif_mld6_mac_hash_update (struct netif *netif, u8_t *ip_addr,
  404. u8_t action)
  405. {
  406. u8_t multicast_mac_addr[6];
  407. struct xemac_s *xemac = (struct xemac_s *) (netif->state);
  408. xemacpsif_s *xemacpsif = (xemacpsif_s *) (xemac->state);
  409. XEmacPs_BdRing *txring;
  410. txring = &(XEmacPs_GetTxRing(&xemacpsif->emacps));
  411. multicast_mac_addr[0] = LL_IP6_MULTICAST_ADDR_0;
  412. multicast_mac_addr[1] = LL_IP6_MULTICAST_ADDR_1;
  413. multicast_mac_addr[2] = ip_addr[12];
  414. multicast_mac_addr[3] = ip_addr[13];
  415. multicast_mac_addr[4] = ip_addr[14];
  416. multicast_mac_addr[5] = ip_addr[15];
  417. /* Wait till all sent packets are acknowledged from HW */
  418. while(txring->HwCnt);
  419. SYS_ARCH_DECL_PROTECT(lev);
  420. SYS_ARCH_PROTECT(lev);
  421. /* Stop Ethernet */
  422. XEmacPs_Stop(&xemacpsif->emacps);
  423. if (action == NETIF_ADD_MAC_FILTER) {
  424. /* Set Mulitcast mac address in hash table */
  425. XEmacPs_SetHash(&xemacpsif->emacps, multicast_mac_addr);
  426. } else if (action == NETIF_DEL_MAC_FILTER) {
  427. /* Remove Mulitcast mac address in hash table */
  428. XEmacPs_DeleteHash(&xemacpsif->emacps, multicast_mac_addr);
  429. }
  430. /* Reset DMA */
  431. reset_dma(xemac);
  432. /* Start Ethernet */
  433. XEmacPs_Start(&xemacpsif->emacps);
  434. SYS_ARCH_UNPROTECT(lev);
  435. }
  436. static err_t xemacpsif_mld6_mac_filter_update (struct netif *netif, ip_addr_t *group,
  437. u8_t action)
  438. {
  439. u8_t temp_mask;
  440. unsigned int i;
  441. u8_t * ip_addr = (u8_t *) group;
  442. if(!(xemacpsif_ip6_addr_ismulticast((ip6_addr_t*) ip_addr))) {
  443. LWIP_DEBUGF(NETIF_DEBUG,
  444. ("%s: The requested MAC address is not a multicast address.\r\n", __func__)); LWIP_DEBUGF(NETIF_DEBUG,
  445. ("Multicast address add operation failure !!\r\n"));
  446. return ERR_ARG;
  447. }
  448. if (action == NETIF_ADD_MAC_FILTER) {
  449. for (i = 0; i < XEMACPS_MAX_MAC_ADDR; i++) {
  450. temp_mask = (0x01) << i;
  451. if ((xemacps_mld6_mcast_entry_mask & temp_mask) == temp_mask) {
  452. continue;
  453. }
  454. xemacps_mld6_mcast_entry_mask |= temp_mask;
  455. /* Update mac address in hash table */
  456. xemacpsif_mld6_mac_hash_update(netif, ip_addr, action);
  457. LWIP_DEBUGF(NETIF_DEBUG,
  458. ("%s: Multicast MAC address successfully added.\r\n", __func__));
  459. return ERR_OK;
  460. }
  461. LWIP_DEBUGF(NETIF_DEBUG,
  462. ("%s: No multicast address registers left.\r\n", __func__));
  463. LWIP_DEBUGF(NETIF_DEBUG,
  464. ("Multicast MAC address add operation failure !!\r\n"));
  465. return ERR_MEM;
  466. } else if (action == NETIF_DEL_MAC_FILTER) {
  467. for (i = 0; i < XEMACPS_MAX_MAC_ADDR; i++) {
  468. temp_mask = (0x01) << i;
  469. if ((xemacps_mld6_mcast_entry_mask & temp_mask) != temp_mask) {
  470. continue;
  471. }
  472. xemacps_mld6_mcast_entry_mask &= (~temp_mask);
  473. /* Update mac address in hash table */
  474. xemacpsif_mld6_mac_hash_update(netif, ip_addr, action);
  475. LWIP_DEBUGF(NETIF_DEBUG,
  476. ("%s: Multicast MAC address successfully removed.\r\n", __func__));
  477. return ERR_OK;
  478. }
  479. LWIP_DEBUGF(NETIF_DEBUG,
  480. ("%s: No multicast address registers present with\r\n", __func__));
  481. LWIP_DEBUGF(NETIF_DEBUG,
  482. ("the requested Multicast MAC address.\r\n"));
  483. LWIP_DEBUGF(NETIF_DEBUG,
  484. ("Multicast MAC address removal failure!!.\r\n"));
  485. return ERR_MEM;
  486. }
  487. return ERR_ARG;
  488. }
  489. #endif
  490. #if LWIP_IGMP
  491. static void xemacpsif_mac_hash_update (struct netif *netif, u8_t *ip_addr,
  492. u8_t action)
  493. {
  494. u8_t multicast_mac_addr[6];
  495. struct xemac_s *xemac = (struct xemac_s *) (netif->state);
  496. xemacpsif_s *xemacpsif = (xemacpsif_s *) (xemac->state);
  497. XEmacPs_BdRing *txring;
  498. txring = &(XEmacPs_GetTxRing(&xemacpsif->emacps));
  499. multicast_mac_addr[0] = 0x01;
  500. multicast_mac_addr[1] = 0x00;
  501. multicast_mac_addr[2] = 0x5E;
  502. multicast_mac_addr[3] = ip_addr[1] & 0x7F;
  503. multicast_mac_addr[4] = ip_addr[2];
  504. multicast_mac_addr[5] = ip_addr[3];
  505. /* Wait till all sent packets are acknowledged from HW */
  506. while(txring->HwCnt);
  507. SYS_ARCH_DECL_PROTECT(lev);
  508. SYS_ARCH_PROTECT(lev);
  509. /* Stop Ethernet */
  510. XEmacPs_Stop(&xemacpsif->emacps);
  511. if (action == IGMP_ADD_MAC_FILTER) {
  512. /* Set Mulitcast mac address in hash table */
  513. XEmacPs_SetHash(&xemacpsif->emacps, multicast_mac_addr);
  514. } else if (action == IGMP_DEL_MAC_FILTER) {
  515. /* Remove Mulitcast mac address in hash table */
  516. XEmacPs_DeleteHash(&xemacpsif->emacps, multicast_mac_addr);
  517. }
  518. /* Reset DMA */
  519. reset_dma(xemac);
  520. /* Start Ethernet */
  521. XEmacPs_Start(&xemacpsif->emacps);
  522. SYS_ARCH_UNPROTECT(lev);
  523. }
  524. static err_t xemacpsif_mac_filter_update (struct netif *netif, ip_addr_t *group,
  525. u8_t action)
  526. {
  527. u8_t temp_mask;
  528. unsigned int i;
  529. u8_t * ip_addr = (u8_t *) group;
  530. if ((ip_addr[0] < 224) && (ip_addr[0] > 239)) {
  531. LWIP_DEBUGF(NETIF_DEBUG,
  532. ("%s: The requested MAC address is not a multicast address.\r\n", __func__));
  533. LWIP_DEBUGF(NETIF_DEBUG,
  534. ("Multicast address add operation failure !!\r\n"));
  535. return ERR_ARG;
  536. }
  537. if (action == IGMP_ADD_MAC_FILTER) {
  538. for (i = 0; i < XEMACPS_MAX_MAC_ADDR; i++) {
  539. temp_mask = (0x01) << i;
  540. if ((xemacps_mcast_entry_mask & temp_mask) == temp_mask) {
  541. continue;
  542. }
  543. xemacps_mcast_entry_mask |= temp_mask;
  544. /* Update mac address in hash table */
  545. xemacpsif_mac_hash_update(netif, ip_addr, action);
  546. LWIP_DEBUGF(NETIF_DEBUG,
  547. ("%s: Multicast MAC address successfully added.\r\n", __func__));
  548. return ERR_OK;
  549. }
  550. if (i == XEMACPS_MAX_MAC_ADDR) {
  551. LWIP_DEBUGF(NETIF_DEBUG,
  552. ("%s: No multicast address registers left.\r\n", __func__));
  553. LWIP_DEBUGF(NETIF_DEBUG,
  554. ("Multicast MAC address add operation failure !!\r\n"));
  555. return ERR_MEM;
  556. }
  557. } else if (action == IGMP_DEL_MAC_FILTER) {
  558. for (i = 0; i < XEMACPS_MAX_MAC_ADDR; i++) {
  559. temp_mask = (0x01) << i;
  560. if ((xemacps_mcast_entry_mask & temp_mask) != temp_mask) {
  561. continue;
  562. }
  563. xemacps_mcast_entry_mask &= (~temp_mask);
  564. /* Update mac address in hash table */
  565. xemacpsif_mac_hash_update(netif, ip_addr, action);
  566. LWIP_DEBUGF(NETIF_DEBUG,
  567. ("%s: Multicast MAC address successfully removed.\r\n", __func__));
  568. return ERR_OK;
  569. }
  570. if (i == XEMACPS_MAX_MAC_ADDR) {
  571. LWIP_DEBUGF(NETIF_DEBUG,
  572. ("%s: No multicast address registers present with\r\n", __func__));
  573. LWIP_DEBUGF(NETIF_DEBUG,
  574. ("the requested Multicast MAC address.\r\n"));
  575. LWIP_DEBUGF(NETIF_DEBUG,
  576. ("Multicast MAC address removal failure!!.\r\n"));
  577. return ERR_MEM;
  578. }
  579. }
  580. return ERR_OK;
  581. }
  582. #endif
  583. /*
  584. * xemacpsif_init():
  585. *
  586. * Should be called at the beginning of the program to set up the
  587. * network interface. It calls the function low_level_init() to do the
  588. * actual setup of the hardware.
  589. *
  590. */
  591. err_t xemacpsif_init(struct netif *netif)
  592. {
  593. #if LWIP_SNMP
  594. /* ifType ethernetCsmacd(6) @see RFC1213 */
  595. netif->link_type = 6;
  596. /* your link speed here */
  597. netif->link_speed = ;
  598. netif->ts = 0;
  599. netif->ifinoctets = 0;
  600. netif->ifinucastpkts = 0;
  601. netif->ifinnucastpkts = 0;
  602. netif->ifindiscards = 0;
  603. netif->ifoutoctets = 0;
  604. netif->ifoutucastpkts = 0;
  605. netif->ifoutnucastpkts = 0;
  606. netif->ifoutdiscards = 0;
  607. #endif
  608. netif->name[0] = IFNAME0;
  609. netif->name[1] = IFNAME1;
  610. netif->output = xemacpsif_output;
  611. netif->linkoutput = low_level_output;
  612. #if LWIP_IPV6
  613. netif->output_ip6 = ethip6_output;
  614. #endif
  615. low_level_init(netif);
  616. return ERR_OK;
  617. }
  618. /*
  619. * xemacpsif_resetrx_on_no_rxdata():
  620. *
  621. * Should be called by the user at regular intervals, typically
  622. * from a timer (100 msecond). This is to provide a SW workaround
  623. * for the HW bug (SI #692601). Please refer to the function header
  624. * for the function resetrx_on_no_rxdata in xemacpsif_dma.c to
  625. * know more about the SI.
  626. *
  627. */
  628. void xemacpsif_resetrx_on_no_rxdata(struct netif *netif)
  629. {
  630. struct xemac_s *xemac = (struct xemac_s *)(netif->state);
  631. xemacpsif_s *xemacpsif = (xemacpsif_s *)(xemac->state);
  632. resetrx_on_no_rxdata(xemacpsif);
  633. }