net: macb: implement ethtool set channels count operation
Subsystems (3)
Théo Lebrun
maintainerv4 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.
Have a nice day,
Thanks,
Théo
---
Changes in v4:
- macb_set_channels(): drop count == old_count check, it is redundant
with both netlink and ioctl codepaths. See early returns in ethnl_set_channels() and ethtool_set_channels(). Discussion @ V2.
- Rebase to latest net-next (8737d7194d6d); nothing to report.
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- Makefile: fix compilation of kunit tests. This bug was hidden before
because everything was builtin so it ended up linked together anyway.
Now macb.o is the amalgation of macb_{main,ptp,utils}.o and
macb_test.o is the amalgation of macb_{kunit,utils}.o.
- Kconfig CONFIG_MACB_KUNIT_TEST entry:
- Turn into tristate; no good reason to restrict to bool.
- Drop "depends on MACB". We do not need to compile the full MACB
driver to test gem_sram_distribute_segments().
- Add usual MODULE_*() macros to macb_kunit.c.
- Drop unused init value of ret in macb_set_channels().
- Rebase to latest net-next; nothing to report.
- Link to v2: https://patch.msgid.link/[email protected]
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 | 12 ++++ drivers/net/ethernet/cadence/Makefile | 5 +- drivers/net/ethernet/cadence/macb.h | 9 +++ drivers/net/ethernet/cadence/macb_kunit.c | 77 ++++++++++++++++++++ drivers/net/ethernet/cadence/macb_main.c | 115 +++++++++++++++++++++++++----- drivers/net/ethernet/cadence/macb_utils.c | 56 +++++++++++++++ 6 files changed, 257 insertions(+), 17 deletions(-)
---
base-commit: 1b0fdd40f80731e63c6c7559d67ffde224a63bd2
change-id: 20260305-macb-set-channels-5bf6e07f3270
Best regards,
Signature · 1 line
Théo Lebrun <[email protected]>
Théo Lebrun
maintainerPATCH v4 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 | 100 ++++++++++++++++++++++++++----- 2 files changed, 85 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 +84 -16
170 lines not on this page · raw message
Signature · 1 line
2.53.0
Jakub Kicinski
maintainerRe: [PATCH net-next v4 1/2] net: macb: implement ethtool_ops.get|set_channels()
Quoted 25 lines · On Tue, 17 Mar 2026 17:19:08 +0100 Théo Lebrun wrote:
drivers/net/ethernet/cadence/macb_main.c +25
-0+static int macb_set_channels (struct net_device *netdev, +struct ethtool_channels *ch) +{ +struct macb *bp =netdev_priv (netdev); +unsigned int old_count = bp->num_queues; +unsigned int count = ch->combined_count; +int ret; + +/* +* 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. +*/
drivers/net/ethernet/cadence/macb_main.c static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) · line 4203
Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10
-0+if (netif_running (bp->dev)) +return -EBUSY ; + + ret =netif_set_real_num_queues (bp->dev, count, count); +if (ret) +return ret; + + bp->num_queues = count; +return 0 ; +}
Looks like some left over here:
drivers/net/ethernet/cadence/macb_main.c:4173:15: warning: unused variable 'old_count' [-Wunused-variable]
4173 | unsigned int old_count = bp->num_queues;
| ^~~~~~~~~Théo Lebrun
maintainerPATCH v4 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 macb
--- drivers/net/ethernet/cadence/Kconfig | 12 +++++ drivers/net/ethernet/cadence/Makefile | 5 +- drivers/net/ethernet/cadence/macb.h | 8 ++++ drivers/net/ethernet/cadence/macb_kunit.c | 77 +++++++++++++++++++++++++++++++ drivers/net/ethernet/cadence/macb_main.c | 15 ++++++ drivers/net/ethernet/cadence/macb_utils.c | 56 ++++++++++++++++++++++ 6 files changed, 172 insertions(+), 1 deletion(-)
drivers/net/ethernet/cadence/Kconfig +12 -0
@@ -51,4 +51,16 @@ config MACB_PCI To compile this driver as a module, choose M here: the module will be called macb_pci. +config MACB_KUNIT_TEST + tristate "KUnit test for MACB" if !KUNIT_ALL_TESTS + depends on KUNIT + 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_test.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 +77 -0
77 lines not on this page · raw message
drivers/net/ethernet/cadence/macb_main.c +15 -0
@@ -3053,6 +3053,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