static void MACNET_rx_free ( /* [IN] the PCB to enqueue */ PCB_PTR pcb_ptr ) { ENET_CONTEXT_STRUCT_PTR enet_ptr = (ENET_CONTEXT_STRUCT_PTR)pcb_ptr->PRIVATE; MACNET_CONTEXT_STRUCT_PTR macnet_context_ptr = (MACNET_CONTEXT_STRUCT_PTR) enet_ptr->MAC_CONTEXT_PTR; #if ENETCFG_SUPPORT_FRAGMENTED_PCBS uint_32 i; #endif /* ** This function can be called from any context, and it needs mutual ** exclusion with itself. */ MACNET_int_disable(); #if ENETCFG_SUPPORT_FRAGMENTED_PCBS for (i=0;pcb_ptr->FRAG[i].FRAGMENT;i++) { ENET_Enqueue_Buffer((pointer*)&macnet_context_ptr->RX_BUFFERS, pcb_ptr->FRAG[i].FRAGMENT ); pcb_ptr->FRAG[i].FRAGMENT = NULL; } #else ENET_Enqueue_Buffer((pointer*)&macnet_context_ptr->RX_BUFFERS, pcb_ptr->FRAG[0].FRAGMENT ); pcb_ptr->FRAG[0].FRAGMENT = NULL; #endif QADD(macnet_context_ptr->RxPCBHead, macnet_context_ptr->RxPCBTail, pcb_ptr); MACNET_add_buffers_to_rx_ring(macnet_context_ptr); MACNET_int_enable(); }
static void MACNET_rx_free_specific ( MACNET_CONTEXT_STRUCT_PTR macnet_context_ptr, /* [IN] the PCB to enqueue */ PCB_PTR pcb_ptr, pointer * list ) { /* ** This function can be called from any context, and it needs mutual ** exclusion with itself. */ MACNET_int_disable(); ENET_Enqueue_Buffer(list, pcb_ptr->FRAG[0].FRAGMENT ); pcb_ptr->FRAG[0].FRAGMENT = NULL; QADD(macnet_context_ptr->RxPCBHead, macnet_context_ptr->RxPCBTail, pcb_ptr); MACNET_int_enable(); }
uint32_t MACNET_initialize ( ENET_CONTEXT_STRUCT_PTR enet_ptr ) { MACNET_CONTEXT_STRUCT_PTR macnet_context_ptr=NULL; ENET_MemMapPtr macnet_ptr; VENET_BD_STRUCT_PTR bd_ptr; unsigned char *buf_ptr; MACNET_RX_PCB_PTR pcb_ptr; uint32_t i, rxsize, txsize, ssize, lsize, pcbsize,rx_pcb_size, large_packet_size, rcr; uint32_t timeout, error = ENET_OK; bool bOK; #if ENETCFG_SUPPORT_PTP MACNET_PTP_PRIVATE_PTR macnet_ptp_ptr = NULL; #endif /* ENETCFG_SUPPORT_PTP */ // Initialize the MACNET I/O Pins MACNET_io_init(enet_ptr->PARAM_PTR->ENET_IF->MAC_NUMBER); macnet_ptr = MACNET_get_base_address(enet_ptr->PARAM_PTR->ENET_IF->MAC_NUMBER); if (macnet_ptr == NULL) { return ENETERR_INVALID_DEVICE; } // currently limit number of TX BDs to 32, as a bitmask is used in the free function. if (enet_ptr->PARAM_PTR->NUM_TX_ENTRIES > 32) { return ENETERR_INVALID_INIT_PARAM; } /* ** This function can be called from any context, and it needs mutual ** exclusion with itself. */ MACNET_int_disable(); macnet_context_ptr = _mem_alloc_system_zero(sizeof(MACNET_CONTEXT_STRUCT)); IF_ERROR_EXIT((NULL==macnet_context_ptr), ENETERR_ALLOC_MAC_CONTEXT); _mem_set_type((void *)macnet_context_ptr, MEM_TYPE_IO_ENET_MAC_CONTEXT_STRUCT); enet_ptr->MAC_CONTEXT_PTR = (void *) macnet_context_ptr; macnet_context_ptr->MACNET_ADDRESS = macnet_ptr; macnet_context_ptr->PHY_PTR = MACNET_get_base_address(enet_ptr->PARAM_PTR->ENET_IF->PHY_NUMBER); /* Stop the chip */ macnet_ptr->ECR = ENET_ECR_RESET_MASK; /* wait until the initialization cycle completes */ timeout = 0; while ((macnet_ptr->ECR & ENET_ECR_RESET_MASK) && (timeout<MACNET_RESET_TIMEOUT)){ _time_delay(1); timeout++; } IF_ERROR_EXIT((macnet_ptr->ECR & ENET_ECR_RESET_MASK), ENETERR_INIT_FAILED); /* Disable all MACNET interrupts */ macnet_ptr->EIMR = 0; /* clear any pending interrpts */ macnet_ptr->EIR = ENET_EIR_ALL_PENDING; macnet_context_ptr->NumRxBDs = enet_ptr->PARAM_PTR->NUM_RX_ENTRIES; macnet_context_ptr->NumTxBDs = enet_ptr->PARAM_PTR->NUM_TX_ENTRIES; // Compute aligned buffer sizes if (enet_ptr->PARAM_PTR->TX_BUFFER_SIZE) { macnet_context_ptr->AlignedTxBufferSize = MACNET_TX_ALIGN(enet_ptr->PARAM_PTR->TX_BUFFER_SIZE); } else { macnet_context_ptr->AlignedTxBufferSize = MACNET_TX_ALIGN(enet_ptr->MaxTxFrameSize); } if (enet_ptr->PARAM_PTR->RX_BUFFER_SIZE) { macnet_context_ptr->AlignedRxBufferSize = MACNET_RX_ALIGN(enet_ptr->PARAM_PTR->RX_BUFFER_SIZE); } else { macnet_context_ptr->AlignedRxBufferSize = MACNET_RX_ALIGN(enet_ptr->MaxRxFrameSize); } // Allocate the Transmit and Receive buffer descriptors // TODO remake to using alloc_align fn #if BSPCFG_HAS_SRAM_POOL && BSPCFG_ENET_SRAM_BUF bd_ptr = (VENET_BD_STRUCT_PTR)_mem_alloc_system_zero_from(_BSP_sram_pool, (sizeof(ENET_BD_STRUCT)*(macnet_context_ptr->NumRxBDs+macnet_context_ptr->NumTxBDs))+MACNET_BD_ALIGNMENT); #else bd_ptr = (VENET_BD_STRUCT_PTR)_mem_alloc_system_zero_uncached((sizeof(ENET_BD_STRUCT)*(macnet_context_ptr->NumRxBDs+macnet_context_ptr->NumTxBDs))+MACNET_BD_ALIGNMENT); #endif IF_ERROR_EXIT((NULL == bd_ptr), ENETERR_ALLOC_BD); _mem_set_type((void *)bd_ptr, MEM_TYPE_IO_BD_STRUCT); macnet_context_ptr->UNALIGNED_RING_PTR = (void *) bd_ptr; macnet_context_ptr->MACNET_RX_RING_PTR = (VENET_BD_STRUCT_PTR) MACNET_BD_ALIGN((uint32_t)bd_ptr); macnet_context_ptr->MACNET_TX_RING_PTR = &macnet_context_ptr->MACNET_RX_RING_PTR[macnet_context_ptr->NumRxBDs]; /* Set wrap bit in last BD */ macnet_context_ptr->MACNET_RX_RING_PTR[macnet_context_ptr->NumRxBDs - 1].CONTROL = HOST_TO_BE_SHORT_CONST(ENET_BD_ETHER_RX_WRAP); macnet_context_ptr->MACNET_TX_RING_PTR[macnet_context_ptr->NumTxBDs - 1].CONTROL = HOST_TO_BE_SHORT_CONST(ENET_BD_ETHER_TX_WRAP); macnet_context_ptr->AvailableTxBDs = macnet_context_ptr->NumTxBDs; // Allocate array to hold Transmit PCB pointers while they are queued for transmission macnet_context_ptr->TxPCBS_PTR = (PCB_PTR *) _mem_alloc_system_zero(sizeof(PCB_PTR)*macnet_context_ptr->NumTxBDs); IF_ERROR_EXIT((NULL==macnet_context_ptr->TxPCBS_PTR), ENETERR_ALLOC_PCB); _mem_set_type((void *)macnet_context_ptr->TxPCBS_PTR, MEM_TYPE_IO_PCB_PTR); // Allocate the Receive PCBs rx_pcb_size = sizeof(MACNET_RX_PCB); pcbsize = enet_ptr->PARAM_PTR->NUM_RX_PCBS * rx_pcb_size; macnet_context_ptr->RX_PCB_BASE = (MACNET_RX_PCB_PTR) _mem_alloc_system_zero(pcbsize); IF_ERROR_EXIT((NULL==macnet_context_ptr->RX_PCB_BASE), ENETERR_ALLOC_PCB); _mem_set_type((void *)macnet_context_ptr->RX_PCB_BASE, MEM_TYPE_IO_PCB_STRUCT); // Allocate the Transmit and Receive buffers txsize = (enet_ptr->PARAM_PTR->NUM_TX_BUFFERS * macnet_context_ptr->AlignedTxBufferSize) + MACNET_TX_BUFFER_ALIGNMENT; rxsize = (enet_ptr->PARAM_PTR->NUM_RX_BUFFERS * macnet_context_ptr->AlignedRxBufferSize) +MACNET_RX_BUFFER_ALIGNMENT; ssize = (enet_ptr->PARAM_PTR->NUM_SMALL_BUFFERS * MACNET_SMALL_PACKET_SIZE); large_packet_size = enet_ptr->PARAM_PTR->OPTIONS&ENET_OPTION_VLAN ? ENET_FRAMESIZE_VLAN : ENET_FRAMESIZE; lsize = (enet_ptr->PARAM_PTR->NUM_LARGE_BUFFERS * large_packet_size); #if BSPCFG_HAS_SRAM_POOL && BSPCFG_ENET_SRAM_BUF buf_ptr = _mem_alloc_system_from(_BSP_sram_pool, rxsize + txsize + ssize + lsize); #else buf_ptr = _mem_alloc_system_uncached(rxsize + txsize + ssize + lsize); // temporary fix for cache problems... previously _mem_alloc_system #endif IF_ERROR_EXIT((NULL==buf_ptr), ENETERR_ALLOC_BUFFERS); _mem_set_type(buf_ptr, MEM_TYPE_IO_ENET_BUFFERS); macnet_context_ptr->UNALIGNED_BUFFERS = buf_ptr; // Align to TX buffer boundary buf_ptr = (unsigned char *)MACNET_TX_ALIGN((uint32_t)buf_ptr); // Queue packets on TX Buffer Q. macnet_context_ptr->TX_BUFFERS = NULL; for (i=0;i<enet_ptr->PARAM_PTR->NUM_TX_BUFFERS;i++) { ENET_Enqueue_Buffer((void **)&macnet_context_ptr->TX_BUFFERS, buf_ptr); buf_ptr += macnet_context_ptr->AlignedTxBufferSize; } // Align to RX buffer boundary buf_ptr = (unsigned char *)MACNET_RX_ALIGN((uint32_t)buf_ptr); // Queue packets on RX Buffer Q. macnet_context_ptr->RX_BUFFERS = NULL; for (i=0;i<enet_ptr->PARAM_PTR->NUM_RX_BUFFERS;i++) { ENET_Enqueue_Buffer((void **)&macnet_context_ptr->RX_BUFFERS, buf_ptr); buf_ptr += macnet_context_ptr->AlignedRxBufferSize; } // Queue small packets on small buffer Q. for (i=0;i<enet_ptr->PARAM_PTR->NUM_SMALL_BUFFERS;i++) { ENET_Enqueue_Buffer((void **)&macnet_context_ptr->SMALL_BUFFERS, buf_ptr); buf_ptr += MACNET_SMALL_PACKET_SIZE; } // Queue large packets on large buffer Q. for (i=0;i<enet_ptr->PARAM_PTR->NUM_LARGE_BUFFERS;i++) { ENET_Enqueue_Buffer((void **)&macnet_context_ptr->LARGE_BUFFERS, buf_ptr); buf_ptr += large_packet_size; } // Enqueue the RX PCBs onto the receive PCB queue pcb_ptr = macnet_context_ptr->RX_PCB_BASE; for (i = 0; i < enet_ptr->PARAM_PTR->NUM_RX_PCBS; i++) { QADD(macnet_context_ptr->RxPCBHead, macnet_context_ptr->RxPCBTail, (PCB_PTR) pcb_ptr); pcb_ptr++; } // Fill up the receive ring MACNET_add_buffers_to_rx_ring(macnet_context_ptr); /* Program this station's Ethernet physical address */ macnet_ptr->PALR = (uint32_t)((enet_ptr->ADDRESS[0] << 24)|(enet_ptr->ADDRESS[1] << 16)|(enet_ptr->ADDRESS[2] << 8)|(enet_ptr->ADDRESS[3] << 0)); macnet_ptr->PAUR = (uint32_t)((enet_ptr->ADDRESS[4] << 24)|(enet_ptr->ADDRESS[5] << 16)); // Clear the individual hash table registers macnet_ptr->IAUR = 0; macnet_ptr->IALR = 0; // Clear the group hash table registers macnet_ptr->GAUR = 0; macnet_ptr->GALR = 0; /* Program receive buffer size */ macnet_ptr->MRBR = macnet_context_ptr->AlignedRxBufferSize; // Configure start of Rx and Tx BD rings macnet_ptr->RDSR = (uint32_t)(macnet_context_ptr->MACNET_RX_RING_PTR); macnet_ptr->TDSR = (uint32_t)(macnet_context_ptr->MACNET_TX_RING_PTR); // Set Receive Frame size // NOTE: Oddly, the Receive Control Register (RCR) afmacnetts the transmit side too. The RCR is used to determine if the // transmitter is babbling, which means, if the RX buffer size < Tx Buffer size, we can get babling transmitter // errors if we set RCR to the maximum Receive frame length. We really have no choice but to set RCR to one // of ENET_FRAMESIZE or ENET_FRAMESIZE_VLAN. // rcr = ENET_RCR_MII_MODE_MASK | (ENET_RCR_MAX_FL_MASK & (large_packet_size << ENET_RCR_MAX_FL_SHIFT)); if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_RMII) { rcr |= ENET_RCR_RMII_MODE_MASK; } else if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_7WIRE) { rcr &= ~ENET_RCR_MII_MODE_MASK; } if (enet_ptr->PARAM_PTR->MODE & ENET_10M) { rcr |= ENET_RCR_RMII_10T_MASK; } macnet_ptr->RCR = rcr; if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_MAC_LOOPBACK) { macnet_ptr->RCR |= ENET_RCR_LOOP_MASK; } // Set Full/Half duplex based on mode. if (enet_ptr->PARAM_PTR->MODE & ENET_HALF_DUPLEX) { macnet_ptr->TCR = 0; // half duplex } else { macnet_ptr->TCR = 4; // full duplex } // Enable MII_SPEED register i = (MACNET_device[enet_ptr->PARAM_PTR->ENET_IF->MAC_NUMBER].BUS_CLOCK / enet_ptr->PARAM_PTR->ENET_IF->PHY_MII_SPEED + 1) & ~1; if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_NO_PREAMBLE) { i |= ENET_MSCR_DIS_PRE_MASK; } macnet_context_ptr->PHY_PTR->MSCR = i; // Zero counters macnet_ptr->MIBC |= ENET_MIBC_MIB_CLEAR_MASK; // Install the ISRs bOK = MACNET_install_isrs( enet_ptr, &MACNET_device[enet_ptr->PARAM_PTR->ENET_IF->MAC_NUMBER] ); IF_ERROR_EXIT(!bOK, ENETERR_INSTALL_ISR); // Unmask transmit/receive interrupts // NOTE: need to enable both RXF and RXB, but only TXB, as RXB does not get generated with RXF, // but TXB does get generated with TXF // However, on 52259, enabling RXB is resulting in an HBERR interrupt. RXB is not required, so leave it disabled. macnet_ptr->EIMR = ENET_EIR_TXB_MASK | ENET_EIR_RXF_MASK; // Enable MACNET macnet_ptr->ECR = (ENET_ECR_ETHEREN_MASK | ENET_ECR_EN1588_MASK);// | 0x100; // Discover PHY address if PHY_DISCOVER option is set if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_PHY_DISCOVER) { bOK = (*enet_ptr->PARAM_PTR->ENET_IF->PHY_IF->DISCOVER)(enet_ptr); IF_ERROR_EXIT(!bOK, ENETERR_INIT_FAILED); } else { // Set Phy address from initialization parameter enet_ptr->PHY_ADDRESS = enet_ptr->PARAM_PTR->ENET_IF->PHY_ADDRESS; } /******* Support of TCP/IP offload engine.*******/ /* Transmit Accelerator Function Configuration. */ macnet_ptr->TACC = 0; #if BSPCFG_ENET_HW_TX_IP_CHECKSUM if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_HW_TX_IP_CHECKSUM) { macnet_ptr->TACC |= ENET_TACC_IPCHK_MASK; /* Insert IP header checksum.*/ } #endif #if BSPCFG_ENET_HW_TX_PROTOCOL_CHECKSUM if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_HW_TX_PROTOCOL_CHECKSUM) { macnet_ptr->TACC |= ENET_TACC_PROCHK_MASK; /* Insert Protocol checksum.*/ } #endif /* Receive Accelerator Function Configuration. */ macnet_ptr->RACC = 0; #if BSPCFG_ENET_HW_RX_IP_CHECKSUM if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_HW_RX_IP_CHECKSUM) { macnet_ptr->RACC |= ENET_RACC_IPDIS_MASK; /* Discard of frames with wrong IPv4 header checksum.*/ } #endif #if BSPCFG_ENET_HW_RX_PROTOCOL_CHECKSUM if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_HW_RX_PROTOCOL_CHECKSUM) { macnet_ptr->RACC |= ENET_RACC_PRODIS_MASK; /* Discard of frames with wrong protocol checksum.*/ } #endif #if BSPCFG_ENET_HW_RX_MAC_ERR if (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_HW_RX_MAC_ERR) { macnet_ptr->RACC |= ENET_RACC_LINEDIS_MASK; /* Discard of frames with MAC layer errors.*/ } #endif if ( #if BSPCFG_ENET_HW_TX_IP_CHECKSUM || BSPCFG_ENET_HW_TX_PROTOCOL_CHECKSUM (enet_ptr->PARAM_PTR->OPTIONS & (ENET_OPTION_HW_TX_IP_CHECKSUM|ENET_OPTION_HW_TX_PROTOCOL_CHECKSUM))|| #endif (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_STORE_AND_FORW) ) { /* Transmit FIFO Watermark Register. * The TFWR[STRFWD] bit must be set to use the checksum feature. * In this case, the MAC starts to transmit data only when a complete * frame is stored in the transmit FIFO.*/ macnet_ptr->TFWR = ENET_TFWR_STRFWD_MASK; } if ( #if BSPCFG_ENET_HW_RX_IP_CHECKSUM || BSPCFG_ENET_HW_RX_PROTOCOL_CHECKSUM (enet_ptr->PARAM_PTR->OPTIONS & (ENET_OPTION_HW_RX_IP_CHECKSUM|ENET_OPTION_HW_RX_PROTOCOL_CHECKSUM))|| #endif (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_STORE_AND_FORW) ) { /* Discarding is only available when the RX FIFO operates in store and forward * mode (RSFL cleared).*/ macnet_ptr->RSFL = 0; } // Perform Phy initialization bOK = (*enet_ptr->PARAM_PTR->ENET_IF->PHY_IF->INIT)(enet_ptr); IF_ERROR_EXIT(!bOK, ENETERR_INIT_FAILED); // Signals the MACNET that empty buffers are available. // It is NECESSARY to do this AFTER enabling the MACNET. macnet_ptr->RDAR = ENET_RDAR_RDAR_MASK; //jm _mem_copy((void*)0x400d0000, (void*)0x3f400000, 0x400); #if ENETCFG_SUPPORT_PTP macnet_ptp_ptr = _mem_alloc_system_zero(sizeof(MACNET_PTP_PRIVATE)); IF_ERROR_EXIT((NULL==macnet_context_ptr), ENETERR_ALLOC_MAC_CONTEXT); macnet_ptp_ptr->MACNET_PTR = macnet_context_ptr->MACNET_ADDRESS; macnet_ptp_ptr->PTIMER_PRESENT = 1; bOK = _lwevent_create(&(macnet_ptp_ptr->LWEVENT_PTP), 0); IF_ERROR_EXIT(bOK, ENETERR_1588_LWEVENT); macnet_ptp_ptr->TXSTAMP = (MACNET_PTP_TIME){0,0}; macnet_ptp_ptr->L2PCKS_PTR = NULL; macnet_context_ptr->PTP_PRIV = macnet_ptp_ptr; MACNET_ptp_init(enet_ptr); if (macnet_ptp_ptr->PTIMER_PRESENT) { /* Set Timer count */ MACNET_ptp_start(macnet_ptp_ptr, (enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_PTP_MASTER_CLK)); if(enet_ptr->PARAM_PTR->OPTIONS & ENET_OPTION_PTP_MASTER_CLK) MACNET_ptp_set_master_base_address(macnet_ptr); macnet_ptp_ptr->MACNET_PTR->ECR |= ENET_ECR_EN1588_MASK; } /* Enable timer-relative interrupts */ macnet_ptp_ptr->MACNET_PTR->EIMR |= (ENET_EIR_TS_TIMER_MASK | ENET_EIR_TS_AVAIL_MASK); #endif /* ENETCFG_SUPPORT_PTP */ // control transfers to this point on any error, with error set to error code. EXIT: if (ENET_OK!=error) { #if BSPCFG_ENET_RESTORE MACNET_uninstall_all_isrs(enet_ptr); #endif MACNET_free_context(macnet_context_ptr); } MACNET_int_enable(); return error; }
/*FUNCTION*------------------------------------------------------------- * * Function Name : MACNET_process_rx_bds * Returned Value : void * Comments : * Processes received buffers. * * NOTE: For MACNET, a frame consists of one or more buffers. * For ENET, a packet consists of one or more fragments * Therefore, a frame = a packet, and a buffer = a fragment. * * *END*-----------------------------------------------------------------*/ static void MACNET_process_rx_bds ( /* [IN] the Ethernet state structure */ ENET_CONTEXT_STRUCT_PTR enet_ptr ) { /* Body */ MACNET_CONTEXT_STRUCT_PTR macnet_context_ptr = (MACNET_CONTEXT_STRUCT_PTR) enet_ptr->MAC_CONTEXT_PTR; VENET_BD_STRUCT_PTR bd_ptr; PCB_PTR pcb_ptr=NULL; uint_16 rx_status; uint_32 i,length; int_32 ilength; uchar_ptr buffer, small_packet, large_packet; boolean buffer_is_valid, consumed, first, last; //_DCACHE_INVALIDATE(); // temporary - I can't find fast actual part of memory for cache invalidation /* Dequeue all received buffers (fragments) */ while (macnet_context_ptr->ActiveRxBDs > 0) { bd_ptr = &macnet_context_ptr->MACNET_RX_RING_PTR[macnet_context_ptr->NextRxBD]; _DCACHE_INVALIDATE_MBYTES((pointer)bd_ptr, sizeof(ENET_BD_STRUCT)); if ((bd_ptr->CONTROL & HOST_TO_BE_SHORT_CONST(ENET_BD_ETHER_RX_EMPTY)) != 0) { break; // Currently being written to by MII } BD_INC(macnet_context_ptr->NextRxBD,macnet_context_ptr->NumRxBDs); macnet_context_ptr->ActiveRxBDs--; ENET_INC_STATS(COMMON.ST_RX_TOTAL); rx_status = SHORT_BE_TO_HOST(bd_ptr->CONTROL); length = SHORT_BE_TO_HOST(bd_ptr->LENGTH); buffer = (uchar_ptr)LONG_BE_TO_HOST((uint_32)(bd_ptr->BUFFER)); //buffer = LONG_BE_TO_HOST(bd_ptr->BUFFER); consumed = FALSE; first = macnet_context_ptr->CurrentRxFrag == 0; last = TRUE; // Start by validating the received buffer // a buffer is valid if: // (it is error free) AND ( (it is the first buffer in a frame) OR (it is a subsequent buffer in a frame we want ) ) // buffer_is_valid = FALSE; if (rx_status & ENET_BD_ETHER_RX_TRUNCATED) { ENET_INC_STATS(ST_RX_RUNT); } else if (rx_status & ENET_BD_ETHER_RX_LAST_FRAME) { // Some error bits can only be checked on the last buffer // Overrun needs to be checked first, If this bit is // set, the other status bits, M, LG, NO, CR, and CL lose their normal meaning and are zero. This bit // is valid only if the L-bit is set. if (rx_status & ENET_BD_ETHER_RX_OVERRUN) { ENET_INC_STATS(ST_RX_OVERRUN); } else if (rx_status & ENET_BD_ETHER_RX_LENGTH_VIOLATION) { ENET_INC_STATS(ST_RX_GIANT); } else if (rx_status & ENET_BD_ETHER_RX_NON_OCTET) { ENET_INC_STATS(ST_RX_ALIGN); } else if (rx_status & ENET_BD_ETHER_RX_CRC_ERROR) { ENET_INC_STATS(ST_RX_FCS); } else { #if BSPCFG_ENABLE_ENET_HISTOGRAM uint_32 index = length>> ENET_HISTOGRAM_SHIFT; if (index < ENET_HISTOGRAM_ENTRIES) { ENET_INC_STATS(RX_HISTOGRAM[index]); } #endif // Good buffer (with Last bit set). buffer_is_valid = TRUE; // Remove CRC and compute buffer length ilength = length - (ENET_FRAMESIZE_TAIL + (macnet_context_ptr->CurrentRxFrag * enet_ptr->PARAM_PTR->RX_BUFFER_SIZE)); // The last fragment may contain nothing but part of the CRC. If this is the case, length will be <= 0, // and if length < 0, the previous fragment contains part of the CRC, which needs to be removed. if (ilength < 0) { length = 0; if (!first) { macnet_context_ptr->FRAGS[macnet_context_ptr->CurrentRxFrag-1].LENGTH += ilength; } else { // Should never get here - would mean we received a really small packet which should have already been // caught above. But if we do, discard it. buffer_is_valid = TRUE; } } else { length = (uint_32) ilength; } } } else { // Good buffer (without Last bit set). Can only be processed if we are not currently discarding. buffer_is_valid = TRUE; last = FALSE; } if (!buffer_is_valid) { // Was a buffer with an error ENET_INC_STATS(COMMON.ST_RX_ERRORS); MACNET_discard_current_rx(enet_ptr,macnet_context_ptr,last); } else { // If it is the first buffer in the frame, we have to see if the frame is of interest. if (first) { // make sure we don't examine a cached copy of the buffer _DCACHE_INVALIDATE_MBYTES(buffer, length); // the frame is of interest if there is a receiver registered for the destination and protocol. We can do this on the first buffer // of the frame, since the ethernet header information always fits in the first buffer of a frame. macnet_context_ptr->CurrentRxECB = ENET_find_receiver(enet_ptr, (ENET_HEADER_PTR) buffer, &length); } // If we have an Rx ECB, it means we want this packet if (macnet_context_ptr->CurrentRxECB ) { if (!first) { // If it is not the first packet, the cache is still valid. We could have put the invalidate before the first "if (first)", // but that would mean we may invalidate the cache on buffers that we know we are discarding. _DCACHE_INVALIDATE_MBYTES(buffer, length); } // Add the packet to the list of fragments in the PCB macnet_context_ptr->FRAGS[macnet_context_ptr->CurrentRxFrag].FRAGMENT = buffer; macnet_context_ptr->FRAGS[macnet_context_ptr->CurrentRxFrag].LENGTH = length; macnet_context_ptr->CurrentRxFrag++; consumed = TRUE; // Now, there are three cases: // !l - start or mid frame buffer - store it for later coalecing into a large buffer // !f, l - end of multi-buffer packet - coalece into a large buffer // f, l - single buffer packet - ready to process if (!last) { // Not the last frame in the packet. Have to make sure there is room for at least one // more fragment. If not, the packet is too large if (macnet_context_ptr->CurrentRxFrag >= MACNET_MAX_FRAGS) { MACNET_discard_current_rx(enet_ptr,macnet_context_ptr,last); ENET_INC_STATS(RX_FRAGS_EXCEEDED); } } else { // last buffer (may or may not also be the first buffer) so process it // There is a receiver, so need to allocate a receive PCB QGET(macnet_context_ptr->RxPCBHead, macnet_context_ptr->RxPCBTail, pcb_ptr); if (pcb_ptr) { // allocation successful, initialize new receive PCB pcb_ptr->PRIVATE = (pointer)enet_ptr; pcb_ptr->FREE = MACNET_rx_free; // Check to see if it is a large packet if (!first) { // Coalese large_packet = ENET_Dequeue_Buffer((pointer *)&macnet_context_ptr->LARGE_BUFFERS); if (large_packet) { // need to compute the length of this fragment, because the length on a BD with the L bit set // is not the length of the buffer, it is the length of the frame. pcb_ptr->FRAG[0].FRAGMENT = large_packet; pcb_ptr->FRAG[0].LENGTH = 0; pcb_ptr->FREE = MACNET_rx_free_large; for (i=0;i<macnet_context_ptr->CurrentRxFrag;i++) { if (macnet_context_ptr->FRAGS[i].LENGTH ) { _mem_copy(macnet_context_ptr->FRAGS[i].FRAGMENT, large_packet, macnet_context_ptr->FRAGS[i].LENGTH ); large_packet += macnet_context_ptr->FRAGS[i].LENGTH; pcb_ptr->FRAG[0].LENGTH += macnet_context_ptr->FRAGS[i].LENGTH; } ENET_Enqueue_Buffer((pointer*)&macnet_context_ptr->RX_BUFFERS, macnet_context_ptr->FRAGS[i].FRAGMENT); macnet_context_ptr->FRAGS[i].FRAGMENT = NULL; } ENET_INC_STATS(ST_RX_COPY_LARGE); } else { ENET_INC_STATS(RX_LARGE_BUFFERS_EXHAUSTED); MACNET_discard_current_rx(enet_ptr,macnet_context_ptr,last); QADD(macnet_context_ptr->RxPCBHead, macnet_context_ptr->RxPCBTail, pcb_ptr); pcb_ptr=NULL; } // Check to see if it is a small packet } else { pcb_ptr->FRAG[0].LENGTH = length; pcb_ptr->FRAG[0].FRAGMENT = buffer; if (length < MACNET_SMALL_PACKET_SIZE) { small_packet = ENET_Dequeue_Buffer((pointer *)&macnet_context_ptr->SMALL_BUFFERS); if (small_packet) { _mem_copy( macnet_context_ptr->FRAGS[0].FRAGMENT, small_packet, length ); pcb_ptr->FRAG[0].FRAGMENT = small_packet; pcb_ptr->FREE = MACNET_rx_free_small; consumed = FALSE; ENET_INC_STATS(ST_RX_COPY_SMALL); } } } if (pcb_ptr) { pcb_ptr->FRAG[1].LENGTH = 0; pcb_ptr->FRAG[1].FRAGMENT = NULL; #if ENETCFG_SUPPORT_PTP if (macnet_context_ptr->PTP_PRIV->PTIMER_PRESENT) { MACNET_ptp_store_rxstamp(enet_ptr, pcb_ptr, bd_ptr); } #endif /* ENETCFG_SUPPORT_PTP */ // Call the receiver's service function to pass the PCB to the receiver macnet_context_ptr->CurrentRxECB->SERVICE(pcb_ptr, macnet_context_ptr->CurrentRxECB->PRIVATE); // and clear the current ECB and PCB info, as it's gone to the receiver now. macnet_context_ptr->CurrentRxECB = NULL; macnet_context_ptr->CurrentRxFrag = 0; } } else { // Couldn't get a Receive PCB, so need to discard buffers until last buffer in frame. MACNET_discard_current_rx(enet_ptr,macnet_context_ptr,last); ENET_INC_STATS(RX_PCBS_EXHAUSTED); } } } } if (!consumed) { ENET_INC_STATS(COMMON.ST_RX_DISCARDED); ENET_Enqueue_Buffer((pointer*)&macnet_context_ptr->RX_BUFFERS, buffer); } }