Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by James Hilliard
Fri, 13 Nov 2026 17:12
[PATCH net-next v4 2/3] net: phy: acx00: report receive statistics
Author: James Hilliard
Date: Fri, 13 Nov 2026 17:12
Date: Fri, 13 Nov 2026 17:12
329 lines
22252 bytes
22252 bytes
Expose receive frame and byte totals through the generic PHY statistics
interface as rx_packets_total and rx_bytes_total, alongside CRC errors in
rx_errors. The packet and byte counters include errored frames as well as
successfully received traffic, so leave the successful-only fields unset.
Collect the free-running 32-bit counters through phylib's update_stats
callback and accumulate wrap-adjusted deltas in 64-bit totals. Hardware
measurements on AC200 and AC300 established a receive-byte modulus of
4,000,000,000. The byte counts include Ethernet headers, padding and FCS,
but exclude preamble and SFD. The packet and CRC-error counters wrap at
2^32. Reject byte samples at or above the byte modulus before updating
either the baseline or the totals.
Read the high half on either side of the low half to detect a carry.
Limit each counter to three attempts so persistent inconsistency cannot
retain the PHY, package-state and MDIO locks indefinitely. Skip an
unstable snapshot and retry on the next poll: reporting a synthetic
error through update_stats would make phylib take the link down.
Keep incomplete samples distinct from MDIO errors so page-restoration
failures still propagate.
Serialize collection and reporting with the package-state mutex so reads
cannot race reset or power-off, and the totals are safe on 32-bit systems.
Commit a snapshot only after all counter reads, validation and page
restoration succeed. The reporting callback returns cached totals without
accessing MDIO.
Take a best-effort final snapshot before suspend, but invalidate the
baseline at entry to soft_reset: phylib may already have reset the PHY
through BMCR or its reset signal. Sampling against the previous baseline
then would count the cleared registers as a wrap. Invalidate it before
power-off too, including failed power-off attempts.
Start a new collection interval on resume, including when WoL kept the
PHY powered while polling stopped. Also discard deltas spanning at least
320 seconds, the minimum time for a full byte-counter cycle at 100 Mb/s.
Measure that interval with boottime, including system sleep and register
read duration, since the MDIO sleep path can bypass the PHY PM callbacks.
Retain previously accumulated totals, but do not account for traffic in
these unobserved intervals: complete counter cycles cannot be recovered
from the endpoint values. An unusable initial snapshot is retried by the
periodic collector without preventing initialization.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/net/phy/xpowers/acx00.c | 184 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 181 insertions(+), 3 deletions(-)
diff --git a/drivers/net/phy/xpowers/acx00.c b/drivers/net/phy/xpowers/acx00.c
index cee6c7ff0278..fb53bac12195 100644
--- a/drivers/net/phy/xpowers/acx00.c
+++ b/drivers/net/phy/xpowers/acx00.c
@@ -8,12 +8,14 @@
#include <linux/bitfield.h>
#include <linux/device.h>
+#include <linux/ethtool.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
#include <linux/of.h>
#include <linux/phy.h>
#include <linux/slab.h>
+#include <linux/timekeeping.h>
#include "../phylib.h"
#include "acx00.h"
@@ -31,6 +33,7 @@
#define ACX00_PAGE_2 2
#define ACX00_PAGE_6 6
#define ACX00_PAGE_8 8
+#define ACX00_PAGE_9 9
#define ACX00_PAGE0_GLOBAL_CONFIG_REG 0x13
#define ACX00_PAGE0_XMII_RX_CLOCK_INVERT BIT(12)
@@ -76,6 +79,32 @@
(ACX00_PAGE8_AUTO_CAL_TX_LEVEL_ADJUST_BYPASS | \
ACX00_PAGE8_AUTO_CAL_LOW_CAL_OPAQUE_BITS)
+#define ACX00_PAGE9_RX_BYTES_HIGH_REG 0x19
+#define ACX00_PAGE9_RX_PACKETS_HIGH_REG 0x1b
+#define ACX00_PAGE9_RX_CRC_ERRORS_HIGH_REG 0x1d
+
+/*
+ * AC200 and AC300 hardware testing confirmed that the receive byte counter
+ * wraps at 4,000,000,000 rather than 2^32.
+ */
+#define ACX00_RX_BYTES_WRAP 4000000000ULL
+/* At 100 Mb/s, a full byte-counter cycle takes at least 320 seconds. */
+#define ACX00_STATS_MAX_INTERVAL_MS (ACX00_RX_BYTES_WRAP * 8 / (SPEED_100 * 1000))
+#define ACX00_COUNTER_READ_ATTEMPTS 3
+
+enum acx00_ephy_stat {
+ ACX00_STAT_RX_BYTES,
+ ACX00_STAT_RX_PACKETS,
+ ACX00_STAT_RX_CRC_ERRORS,
+ ACX00_NUM_STATS,
+};
+
+static const u8 acx00_ephy_stat_regs[ACX00_NUM_STATS] = {
+ [ACX00_STAT_RX_BYTES] = ACX00_PAGE9_RX_BYTES_HIGH_REG,
+ [ACX00_STAT_RX_PACKETS] = ACX00_PAGE9_RX_PACKETS_HIGH_REG,
+ [ACX00_STAT_RX_CRC_ERRORS] = ACX00_PAGE9_RX_CRC_ERRORS_HIGH_REG,
+};
+
/*
* Another integration of this exact-ID PHY documents its digital vendor
* register map, which also matches the observed ACx00 reset values. The ACx00
@@ -86,11 +115,16 @@
struct acx00_ephy_priv {
struct phy_device *phydev;
struct acx00_ephy_control *control;
- /* Serializes package state and multi-register power sequences. */
+ /* Serializes package state, power sequences and statistics. */
struct mutex state_lock;
bool is_ac300;
bool use_low_calibration_tuning;
bool xmii_rx_clock_inverted;
+ bool stats_active;
+ bool stats_valid;
+ ktime_t stats_start;
+ u32 stats_last[ACX00_NUM_STATS];
+ u64 stats[ACX00_NUM_STATS];
};
static int acx00_ephy_read_page(struct phy_device *phydev)
@@ -110,6 +144,124 @@ static int acx00_ephy_write_page(struct phy_device *phydev, int page)
FIELD_PREP(ACX00_PAGE_SELECT_MASK, page));
}
+/* Return 0 on success, 1 for an unstable sample, or a negative MDIO error. */
+static int acx00_ephy_read_counter(struct phy_device *phydev, u8 reg, u32 *count)
+{
+ int high, low, check;
+ int i;
+
+ /* The free-running counter can carry while its two halves are read. */
+ for (i = 0; i < ACX00_COUNTER_READ_ATTEMPTS; i++) {
+ high = __phy_read(phydev, reg);
+ if (high < 0)
+ return high;
+ low = __phy_read(phydev, reg + 1);
+ if (low < 0)
+ return low;
+ check = __phy_read(phydev, reg);
+ if (check < 0)
+ return check;
+ if (high != check)
+ continue;
+
+ *count = (u32)high << 16 | low;
+ return 0;
+ }
+
+ return 1;
+}
+
+static int __acx00_ephy_update_stats(struct phy_device *phydev)
+{
+ struct acx00_ephy_priv *priv = phydev->priv;
+ u32 counts[ACX00_NUM_STATS];
+ ktime_t start;
+ int oldpage;
+ int ret;
+ int i;
+
+ lockdep_assert_held(&priv->state_lock);
+
+ if (!priv->stats_active)
+ return 0;
+
+ start = ktime_get_boottime();
+ oldpage = phy_select_page(phydev, ACX00_PAGE_9);
+ if (oldpage < 0)
+ return phy_restore_page(phydev, oldpage, oldpage);
+
+ for (i = 0; i < ACX00_NUM_STATS; i++) {
+ ret = acx00_ephy_read_counter(phydev, acx00_ephy_stat_regs[i],
+ &counts[i]);
+ if (ret)
+ break;
+ }
+ ret = phy_restore_page(phydev, oldpage, ret);
+ if (ret < 0)
+ return ret;
+
+ /* Skip unusable samples without turning a statistics miss into a link error. */
+ if (ret || counts[ACX00_STAT_RX_BYTES] >= ACX00_RX_BYTES_WRAP)
+ return 0;
+
+ /* Include sleep and read time; complete unseen cycles cannot be recovered. */
+ if (priv->stats_valid &&
+ ktime_ms_delta(ktime_get_boottime(), priv->stats_start) >=
+ (s64)ACX00_STATS_MAX_INTERVAL_MS)
+ priv->stats_valid = false;
+
+ for (i = 0; i < ACX00_NUM_STATS; i++) {
+ u64 count = counts[i];
+
+ if (priv->stats_valid) {
+ if (count < priv->stats_last[i])
+ count += i == ACX00_STAT_RX_BYTES ?
+ ACX00_RX_BYTES_WRAP : BIT_ULL(32);
+ priv->stats[i] += count - priv->stats_last[i];
+ }
+ priv->stats_last[i] = counts[i];
+ }
+ priv->stats_valid = true;
+ priv->stats_start = start;
+
+ return 0;
+}
+
+static int acx00_ephy_update_stats(struct phy_device *phydev)
+{
+ struct acx00_ephy_priv *priv = phydev->priv;
+ int ret;
+
+ mutex_lock(&priv->state_lock);
+ ret = __acx00_ephy_update_stats(phydev);
+ mutex_unlock(&priv->state_lock);
+
+ return ret;
+}
+
+static void acx00_ephy_get_phy_stats(struct phy_device *phydev,
+ struct ethtool_eth_phy_stats *eth_stats,
+ struct ethtool_phy_stats *stats)
+{
+ struct acx00_ephy_priv *priv = phydev->priv;
+
+ mutex_lock(&priv->state_lock);
+ stats->rx_packets_total = priv->stats[ACX00_STAT_RX_PACKETS];
+ stats->rx_bytes_total = priv->stats[ACX00_STAT_RX_BYTES];
+ stats->rx_errors = priv->stats[ACX00_STAT_RX_CRC_ERRORS];
+ mutex_unlock(&priv->state_lock);
+}
+
+static void acx00_ephy_snapshot_stats(struct phy_device *phydev)
+{
+ int ret;
+
+ /* Statistics collection must not prevent reset or power management. */
+ ret = __acx00_ephy_update_stats(phydev);
+ if (ret)
+ phydev_dbg(phydev, "failed to snapshot statistics: %pe\n", ERR_PTR(ret));
+}
+
static int acx00_ephy_control_power_on(struct acx00_ephy_priv *priv)
{
lockdep_assert_held(&priv->state_lock);
@@ -122,6 +274,10 @@ static int acx00_ephy_control_power_off(struct acx00_ephy_priv *priv)
{
lockdep_assert_held(&priv->state_lock);
+ /* Even a failed power-off can have reset the hardware counter. */
+ priv->stats_active = false;
+ priv->stats_valid = false;
+
return priv->control->power_off(priv->control);
}
@@ -350,6 +506,10 @@ static int acx00_ephy_soft_reset(struct phy_device *phydev)
mutex_lock(&priv->state_lock);
+ /* phylib may already have reset the counters before calling us. */
+ priv->stats_active = false;
+ priv->stats_valid = false;
+
ret = acx00_ephy_set_interface(phydev);
if (ret)
goto out_power_off;
@@ -475,10 +635,21 @@ static int __acx00_ephy_config_init(struct phy_device *phydev)
/* Restore the standard EEE policy retained by phylib across resets. */
ret = genphy_c45_an_config_eee_aneg(phydev);
- if (ret <= 0)
+ if (ret < 0)
return ret;
+ if (ret > 0) {
+ ret = genphy_restart_aneg(phydev);
+ if (ret)
+ return ret;
+ }
- return genphy_restart_aneg(phydev);
+ if (!priv->stats_active) {
+ /* Establish a new baseline after reset, retaining the software total. */
+ priv->stats_active = true;
+ acx00_ephy_snapshot_stats(phydev);
+ }
+
+ return 0;
}
static int acx00_ephy_config_init(struct phy_device *phydev)
@@ -534,6 +705,9 @@ static int acx00_ephy_resume(struct phy_device *phydev)
int ret;
mutex_lock(&priv->state_lock);
+ /* WoL can keep the PHY counting while phylib polling is stopped. */
+ priv->stats_active = false;
+ priv->stats_valid = false;
ret = __acx00_ephy_power_on_and_resume(phydev);
mutex_unlock(&priv->state_lock);
@@ -550,6 +724,8 @@ static int acx00_ephy_suspend(struct phy_device *phydev)
mutex_lock(&phydev->lock);
mutex_lock(&priv->state_lock);
+ acx00_ephy_snapshot_stats(phydev);
+
ret = genphy_suspend(phydev);
if (ret)
goto out_unlock;
@@ -616,6 +792,8 @@ static struct phy_driver acx00_ephy_driver[] = {
.soft_reset = acx00_ephy_soft_reset,
.config_init = acx00_ephy_config_init,
.disable_autonomous_eee = acx00_ephy_disable_autonomous_eee,
+ .get_phy_stats = acx00_ephy_get_phy_stats,
+ .update_stats = acx00_ephy_update_stats,
.suspend = acx00_ephy_suspend,
.resume = acx00_ephy_resume,
},
--
2.53.0
Re: [PATCH net-next v4 2/3] net: phy: acx00: report receive statistics
Author: netdev-bot+sashiko
Date: Tue, 17 Nov 2026 18:50
Date: Tue, 17 Nov 2026 18:50
138 lines
12991 bytes
12991 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The receive-statistics baseline is invalidated only inside
acx00_ephy_soft_reset() (drivers/net/phy/xpowers/acx00.c), once that…
- [Medium] In __acx00_ephy_update_stats()
(drivers/net/phy/xpowers/acx00.c), the wrap adjustment hard-codes a…
--
net: phy: acx00: report receive statistics
This commit reports the ACx00 PHY receive packet, byte and CRC-error
counters through the generic PHY statistics interface. The free-running
hardware counters are polled through the update_stats callback and added
into 64-bit totals with wrap adjustment. The baseline is invalidated on
soft reset, power-off, resume, and after long polling gaps.
> diff --git a/drivers/net/phy/xpowers/acx00.c b/drivers/net/phy/xpowers/acx00.c
> index cee6c7ff0278e..fb53bac12195c 100644
> --- a/drivers/net/phy/xpowers/acx00.c
> +++ b/drivers/net/phy/xpowers/acx00.c
[ ... ]
> @@ -110,6 +144,124 @@ static int acx00_ephy_write_page(struct phy_device *phydev, int page)
[ ... ]
> +static int __acx00_ephy_update_stats(struct phy_device *phydev)
> +{
[ ... ]
> + /* Skip unusable samples without turning a statistics miss into a link error. */
> + if (ret || counts[ACX00_STAT_RX_BYTES] >= ACX00_RX_BYTES_WRAP)
> + return 0;
> +
> + /* Include sleep and read time; complete unseen cycles cannot be recovered. */
> + if (priv->stats_valid &&
> + ktime_ms_delta(ktime_get_boottime(), priv->stats_start) >=
> + (s64)ACX00_STATS_MAX_INTERVAL_MS)
> + priv->stats_valid = false;
> +
> + for (i = 0; i < ACX00_NUM_STATS; i++) {
> + u64 count = counts[i];
> +
> + if (priv->stats_valid) {
> + if (count < priv->stats_last[i])
> + count += i == ACX00_STAT_RX_BYTES ?
> + ACX00_RX_BYTES_WRAP : BIT_ULL(32);
[Severity: Medium]
Has the 2^32 rollover been measured for the packet (0x1b/0x1c) and
CRC-error (0x1d/0x1e) counters? The commit message gives hardware
measurements for the byte counter's 4,000,000,000 modulus. For the other
two counters it only says:
"The packet and CRC-error counters wrap at 2^32."
All three counters sit next to each other in page 9 and are read the same
way. If the packet or CRC counters wrap at 4e9 like the byte counter, each
wrap adds BIT_ULL(32) instead of 4e9. That would add an extra 294,967,296
to rx_packets_total or rx_errors on every wrap.
Only the byte sample is checked against its modulus above. The packet and
CRC samples are never range-checked, so nothing here would catch a wrong
modulus.
> + priv->stats[i] += count - priv->stats_last[i];
> + }
> + priv->stats_last[i] = counts[i];
> + }
> + priv->stats_valid = true;
> + priv->stats_start = start;
[ ... ]
> @@ -350,6 +506,10 @@ static int acx00_ephy_soft_reset(struct phy_device *phydev)
>
> mutex_lock(&priv->state_lock);
>
> + /* phylib may already have reset the counters before calling us. */
> + priv->stats_active = false;
> + priv->stats_valid = false;
> +
[Severity: Medium]
Can a concurrent poll count a BMCR reset as a counter wrap before this
invalidation runs?
phy_mii_ioctl() in drivers/net/phy/phy.c writes BMCR_RESET before it
reaches the driver's soft_reset:
mdiobus_write(phydev->mdio.bus, prtad, devad, val);
if (prtad == phydev->mdio.addr &&
devad == MII_BMCR &&
val & BMCR_RESET)
return phy_init_hw(phydev);
This path runs under RTNL only. It holds neither phydev->lock nor
state_lock, and it does not stop the state machine.
Meanwhile, _phy_state_machine() in PHY_NOLINK or PHY_RUNNING calls
phy_update_stats()->acx00_ephy_update_stats().
Suppose that poll takes state_lock after the BMCR_RESET write but before
acx00_ephy_soft_reset() takes it. Then stats_active and stats_valid are
still true. __acx00_ephy_update_stats() reads the cleared page-9 counters
and sees count < stats_last[i]. It then adds ACX00_RX_BYTES_WRAP or
BIT_ULL(32) and commits the result to priv->stats[].
Clearing stats_valid here afterwards does not remove that delta.
rx_bytes_total would grow by about 4e9 minus the old byte baseline.
rx_packets_total and rx_errors would each grow by about 2^32 minus their
baselines.
The commit message says the invalidation covers the case where "phylib may
already have reset the PHY through BMCR or its reset signal". Does that
hold if a poll runs between the reset write and soft_reset?
This needs CAP_NET_ADMIN and a narrow timing window. Still, nothing in the
driver orders the reset write against the collector.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113-submit-acx00-phy-stats-v1-v4-0-986b8a4a1871%40gmail.com
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads