[PATCH net-next 07/15] net: macb: simplify macb_adj_dma_desc_idx()

Théo Lebrun posted 15 patches 2 months ago
[PATCH net-next 07/15] net: macb: simplify macb_adj_dma_desc_idx()
Posted by Théo Lebrun 2 months ago
The function body uses a switch statement on bp->hw_dma_cap and handles
its four possible values: 0, is_64b, is_ptp, is_64b && is_ptp.

Instead, refactor by noticing that the return value is:
   desc_size * MULT
with MULT = 3 if is_64b && is_ptp,
            2 if is_64b || is_ptp,
            1 otherwise.

MULT can be expressed as:
   1 + is_64b + is_ptp

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
---
 drivers/net/ethernet/cadence/macb_main.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index 7f74e280a3351ee7f961ff5ecd9550470b2e68eb..44a411662786ca4f309d6f9389b0d36819fc40ad 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -136,19 +136,13 @@ static unsigned int macb_dma_desc_get_size(struct macb *bp)
 static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
 {
 #ifdef MACB_EXT_DESC
-	switch (bp->hw_dma_cap) {
-	case HW_DMA_CAP_64B:
-	case HW_DMA_CAP_PTP:
-		desc_idx <<= 1;
-		break;
-	case HW_DMA_CAP_64B_PTP:
-		desc_idx *= 3;
-		break;
-	default:
-		break;
-	}
-#endif
+	bool is_ptp = bp->hw_dma_cap & HW_DMA_CAP_PTP;
+	bool is_64b = bp->hw_dma_cap & HW_DMA_CAP_64B;
+
+	return desc_idx * (1 + is_64b + is_ptp);
+#else
 	return desc_idx;
+#endif
 }
 
 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT

-- 
2.51.0

Re: [PATCH net-next 07/15] net: macb: simplify macb_adj_dma_desc_idx()
Posted by Andrew Lunn 2 months ago
On Tue, Oct 14, 2025 at 05:25:08PM +0200, Théo Lebrun wrote:
> The function body uses a switch statement on bp->hw_dma_cap and handles
> its four possible values: 0, is_64b, is_ptp, is_64b && is_ptp.
> 
> Instead, refactor by noticing that the return value is:
>    desc_size * MULT
> with MULT = 3 if is_64b && is_ptp,
>             2 if is_64b || is_ptp,
>             1 otherwise.
> 
> MULT can be expressed as:
>    1 + is_64b + is_ptp
> 
> Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
> ---
>  drivers/net/ethernet/cadence/macb_main.c | 18 ++++++------------
>  1 file changed, 6 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
> index 7f74e280a3351ee7f961ff5ecd9550470b2e68eb..44a411662786ca4f309d6f9389b0d36819fc40ad 100644
> --- a/drivers/net/ethernet/cadence/macb_main.c
> +++ b/drivers/net/ethernet/cadence/macb_main.c
> @@ -136,19 +136,13 @@ static unsigned int macb_dma_desc_get_size(struct macb *bp)
>  static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
>  {
>  #ifdef MACB_EXT_DESC
> -	switch (bp->hw_dma_cap) {
> -	case HW_DMA_CAP_64B:
> -	case HW_DMA_CAP_PTP:
> -		desc_idx <<= 1;
> -		break;
> -	case HW_DMA_CAP_64B_PTP:

I _think_ this makes HW_DMA_CAP_64B_PTP unused and it can be removed?

  Andrew
Re: [PATCH net-next 07/15] net: macb: simplify macb_adj_dma_desc_idx()
Posted by Théo Lebrun 2 months ago
Hello Andrew,

On Fri Oct 17, 2025 at 8:00 PM CEST, Andrew Lunn wrote:
> On Tue, Oct 14, 2025 at 05:25:08PM +0200, Théo Lebrun wrote:
>> The function body uses a switch statement on bp->hw_dma_cap and handles
>> its four possible values: 0, is_64b, is_ptp, is_64b && is_ptp.
>> 
>> Instead, refactor by noticing that the return value is:
>>    desc_size * MULT
>> with MULT = 3 if is_64b && is_ptp,
>>             2 if is_64b || is_ptp,
>>             1 otherwise.
>> 
>> MULT can be expressed as:
>>    1 + is_64b + is_ptp
>> 
>> Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
>> ---
>>  drivers/net/ethernet/cadence/macb_main.c | 18 ++++++------------
>>  1 file changed, 6 insertions(+), 12 deletions(-)
>> 
>> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
>> index 7f74e280a3351ee7f961ff5ecd9550470b2e68eb..44a411662786ca4f309d6f9389b0d36819fc40ad 100644
>> --- a/drivers/net/ethernet/cadence/macb_main.c
>> +++ b/drivers/net/ethernet/cadence/macb_main.c
>> @@ -136,19 +136,13 @@ static unsigned int macb_dma_desc_get_size(struct macb *bp)
>>  static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
>>  {
>>  #ifdef MACB_EXT_DESC
>> -	switch (bp->hw_dma_cap) {
>> -	case HW_DMA_CAP_64B:
>> -	case HW_DMA_CAP_PTP:
>> -		desc_idx <<= 1;
>> -		break;
>> -	case HW_DMA_CAP_64B_PTP:
>
> I _think_ this makes HW_DMA_CAP_64B_PTP unused and it can be removed?

It does indeed. That constant gets removed in the following patch
([08/15], "net: macb: move bp->hw_dma_cap flags to bp->caps").
It  appeared to make more sense to remove all HW_DMA_CAP_* at once.
You probably noticed as you continued your review.

Thanks for the review! I guess you have spotted that the series got
applied to netdev/net-next by Jakub [0].

[0]: https://lore.kernel.org/lkml/176066582948.1978978.752807229943547484.git-patchwork-notify@kernel.org/

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com