lan743x_tx_close() and lan743x_rx_close() ask the DMA controller to stop the channel and wait for it, but ignore the result. If the channel is still stop-pending when the wait gives up, the descriptor ring, the head write-back buffer and the packet buffers are unmapped and freed while the channel may still be fetching from or writing to them. A TX channel has been seen to stop making progress, with its head index no longer advancing while DMAC_CMD still reports it as started. A channel in that state may not honour the stop request either, and closing the interface would then free memory the engine can still access. If the channel is still stop-pending after the wait, or still reads as started, issue a channel soft reset and wait for it before anything is freed, and log when that happens. Use the same helpers for the channel resets that lan743x_tx_open() and lan743x_rx_open() already open-code, and log there too if the reset times out. Fixes: 23f0703c125b ("lan743x: Add main source files for new lan743x driver") Assisted-by: LLM Signed-off-by: Chris Babroski --- drivers/net/ethernet/microchip/lan743x_main.c | 64 +++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/drivers/net/ethernet/microchip/lan743x_main.c b/drivers/net/ethernet/microchip/lan743x_main.c index 82d3ec20ba25..9e1276cf313c 100644 --- a/drivers/net/ethernet/microchip/lan743x_main.c +++ b/drivers/net/ethernet/microchip/lan743x_main.c @@ -1584,6 +1584,15 @@ static int lan743x_dmac_tx_wait_till_stopped(struct lan743x_adapter *adapter, return result; } +static int lan743x_dmac_tx_reset(struct lan743x_adapter *adapter, + int tx_channel) +{ + lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_TX_SWR_(tx_channel)); + return lan743x_csr_wait_for_bit(adapter, DMAC_CMD, + DMAC_CMD_TX_SWR_(tx_channel), + 0, 1000, 20000, 100); +} + static int lan743x_dmac_rx_get_state(struct lan743x_adapter *adapter, int rx_channel) { @@ -1613,6 +1622,15 @@ static int lan743x_dmac_rx_wait_till_stopped(struct lan743x_adapter *adapter, return result; } +static int lan743x_dmac_rx_reset(struct lan743x_adapter *adapter, + int rx_channel) +{ + lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_RX_SWR_(rx_channel)); + return lan743x_csr_wait_for_bit(adapter, DMAC_CMD, + DMAC_CMD_RX_SWR_(rx_channel), + 0, 1000, 20000, 100); +} + static void lan743x_tx_release_desc(struct lan743x_tx *tx, int descriptor_index, bool cleanup) { @@ -2221,11 +2239,22 @@ static int lan743x_tx_ring_init(struct lan743x_tx *tx) static void lan743x_tx_close(struct lan743x_tx *tx) { struct lan743x_adapter *adapter = tx->adapter; + int ret; lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_STOP_T_(tx->channel_number)); - lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number); + ret = lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number); + if (ret < 0 || ret == DMAC_CHANNEL_STATE_STARTED) { + netdev_warn(adapter->netdev, + "TX channel %d failed to stop, resetting it\n", + tx->channel_number); + ret = lan743x_dmac_tx_reset(adapter, tx->channel_number); + if (ret) + netdev_err(adapter->netdev, + "TX channel %d reset timed out\n", + tx->channel_number); + } lan743x_csr_write(adapter, DMAC_INT_EN_CLR, @@ -2271,11 +2300,11 @@ static int lan743x_tx_open(struct lan743x_tx *tx) FCT_TX_CTL_EN_(tx->channel_number)); /* reset tx channel */ - lan743x_csr_write(adapter, DMAC_CMD, - DMAC_CMD_TX_SWR_(tx->channel_number)); - lan743x_csr_wait_for_bit(adapter, DMAC_CMD, - DMAC_CMD_TX_SWR_(tx->channel_number), - 0, 1000, 20000, 100); + ret = lan743x_dmac_tx_reset(adapter, tx->channel_number); + if (ret) + netdev_err(adapter->netdev, + "TX channel %d reset timed out\n", + tx->channel_number); /* Write TX_BASE_ADDR */ lan743x_csr_write(adapter, @@ -2775,6 +2804,7 @@ static int lan743x_rx_ring_init(struct lan743x_rx *rx) static void lan743x_rx_close(struct lan743x_rx *rx) { struct lan743x_adapter *adapter = rx->adapter; + int ret; lan743x_csr_write(adapter, FCT_RX_CTL, FCT_RX_CTL_DIS_(rx->channel_number)); @@ -2784,7 +2814,17 @@ static void lan743x_rx_close(struct lan743x_rx *rx) lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_STOP_R_(rx->channel_number)); - lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number); + ret = lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number); + if (ret < 0 || ret == DMAC_CHANNEL_STATE_STARTED) { + netdev_warn(adapter->netdev, + "RX channel %d failed to stop, resetting it\n", + rx->channel_number); + ret = lan743x_dmac_rx_reset(adapter, rx->channel_number); + if (ret) + netdev_err(adapter->netdev, + "RX channel %d reset timed out\n", + rx->channel_number); + } lan743x_csr_write(adapter, DMAC_INT_EN_CLR, DMAC_INT_BIT_RXFRM_(rx->channel_number)); @@ -2810,11 +2850,11 @@ static int lan743x_rx_open(struct lan743x_rx *rx) netif_napi_add(adapter->netdev, &rx->napi, lan743x_rx_napi_poll); - lan743x_csr_write(adapter, DMAC_CMD, - DMAC_CMD_RX_SWR_(rx->channel_number)); - lan743x_csr_wait_for_bit(adapter, DMAC_CMD, - DMAC_CMD_RX_SWR_(rx->channel_number), - 0, 1000, 20000, 100); + ret = lan743x_dmac_rx_reset(adapter, rx->channel_number); + if (ret) + netdev_err(adapter->netdev, + "RX channel %d reset timed out\n", + rx->channel_number); /* set ring base address */ lan743x_csr_write(adapter, -- 2.34.1