The FDMA RX buffers are skbs on Sparx5, and pages from a page_pool on lan969x, which are handed to the FDMA through the RX DCBs. On lan969x, the TX DCBs also have an array holding the state of the TX buffers. These are not all freed. On Sparx5, the RX skbs are never freed, and if the TX buffers cannot be allocated, the RX DCBs are not freed. On lan969x, if the RX DCBs cannot be allocated, the page_pool is not destroyed, if the TX buffers cannot be allocated, only the RX DCBs are freed, and lan969x_fdma_deinit() never frees the TX buffer state, nor the TX buffers that are still in flight. On both, the return value of fdma_dcbs_init() is also ignored for RX, so if a buffer cannot be allocated while filling the RX ring, the FDMA is started with a partly filled ring, and on lan969x, lan969x_fdma_free_pages() later puts the pages that were never allocated. Add sparx5_fdma_rx_free(), which frees the RX skbs and DCBs, lan969x_fdma_rx_free(), which returns the pages to the page_pool and frees the page_pool and the RX DCBs, and lan969x_fdma_tx_free(), which releases the TX buffers still in flight and frees the TX buffer state and the TX DCBs, and use them on the error paths and on deinit. Check the return value of fdma_dcbs_init() for RX. To know which RX buffers are still owned by the driver, clear the RX buffer slot once the buffer has left the ring on Sparx5, either to the stack or dropped, and make lan969x_fdma_free_pages() skip and clear the empty slots of the ring. The two-step PTP skbs are owned by the PTP code, which may already have freed them when a TX buffer is released, so store the length of the mapped TX buffer, and use it instead of the skb length. Signed-off-by: Daniel Machon --- .../microchip/sparx5/lan969x/lan969x_fdma.c | 85 +++++++++++++++++----- .../net/ethernet/microchip/sparx5/sparx5_fdma.c | 30 +++++++- .../net/ethernet/microchip/sparx5/sparx5_main.h | 1 + 3 files changed, 95 insertions(+), 21 deletions(-) diff --git a/drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c b/drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c index fd532c89f9110..5f96910df5383 100644 --- a/drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c +++ b/drivers/net/ethernet/microchip/sparx5/lan969x/lan969x_fdma.c @@ -67,13 +67,13 @@ static void lan969x_fdma_tx_clear_buf(struct sparx5 *sparx5, int weight) if (!fdma_db_is_done(fdma_db_get(fdma, i, 0))) continue; - db->dev->stats.tx_bytes += db->skb->len; + db->dev->stats.tx_bytes += db->len; db->dev->stats.tx_packets++; sparx5->tx.packets++; dma_unmap_single(sparx5->dev, db->dma_addr, - db->skb->len, + db->len, DMA_TO_DEVICE); if (!db->ptp) @@ -90,12 +90,26 @@ static void lan969x_fdma_free_pages(struct sparx5_rx *rx) struct fdma *fdma = &rx->fdma; for (int i = 0; i < fdma->n_dcbs; ++i) { - for (int j = 0; j < fdma->n_dbs; ++j) + for (int j = 0; j < fdma->n_dbs; ++j) { + if (!rx->page[i][j]) + continue; + page_pool_put_full_page(rx->page_pool, rx->page[i][j], false); + rx->page[i][j] = NULL; + } } } +static void lan969x_fdma_rx_free(struct sparx5 *sparx5) +{ + struct sparx5_rx *rx = &sparx5->rx; + + lan969x_fdma_free_pages(rx); + fdma_free_coherent(sparx5->dev, &rx->fdma); + page_pool_destroy(rx->page_pool); +} + static struct sk_buff *lan969x_fdma_rx_get_frame(struct sparx5 *sparx5, struct sparx5_rx *rx) { @@ -172,14 +186,18 @@ static int lan969x_fdma_rx_alloc(struct sparx5 *sparx5) return PTR_ERR(rx->page_pool); err = fdma_alloc_coherent(sparx5->dev, fdma); - if (err) + if (err) { + page_pool_destroy(rx->page_pool); return err; + } - fdma_dcbs_init(fdma, - FDMA_DCB_INFO_DATAL(fdma->db_size), - FDMA_DCB_STATUS_INTR); + err = fdma_dcbs_init(fdma, + FDMA_DCB_INFO_DATAL(fdma->db_size), + FDMA_DCB_STATUS_INTR); + if (err) + lan969x_fdma_rx_free(sparx5); - return 0; + return err; } static int lan969x_fdma_tx_alloc(struct sparx5 *sparx5) @@ -205,6 +223,44 @@ static int lan969x_fdma_tx_alloc(struct sparx5 *sparx5) return 0; } +/* Release the TX buffers that are still in flight. The two-step PTP skbs are + * owned by the port tx_skbs list, so they are only unmapped. + */ +static void lan969x_fdma_tx_release(struct sparx5 *sparx5) +{ + struct sparx5_tx *tx = &sparx5->tx; + struct sparx5_tx_buf *db; + + for (int i = 0; i < tx->fdma.n_dcbs; ++i) { + db = &tx->dbs[i]; + + if (!db->used) + continue; + + db->dev->stats.tx_dropped++; + tx->dropped++; + + dma_unmap_single(sparx5->dev, + db->dma_addr, + db->len, + DMA_TO_DEVICE); + + if (!db->ptp) + dev_kfree_skb_any(db->skb); + + db->used = false; + } +} + +static void lan969x_fdma_tx_free(struct sparx5 *sparx5) +{ + struct sparx5_tx *tx = &sparx5->tx; + + lan969x_fdma_tx_release(sparx5); + fdma_free_coherent(sparx5->dev, &tx->fdma); + kfree(tx->dbs); +} + static void lan969x_fdma_rx_init(struct sparx5 *sparx5) { struct fdma *fdma = &sparx5->rx.fdma; @@ -331,6 +387,7 @@ int lan969x_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb, db_buf->dev = dev; db_buf->skb = skb; + db_buf->len = skb->len; db_buf->ptp = false; db_buf->used = true; @@ -354,7 +411,6 @@ int lan969x_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb, int lan969x_fdma_init(struct sparx5 *sparx5) { - struct sparx5_rx *rx = &sparx5->rx; int err; lan969x_fdma_rx_init(sparx5); @@ -376,7 +432,7 @@ int lan969x_fdma_init(struct sparx5 *sparx5) err = lan969x_fdma_tx_alloc(sparx5); if (err) { - fdma_free_coherent(sparx5->dev, &rx->fdma); + lan969x_fdma_rx_free(sparx5); dev_err(sparx5->dev, "Failed to allocate TX buffers: %d\n", err); return err; @@ -391,14 +447,9 @@ int lan969x_fdma_init(struct sparx5 *sparx5) int lan969x_fdma_deinit(struct sparx5 *sparx5) { - struct sparx5_rx *rx = &sparx5->rx; - struct sparx5_tx *tx = &sparx5->tx; - sparx5_fdma_stop(sparx5); - fdma_free_coherent(sparx5->dev, &tx->fdma); - fdma_free_coherent(sparx5->dev, &rx->fdma); - lan969x_fdma_free_pages(rx); - page_pool_destroy(rx->page_pool); + lan969x_fdma_tx_free(sparx5); + lan969x_fdma_rx_free(sparx5); return 0; } diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c b/drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c index 8a847ec067de6..f1d44e5e080b6 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_fdma.c @@ -147,6 +147,10 @@ static bool sparx5_fdma_rx_get_frame(struct sparx5 *sparx5, struct sparx5_rx *rx if (unlikely(!fdma_db_is_done(db_hw))) return false; skb = rx->skb[fdma->dcb_index][fdma->db_index]; + + /* The skb leaves the ring here, so clear its slot */ + rx->skb[fdma->dcb_index][fdma->db_index] = NULL; + skb_put(skb, fdma_db_len_get(db_hw)); /* Now do the normal processing of the skb */ sparx5_ifh_parse(sparx5, (u32 *)skb->data, &fi); @@ -245,6 +249,21 @@ int sparx5_fdma_xmit(struct sparx5 *sparx5, u32 *ifh, struct sk_buff *skb, return NETDEV_TX_OK; } +/* Free the RX skbs owned by the driver, and the RX DCBs */ +static void sparx5_fdma_rx_free(struct sparx5_rx *rx) +{ + struct fdma *fdma = &rx->fdma; + + for (int i = 0; i < fdma->n_dcbs; i++) { + for (int j = 0; j < fdma->n_dbs; j++) { + consume_skb(rx->skb[i][j]); + rx->skb[i][j] = NULL; + } + } + + fdma_free_phys(fdma); +} + static int sparx5_fdma_rx_alloc(struct sparx5 *sparx5) { struct sparx5_rx *rx = &sparx5->rx; @@ -255,10 +274,12 @@ static int sparx5_fdma_rx_alloc(struct sparx5 *sparx5) if (err) return err; - fdma_dcbs_init(fdma, FDMA_DCB_INFO_DATAL(fdma->db_size), - FDMA_DCB_STATUS_INTR); + err = fdma_dcbs_init(fdma, FDMA_DCB_INFO_DATAL(fdma->db_size), + FDMA_DCB_STATUS_INTR); + if (err) + sparx5_fdma_rx_free(rx); - return 0; + return err; } static int sparx5_fdma_tx_alloc(struct sparx5 *sparx5) @@ -431,6 +452,7 @@ int sparx5_fdma_init(struct sparx5 *sparx5) } err = sparx5_fdma_tx_alloc(sparx5); if (err) { + sparx5_fdma_rx_free(&sparx5->rx); dev_err(sparx5->dev, "Could not allocate TX buffers: %d\n", err); return err; } @@ -440,7 +462,7 @@ int sparx5_fdma_init(struct sparx5 *sparx5) int sparx5_fdma_deinit(struct sparx5 *sparx5) { sparx5_fdma_stop(sparx5); - fdma_free_phys(&sparx5->rx.fdma); + sparx5_fdma_rx_free(&sparx5->rx); fdma_free_phys(&sparx5->tx.fdma); return 0; diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h index f6316086e6ecb..19b38922823ec 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h @@ -180,6 +180,7 @@ struct sparx5_tx_buf { dma_addr_t dma_addr; bool used; bool ptp; + u32 len; }; /* Frame DMA transmit state: -- 2.34.1