net: macb: implement ethtool set channels count operation
Subsystems (3)
Théo Lebrun
maintainerv2 cover net: macb: implement ethtool set channels count operation
Add support for changing the active number of queues. Tested on Mobileye EyeQ5. The first patch is as expected. However the second one might be more surprising:
GEM has per-queue Tx SRAM segmentation. If we do not touch SRAM distribution then we'll only be able to exploit a portion of it when a smaller queue count is configured. It also is beneficial if bootloader stages write to the register and we don't reset it but attempt to use all queues (the default).
The operation is only hidden behind MACB_CAPS_QUEUE_DISABLE, we do not introduce yet another feature flag.
As this series must s/num_queues/max_num_queues/ quite a lot, it conflicts with other in-flights series touching MACB in the netdev ML. I'll make sure to resend once net-next gets updated.
Have a nice day,
Thanks,
Théo
---
Changes in v2:
- Move gem_sram_distribute_segments() from macb_main.c to macb_utils.c
and kunit tests from macb_main.c to macb_kunit.c.
- macb_set_channels():
- Add comment about MACB_CAPS_QUEUE_DISABLE.
- Refuse operation if netif_running(), with code comment.
- Drop useless sanity checks on ch->{combined,rx,tx}_count.
- Add help text to CONFIG_MACB_KUNIT_TEST.
- Fix checkpatch --max-line-length=80 warnings.
- Rebase onto latest net-next; nothing to report.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Théo Lebrun (2):
net: macb: implement ethtool_ops.get|set_channels()
net: macb: distribute evenly Tx SRAM segments
drivers/net/ethernet/cadence/Kconfig | 13 ++++ drivers/net/ethernet/cadence/Makefile | 5 +- drivers/net/ethernet/cadence/macb.h | 9 +++ drivers/net/ethernet/cadence/macb_kunit.c | 73 ++++++++++++++++++ drivers/net/ethernet/cadence/macb_main.c | 118 ++++++++++++++++++++++++++---- drivers/net/ethernet/cadence/macb_utils.c | 56 ++++++++++++++ 6 files changed, 257 insertions(+), 17 deletions(-)
---
base-commit: df99d268555f52fb2ac3b9fc9a6198ea0579c48a
change-id: 20260305-macb-set-channels-5bf6e07f3270
Best regards,
Signature · 1 line
Théo Lebrun <[email protected]>
Théo Lebrun
maintainerPATCH v2 1/2 net: macb: implement ethtool_ops.get|set_channels()
bp->num_queues is the total number of queues and is constant from probe. Introduce bp->max_num_queues which takes the current role of bp->num_queues and allow 0 < bp->num_queues <= bp->max_num_queues. MACB/GEM does not know about rx/tx specific queues; it only has combined queues.
.set_channels() is reserved to devices with MACB_CAPS_QUEUE_DISABLE. The tieoff workaround would not work as packets would still be routed into queues with a tieoff descriptor.
Implement .set_channels() operation by refusing if netif_running(). The reason to implement .set_channels() is memory savings, we cannot preallocate and swap at runtime.
Nit: fix an alignment issue inside gem_ethtool_ops which does not deserve its own patch.
--- drivers/net/ethernet/cadence/macb.h | 1 + drivers/net/ethernet/cadence/macb_main.c | 103 ++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 16 deletions(-)
drivers/net/ethernet/cadence/macb.h +1 -0
@@ -1309,6 +1309,7 @@ struct macb {unsigned int tx_ring_size;unsigned int num_queues; +unsigned int max_num_queues;struct macb_queue queues[MACB_MAX_QUEUES ];spinlock_t lock;
drivers/net/ethernet/cadence/macb_main.c +87 -16
173 lines not on this page · raw message
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v2 1/2] net: macb: implement ethtool_ops.get|set_channels()
drivers/net/ethernet/cadence/macb_main.c static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) · line 4076
Quoted 4 lines · On Wed, 11 Mar 2026 17:41:53 +0100 Théo Lebrun wrote:
drivers/net/ethernet/cadence/macb_main.c +4
-0+struct macb *bp =netdev_priv (netdev); +unsigned int old_count = bp->num_queues; +unsigned int count = ch->combined_count; +int ret =0 ;
unnecessary init
Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +12
-0+/* +* MACB_CAPS_QUEUE_DISABLE means that the field QUEUE_DISABLE/BIT0 in +* the per-queue RBQP register disables queue Rx. If we don't have that +* capability we can have multiple queues but we must always run with +* all enabled. +*/ +if (!(bp->caps &MACB_CAPS_QUEUE_DISABLE )) +return -EOPNOTSUPP ; + +/* +* An ideal .set_channels() implementation uses upfront allocated +* resources and swaps them in, bringing reliability under memory
drivers/net/ethernet/cadence/macb_main.c static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) · line 4099
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10
-0+* pressure. However, here we implement it for memory savings in +* setups with less than max number of queues active. +* +* Signal it by refusing .set_channels() once interface is opened. +*/ +if (netif_running (bp->dev)) +return -EBUSY ; + +if (count == old_count) +return 0 ;
should we reorder this with the running() check?
Théo Lebrun
maintainerRe: [PATCH net-next v2 1/2] net: macb: implement ethtool_ops.get|set_channels()
Hello Jakub,
Quoted 7 lines · Jakub Kicinski
drivers/net/ethernet/cadence/macb_main.c static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) · line 4076
Quoted 4 lines · On Wed, 11 Mar 2026 17:41:53 +0100 Théo Lebrun wrote:
drivers/net/ethernet/cadence/macb_main.c +4
-0+struct macb *bp =netdev_priv (netdev); +unsigned int old_count = bp->num_queues; +unsigned int count = ch->combined_count; +int ret =0 ;unnecessary init
ack
Quoted 16 lines
Quoted 16 lines
drivers/net/ethernet/cadence/macb_main.c +16
-0+/* +* MACB_CAPS_QUEUE_DISABLE means that the field QUEUE_DISABLE/BIT0 in +* the per-queue RBQP register disables queue Rx. If we don't have that +* capability we can have multiple queues but we must always run with +* all enabled. +*/ +if (!(bp->caps &MACB_CAPS_QUEUE_DISABLE )) +return -EOPNOTSUPP ; + +/* +* An ideal .set_channels() implementation uses upfront allocated +* resources and swaps them in, bringing reliability under memory +* pressure. However, here we implement it for memory savings in +* setups with less than max number of queues active. +* +* Signal it by refusing .set_channels() once interface is opened.
Quoted 8 lines
drivers/net/ethernet/cadence/macb_main.c static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) · line 4099
Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +6
-0+*/ +if (netif_running (bp->dev)) +return -EBUSY ; + +if (count == old_count) +return 0 ;should we reorder this with the
running()check?
I don't agree. For example when an operation is not supported, we start by checking that and returning EOPNOTSUPP. Then we validate the input data. Then we act.
Here it is the same. When netif_running(), we never reply to any request even if it happens to be a no-op.
I'll go ahead and send V3. Seeing how this was only a question I'll make the guess you don't care much about it and are fine either way. Same for me.
Thanks!
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Jakub Kicinski
maintainerRe: [PATCH net-next v2 1/2] net: macb: implement ethtool_ops.get|set_channels()
Quoted 6 lines · On Fri, 13 Mar 2026 16:14:23 +0100 Théo Lebrun wrote:
Quoted 1 line
should we reorder this with the
running()check?I don't agree. For example when an operation is not supported, we start by checking that and returning EOPNOTSUPP. Then we validate the input data. Then we act.
Quoted 6 lines
Here it is the same. When
netif_running(), we never reply to any request even if it happens to be a no-op.I'll go ahead and send V3. Seeing how this was only a question I'll make the guess you don't care much about it and are fine either way. Same for me.
Sorry for the delay. This code can only be reached from the IOCTL path. The Netlink path will check that params haven't changed in ethnl_set_channels() (look at the @mod variable) and return 0 directly. So you're basically adding a discrepancy between ioctl and Netlink. Not a huge deal but I don't envy any user having to debug this..
Théo Lebrun
maintainerRe: [PATCH net-next v2 1/2] net: macb: implement ethtool_ops.get|set_channels()
Quoted 14 lines · Jakub Kicinski
Quoted 6 lines · On Fri, 13 Mar 2026 16:14:23 +0100 Théo Lebrun wrote:
Quoted 1 line
should we reorder this with the
running()check?I don't agree. For example when an operation is not supported, we start by checking that and returning EOPNOTSUPP. Then we validate the input data. Then we act.
Quoted 6 lines
Here it is the same. When
netif_running(), we never reply to any request even if it happens to be a no-op.I'll go ahead and send V3. Seeing how this was only a question I'll make the guess you don't care much about it and are fine either way. Same for me.
Quoted 5 lines
Sorry for the delay. This code can only be reached from the IOCTL path. The Netlink path will check that params haven't changed in
ethnl_set_channels()(look at the @mod variable) and return 0 directly. So you're basically adding a discrepancy between ioctl and Netlink. Not a huge deal but I don't envy any user having to debug this..
Actually, the IOCTL path also does the check (see below). So the count == old_count check shall be dropped from the driver .set_channels() callback because it is redundant. Agreed?
Extract below for the ioctl codepath:
static int ethtool_set_channels(struct net_device *dev,
void __user *useraddr)
{
struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
// ...
if (copy_from_user(&channels, useraddr, sizeof(channels)))
return -EFAULT;
dev->ethtool_ops->get_channels(dev, &curr);
if (channels.rx_count == curr.rx_count &&
channels.tx_count == curr.tx_count &&
channels.combined_count == curr.combined_count &&
channels.other_count == curr.other_count)
return 0;
// ...
}https://elixir.bootlin.com/linux/v6.19.8/source/net/ethtool/ioctl.c#L2263-L2267
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Jakub Kicinski
maintainerRe: [PATCH net-next v2 1/2] net: macb: implement ethtool_ops.get|set_channels()
Quoted 9 lines · On Mon, 16 Mar 2026 17:24:28 +0100 Théo Lebrun wrote:
Quoted 5 lines
Sorry for the delay. This code can only be reached from the IOCTL path. The Netlink path will check that params haven't changed in
ethnl_set_channels()(look at the @mod variable) and return 0 directly. So you're basically adding a discrepancy between ioctl and Netlink. Not a huge deal but I don't envy any user having to debug this..Actually, the IOCTL path also does the check (see below). So the
count == old_countcheck shall be dropped from the driver .set_channels() callback because it is redundant. Agreed?
Oh, even better! Some of the ioctl paths don't validate the equivalence of the config but clearly I don't remember which.
Théo Lebrun
maintainerPATCH v2 2/2 net: macb: distribute evenly Tx SRAM segments
In reply to Théo LebrunGEM has registers to configure the Tx SRAM segments distribution across queues. The reset value is apprioriate (even spread) but we need to care if/when number of active queues is modified (or if we inherited unevenly initialised hardware from bootloader).
To distribute segments, we take as input the number of queues (bp->num_queues) and the number of segments (found inside DCFG6). Its output is a number of segments for each queue, formatted as powers-of-two (eg 2 for queue 0 means it has 2^2=4 segments).
As the distribution logic is quite complex (at least its initial versions had bugs), it is kunit-tested in macb_kunit.c and the implementation lives in macb_utils.c. To test:
⟩ env --unset=CROSS_COMPILE make ARCH=um mrproper
⟩ env --unset=CROSS_COMPILE ./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_NET=y \
--kconfig_add CONFIG_COMMON_CLK=y \
--kconfig_add CONFIG_MACB=y macb--- drivers/net/ethernet/cadence/Kconfig | 13 ++++++ drivers/net/ethernet/cadence/Makefile | 5 ++- drivers/net/ethernet/cadence/macb.h | 8 ++++ drivers/net/ethernet/cadence/macb_kunit.c | 73 +++++++++++++++++++++++++++++++ drivers/net/ethernet/cadence/macb_main.c | 15 +++++++ drivers/net/ethernet/cadence/macb_utils.c | 56 ++++++++++++++++++++++++ 6 files changed, 169 insertions(+), 1 deletion(-)
drivers/net/ethernet/cadence/Kconfig +13 -0
@@ -51,4 +51,17 @@ config MACB_PCI To compile this driver as a module, choose M here: the module will be called macb_pci. +config MACB_KUNIT_TEST + bool "KUnit test for MACB" if !KUNIT_ALL_TESTS + depends on KUNIT + depends on MACB + default KUNIT_ALL_TESTS + help + Build KUnit tests for the MACB driver. + + For more information on KUnit and unit tests in general, + please refer to the KUnit documentation. + + If unsure, say N. + endif # NET_VENDOR_CADENCE
drivers/net/ethernet/cadence/Makefile +4 -1
@@ -2,7 +2,7 @@ # # Makefile for the Atmel network device drivers. # -macb-y := macb_main.o +macb-y := macb_main.o macb_utils.o ifeq ($(CONFIG_MACB_USE_HWSTAMP),y) macb-y += macb_ptp.o
@@ -10,3 +10,6 @@ endif obj-$(CONFIG_MACB) += macb.o obj-$(CONFIG_MACB_PCI) += macb_pci.o + +obj-$(CONFIG_MACB_KUNIT_TEST) += macb_kunit.o +macb-test-y := macb_kunit.o macb_utils.o
drivers/net/ethernet/cadence/macb.h +8 -0
@@ -197,6 +197,9 @@#define GEM_TXBDCTRL 0x04cc/* TX Buffer Descriptor control register */ #define GEM_RXBDCTRL 0x04d0/* RX Buffer Descriptor control register */ +#define GEM_TXQSEGALLOC_LOWER 0x05A0/* Tx queue segment allocation (low) */ +#define GEM_TXQSEGALLOC_UPPER 0x05A4/* Tx queue segment allocation (high) */ +/* Screener Type 2 match registers */ #define GEM_SCRT2 0x540
@@ -549,6 +552,8 @@#define GEM_PBUF_CUTTHRU_SIZE 1#define GEM_DAW64_OFFSET 23#define GEM_DAW64_SIZE 1 +#define GEM_SEGMENTS_BIT_SIZE_OFFSET 16 +#define GEM_SEGMENTS_BIT_SIZE_SIZE 3/* Bitfields in DCFG8. */ #define GEM_T1SCR_OFFSET 24
@@ -1494,4 +1499,7 @@ struct macb_queue_enst_config { u8 queue_id; }; +u64 gem_sram_distribute_segments (unsigned int num_queues, +unsigned int num_segments); +#endif /* _MACB_H */
drivers/net/ethernet/cadence/macb_kunit.c new file +73 -0
73 lines not on this page · raw message
drivers/net/ethernet/cadence/macb_main.c +15 -0
@@ -2954,6 +2954,21 @@ static void macb_init_hw(struct macb *bp)if (bp->caps &MACB_CAPS_JUMBO ) bp->rx_frm_len_mask =MACB_RX_JFRMLEN_MASK ; +/* +* Distribute Tx SRAM segments evenly based on active number of queues. +*/ +if (macb_is_gem (bp)) { +unsigned int num_segments; +u64 val; + + num_segments =1U <<GEM_BFEXT (SEGMENTS_BIT_SIZE , +gem_readl (bp,DCFG6 )); + val =gem_sram_distribute_segments (bp->num_queues, + num_segments); +gem_writel (bp,TXQSEGALLOC_LOWER , val); +gem_writel (bp,TXQSEGALLOC_UPPER , val >>32 ); + } +macb_configure_dma (bp);/* Enable RX partial store and forward and set watermark */
drivers/net/ethernet/cadence/macb_utils.c new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v2 2/2] net: macb: distribute evenly Tx SRAM segments
Quoted 9 lines · On Wed, 11 Mar 2026 17:41:54 +0100 Théo Lebrun wrote:
GEM has registers to configure the Tx SRAM segments distribution across queues. The reset value is apprioriate (even spread) but we need to care if/when number of active queues is modified (or if we inherited unevenly initialised hardware from bootloader).
To distribute segments, we take as input the number of queues (
bp->num_queues) and the number of segments (found inside DCFG6). Its output is a number of segments for each queue, formatted as powers-of-two (eg 2 for queue 0 means it has 2^2=4 segments)
allmodconfig builds are not happy with whatever is going on here:
ld.lld: error: undefined symbol: gem_sram_distribute_segments
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
referenced by macb_kunit.c:13 (
drivers/net/ethernet/cadence/macb_kunit.c:13)
vmlinux.o:(macb_sram_segments_test)
referenced by macb_kunit.c:51 (drivers/net/ethernet/cadence/macb_kunit.c:51)
vmlinux.o:(macb_sram_segments_gen_params)
ld.lld: error: undefined symbol: kunit_binary_assert_format
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
referenced by macb_kunit.c:29 (
drivers/net/ethernet/cadence/macb_kunit.c:29)
vmlinux.o:(macb_sram_segments_test)
referenced by macb_kunit.c:34 (drivers/net/ethernet/cadence/macb_kunit.c:34)
vmlinux.o:(macb_sram_segments_test)
ld.lld: error: undefined symbol: __kunit_do_failed_assertion
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
referenced by macb_kunit.c:29 (
drivers/net/ethernet/cadence/macb_kunit.c:29)
vmlinux.o:(macb_sram_segments_test)
referenced by macb_kunit.c:34 (drivers/net/ethernet/cadence/macb_kunit.c:34)
vmlinux.o:(macb_sram_segments_test)
ld.lld: error: undefined symbol: __kunit_abort
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
referenced by macb_kunit.c:29 (
drivers/net/ethernet/cadence/macb_kunit.c:29)
vmlinux.o:(macb_sram_segments_test)
referenced by macb_kunit.c:34 (drivers/net/ethernet/cadence/macb_kunit.c:34)
vmlinux.o:(macb_sram_segments_test)