RE: [PATCH net-next 07/11] net: fec: use switch statement to check the type of tx_buf
From: Wei Fang
Date: Wed Jan 14 2026 - 05:01:35 EST
> On Tue, Jan 13, 2026 at 11:29:35AM +0800, Wei Fang wrote:
> > The tx_buf has three types: FEC_TXBUF_T_SKB, FEC_TXBUF_T_XDP_NDO and
> > FEC_TXBUF_T_XDP_TX. Currently, the driver uses 'if...else...'
> > statements to check the type and perform the corresponding processing.
> > This is very detrimental to future expansion. For example, if new
> > types are added to support XDP zero copy in the future, continuing to use
> 'if...else...'
> > would be a very bad coding style. So the 'if...else...' statements in
> > the current driver are replaced with switch statements to support XDP
> > zero copy in the future.
> >
> > Signed-off-by: Wei Fang <wei.fang@xxxxxxx>
> > ---
> > drivers/net/ethernet/freescale/fec_main.c | 167
> > +++++++++++-----------
> > 1 file changed, 82 insertions(+), 85 deletions(-)
> >
> > diff --git a/drivers/net/ethernet/freescale/fec_main.c
> > b/drivers/net/ethernet/freescale/fec_main.c
> > index f3e93598a27c..3bd89d7f105b 100644
> > --- a/drivers/net/ethernet/freescale/fec_main.c
> > +++ b/drivers/net/ethernet/freescale/fec_main.c
> > @@ -1023,33 +1023,33 @@ static void fec_enet_bd_init(struct net_device
> *dev)
> > txq->bd.cur = bdp;
> >
> > for (i = 0; i < txq->bd.ring_size; i++) {
> > + dma_addr_t dma = fec32_to_cpu(bdp->cbd_bufaddr);
> > + struct page *page;
> > +
> > /* Initialize the BD for every fragment in the page. */
> > bdp->cbd_sc = cpu_to_fec16(0);
> > - if (txq->tx_buf[i].type == FEC_TXBUF_T_SKB) {
> > - if (bdp->cbd_bufaddr &&
> > - !IS_TSO_HEADER(txq,
> fec32_to_cpu(bdp->cbd_bufaddr)))
> > - dma_unmap_single(&fep->pdev->dev,
> > - fec32_to_cpu(bdp->cbd_bufaddr),
> > - fec16_to_cpu(bdp->cbd_datlen),
> > - DMA_TO_DEVICE);
> > - if (txq->tx_buf[i].buf_p)
> > - dev_kfree_skb_any(txq->tx_buf[i].buf_p);
> > - } else if (txq->tx_buf[i].type == FEC_TXBUF_T_XDP_NDO) {
> > - if (bdp->cbd_bufaddr)
> > - dma_unmap_single(&fep->pdev->dev,
> > - fec32_to_cpu(bdp->cbd_bufaddr),
> > + switch (txq->tx_buf[i].type) {
> > + case FEC_TXBUF_T_SKB:
> > + if (dma && !IS_TSO_HEADER(txq, dma))
> > + dma_unmap_single(&fep->pdev->dev, dma,
> > fec16_to_cpu(bdp->cbd_datlen),
> > DMA_TO_DEVICE);
> >
> > - if (txq->tx_buf[i].buf_p)
> > - xdp_return_frame(txq->tx_buf[i].buf_p);
> > - } else {
> > - struct page *page = txq->tx_buf[i].buf_p;
> > -
> > - if (page)
> > - page_pool_put_page(pp_page_to_nmdesc(page)->pp,
> > - page, 0,
> > - false);
> > + dev_kfree_skb_any(txq->tx_buf[i].buf_p);
> > + break;
> > + case FEC_TXBUF_T_XDP_NDO:
> > + dma_unmap_single(&fep->pdev->dev, dma,
> > + fec16_to_cpu(bdp->cbd_datlen),
> > + DMA_TO_DEVICE);
> > + xdp_return_frame(txq->tx_buf[i].buf_p);
>
> look like logic is not exactly same as original one
>
> if (txq->tx_buf[i].type == FEC_TXBUF_T_XDP_NDO) {
> if (bdp->cbd_bufaddr)
> ...
Yes, I did some cleanup as well, because the case of NULL pointer is
always false.