Skip to content

Commit da3e3d3

Browse files
cperera-audstephanosio
authored andcommitted
drivers: ethernet: stm32: Enabling stats for the driver.
The change enables the ethernet driver to save statistics in a structure in the ethernet driver API structure. In addition, the change also attempts to update error statistics based on errors reported in the STM32 ethernet HAL API. Fixes #53995 Signed-off-by: Chamira Perera <[email protected]>
1 parent cde1573 commit da3e3d3

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

drivers/ethernet/eth_stm32_hal.c

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,10 +957,78 @@ void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth_handle)
957957
#if defined(CONFIG_ETH_STM32_HAL_API_V2)
958958
void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
959959
{
960-
/* Do nothing */
961-
/* Do not log errors. If errors are reported du to high traffic,
960+
/* Do not log errors. If errors are reported due to high traffic,
962961
* logging errors will only increase traffic issues
963962
*/
963+
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
964+
__ASSERT_NO_MSG(heth != NULL);
965+
966+
uint32_t dma_error;
967+
#if defined(CONFIG_SOC_SERIES_STM32H7X)
968+
uint32_t mac_error;
969+
#endif /* CONFIG_SOC_SERIES_STM32H7X */
970+
const uint32_t error_code = HAL_ETH_GetError(heth);
971+
972+
struct eth_stm32_hal_dev_data *dev_data =
973+
CONTAINER_OF(heth, struct eth_stm32_hal_dev_data, heth);
974+
975+
switch (error_code) {
976+
case HAL_ETH_ERROR_DMA:
977+
dma_error = HAL_ETH_GetDMAError(heth);
978+
979+
#if defined(CONFIG_SOC_SERIES_STM32H7X)
980+
if ((dma_error & ETH_DMA_RX_WATCHDOG_TIMEOUT_FLAG) ||
981+
(dma_error & ETH_DMA_RX_PROCESS_STOPPED_FLAG) ||
982+
(dma_error & ETH_DMA_RX_BUFFER_UNAVAILABLE_FLAG)) {
983+
eth_stats_update_errors_rx(dev_data->iface);
984+
}
985+
if ((dma_error & ETH_DMA_EARLY_TX_IT_FLAG) ||
986+
(dma_error & ETH_DMA_TX_PROCESS_STOPPED_FLAG)) {
987+
eth_stats_update_errors_tx(dev_data->iface);
988+
}
989+
#else
990+
if ((dma_error & ETH_DMASR_RWTS) ||
991+
(dma_error & ETH_DMASR_RPSS) ||
992+
(dma_error & ETH_DMASR_RBUS)) {
993+
eth_stats_update_errors_rx(dev_data->iface);
994+
}
995+
if ((dma_error & ETH_DMASR_ETS) ||
996+
(dma_error & ETH_DMASR_TPSS) ||
997+
(dma_error & ETH_DMASR_TJTS)) {
998+
eth_stats_update_errors_tx(dev_data->iface);
999+
}
1000+
#endif /* CONFIG_SOC_SERIES_STM32H7X */
1001+
break;
1002+
1003+
#if defined(CONFIG_SOC_SERIES_STM32H7X)
1004+
case HAL_ETH_ERROR_MAC:
1005+
mac_error = HAL_ETH_GetMACError(heth);
1006+
1007+
if (mac_error & ETH_RECEIVE_WATCHDOG_TIMEOUT) {
1008+
eth_stats_update_errors_rx(dev_data->iface);
1009+
}
1010+
1011+
if ((mac_error & ETH_EXECESSIVE_COLLISIONS) ||
1012+
(mac_error & ETH_LATE_COLLISIONS) ||
1013+
(mac_error & ETH_EXECESSIVE_DEFERRAL) ||
1014+
(mac_error & ETH_TRANSMIT_JABBR_TIMEOUT) ||
1015+
(mac_error & ETH_LOSS_OF_CARRIER) ||
1016+
(mac_error & ETH_NO_CARRIER)) {
1017+
eth_stats_update_errors_tx(dev_data->iface);
1018+
}
1019+
break;
1020+
#endif /* CONFIG_SOC_SERIES_STM32H7X */
1021+
}
1022+
1023+
#if defined(CONFIG_SOC_SERIES_STM32H7X)
1024+
dev_data->stats.error_details.rx_crc_errors = heth->Instance->MMCRCRCEPR;
1025+
dev_data->stats.error_details.rx_align_errors = heth->Instance->MMCRAEPR;
1026+
#else
1027+
dev_data->stats.error_details.rx_crc_errors = heth->Instance->MMCRFCECR;
1028+
dev_data->stats.error_details.rx_align_errors = heth->Instance->MMCRFAECR;
1029+
#endif /* CONFIG_SOC_SERIES_STM32H7X */
1030+
1031+
#endif /* CONFIG_NET_STATISTICS_ETHERNET */
9641032
}
9651033
#elif defined(CONFIG_SOC_SERIES_STM32H7X)
9661034
/* DMA and MAC errors callback only appear in H7 series */
@@ -1532,6 +1600,15 @@ static const struct device *eth_stm32_get_ptp_clock(const struct device *dev)
15321600
}
15331601
#endif /* CONFIG_PTP_CLOCK_STM32_HAL */
15341602

1603+
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
1604+
static struct net_stats_eth *eth_stm32_hal_get_stats(const struct device *dev)
1605+
{
1606+
struct eth_stm32_hal_dev_data *dev_data = dev->data;
1607+
1608+
return &dev_data->stats;
1609+
}
1610+
#endif /* CONFIG_NET_STATISTICS_ETHERNET */
1611+
15351612
static const struct ethernet_api eth_api = {
15361613
.iface_api.init = eth_iface_init,
15371614
#if defined(CONFIG_PTP_CLOCK_STM32_HAL)
@@ -1540,6 +1617,9 @@ static const struct ethernet_api eth_api = {
15401617
.get_capabilities = eth_stm32_hal_get_capabilities,
15411618
.set_config = eth_stm32_hal_set_config,
15421619
.send = eth_tx,
1620+
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
1621+
.get_stats = eth_stm32_hal_get_stats,
1622+
#endif /* CONFIG_NET_STATISTICS_ETHERNET */
15431623
};
15441624

15451625
static void eth0_irq_config(void)

drivers/ethernet/eth_stm32_hal_priv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ struct eth_stm32_hal_dev_data {
5353
float clk_ratio;
5454
float clk_ratio_adj;
5555
#endif /* CONFIG_PTP_CLOCK_STM32_HAL */
56+
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
57+
struct net_stats_eth stats;
58+
#endif
5659
};
5760

5861
#endif /* ZEPHYR_DRIVERS_ETHERNET_ETH_STM32_HAL_PRIV_H_ */

0 commit comments

Comments
 (0)