Implement Time-Aware Traffic Scheduling (TAPRIO) hardware offload for
"tc qdisc replace" operations, enabling IEEE 802.1Qbv compliant gate
scheduling on Cadence MACB/GEM controllers.
Parameter validation checks performed:
- Queue count bounds checking (1 < queues <= MACB_MAX_QUEUES)
- TC entry limit validation against available hardware queues
- Base time non-negativity enforcement
- Speed-adaptive timing constraint verification
- Cycle time vs. total gate time consistency checks
- Single-queue gate mask enforcement per scheduling entry
Hardware programming sequence:
- GEM doesn't support changing register values if ENST is running,
hence disable ENST before programming
- Atomic timing register configuration (START_TIME, ON_TIME, OFF_TIME)
- Enable the configured queues via ENST_CONTROL register
This implementation ensures deterministic gate scheduling while preventing
invalid configurations.
Signed-off-by: Vineeth Karumanchi <vineeth.karumanchi@amd.com>
---
drivers/net/ethernet/cadence/macb_main.c | 155 +++++++++++++++++++++++
1 file changed, 155 insertions(+)
diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index ff87d3e1d8a0..4518b59168d5 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -36,6 +36,7 @@
#include <linux/reset.h>
#include <linux/firmware/xlnx-zynqmp.h>
#include <linux/inetdevice.h>
+#include <net/pkt_sched.h>
#include "macb.h"
/* This structure is only used for MACB on SiFive FU540 devices */
@@ -4084,6 +4085,160 @@ static void macb_restore_features(struct macb *bp)
macb_set_rxflow_feature(bp, features);
}
+static int macb_taprio_setup_replace(struct net_device *ndev,
+ struct tc_taprio_qopt_offload *conf)
+{
+ u64 total_on_time = 0, start_time_sec = 0, start_time = conf->base_time;
+ struct queue_enst_configs *enst_queue;
+ u32 configured_queues = 0, speed = 0;
+ struct tc_taprio_sched_entry *entry;
+ struct macb *bp = netdev_priv(ndev);
+ struct ethtool_link_ksettings kset;
+ struct macb_queue *queue;
+ unsigned long flags;
+ int err = 0, i;
+
+ /* Validate queue configuration */
+ if (bp->num_queues < 1 || bp->num_queues > MACB_MAX_QUEUES) {
+ netdev_err(ndev, "Invalid number of queues: %d\n", bp->num_queues);
+ return -EINVAL;
+ }
+
+ if (conf->num_entries > bp->num_queues) {
+ netdev_err(ndev, "Too many TAPRIO entries: %lu > %d queues\n",
+ conf->num_entries, bp->num_queues);
+ return -EINVAL;
+ }
+
+ if (start_time < 0) {
+ netdev_err(ndev, "Invalid base_time: must be 0 or positive, got %lld\n",
+ conf->base_time);
+ return -ERANGE;
+ }
+
+ /* Get the current link speed */
+ err = phylink_ethtool_ksettings_get(bp->phylink, &kset);
+ if (unlikely(err)) {
+ netdev_err(ndev, "Failed to get link settings: %d\n", err);
+ return err;
+ }
+
+ speed = kset.base.speed;
+ if (unlikely(speed <= 0)) {
+ netdev_err(ndev, "Invalid speed: %d\n", speed);
+ return -EINVAL;
+ }
+
+ enst_queue = kcalloc(conf->num_entries, sizeof(*enst_queue), GFP_KERNEL);
+ if (!enst_queue)
+ return -ENOMEM;
+
+ /* Pre-validate all entries before making any hardware changes */
+ for (i = 0; i < conf->num_entries; i++) {
+ entry = &conf->entries[i];
+
+ if (entry->command != TC_TAPRIO_CMD_SET_GATES) {
+ netdev_err(ndev, "Entry %d: unsupported command %d\n",
+ i, entry->command);
+ err = -EOPNOTSUPP;
+ goto cleanup;
+ }
+
+ /* Validate gate_mask: must be nonzero, single queue, and within range */
+ if (!is_power_of_2(entry->gate_mask)) {
+ netdev_err(ndev, "Entry %d: gate_mask 0x%x is not a power of 2 (only one queue per entry allowed)\n",
+ i, entry->gate_mask);
+ err = -EINVAL;
+ goto cleanup;
+ }
+
+ /* gate_mask must not select queues outside the valid queue_mask */
+ if (entry->gate_mask & ~bp->queue_mask) {
+ netdev_err(ndev, "Entry %d: gate_mask 0x%x exceeds queue range (max_queues=%d)\n",
+ i, entry->gate_mask, bp->num_queues);
+ err = -EINVAL;
+ goto cleanup;
+ }
+
+ /* Check for start time limits */
+ start_time_sec = div_u64(start_time, NSEC_PER_SEC);
+ if (start_time_sec > ENST_MAX_START_TIME_SEC) {
+ netdev_err(ndev, "Entry %d: Start time %llu s exceeds hardware limit\n",
+ i, start_time_sec);
+ err = -ERANGE;
+ goto cleanup;
+ }
+
+ /* Check for on time limit*/
+ if (entry->interval > ENST_MAX_HW_INTERVAL(speed)) {
+ netdev_err(ndev, "Entry %d: interval %u ns exceeds hardware limit %lu ns\n",
+ i, entry->interval, ENST_MAX_HW_INTERVAL(speed));
+ err = -ERANGE;
+ goto cleanup;
+ }
+
+ /* Check for off time limit*/
+ if ((conf->cycle_time - entry->interval) > ENST_MAX_HW_INTERVAL(speed)) {
+ netdev_err(ndev, "Entry %d: off_time %llu ns exceeds hardware limit %lu ns\n",
+ i, conf->cycle_time - entry->interval,
+ ENST_MAX_HW_INTERVAL(speed));
+ err = -ERANGE;
+ goto cleanup;
+ }
+
+ enst_queue[i].queue_id = order_base_2(entry->gate_mask);
+ enst_queue[i].start_time_mask =
+ (start_time_sec << GEM_START_TIME_SEC_OFFSET) |
+ (start_time % NSEC_PER_SEC);
+ enst_queue[i].on_time_bytes =
+ ENST_NS_TO_HW_UNITS(entry->interval, speed);
+ enst_queue[i].off_time_bytes =
+ ENST_NS_TO_HW_UNITS(conf->cycle_time - entry->interval, speed);
+
+ configured_queues |= entry->gate_mask;
+ total_on_time += entry->interval;
+ start_time += entry->interval;
+ }
+
+ /* Check total interval doesn't exceed cycle time */
+ if (total_on_time > conf->cycle_time) {
+ netdev_err(ndev, "Total ON %llu ns exceeds cycle time %llu ns\n",
+ total_on_time, conf->cycle_time);
+ err = -EINVAL;
+ goto cleanup;
+ }
+
+ netdev_dbg(ndev, "TAPRIO setup: %lu entries, base_time=%lld ns, cycle_time=%llu ns\n",
+ conf->num_entries, conf->base_time, conf->cycle_time);
+
+ /* All validations passed - proceed with hardware configuration */
+ spin_lock_irqsave(&bp->lock, flags);
+
+ /* Disable ENST queues if running before configuring */
+ if (gem_readl(bp, ENST_CONTROL))
+ gem_writel(bp, ENST_CONTROL,
+ GENMASK(bp->num_queues - 1, 0) << GEM_ENST_DISABLE_QUEUE_OFFSET);
+
+ for (i = 0; i < conf->num_entries; i++) {
+ queue = &bp->queues[enst_queue[i].queue_id];
+ /* Configure queue timing registers */
+ queue_writel(queue, ENST_START_TIME, enst_queue[i].start_time_mask);
+ queue_writel(queue, ENST_ON_TIME, enst_queue[i].on_time_bytes);
+ queue_writel(queue, ENST_OFF_TIME, enst_queue[i].off_time_bytes);
+ }
+
+ /* Enable ENST for all configured queues in one write */
+ gem_writel(bp, ENST_CONTROL, configured_queues);
+ spin_unlock_irqrestore(&bp->lock, flags);
+
+ netdev_info(ndev, "TAPRIO configuration completed successfully: %lu entries, %d queues configured\n",
+ conf->num_entries, hweight32(configured_queues));
+
+cleanup:
+ kfree(enst_queue);
+ return err;
+}
+
static const struct net_device_ops macb_netdev_ops = {
.ndo_open = macb_open,
.ndo_stop = macb_close,
--
2.34.1
On 7/22/25 18:41, Vineeth Karumanchi wrote: > Implement Time-Aware Traffic Scheduling (TAPRIO) hardware offload for > "tc qdisc replace" operations, enabling IEEE 802.1Qbv compliant gate > scheduling on Cadence MACB/GEM controllers. > > Parameter validation checks performed: > - Queue count bounds checking (1 < queues <= MACB_MAX_QUEUES) > - TC entry limit validation against available hardware queues > - Base time non-negativity enforcement > - Speed-adaptive timing constraint verification > - Cycle time vs. total gate time consistency checks > - Single-queue gate mask enforcement per scheduling entry > > Hardware programming sequence: > - GEM doesn't support changing register values if ENST is running, > hence disable ENST before programming > - Atomic timing register configuration (START_TIME, ON_TIME, OFF_TIME) > - Enable the configured queues via ENST_CONTROL register > > This implementation ensures deterministic gate scheduling while preventing > invalid configurations. > > Signed-off-by: Vineeth Karumanchi <vineeth.karumanchi@amd.com> > --- > drivers/net/ethernet/cadence/macb_main.c | 155 +++++++++++++++++++++++ > 1 file changed, 155 insertions(+) > > diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c > index ff87d3e1d8a0..4518b59168d5 100644 > --- a/drivers/net/ethernet/cadence/macb_main.c > +++ b/drivers/net/ethernet/cadence/macb_main.c > @@ -36,6 +36,7 @@ > #include <linux/reset.h> > #include <linux/firmware/xlnx-zynqmp.h> > #include <linux/inetdevice.h> > +#include <net/pkt_sched.h> > #include "macb.h" > > /* This structure is only used for MACB on SiFive FU540 devices */ > @@ -4084,6 +4085,160 @@ static void macb_restore_features(struct macb *bp) > macb_set_rxflow_feature(bp, features); > } > > +static int macb_taprio_setup_replace(struct net_device *ndev, > + struct tc_taprio_qopt_offload *conf) This function is unused in this patch. Nothing mentions it. > +{ > + u64 total_on_time = 0, start_time_sec = 0, start_time = conf->base_time; > + struct queue_enst_configs *enst_queue; Extra space here -----------------^ > + u32 configured_queues = 0, speed = 0; > + struct tc_taprio_sched_entry *entry; > + struct macb *bp = netdev_priv(ndev); > + struct ethtool_link_ksettings kset; > + struct macb_queue *queue; > + unsigned long flags; > + int err = 0, i; No need to initialize err with zero. size_t i; as it is used along with conf->num_entries which has size_t type. > + > + /* Validate queue configuration */ > + if (bp->num_queues < 1 || bp->num_queues > MACB_MAX_QUEUES) { Can this happen? > + netdev_err(ndev, "Invalid number of queues: %d\n", bp->num_queues); > + return -EINVAL; > + } > + > + if (conf->num_entries > bp->num_queues) { > + netdev_err(ndev, "Too many TAPRIO entries: %lu > %d queues\n", > + conf->num_entries, bp->num_queues); > + return -EINVAL; > + } > + > + if (start_time < 0) { > + netdev_err(ndev, "Invalid base_time: must be 0 or positive, got %lld\n", > + conf->base_time); > + return -ERANGE; > + } > + > + /* Get the current link speed */ > + err = phylink_ethtool_ksettings_get(bp->phylink, &kset); > + if (unlikely(err)) { > + netdev_err(ndev, "Failed to get link settings: %d\n", err); > + return err; > + } > + > + speed = kset.base.speed; > + if (unlikely(speed <= 0)) { > + netdev_err(ndev, "Invalid speed: %d\n", speed); > + return -EINVAL; > + } > + > + enst_queue = kcalloc(conf->num_entries, sizeof(*enst_queue), GFP_KERNEL); To simplify the error path you can use something like: struct queue_enst_configs *enst_queue __free(kfree) = kcalloc(...); and drop the "goto cleanup" below. > + if (!enst_queue) > + return -ENOMEM; > + > + /* Pre-validate all entries before making any hardware changes */ > + for (i = 0; i < conf->num_entries; i++) { > + entry = &conf->entries[i]; > + > + if (entry->command != TC_TAPRIO_CMD_SET_GATES) { > + netdev_err(ndev, "Entry %d: unsupported command %d\n", > + i, entry->command); > + err = -EOPNOTSUPP; > + goto cleanup; > + } > + > + /* Validate gate_mask: must be nonzero, single queue, and within range */ > + if (!is_power_of_2(entry->gate_mask)) { > + netdev_err(ndev, "Entry %d: gate_mask 0x%x is not a power of 2 (only one queue per entry allowed)\n", > + i, entry->gate_mask); > + err = -EINVAL; > + goto cleanup; > + } > + > + /* gate_mask must not select queues outside the valid queue_mask */ > + if (entry->gate_mask & ~bp->queue_mask) { > + netdev_err(ndev, "Entry %d: gate_mask 0x%x exceeds queue range (max_queues=%d)\n", > + i, entry->gate_mask, bp->num_queues); > + err = -EINVAL; > + goto cleanup; > + } > + > + /* Check for start time limits */ > + start_time_sec = div_u64(start_time, NSEC_PER_SEC); > + if (start_time_sec > ENST_MAX_START_TIME_SEC) { > + netdev_err(ndev, "Entry %d: Start time %llu s exceeds hardware limit\n", > + i, start_time_sec); > + err = -ERANGE; > + goto cleanup; > + } > + > + /* Check for on time limit*/ Missing one space here -------------------^ > + if (entry->interval > ENST_MAX_HW_INTERVAL(speed)) { > + netdev_err(ndev, "Entry %d: interval %u ns exceeds hardware limit %lu ns\n", > + i, entry->interval, ENST_MAX_HW_INTERVAL(speed)); > + err = -ERANGE; > + goto cleanup; > + } > + > + /* Check for off time limit*/ > + if ((conf->cycle_time - entry->interval) > ENST_MAX_HW_INTERVAL(speed)) { > + netdev_err(ndev, "Entry %d: off_time %llu ns exceeds hardware limit %lu ns\n", > + i, conf->cycle_time - entry->interval, > + ENST_MAX_HW_INTERVAL(speed)); > + err = -ERANGE; > + goto cleanup; > + } > + > + enst_queue[i].queue_id = order_base_2(entry->gate_mask); > + enst_queue[i].start_time_mask = > + (start_time_sec << GEM_START_TIME_SEC_OFFSET) | > + (start_time % NSEC_PER_SEC); > + enst_queue[i].on_time_bytes = > + ENST_NS_TO_HW_UNITS(entry->interval, speed); > + enst_queue[i].off_time_bytes = > + ENST_NS_TO_HW_UNITS(conf->cycle_time - entry->interval, speed); > + > + configured_queues |= entry->gate_mask; > + total_on_time += entry->interval; > + start_time += entry->interval; > + } > + > + /* Check total interval doesn't exceed cycle time */ > + if (total_on_time > conf->cycle_time) { > + netdev_err(ndev, "Total ON %llu ns exceeds cycle time %llu ns\n", > + total_on_time, conf->cycle_time); > + err = -EINVAL; > + goto cleanup; > + } > + > + netdev_dbg(ndev, "TAPRIO setup: %lu entries, base_time=%lld ns, cycle_time=%llu ns\n", > + conf->num_entries, conf->base_time, conf->cycle_time); > + > + /* All validations passed - proceed with hardware configuration */ > + spin_lock_irqsave(&bp->lock, flags); You can use guard(spinlock_irqsave)(&bp->lock) or scoped_guard(spinlock_irqsave, &bp->lock) > + > + /* Disable ENST queues if running before configuring */ > + if (gem_readl(bp, ENST_CONTROL)) Is this read necessary? > + gem_writel(bp, ENST_CONTROL, > + GENMASK(bp->num_queues - 1, 0) << GEM_ENST_DISABLE_QUEUE_OFFSET); This could be replaced by GEM_BF(GENMASK(...), ENST_DISABLE_QUEUE) if you define GEM_ENST_DISABLE_QUEUE_SIZE along with GEM_ENST_DISABLE_QUEUE_OFFSET. > + > + for (i = 0; i < conf->num_entries; i++) { > + queue = &bp->queues[enst_queue[i].queue_id]; > + /* Configure queue timing registers */ > + queue_writel(queue, ENST_START_TIME, enst_queue[i].start_time_mask); > + queue_writel(queue, ENST_ON_TIME, enst_queue[i].on_time_bytes); > + queue_writel(queue, ENST_OFF_TIME, enst_queue[i].off_time_bytes); > + } > + > + /* Enable ENST for all configured queues in one write */ > + gem_writel(bp, ENST_CONTROL, configured_queues); Can this function be executed while other queues are configured? If so, would the configured_queues contains it (as well as conf)? > + spin_unlock_irqrestore(&bp->lock, flags); > + > + netdev_info(ndev, "TAPRIO configuration completed successfully: %lu entries, %d queues configured\n", > + conf->num_entries, hweight32(configured_queues)); > + > +cleanup: > + kfree(enst_queue); With the suggestions above, this could be dropped. Thank you, Claudiu > + return err; > +} > + > static const struct net_device_ops macb_netdev_ops = { > .ndo_open = macb_open, > .ndo_stop = macb_close,
Hi Claudiu, Thanks for the review. On 7/26/2025 5:55 PM, claudiu beznea (tuxon) wrote: > > > On 7/22/25 18:41, Vineeth Karumanchi wrote: >> Implement Time-Aware Traffic Scheduling (TAPRIO) hardware offload for <..> > > as it is used along with conf->num_entries which has size_t type. > >> + >> + /* Validate queue configuration */ >> + if (bp->num_queues < 1 || bp->num_queues > MACB_MAX_QUEUES) { > > Can this happen? Yes, GEM in Zynq devices has single queue. Currently, TAPRIO offload validation depends solely on the presence of .ndo_setup_tc. On Zynq-based devices, if the user configures the scheduler using tc replace, the operation fails at this point. <...> >> + /* All validations passed - proceed with hardware configuration */ >> + spin_lock_irqsave(&bp->lock, flags); > > You can use guard(spinlock_irqsave)(&bp->lock) or > scoped_guard(spinlock_irqsave, &bp->lock) > ok, will leverage scoped_guard(spinlock_irqsave, &bp->lock). >> + >> + /* Disable ENST queues if running before configuring */ >> + if (gem_readl(bp, ENST_CONTROL)) > > Is this read necessary? > Not necessary, I thought of disabling only if enabled. But, will disable directly. >> + gem_writel(bp, ENST_CONTROL, >> + GENMASK(bp->num_queues - 1, 0) << >> GEM_ENST_DISABLE_QUEUE_OFFSET); > > This could be replaced by GEM_BF(GENMASK(...), ENST_DISABLE_QUEUE) if > you define GEM_ENST_DISABLE_QUEUE_SIZE along with > GEM_ENST_DISABLE_QUEUE_OFFSET. > I can leverage bp->queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET. And remove GEM_ENST_ENABLE_QUEUE(hw_q) and GEM_ENST_DISABLE_QUEUE(hw_q) implementations. >> + >> + for (i = 0; i < conf->num_entries; i++) { >> + queue = &bp->queues[enst_queue[i].queue_id]; >> + /* Configure queue timing registers */ >> + queue_writel(queue, ENST_START_TIME, >> enst_queue[i].start_time_mask); >> + queue_writel(queue, ENST_ON_TIME, enst_queue[i].on_time_bytes); >> + queue_writel(queue, ENST_OFF_TIME, >> enst_queue[i].off_time_bytes); >> + } >> + >> + /* Enable ENST for all configured queues in one write */ >> + gem_writel(bp, ENST_CONTROL, configured_queues); > > Can this function be executed while other queues are configured? If so, > would the configured_queues contains it (as well as conf)? > No, the tc add/replace command re-configures all queues, replacing any previous setup. Parameters such as START_TIME, ON_TIME, and CYCLE_TIME are recalculated based on the new configuration. >> + spin_unlock_irqrestore(&bp->lock, flags); >> + >> + netdev_info(ndev, "TAPRIO configuration completed successfully: >> %lu entries, %d queues configured\n", >> + conf->num_entries, hweight32(configured_queues)); >> + >> +cleanup: >> + kfree(enst_queue); > > With the suggestions above, this could be dropped. > ok. > Thank you, > Claudiu > >> + return err; >> +} >> + >> static const struct net_device_ops macb_netdev_ops = { >> .ndo_open = macb_open, >> .ndo_stop = macb_close, > Thanks -- 🙏 vineeth
On 29.07.2025 11:59, Karumanchi, Vineeth wrote: > Hi Claudiu, > > Thanks for the review. > > On 7/26/2025 5:55 PM, claudiu beznea (tuxon) wrote: >> >> >> On 7/22/25 18:41, Vineeth Karumanchi wrote: >>> Implement Time-Aware Traffic Scheduling (TAPRIO) hardware offload for > > <..> > >> >> as it is used along with conf->num_entries which has size_t type. >> >>> + >>> + /* Validate queue configuration */ >>> + if (bp->num_queues < 1 || bp->num_queues > MACB_MAX_QUEUES) { >> >> Can this happen? > > Yes, GEM in Zynq devices has single queue. I was asking as it looked to me that this validates the number of queues the IP supports, which should have already been validated in probe. > > Currently, TAPRIO offload validation depends solely on the presence > of .ndo_setup_tc. On Zynq-based devices, if the user configures the > scheduler using tc replace, the operation fails at this point. I can't see how. That should translate into: if (1 < 1 || 1 > 8) which is in the end: if (0) Maybe it fails due to some other condition? > > <...> > >>> + /* All validations passed - proceed with hardware configuration */ >>> + spin_lock_irqsave(&bp->lock, flags); >> >> You can use guard(spinlock_irqsave)(&bp->lock) or >> scoped_guard(spinlock_irqsave, &bp->lock) >> > > ok, will leverage scoped_guard(spinlock_irqsave, &bp->lock). > >>> + >>> + /* Disable ENST queues if running before configuring */ >>> + if (gem_readl(bp, ENST_CONTROL)) >> >> Is this read necessary? >> > > Not necessary, I thought of disabling only if enabled. > But, will disable directly. > >>> + gem_writel(bp, ENST_CONTROL, >>> + GENMASK(bp->num_queues - 1, 0) << >>> GEM_ENST_DISABLE_QUEUE_OFFSET); >> >> This could be replaced by GEM_BF(GENMASK(...), ENST_DISABLE_QUEUE) if you >> define GEM_ENST_DISABLE_QUEUE_SIZE along with GEM_ENST_DISABLE_QUEUE_OFFSET. >> > > I can leverage bp->queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET. > And remove GEM_ENST_ENABLE_QUEUE(hw_q) and GEM_ENST_DISABLE_QUEUE(hw_q) > implementations. > >>> + >>> + for (i = 0; i < conf->num_entries; i++) { >>> + queue = &bp->queues[enst_queue[i].queue_id]; >>> + /* Configure queue timing registers */ >>> + queue_writel(queue, ENST_START_TIME, >>> enst_queue[i].start_time_mask); >>> + queue_writel(queue, ENST_ON_TIME, enst_queue[i].on_time_bytes); >>> + queue_writel(queue, ENST_OFF_TIME, enst_queue[i].off_time_bytes); >>> + } >>> + >>> + /* Enable ENST for all configured queues in one write */ >>> + gem_writel(bp, ENST_CONTROL, configured_queues); >> >> Can this function be executed while other queues are configured? If so, >> would the configured_queues contains it (as well as conf)? >> > > No, the tc add/replace command re-configures all queues, replacing any > previous setup. Parameters such as START_TIME, ON_TIME, and CYCLE_TIME are > recalculated based on the new configuration. > >>> + spin_unlock_irqrestore(&bp->lock, flags); >>> + >>> + netdev_info(ndev, "TAPRIO configuration completed successfully: %lu >>> entries, %d queues configured\n", >>> + conf->num_entries, hweight32(configured_queues)); >>> + >>> +cleanup: >>> + kfree(enst_queue); >> >> With the suggestions above, this could be dropped. >> > > ok. Please check the documentation pointed by Andrew. With that, my suggestion here should be dropped. Thank you, Claudiu > > >> Thank you, >> Claudiu >> >>> + return err; >>> +} >>> + >>> static const struct net_device_ops macb_netdev_ops = { >>> .ndo_open = macb_open, >>> .ndo_stop = macb_close, >> > > Thanks
> > + enst_queue = kcalloc(conf->num_entries, sizeof(*enst_queue), GFP_KERNEL); > > To simplify the error path you can use something like: > > struct queue_enst_configs *enst_queue __free(kfree) = kcalloc(...); > > and drop the "goto cleanup" below. https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html 1.6.5. Using device-managed and cleanup.h constructs Netdev remains skeptical about promises of all “auto-cleanup” APIs, including even devm_ helpers, historically. They are not the preferred style of implementation, merely an acceptable one. Use of guard() is discouraged within any function longer than 20 lines, scoped_guard() is considered more readable. Using normal lock/unlock is still (weakly) preferred. Low level cleanup constructs (such as __free()) can be used when building APIs and helpers, especially scoped iterators. However, direct use of __free() within networking core and drivers is discouraged. Similar guidance applies to declaring variables mid-function. > > You can use guard(spinlock_irqsave)(&bp->lock) or > scoped_guard(spinlock_irqsave, &bp->lock) scoped_guard() if anything. > > > + > > + /* Disable ENST queues if running before configuring */ > > + if (gem_readl(bp, ENST_CONTROL)) > > Is this read necessary? > > > + gem_writel(bp, ENST_CONTROL, > > + GENMASK(bp->num_queues - 1, 0) << GEM_ENST_DISABLE_QUEUE_OFFSET); > > This could be replaced by GEM_BF(GENMASK(...), ENST_DISABLE_QUEUE) if you > define GEM_ENST_DISABLE_QUEUE_SIZE along with GEM_ENST_DISABLE_QUEUE_OFFSET. > > > + > > + for (i = 0; i < conf->num_entries; i++) { > > + queue = &bp->queues[enst_queue[i].queue_id]; > > + /* Configure queue timing registers */ > > + queue_writel(queue, ENST_START_TIME, enst_queue[i].start_time_mask); > > + queue_writel(queue, ENST_ON_TIME, enst_queue[i].on_time_bytes); > > + queue_writel(queue, ENST_OFF_TIME, enst_queue[i].off_time_bytes); > > + } > > + > > + /* Enable ENST for all configured queues in one write */ > > + gem_writel(bp, ENST_CONTROL, configured_queues); > > Can this function be executed while other queues are configured? If so, > would the configured_queues contains it (as well as conf)? > > > + spin_unlock_irqrestore(&bp->lock, flags); > > + > > + netdev_info(ndev, "TAPRIO configuration completed successfully: %lu entries, %d queues configured\n", > > + conf->num_entries, hweight32(configured_queues)); guard() would put that netdev_info() print inside the guard. Do you really want to be doing a print, inside a spinlock, with interrupts potentially disabled? This is one of the reasons i don't like this magical guard() construct, it is easy to forget how the magic works and end up with sub optimal code. A scoped_guard() would avoid this issue. You have to think about where you want to lock released in order to place the } . Andrew
Hi Vineeth, kernel test robot noticed the following build warnings: [auto build test WARNING on net-next/main] url: https://github.com/intel-lab-lkp/linux/commits/Vineeth-Karumanchi/net-macb-Define-ENST-hardware-registers-for-time-aware-scheduling/20250722-234618 base: net-next/main patch link: https://lore.kernel.org/r/20250722154111.1871292-4-vineeth.karumanchi%40amd.com patch subject: [PATCH net-next 3/6] net: macb: Add IEEE 802.1Qbv TAPRIO REPLACE command offload support config: i386-randconfig-015-20250723 (https://download.01.org/0day-ci/archive/20250723/202507231739.LnkR6Gdd-lkp@intel.com/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250723/202507231739.LnkR6Gdd-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202507231739.LnkR6Gdd-lkp@intel.com/ All warnings (new ones prefixed by >>): >> drivers/net/ethernet/cadence/macb_main.c:4109:7: warning: format specifies type 'unsigned long' but the argument has type 'size_t' (aka 'unsigned int') [-Wformat] 4108 | netdev_err(ndev, "Too many TAPRIO entries: %lu > %d queues\n", | ~~~ | %zu 4109 | conf->num_entries, bp->num_queues); | ^~~~~~~~~~~~~~~~~ drivers/net/ethernet/cadence/macb_main.c:4212:6: warning: format specifies type 'unsigned long' but the argument has type 'size_t' (aka 'unsigned int') [-Wformat] 4211 | netdev_dbg(ndev, "TAPRIO setup: %lu entries, base_time=%lld ns, cycle_time=%llu ns\n", | ~~~ | %zu 4212 | conf->num_entries, conf->base_time, conf->cycle_time); | ^~~~~~~~~~~~~~~~~ include/net/net_debug.h:66:46: note: expanded from macro 'netdev_dbg' 66 | netdev_printk(KERN_DEBUG, __dev, format, ##args); \ | ~~~~~~ ^~~~ drivers/net/ethernet/cadence/macb_main.c:4235:7: warning: format specifies type 'unsigned long' but the argument has type 'size_t' (aka 'unsigned int') [-Wformat] 4234 | netdev_info(ndev, "TAPRIO configuration completed successfully: %lu entries, %d queues configured\n", | ~~~ | %zu 4235 | conf->num_entries, hweight32(configured_queues)); | ^~~~~~~~~~~~~~~~~ drivers/net/ethernet/cadence/macb_main.c:4088:12: warning: unused function 'macb_taprio_setup_replace' [-Wunused-function] 4088 | static int macb_taprio_setup_replace(struct net_device *ndev, | ^~~~~~~~~~~~~~~~~~~~~~~~~ 4 warnings generated. vim +4109 drivers/net/ethernet/cadence/macb_main.c 4087 4088 static int macb_taprio_setup_replace(struct net_device *ndev, 4089 struct tc_taprio_qopt_offload *conf) 4090 { 4091 u64 total_on_time = 0, start_time_sec = 0, start_time = conf->base_time; 4092 struct queue_enst_configs *enst_queue; 4093 u32 configured_queues = 0, speed = 0; 4094 struct tc_taprio_sched_entry *entry; 4095 struct macb *bp = netdev_priv(ndev); 4096 struct ethtool_link_ksettings kset; 4097 struct macb_queue *queue; 4098 unsigned long flags; 4099 int err = 0, i; 4100 4101 /* Validate queue configuration */ 4102 if (bp->num_queues < 1 || bp->num_queues > MACB_MAX_QUEUES) { 4103 netdev_err(ndev, "Invalid number of queues: %d\n", bp->num_queues); 4104 return -EINVAL; 4105 } 4106 4107 if (conf->num_entries > bp->num_queues) { 4108 netdev_err(ndev, "Too many TAPRIO entries: %lu > %d queues\n", > 4109 conf->num_entries, bp->num_queues); 4110 return -EINVAL; 4111 } 4112 4113 if (start_time < 0) { 4114 netdev_err(ndev, "Invalid base_time: must be 0 or positive, got %lld\n", 4115 conf->base_time); 4116 return -ERANGE; 4117 } 4118 4119 /* Get the current link speed */ 4120 err = phylink_ethtool_ksettings_get(bp->phylink, &kset); 4121 if (unlikely(err)) { 4122 netdev_err(ndev, "Failed to get link settings: %d\n", err); 4123 return err; 4124 } 4125 4126 speed = kset.base.speed; 4127 if (unlikely(speed <= 0)) { 4128 netdev_err(ndev, "Invalid speed: %d\n", speed); 4129 return -EINVAL; 4130 } 4131 4132 enst_queue = kcalloc(conf->num_entries, sizeof(*enst_queue), GFP_KERNEL); 4133 if (!enst_queue) 4134 return -ENOMEM; 4135 4136 /* Pre-validate all entries before making any hardware changes */ 4137 for (i = 0; i < conf->num_entries; i++) { 4138 entry = &conf->entries[i]; 4139 4140 if (entry->command != TC_TAPRIO_CMD_SET_GATES) { 4141 netdev_err(ndev, "Entry %d: unsupported command %d\n", 4142 i, entry->command); 4143 err = -EOPNOTSUPP; 4144 goto cleanup; 4145 } 4146 4147 /* Validate gate_mask: must be nonzero, single queue, and within range */ 4148 if (!is_power_of_2(entry->gate_mask)) { 4149 netdev_err(ndev, "Entry %d: gate_mask 0x%x is not a power of 2 (only one queue per entry allowed)\n", 4150 i, entry->gate_mask); 4151 err = -EINVAL; 4152 goto cleanup; 4153 } 4154 4155 /* gate_mask must not select queues outside the valid queue_mask */ 4156 if (entry->gate_mask & ~bp->queue_mask) { 4157 netdev_err(ndev, "Entry %d: gate_mask 0x%x exceeds queue range (max_queues=%d)\n", 4158 i, entry->gate_mask, bp->num_queues); 4159 err = -EINVAL; 4160 goto cleanup; 4161 } 4162 4163 /* Check for start time limits */ 4164 start_time_sec = div_u64(start_time, NSEC_PER_SEC); 4165 if (start_time_sec > ENST_MAX_START_TIME_SEC) { 4166 netdev_err(ndev, "Entry %d: Start time %llu s exceeds hardware limit\n", 4167 i, start_time_sec); 4168 err = -ERANGE; 4169 goto cleanup; 4170 } 4171 4172 /* Check for on time limit*/ 4173 if (entry->interval > ENST_MAX_HW_INTERVAL(speed)) { 4174 netdev_err(ndev, "Entry %d: interval %u ns exceeds hardware limit %lu ns\n", 4175 i, entry->interval, ENST_MAX_HW_INTERVAL(speed)); 4176 err = -ERANGE; 4177 goto cleanup; 4178 } 4179 4180 /* Check for off time limit*/ 4181 if ((conf->cycle_time - entry->interval) > ENST_MAX_HW_INTERVAL(speed)) { 4182 netdev_err(ndev, "Entry %d: off_time %llu ns exceeds hardware limit %lu ns\n", 4183 i, conf->cycle_time - entry->interval, 4184 ENST_MAX_HW_INTERVAL(speed)); 4185 err = -ERANGE; 4186 goto cleanup; 4187 } 4188 4189 enst_queue[i].queue_id = order_base_2(entry->gate_mask); 4190 enst_queue[i].start_time_mask = 4191 (start_time_sec << GEM_START_TIME_SEC_OFFSET) | 4192 (start_time % NSEC_PER_SEC); 4193 enst_queue[i].on_time_bytes = 4194 ENST_NS_TO_HW_UNITS(entry->interval, speed); 4195 enst_queue[i].off_time_bytes = 4196 ENST_NS_TO_HW_UNITS(conf->cycle_time - entry->interval, speed); 4197 4198 configured_queues |= entry->gate_mask; 4199 total_on_time += entry->interval; 4200 start_time += entry->interval; 4201 } 4202 4203 /* Check total interval doesn't exceed cycle time */ 4204 if (total_on_time > conf->cycle_time) { 4205 netdev_err(ndev, "Total ON %llu ns exceeds cycle time %llu ns\n", 4206 total_on_time, conf->cycle_time); 4207 err = -EINVAL; 4208 goto cleanup; 4209 } 4210 4211 netdev_dbg(ndev, "TAPRIO setup: %lu entries, base_time=%lld ns, cycle_time=%llu ns\n", 4212 conf->num_entries, conf->base_time, conf->cycle_time); 4213 4214 /* All validations passed - proceed with hardware configuration */ 4215 spin_lock_irqsave(&bp->lock, flags); 4216 4217 /* Disable ENST queues if running before configuring */ 4218 if (gem_readl(bp, ENST_CONTROL)) 4219 gem_writel(bp, ENST_CONTROL, 4220 GENMASK(bp->num_queues - 1, 0) << GEM_ENST_DISABLE_QUEUE_OFFSET); 4221 4222 for (i = 0; i < conf->num_entries; i++) { 4223 queue = &bp->queues[enst_queue[i].queue_id]; 4224 /* Configure queue timing registers */ 4225 queue_writel(queue, ENST_START_TIME, enst_queue[i].start_time_mask); 4226 queue_writel(queue, ENST_ON_TIME, enst_queue[i].on_time_bytes); 4227 queue_writel(queue, ENST_OFF_TIME, enst_queue[i].off_time_bytes); 4228 } 4229 4230 /* Enable ENST for all configured queues in one write */ 4231 gem_writel(bp, ENST_CONTROL, configured_queues); 4232 spin_unlock_irqrestore(&bp->lock, flags); 4233 4234 netdev_info(ndev, "TAPRIO configuration completed successfully: %lu entries, %d queues configured\n", 4235 conf->num_entries, hweight32(configured_queues)); 4236 4237 cleanup: 4238 kfree(enst_queue); 4239 return err; 4240 } 4241 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
© 2016 - 2025 Red Hat, Inc.