ice: E825 timer adjustment fixes
Subsystems (2)
v3 cover ice: E825 timer adjustment fixes
Recently we've had reports of bad timer adjustment on E825. This series fixes the issues by replacing direct timer adjustments with a struct timecounter to maintain the phase in software instead.
To motivate this, the following is an analysis of how the E825 PHY timestamps function.
The ice driver maintains PTP clock time in the MAC with a 96-bit register. Timestamps are captured in the PHY which only stores 64 bits, only including the lower 32-bits of nanoseconds. The driver performs clock adjustments using a hardware timer command mechanism, where the clock adjustment is programmed into shadow registers in both the MAC and PHY, and then a timer command is initiated to synchronously update the timers.
Since the PHY only maintains the lower 32 bits of nanoseconds, adjustments are also limited such that the maximum atomic adjustment is ~2.1 seconds (2^31-1 nanoseconds).
For large offsets, the driver uses a non-atomic flow where the current time is read, an adjustment is applied in software, and then written back. This necessarily loses some elapsed time, which applications must deal with. For particular servo configurations, where the parameters are tuned for a very slow servo, this error can take a long time to finish correction.
On E825, the adjustment in the PHY does not always work reliably. The reason for this is subtle, and is a quirk of how the timestamps are actually generated. The timestamps for Tx and Rx packets come from the PCS timer rather than directly from the PHY timer. The PCS timer is derived from internal PHY signaling.
To keep the PCS timer and PHY timer in sync, every few cycles (testing seems to indicate every 8 cycles) the PHY timer is copied to the PCS timer. On other cycles, the PCS timer is incremented by a value determined from the PCS clock rate.
Crucially, the copy of the PHY timer is skipped if the current value of the PHY timer is lower than the PCS timer (*and* on this cycle the regular PCS clock increment is also skipped).
During normal operation, the PCS timer will tick from its own source for 7 ticks then copy the PCS timer. If the PCS timer is slightly ahead, then PHY timer update is skipped, and the PCS timer skips an update every 8 clock cycles until it is back in alignment with the PHY timer.
Unfortunately, this hardware logic did not take into account manual adjustments to the PHY. If the PHY timer is adjusted backwards, (or if a forward adjustment causes the PHY timer to overflow), the PHY to PCS sync will not occur again until the PCS timer wraps back around to zero. In the worst case, an adjustment could land the PHY timer to just below the PCS timer just after it wraps around. PHY to PCS syncs would stop for ~3.6 seconds.
During this time, the Tx and Rx timestamps will read from the PCS timer. These timestamps will be fed into the timestamp extension algorithm which will produce incoherent results, and data will be fed into the PTP application servo. During the invalid window, faulty timestamps will cause the servo to steer incorrectly, likely away from the true target time. After the PCS timer synchronizes again, the timestamps will return to normal. However, the servo may have steered the clock incorrectly. In default ptp4l configurations, direct clock adjustment is only performed once during startup. This forces the application to use frequency slewing to correct any remaining offset. With slow servo parameters, this can take a *very* long time. In some setups, we had reports of taking several minutes to nearly an hour before stabilizing, and in some configurations the servo was unable to ever recover.
Note that adjustments work properly on the MAC timer, and indeed on the first-layer PHY timer value that the driver can read over the sideband queue. Only the PCS timer (which is inaccessible to the driver) fails to adjust. This made tracking the precise origin of the failure difficult.
Testing shows that pretty much all "negative" adjustments and about half of the positive adjustments can trigger this failure. It is also difficult to see without using extremely slow servo parameters. With a servo tuned for fast reaction, the incorrect timestamps will cause some difficulty in the first adjustment, but the servo will quickly correct and recover within a few seconds at most.
I used the following P/I servo parameters in my testing:
pi_proportional_const 0.60
pi_integral_const 0.001
pi_proportional_scale 0.0
pi_proportional_exponent -0.3
pi_proportional_norm_max 0.7
pi_integral_scale 0.0
pi_integral_exponent 0.4
pi_integral_norm_max 0.3In particular, the pi_integral_const will force the servo to be very slow at reacting.
Even trying to compare the MAC timer with the Tx and Rx timestamps is not trivial. There is no mechanism to synchronously read the MAC timer along with a timestamp. Additionally, timestamp extension makes this even harder to track. The MAC timer is immediately adjusted and the cached PHC time is updated. Normally timestamps from before the adjustment should reasonably convert, however if the adjustment is large enough the timestamp can flip to being detected as "in the past" and in the worst cases the resulting extended timestamps will have a second sudden jump of -4.2 seconds before the PCS timer adjustment is applied and things settle. All of this while being fed into a servo trying to steer the clock can make it difficult to understand what is going on.
With that background out of the way, here's a summary of the patch content:
First, the SO_TX_TIME support in ice_tx_map() is adjusted to compute the timestamp from the full nanoseconds instead of only the sub-second portion of the timestamp.
Next, the PTP callback functions are adjusted to check that the PTP state is ready. This makes them fail early if the driver is resetting or enters an error state. This isn't a complete synchronization fix as it doesn't protect against an already-running command when a reset starts. More work is underway to investigate and determine the best synchronization solution.
Next, the timestamp extension logic is refactored to store the cached PHC time value inside the ice_adapter structure under a new ice_phc structure which will be used to store timecounter data. This change reduces the number of threads that need to perform timestamp extension and makes introduction of timecounter simpler as it gives a natural place for accessing the structure and simplifies modifications to the Rx hotpath in particular. This does further solidify the dependency on the clock owning PF.
Finally, the driver is modified to allow use of a timecounter for representing the time instead of directly adjusting the phase in hardware. Frequency adjustments continue to be handled in hardware. For now, only the E825 and E830 devices are switched. The legacy implementation remains for supporting older installations of E810 and E822 platform devices. We plan to complete migration in the future, but felt that immediate conversion would slow down ability to validate that all older installations will behave correctly. Since the E825 device timer adjustment logic is completely useless, this can speed up ability to adopt the fix. Although E830 does not appear to have the same timer command issue, it was decided to convert both since they are the most recent devices and undergoing the most testing and active development.
Because the timecounter already exists within the kernel, it was deemed the best solution, despite its trade offs required for implementation. I'm open to other alternative fixes if anyone has a better solution. One key thing is that we need the 32-bit cycle counter mask in order to enable timestamp extension, but this means we effectively (mostly) ignore the upper 32-bits of the MAC timer when they could be used for tracking time over the wraparound. Sashiko is not happy about this dependency on requiring the periodic 2 second check on the clock owner, and complains about ways it could break if that stops.
This series fixes a separate issue from [1] and does not *directly* depend on it. The fixes in that series are necessary but not sufficient to completely eliminate PHY timer desync issues on E825.
There are some structural conflicts with the ctrl_pf RCU changes at [2] which I have tried to integrate, so this won't be a straightforward cherry-pick to trees without that series.
One final note, the conversion to timecounter supersedes the changes from [3], and that patch has been removed from more recent versions of that series. The reset partially helped, but was only applied to the non-atomic settime interface. In particular, the settime interface was vulnerable to the same PCS issue, but the PHY soft reset *does* clear both the PHY and PCS timers back to 0. Performing a reset after the adjustment will force both timers back to zero and then perform a forward adjustment that won't trigger the failure condition. However, attempting to add this reset to *every* timer adjustment comes with unnecessary delay since the whole process takes a long time especially with the PHY soft reset. It was ultimately deemed as not a proper fix suitable for deployment.
---
Changes in v3:
- Fix locking for periodic output signal configuration
- Use ktime instead of jiffies for cached PHC time check and staleness
check on Tx timestamps
- Use last cached PHC time and ktime to reset clock after a reset instead
of reading the reset time at the end of ice_ptp_prepare_for_reset().
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Replace the seqlock_t with a seqcount_latch_t and a mutex that protects serialization of the periodic update with the direct updates from the PTP
hardware clock callbacks.
- Replace use of "0" as a canary value of cached_time and apply the
cached_jiffies stale discard check to both Tx and Rx timestamps.
- Fix the rx_hwtstamp trace to record the timecounter values and properly
use the same copied value of the cached_time on non-timecounter devices.
- Fix the SO_TX_TIME support on E830 to account for timecounter phase
offset.
- Modify ice_ptp_read_cycles() to always read and report the full timer
value, enabling proper phase calculation even as the MAC timer rolls
over.
- Add ice_phc_get_phase() to compute the timecounter phase, currently used
only by SO_TX_TIME, but likely necessary for VF Rx timestamping in the
future.
- Disable VF Rx timestamping support for devices using the timecounter.
This will need some investigation and virtchnl development work to enable properly, which I plan on doing in a different series. I don't like
regressing this, but I don't have a simple alternative. I don't believe
that simply accounting for the phase in the GET_TIME would be sufficient,
as I am not certain it produces the correct math in the iavf timestamp
extension algorithm.
- Fix the phase offset calculation in ice_ptp_cfg_perout(), computing the timecounter domain first before adding the 500 msec.
- Separate the E830 ETF fix out to its own patch, as it seems like the
driver is incorrectly programming all ETF timestamps.
- Add a patch to have all the PTP callbacks check for the PTP ready state.
- Target net-next despite being a bug fix since this is unlikely to be
accepted to net due to the scope.
- Link to v1: https://patch.msgid.link/[email protected]
---
Jacob Keller (4):
ice: fix computation of ETF timestamps for E830
ice: exit PTP callbacks early if not ICE_PTP_READY
ice: store cached PHC time in ice_adapter
ice: use struct timecounter for E825 and E830
drivers/net/ethernet/intel/ice/ice_adapter.h | 54 +++ drivers/net/ethernet/intel/ice/ice_ptp.h | 18 +- drivers/net/ethernet/intel/ice/ice_trace.h | 18 +- drivers/net/ethernet/intel/ice/ice_txrx.h | 5 +- drivers/net/ethernet/intel/ice/ice_adapter.c | 51 ++ drivers/net/ethernet/intel/ice/ice_ethtool.c | 1 - drivers/net/ethernet/intel/ice/ice_lib.c | 2 +- drivers/net/ethernet/intel/ice/ice_ptp.c | 615 ++++++++++++++++++------- drivers/net/ethernet/intel/ice/ice_txrx.c | 16 +- drivers/net/ethernet/intel/ice/virt/virtchnl.c | 7 +- 10 files changed, 607 insertions(+), 180 deletions(-)
---
base-commit: 4cd5d9c333fb45a4e70ad6a77c44f13bd7312afb
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/tnguy/next-queue.git dev-queue
change-id: 20261022-jk-e825-e830-timecounter-06c15de02e76
Best regards,
Signature · 1 line
Jacob Keller <[email protected]>
PATCH v3 1/4 ice: fix computation of ETF timestamps for E830
When Earliest TxTime First offload is enabled, the ice_tx_map() function computes the appropriate timestamps for the timestamp ring used to set the transmit times for each packet.
It computes this by converting the skb->tstamp into a timespec and then taking the sub-second portion from tv_nsec and dividing it by the timestamp resolution. This value is then inserted into the timestamp descriptor using ice_build_tstamp_desc().
At first glance this may seem reasonable. After all, the Tx timestamp descriptor field has a mask defined by ICE_TXTIME_STAMP_M which is GENMASK(31,13). This is a 19-bit wide field. With a unit size of 128 nanoseconds, the maximum representable time is only ~67 milliseconds, far below the size of the tv_nsec field (just shy of one second).
The E830 device time is represented as a single 64-bit nanosecond value as seen by ice_ptp_read_src_clk_reg() using a simple rd64 on the GLTSYN_TIME_L register.
According to the data sheet, the hardware uses the following flow to determine when to transmit the next packet in the paced queue:
half_window = window_size / 2
if (ts <= timer) {
if (timer - ts < half_window) {
// timestamp expired
pass
} else {
// wait before sending
stall
}
} else {
// Handle possible rollover case
if (ts - timer < half_window) {
// wait before sending
stall
} else {
// timestamp expired
pass
}
}Here, window_size is the maximum value that fix in the 19 bit sized timestamp field, ts is the next packet timestamp waiting in the queue, and timer is the 19 bits of the GLTSYN_TIME MAC timer. The configured timestamp "resolution" determines at which bit position to capture from the GLTSYN_TIME registers. This logic does not perform any computation to divide out seconds or to handle anything like a timespec. The timestamp and timer values are compared directly with logic to account for handling rollover.
Because of the faulty logic, all of the timestamps computed and placed in the ring will result in the transmit time not matching the desired time per the skb. Consider a timestamp exactly on a second boundary. The computed timestamp will be 0, while the actual value should be (1,000,000,000 / 128) mod 2^19 or 472,468 (0x73594).
Fix this by directly converting the ktime to nanoseconds rather than converting to a timespec64.
--- drivers/net/ethernet/intel/ice/ice_txrx.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/ethernet/intel/ice/ice_txrx.c +4 -5
@@ -1519,10 +1519,9 @@ ice_tx_map(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first,if (ice_is_txtime_cfg (tx_ring)) {struct ice_tstamp_ring *tstamp_ring; -u32 tstamp_count, j;struct ice_ts_desc *ts_desc; -struct timespec64 ts; -u32 tstamp; +u32 tstamp_count, j; +u64 tstamp;smp_rmb ();/* order flag read before pointer read */ tstamp_ring =READ_ONCE (tx_ring->tstamp_ring);
@@ -1532,8 +1531,8 @@ ice_tx_map(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first, tstamp_count = tstamp_ring->count; j = tstamp_ring->next_to_use; - ts =ktime_to_timespec64 (first->skb->tstamp); - tstamp = ts.tv_nsec >>ICE_TXTIME_CTX_RESOLUTION_128NS ; + tstamp =ktime_to_ns (first->skb->tstamp); + tstamp >>=ICE_TXTIME_CTX_RESOLUTION_128NS ; ts_desc =ICE_TS_DESC (tstamp_ring, j); ts_desc->tx_desc_idx_tstamp =ice_build_tstamp_desc (i, tstamp);
Signature · 1 line
2.56.0.215.g47fdf2208c5b
PATCH v3 2/4 ice: exit PTP callbacks early if not ICE_PTP_READY
In reply to Jacob KellerAdd a check for the PTP state at the start of ice_ptp_settime64(), ice_ptp_adjtime(), ice_ptp_adjfine(), and ice_ptp_gpio_enable(). If the driver PTP state is not ICE_PTP_READY, exit with -EIO instead of continuing to try to modify the clock while the driver may still be loading, resetting, unloading, or after a failed rebuild.
The check is currently best effort and doesn't guarantee to prevent a race between the start of a new command and a reset. For that, we will need more time to investigate and develop a proper synchronization mechanism.
The ice_ptp_gpio_enable() still allows requests to disable the pins. This is intentional as it will be called by ptp_clock_unregister() when tearing down. Additionally, the core PTP code often ignores the return value of ptp_disable_pinfunc() anyways. Allowing disables through ensures the internal driver state matches, while new enable requests are blocked until the driver is ready.
--- drivers/net/ethernet/intel/ice/ice_ptp.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/net/ethernet/intel/ice/ice_ptp.c +15 -0
@@ -1607,6 +1607,9 @@ static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)u64 incval;int err; +if (pf->ptp.state !=ICE_PTP_READY ) +return -EIO ; + incval =adjust_by_scaled_ppm (ice_base_incval (pf), scaled_ppm); err =ice_ptp_write_incval_locked (hw, incval);if (err) {
@@ -1997,6 +2000,9 @@ static int ice_ptp_gpio_enable(struct ptp_clock_info *info,struct ptp_perout_request *cached = &pf->ptp.perout_rqs[rq->perout.index]; +if (on && pf->ptp.state !=ICE_PTP_READY ) +return -EIO ; + err =ice_ptp_cfg_perout (pf, &rq->perout, on);if (!err) { *cached = rq->perout;
@@ -2011,6 +2017,9 @@ static int ice_ptp_gpio_enable(struct ptp_clock_info *info,struct ptp_extts_request *cached = &pf->ptp.extts_rqs[rq->extts.index]; +if (on && pf->ptp.state !=ICE_PTP_READY ) +return -EIO ; + err =ice_ptp_cfg_extts (pf, &rq->extts, on);if (!err) *cached = rq->extts;
@@ -2065,6 +2074,9 @@ ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)struct ice_hw *hw = &pf->hw;int err; +if (pf->ptp.state !=ICE_PTP_READY ) +return -EIO ; +/* For Vernier mode on E82X, we need to recalibrate after new settime. * Start with marking timestamps as invalid. */
@@ -2134,6 +2146,9 @@ static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)struct device *dev;int err; +if (pf->ptp.state !=ICE_PTP_READY ) +return -EIO ; + dev =ice_pf_to_dev (pf);/* Hardware only supports atomic adjustments using signed 32-bit
Signature · 1 line
2.56.0.215.g47fdf2208c5b
PATCH v3 3/4 ice: store cached PHC time in ice_adapter
In reply to Jacob KellerThe ice driver uses a cached copy of the PTP Hardware Clock time for its timestamp extension algorithm implemented in ice_ptp_extend_32b_ts(). This algorithm is based on work originally from struct timecounter, and relies on a cached copy of the PTP time used to extend 32-bit snapshots of the PTP time into full 64-bit time values.
The driver currently stores this cache once in the ice_ptp structure for each PF and once per each Rx ring on each PF. Each PF is then responsible for calling ice_ptp_update_cached_phctime() at least once every 2 seconds. This is handled by the per-PF periodic work task that runs on a per-PF kthread. Each Rx ring must be updated, which requires looping the VSI and ring structures, and might fail due to the rings being reconfigured.
In order to fix some device-specific bugs, a following change is going to refactor the driver to support using a timecounter structure to avoid needing to perform direct time adjustments in hardware on E825 and E830.
Use of a timecounter will require the that the Rx path have a reference to that structure. To ensure that all PFs on the same device have the same time domain, the structure must be part of the whole adapter, making ice_adapter the best place to store it. The initial implementation will focus only on migrating the E825 and E830 devices, to minimize impact and potential regression.
Since the Rx path will need to reference a structure anyways, it makes little sense to maintain both a cached copy of the PHC time in addition to a pointer to the adapter.
As a first step, refactor the driver to store the existing cached PHC time in ice_adapter, instead of per-PF in each ice_ptp structure and in the Rx rings.
Create a struct ice_phc structure to store the PTP Hardware Clock fields, and embed it in ice_adapter. Store a pointer to the PHC structure in the Rx rings, replacing the cached_phctime.
Pass a pointer to the PHC structure to ice_ptp_extend_32b_ts() and obtain the cached time from that structure. Modify the Tx and Rx paths to stop using a zero value of cached_time as a canary for the clock owner disappearing. The ice_ptp_read_src_clk_reg() can't fail when called by the clock owner anyways, so this check is not very meaningful when only the clock owner performs the cached update.
Modify the ice_ptp_update_cached_phctime(), removing logic to update the cached time within the Rx rings. Convert it to a void function since it can no longer fail in a meaningful way.
Change ice_ptp_periodic_work() to check ice_pf_src_tmr_owned() and only call ice_ptp_update_cached_phctime() for the clock owner. To ensure a consistent update after the clock adjustments, use a mutex to serialize the calls to ice_ptp_update_cached_phctime(). This lock is acquired *before* the read of the current time. This ensures that even if a periodic update races with a clock adjustment, it will either happen before or will see the clock adjusted after as it updates cached_time. Performing the update only on the clock owner does make the late_cached_phc_updates ethtool stat only apply for the clock owner.
Since the Rx path now has access to the PHC structure, move the timeout check from ice_ptp_extend_40b_ts() into ice_ptp_extend_32b_ts(). This ensures that Rx timestamps also get discarded if the cached PHC update becomes stale.
The ice_ptp_reset_cached_phctime() function previously only applied to the clock owner. This was wrong, as the driver should have been updating all ports cached time copy, and marking each port tracker as stale. Introduce the ice_ptp_mark_all_tx_tracker_stale() and use it here.
When restoring the clock time, the driver currently uses the last cached copy of the PHC time, and adds the time the device took in reset. This is not very precise, since the time delta is calculated from a timestamp captured at the end of ice_ptp_prepare_for_reset(). Instead, replace using jiffies for the stale timestamp check with ktime values, storing the full ktime instead of jiffies in ice_ptp_update_cached_time(). When calculating the elapsed time, use the time since that last update instead. This calculates a more precise time with less error. It also avoids needing to keep an extra copy of the PHC time or reset ktime in the PTP structure. It should also be correct even if the reset has cleared the GLTSYN_TIME values before ice_ptp_prepare_for_reset() is called.
These changes eliminate failure points from the cached PHC time update, reduce needless duplicate work across PFs, and ensure consistency of the cached time update w.r.t clock adjustments. They also pave the way for introduction of a timecounter in a following change.
Careful readers may note that non-owner PFs now completely depend on the clock owner PF being loaded to ensure the cached time is up to date, as they will start discarding timestamps 2 seconds after the last update. However, PTP already has a strong dependency on the clock owner PF. Only the owner initializes the PHC mac timer and exposes the PTP clock device. E822 and E825 devices also depend on the clock owner miscellaneous IRQ for handling all Tx timestamps. PTP also will not behave right under PCIe device passthru unless all PFs are assigned into the same VM, due to depending on coordination via the ice_adapter structure. This is a limitation due to the quirks of the device design.
The Rx path now requires accessing the PHC structure via a dereference and performs the jiffies timeout check which may impact performance. However, this ensures that Rx timestamps do not get extended on stale data and access to the PHC is required for the timecounter refactor anyways.
--- drivers/net/ethernet/intel/ice/ice_adapter.h | 22 +++ drivers/net/ethernet/intel/ice/ice_ptp.h | 6 - drivers/net/ethernet/intel/ice/ice_txrx.h | 4 +- drivers/net/ethernet/intel/ice/ice_adapter.c | 4 + drivers/net/ethernet/intel/ice/ice_ethtool.c | 1 - drivers/net/ethernet/intel/ice/ice_lib.c | 2 +- drivers/net/ethernet/intel/ice/ice_ptp.c | 236 +++++++++++---------------- 7 files changed, 123 insertions(+), 152 deletions(-)
drivers/net/ethernet/intel/ice/ice_adapter.h +22 -0
@@ -35,6 +35,25 @@ struct ice_port_list {struct srcu_struct srcu; }; +/** +* struct ice_phc - PTP Hardware Clock fields +* @cached_time: Cached copy of the PHC time for timestamp extension +* @update_time: Monotonic time when PHC was laste updated +* @lock: For protecting updates to the PHC fields. +* +* Readers are lockless and rely on READ_ONCE/WRITE_ONCE for the cached_time +* and smp_acquire/smp_release for the update_time field. +* +* Writers must acquire the lock mutex to ensure that the periodic task cannot +* write back an out-of-date cached value after a clock adjustment. +*/ +struct ice_phc { +u64 cached_time; +ktime_t update_time; +/* Serialize updates to cached_time */ +struct mutex lock; +}; +/** * struct ice_adapter - PCI adapter resources shared across PFs * @refcount: Reference count. struct ice_pf objects hold the references.
@@ -46,6 +65,7 @@ struct ice_port_list {* @cgu_synce_lock: Mutex serializing changes of the CGU registers R10 and R11, * which all PFs reach. Used on E825C devices. * @ps_lock: Mutex to serialize PHY port start/stop across adapter. +* @phc: PTP Hardware Clock fields * @ctrl_pf_lock: Protect control PF lifetime for sleepable users * @ctrl_pf: Control PF of the adapter * @rebuild_lock: serialize PFR recovery across PFs of the same adapter
@@ -73,6 +93,8 @@ struct ice_adapter {/* For serializing PHY port start/stop sequences */ struct mutex ps_lock; +struct ice_phc phc; +struct rw_semaphore ctrl_pf_lock;struct ice_pf __rcu *ctrl_pf;struct ice_port_list ports;
drivers/net/ethernet/intel/ice/ice_ptp.h +0 -6
@@ -235,8 +235,6 @@ struct ice_ptp_pin_desc {* @tx_interrupt_mode: the TX interrupt mode for the PTP clock * @port: data for the PHY port initialization procedure * @work: delayed work function for periodic tasks -* @cached_phc_time: a cached copy of the PHC time for timestamp extension -* @cached_phc_jiffies: jiffies when cached_phc_time was last updated * @kworker: kwork thread for handling periodic work * @ext_ts_irq: the external timestamp IRQ in use * @pin_desc: structure defining pins
@@ -247,7 +245,6 @@ struct ice_ptp_pin_desc {* @clock: pointer to registered PTP clock device * @tstamp_config: hardware timestamping configuration * @tx_refclks: bitmaps table to store the information about TX reference clocks -* @reset_time: kernel time after clock stop on reset * @tx_hwtstamp_good: number of completed Tx timestamp requests * @tx_hwtstamp_skipped: number of Tx time stamp requests skipped * @tx_hwtstamp_timeouts: number of Tx skbs discarded with no time stamp
@@ -270,8 +267,6 @@ struct ice_ptp {enum ice_ptp_tx_interrupt tx_interrupt_mode;struct ice_ptp_port port;struct kthread_delayed_work work; -u64 cached_phc_time; -unsigned long cached_phc_jiffies;struct kthread_worker *kworker;u8 ext_ts_irq;struct ptp_pin_desc pin_desc[ICE_N_PINS_MAX ];
@@ -282,7 +277,6 @@ struct ice_ptp {struct ptp_clock *clock;struct kernel_hwtstamp_config tstamp_config;unsigned long tx_refclks[ICE_E825_MAX_PHYS ][ICE_REF_CLK_MAX ]; -u64 reset_time;u64 tx_hwtstamp_good;u32 tx_hwtstamp_skipped;u32 tx_hwtstamp_timeouts;
drivers/net/ethernet/intel/ice/ice_txrx.h +2 -2
@@ -221,7 +221,7 @@ enum ice_tx_ring_flags { };struct ice_pkt_ctx { -u64 cached_phctime; +struct ice_phc *phc;__be16 vlan_proto; };
@@ -321,7 +321,7 @@ struct ice_rx_ring {union {struct ice_pkt_ctx pkt_ctx;struct { -u64 cached_phctime; +struct ice_phc *phc;__be16 vlan_proto; }; };
drivers/net/ethernet/intel/ice/ice_adapter.c +4 -0
@@ -83,6 +83,8 @@ static struct ice_adapter *ice_adapter_new(struct pci_dev *pdev)mutex_init (&adapter->ps_lock); +mutex_init (&adapter->phc.lock); +return adapter; }
@@ -98,6 +100,8 @@ static void ice_adapter_free(struct ice_adapter *adapter)cleanup_srcu_struct (&adapter->ports.srcu);mutex_destroy (&adapter->ps_lock); +mutex_destroy (&adapter->phc.lock); +kfree (adapter); }
drivers/net/ethernet/intel/ice/ice_ethtool.c +0 -1
@@ -3338,7 +3338,6 @@ ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,/* clone ring and setup updated count */ rx_rings[i] = *vsi->rx_rings[i]; rx_rings[i].count = new_rx_cnt; - rx_rings[i].cached_phctime = pf->ptp.cached_phc_time; rx_rings[i].desc =NULL ; rx_rings[i].xdp_buf =NULL ; rx_rings[i].xdp_rxq = (struct xdp_rxq_info ){ };
drivers/net/ethernet/intel/ice/ice_lib.c +1 -1
@@ -1527,7 +1527,7 @@ static int ice_vsi_alloc_rings(struct ice_vsi *vsi) ring->vsi = vsi; ring->netdev = vsi->netdev; ring->count = vsi->num_rx_desc; - ring->cached_phctime = pf->ptp.cached_phc_time; + ring->phc = &pf->adapter->phc;if (ice_is_feature_supported (pf,ICE_F_GCS )) ring->flags |=ICE_RX_FLAGS_RING_GCS ;
drivers/net/ethernet/intel/ice/ice_ptp.c +94 -142
371 lines not on this page · raw message
Signature · 1 line
2.56.0.215.g47fdf2208c5b
[email protected]
AI reviewRe: [PATCH iwl-next v3 3/4] ice: store cached PHC time in ice_adapter
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] ice: 32-bit build failure in ice_ptp_extend_32b_ts() on 64-bit update_time --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] ice: 32-bit build failure in ice_ptp_extend_32b_ts() on 64-bit update_time
--
commit a94f5b039e979b49b6aea8ea8e4275f20187401e
Author: Jacob Keller <[email protected]>
ice: store cached PHC time in ice_adapter
This commit moves the cached PHC time and its update timestamp from per-PF and per-Rx-ring fields into a shared struct ice_phc embedded in struct ice_adapter, updated by the clock-owning PF under a mutex and read locklessly during timestamp extension.
Quoted 26 lines
drivers/net/ethernet/intel/ice/ice_adapter.h +18
-0@@ -35,6 +35,25 @@ struct ice_port_list {struct srcu_struct srcu; }; +/** +* struct ice_phc - PTP Hardware Clock fields +* @cached_time: Cached copy of the PHC time for timestamp extension +* @update_time: Monotonic time when PHC was laste updated +* @lock: For protecting updates to the PHC fields. +* +* Readers are lockless and rely on READ_ONCE/WRITE_ONCE for the cached_time +* and smp_acquire/smp_release for the update_time field. +* +* Writers must acquire the lock mutex to ensure that the periodic task cannot +* write back an out-of-date cached value after a clock adjustment. +*/ +struct ice_phc { +u64 cached_time; +ktime_t update_time; +/* Serialize updates to cached_time */ +struct mutex lock; +};
[ ... ]
Quoted 21 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +6
-3@@ -309,13 +310,26 @@ int ice_ptp_read_src_clk_reg(struct ice_pf *pf,* This is prevented by (a) periodically updating the cached PHC time once * a second, and (b) discarding any Tx timestamp packet if it has waited for * a timestamp for more than one second. +* +* Return: the extended timestamp value, or 0 if phc->update_time is stale. */ -static u64 ice_ptp_extend_32b_ts (u64 cached_phc_time,u32 in_tstamp) +static u64 ice_ptp_extend_32b_ts (struct ice_phc *phc, +u32 in_tstamp,bool trace_rx) { +u64 ns, cached_phc_time;u32 delta, phc_time_lo; -u64 ns; +ktime_t update_time; -/* Extract the lower 32 bits of the PHC time */
drivers/net/ethernet/intel/ice/ice_ptp.c int ice_ptp_read_src_clk_reg(struct ice_pf *pf, · line 332
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +10
-0+/* Discard the timestamp if update_time hasn't been initialized */ + update_time =smp_load_acquire (&phc->update_time); +if (!update_time) +return 0 ; + +/* Discard the hardware timestamp if the cached PHC time is too old */ +if (ktime_ms_delta (ktime_get_coarse (), update_time) >2000 ) +return 0 ; + + cached_phc_time =READ_ONCE (phc->cached_time);
[Severity: High]
Does calling smp_load_acquire() and smp_store_release() (in ice_ptp_update_cached_phctime()) on phc->update_time break 32-bit builds?
ktime_t is a 64-bit s64 on all architectures, whereas smp_load_acquire() and smp_store_release() enforce compiletime_assert_atomic_type(), which requires a native word size up to sizeof(long). On 32-bit builds with CONFIG_ICE and CONFIG_PTP_1588_CLOCK enabled, compilation fails with "Need native word sized stores/loads for atomicity".
In addition, lockless READ_ONCE() and WRITE_ONCE() accesses on 64-bit phc->cached_time and phc->update_time are not atomic on 32-bit architectures and can tear when ice_ptp_extend_32b_ts() runs concurrently with ice_ptp_update_cached_phctime(). The follow-up commit "ice: use struct timecounter for E825 and E830" keeps these smp_load_acquire() and smp_store_release() calls on phc->update_time. Protecting the fields with a seqcount_t or u64_stats_sync (or storing a word-sized jiffies timestamp when a 64-bit read isn't needed) would avoid both the build failure and 64-bit tearing.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH iwl-next v3 3/4] ice: store cached PHC time in ice_adapter
Quoted 87 lines · On 11/17/2026 4:44 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] ice: 32-bit build failure inice_ptp_extend_32b_ts()on 64-bitupdate_time
--commit
a94f5b039e979b49b6aea8ea8e4275f20187401e
Author: Jacob Keller <[email protected]>ice: store cached PHC time in
ice_adapterThis commit moves the cached PHC time and its update timestamp from per-PF and per-Rx-ring fields into a shared struct
ice_phcembedded in structice_adapter, updated by the clock-owning PF under a mutex and read locklessly during timestamp extension.Quoted 26 lines
drivers/net/ethernet/intel/ice/ice_adapter.h +18
-0@@ -35,6 +35,25 @@ struct ice_port_list {struct srcu_struct srcu; }; +/** +* struct ice_phc - PTP Hardware Clock fields +* @cached_time: Cached copy of the PHC time for timestamp extension +* @update_time: Monotonic time when PHC was laste updated +* @lock: For protecting updates to the PHC fields. +* +* Readers are lockless and rely on READ_ONCE/WRITE_ONCE for the cached_time +* and smp_acquire/smp_release for the update_time field. +* +* Writers must acquire the lock mutex to ensure that the periodic task cannot +* write back an out-of-date cached value after a clock adjustment. +*/ +struct ice_phc { +u64 cached_time; +ktime_t update_time; +/* Serialize updates to cached_time */ +struct mutex lock; +};[ ... ]
Quoted 21 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +6
-3@@ -309,13 +310,26 @@ int ice_ptp_read_src_clk_reg(struct ice_pf *pf,* This is prevented by (a) periodically updating the cached PHC time once * a second, and (b) discarding any Tx timestamp packet if it has waited for * a timestamp for more than one second. +* +* Return: the extended timestamp value, or 0 if phc->update_time is stale. */ -static u64 ice_ptp_extend_32b_ts (u64 cached_phc_time,u32 in_tstamp) +static u64 ice_ptp_extend_32b_ts (struct ice_phc *phc, +u32 in_tstamp,bool trace_rx) { +u64 ns, cached_phc_time;u32 delta, phc_time_lo; -u64 ns; +ktime_t update_time; -/* Extract the lower 32 bits of the PHC time */ drivers/net/ethernet/intel/ice/ice_ptp.c int ice_ptp_read_src_clk_reg(struct ice_pf *pf, · line 332
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +10
-0+/* Discard the timestamp if update_time hasn't been initialized */ + update_time =smp_load_acquire (&phc->update_time); +if (!update_time) +return 0 ; + +/* Discard the hardware timestamp if the cached PHC time is too old */ +if (ktime_ms_delta (ktime_get_coarse (), update_time) >2000 ) +return 0 ; + + cached_phc_time =READ_ONCE (phc->cached_time);[Severity: High]
Does callingsmp_load_acquire()andsmp_store_release()(inice_ptp_update_cached_phctime()) onphc->update_timebreak 32-bit builds?
ktime_tis a 64-bit s64 on all architectures, whereassmp_load_acquire()andsmp_store_release()enforcecompiletime_assert_atomic_type(), which requires a native word size up to sizeof(long). On 32-bit builds withCONFIG_ICEandCONFIG_PTP_1588_CLOCKenabled, compilation fails with "Need native word sized stores/loads for atomicity".
Quoted 10 lines
In addition, lockless
READ_ONCE()andWRITE_ONCE()accesses on 64-bitphc->cached_timeandphc->update_timeare not atomic on 32-bit architectures and can tear whenice_ptp_extend_32b_ts()runs concurrently withice_ptp_update_cached_phctime(). The follow-up commit "ice: use struct timecounter for E825 and E830" keeps thesesmp_load_acquire()andsmp_store_release()calls onphc->update_time. Protecting the fields with aseqcount_toru64_stats_sync(or storing a word-sized jiffies timestamp when a 64-bit read isn't needed) would avoid both the build failure and 64-bit tearing.
I'll just switch both paths over to using the seqcount_latch_t in v4. I think this will address both sets of complaints from this nad patch 4/4.
PATCH v3 4/4 ice: use struct timecounter for E825 and E830
In reply to Jacob KellerThe ice driver maintains PTP clock time in the MAC with a 96-bit register. Timestamps are captured in the PHY which only stores 64 bits, only including the lower 32-bits of nanoseconds. The driver performs clock adjustments using a hardware timer command mechanism, where the clock adjustment is programmed into shadow registers in both the MAC and PHY, and then a timer command is initiated to synchronously update the timers.
Since the PHY only maintains the lower 32 bits of nanoseconds, adjustments are also limited such that the maximum atomic adjustment is ~2.1 seconds (2^31-1 nanoseconds).
For large offsets, the driver uses a non-atomic flow where the current time is read, an adjustment is applied in software, and then written back. This necessarily loses some elapsed time, which applications must deal with. For particular servo configurations, where the parameters are tuned for a very slow servo, this error can take a long time to finish correction.
Worst, on E825 in specific, the adjustment in the PHY does not always work reliably. The reason for this is subtle, and is a quirk of how the timestamps are actually generated. The timestamps for Tx and Rx packets come from the PCS timer rather than directly from the PHY timer. The PCS timer is derived from internal PHY signaling.
To keep the PCS timer and PHY timer in sync, every few cycles (testing seems to indicate every 8 cycles) the PHY timer is copied to the PCS timer. On other cycles, the PCS timer is incremented by a value determined from the PCS clock rate.
Crucially, the copy of the PHY timer is skipped if the current value of the PHY timer is lower than the PCS timer (*and* on this cycle the regular PCS clock increment is also skipped).
During normal operation, the PCS timer will tick from its own source for 7 ticks then copy the PCS timer. If the PCS timer is slightly ahead, then PHY timer update is skipped, and the PCS timer skips an update every 8 clock cycles until it is back in alignment with the PHY timer.
Unfortunately, this hardware logic did not take into account manual adjustments to the PHY. If the PHY timer is adjusted backwards, (or if a forward adjustment causes the PHY timer to overflow), the PHY to PCS sync will not occur again until the PCS timer wraps back around to zero. In the worst case, an adjustment could land the PHY timer to just below the PCS timer just after it wraps around. PHY to PCS syncs would stop for ~3.6 seconds.
During this time, the Tx and Rx timestamps will read from the PCS timer. These timestamps will be fed into the timestamp extension algorithm which will produce incoherent results, and data will be fed into the PTP application servo. During the invalid window, faulty timestamps will cause the servo to steer incorrectly, likely away from the true target time. After the PCS timer synchronizes again, the timestamps will return to normal. However, the servo may have steered the clock incorrectly. In default ptp4l configurations, direct clock adjustment is only performed once during startup. This forces the application to use frequency slewing to correct any remaining offset. With slow servo parameters, this can take a *very* long time. In some setups, we had reports of taking several minutes to nearly an hour before stabilizing, and in some configurations the servo was unable to ever recover. Note that this issue with the PCS synchronization happens regardless of *how* the PHY timer is set, either with the ICE_PTP_ADJ_TIME or ICE_PTP_INIT_TIME.
Note that adjustments work properly on the MAC timer, and indeed on the first-layer PHY timer value that the driver can read over the sideband queue. Only the PCS timer (which is inaccessible to the driver) fails to adjust. This made tracking the precise origin of the failure difficult.
To handle this, the most robust solution is to simply not adjust the hardware clock directly. The kernel already has struct timecounter and struct cyclecounter for representing hardware counters that cannot handle 64-bits of precision. Indeed, the PHY timer already is only 32 bits, and ice has used its own custom logic in ice_ptp_extend_32b_ts to extend timestamps without using a timecounter.
This makes switching to a timecounter fairly straight forward in most cases. However, such a switch covers many product families and devices. To limit the initial scope, implement the timecounter only for the newer E825 and E830 devices. Older E810 and E822 platform devices will currently remain using the legacy approach which has been in use for years. We may opt to convert all devices in the future, but for now limiting the scope keeps from potentially regressing legacy deployments.
The timecounter and cyclecounter are embedded within the recently introduced ice_phc structure. They are initialized as part of ice_adapter. Locking and concurrency requirements for access are a bit awkward. The timestamps are extended from multiple places including the Rx hotpath, the Tx IRQ function on E830, and the Tx IRQ thread function on E825. The timecounter is updated as part of .adjtime and .settime64 callbacks, as well as periodically from the clock owner kworker. The updates are expected to be rare relative to reads, and we want to avoidt a global lock on the readers.
Original versions of this used a seqlock_t which allows multiple readers, but unfortunately had issues because the updates necessarily call the read() function of the cyclecounter, which acquires a spinlock to access the time registers. On RT kernels, the spinlock can sleep which violates the requirements of the seqlock_t (according to Sashiko AI reports).
Instead, use a seqcount_latch_t along with two copies of the timecounter structure in the ice_adapter. The sequence counter allows readers to retry until they get a consistent snapshot. The seqcount_latch_t directs the reader to the appropriate copy, to allow readers to fully preempt the writer. The timecounters are fully initialized as part of ice_adapter and all updates only ever modify their cycle_last and nsec fields.
Writers will first perform the update on the first timecounter, then latch and finish by copying the values from the first counter to the second. Readers that preempt the writer will be directed to the non-modified copy and see a clean status. If the writer preempts the reader, then the sequence counter is invalid and the reader will re-try.
The reader logic is embedded in ice_phc_cyc2time which wraps the timecounter_cyc2time and handles the seqcount_latch_t. Similarly, writer logic is embedded within ice_phc_refresh(), ice_phc_init(), and ice_phc_adjtime().
In most places, the timecounter is a drop-in replacement for existing logic, including using ice_phc_init instead of ice_ptp_write_init and ice_phc_adjtime instead of ice_ptp_write_adj.
The ice_ptp_update_cached_phctime will conditionally handle whether to update the cached_time value, or perform a timecounter_read(). To minimize the risk of contention, this is the only place which actually performs a timecounter_read(). In this way, only the periodic thread and the actual modifications through reset, .settime64 and .adjtime need the writer lock.
All other accesses to time values use ice_phc_cyc2time when phc->use_timecounter is enabled, ensuring that the timecounter phase offset is handled, including external timestamp events, gettime, cross timestamping, and periodic output signals.
The SO_TX_TIME needs to compensate for the timecounter phase and uses ice_phc_get_phase() which computes the difference between the cycle_last and nsec fields. Note that the cyclecounter mask is 32bits to allow extending the shorter PHY timestamps. However, the cyclecounter still reads and reports the "full" 64 bit value from the MAC. This is important since phase adjustments need to convert from the timecounter to the MAC timer which keeps counting the higher bits after a rollover.
Periodic output signal programming is a slightly tricky conversion:
The ice_ptp_cfg_perout() function programs the SDP output signals. For timecounter, the clk time is adjusted using ice_phc_cyc2time to convert to the timecounter domain. Then, the timecounter phase is subtracted from the requested start time. This works because the MAC timer still maintains the full 96 bits, so even if it overflows, we can still reliably program start times more than 4 seconds in the future.
Finally, VF PTP features are disabled for the devices using the timecounter. To support this feature, the virtchnl interface must be extended to handle the timecounter phase appropriately which will require its own development work including modification to the VF driver. This will be investigated and planned in the future.
Careful readers may note that the driver still performs ICE_PTP_ADJ_TIME on the PHY timers after a device reset and when the PHY needs to be reinitialized at link up. The PHY must be adjusted to align with the MAC time in order for timestamps to be accurate. These adjustments are safe because the PHY restart procedure performs a PHY soft reset which forces the PHY to reinitialize to zero, and then be forward corrected as part of the ice_sync_phy_timer_eth56g() procedure. The soft reset was introduced as part of commit 24e335fff462 ("ice: E825: perform a soft reset when starting the PHY timer").
Although the particular hardware issue is only known to affect E825, this timecounter conversion is applied to both E825 and E830 family. These two products are the most recent devices and consistency between their behavior is desired. Older products could benefit from the timecounter (as arbitrarily large offsets may be programmed atomically). However, at this time it was deemed safest to initially implement timecounter only for the new devices, leaving legacy implementation alone. After some time is given to prove out the implementation and ensure all remaining gaps are closed, we may opt to convert all devices and drop the legacy code entirely.
--- drivers/net/ethernet/intel/ice/ice_adapter.h | 40 ++- drivers/net/ethernet/intel/ice/ice_ptp.h | 12 + drivers/net/ethernet/intel/ice/ice_trace.h | 18 +- drivers/net/ethernet/intel/ice/ice_txrx.h | 1 + drivers/net/ethernet/intel/ice/ice_adapter.c | 47 +++ drivers/net/ethernet/intel/ice/ice_ptp.c | 390 +++++++++++++++++++++++-- drivers/net/ethernet/intel/ice/ice_txrx.c | 7 + drivers/net/ethernet/intel/ice/virt/virtchnl.c | 7 +- 8 files changed, 482 insertions(+), 40 deletions(-)
drivers/net/ethernet/intel/ice/ice_adapter.h +36 -4
@@ -8,6 +8,8 @@#include <linux/mutex.h> #include <linux/rwsem.h> #include <linux/srcu.h> +#include <linux/seqlock.h> +#include <linux/timecounter.h> #include <linux/spinlock_types.h> #include <linux/refcount_types.h>
@@ -37,21 +39,51 @@ struct ice_port_list {/** * struct ice_phc - PTP Hardware Clock fields +* +* Structure used for handling the timestamp extension logic for this device. +* Devices will either use the legacy timestamp extension logic or +* a timecounter, not both. +* +* For simplicity, the timecounter fields are initialized as part of creating +* the ice_adapter regardless of whether the device will actually use them. +* +* Legacy timestamp extension fields: * @cached_time: Cached copy of the PHC time for timestamp extension +* +* struct timecounter fields: +* @tc: timecounter representing the current time. Accessed via seq to allow +* lockless readers during updates. +* @cc: cyclecounter underlying the timecounter +* @seq: seqcount_latch_t used to protect readers and direct to the +* appropriate copy during updates. +* +* General fields: * @update_time: Monotonic time when PHC was laste updated * @lock: For protecting updates to the PHC fields. +* @use_timecounter: if true, the device uses the timecounter * -* Readers are lockless and rely on READ_ONCE/WRITE_ONCE for the cached_time -* and smp_acquire/smp_release for the update_time field. +* Readers are lockless. Devices without a timecounter rely on +* READ_ONCE/WRITE_ONCE of the cached_time for consistency. For devices with +* a timecounter, readers rely on the latched sequence counter to be directed +* to a copy of the timecounter not being modified. Both styles depend on +* smp_load_acquire/smp_store_release for checking update_time. * * Writers must acquire the lock mutex to ensure that the periodic task cannot -* write back an out-of-date cached value after a clock adjustment. +* race with clock adjustment callbacks. */ struct ice_phc { +/* Legacy timestamp extension fields */ u64 cached_time; + +/* struct timecounter fields */ +struct timecounter tc[2 ]; +struct cyclecounter cc; +seqcount_latch_t seq; +ktime_t update_time; -/* Serialize updates to cached_time */ +/* Serialize updates to cached_time or the timecounter */ struct mutex lock; +bool use_timecounter; };/**
drivers/net/ethernet/intel/ice/ice_ptp.h +12 -0
@@ -326,6 +326,8 @@ irqreturn_t ice_ptp_ts_irq(struct ice_pf *pf);bool ice_ptp_tx_tstamps_pending (struct ice_pf *pf,bool in_irq);int ice_ptp_read_src_clk_reg (struct ice_pf *pf,struct ptp_system_timestamp *sts,u64 *time); +u64 ice_ptp_read_cycles (struct cyclecounter *cc); +s64 ice_phc_get_phase (struct ice_phc *phc);u64 ice_ptp_get_rx_hwts (const union ice_32b_rx_flex_desc *rx_desc,const struct ice_pkt_ctx *pkt_ctx);
@@ -384,6 +386,16 @@ static inline int ice_ptp_read_src_clk_reg(struct ice_pf *pf,return -EOPNOTSUPP ; } +static inline u64 ice_ptp_read_cycles (struct cyclecounter *cc) +{ +return 0 ; +} + +static inline s64 ice_phc_get_phase (struct ice_phc *phc) +{ +return 0 ; +} +static inline u64 ice_ptp_get_rx_hwts (const union ice_32b_rx_flex_desc *rx_desc,const struct ice_pkt_ctx *pkt_ctx)
drivers/net/ethernet/intel/ice/ice_trace.h +13 -5
@@ -263,19 +263,27 @@ DEFINE_TX_TSTAMP_OP_EVENT(ice_tx_tstamp_complete);/* Trace event for Rx timestamp */ TRACE_EVENT (ice_rx_hwtstamp, - TP_PROTO(u64 cached_time, u32 ts_high, u64 ts_ns), + TP_PROTO(int timecounter, u64 cycle_last, u64 nsec, + u64 cached_time, u32 ts_high, u64 ts_ns), -TP_ARGS (cached_time, ts_high, ts_ns), +TP_ARGS (timecounter, cycle_last, nsec, cached_time, ts_high, ts_ns), - TP_STRUCT__entry(__field(u64, cached_time) + TP_STRUCT__entry(__field(int, timecounter) + __field(u64, cycle_last) + __field(u64, nsec) + __field(u64, cached_time) __field(u32, ts_high)__field (u64, ts_ns)), - TP_fast_assign(__entry->cached_time = cached_time; + TP_fast_assign(__entry->timecounter = timecounter; + __entry->cycle_last = cycle_last; + __entry->nsec = nsec; + __entry->cached_time = cached_time; __entry->ts_high = ts_high; __entry->ts_ns = ts_ns;), -TP_printk ("cached_time=%llu ts_high=%u ts_ns=%llu" , +TP_printk ("timecounter=%d cycle_last=%llu nsec=%llu cached_time=%llu ts_high=%u ts_ns=%llu" , + __entry->timecounter, __entry->cycle_last, __entry->nsec, __entry->cached_time, __entry->ts_high, __entry->ts_ns) );
drivers/net/ethernet/intel/ice/ice_txrx.h +1 -0
@@ -266,6 +266,7 @@ enum ice_dynamic_itr {/* descriptor ring, associated with a VSI */ struct ice_tstamp_ring {struct ice_tx_ring *tx_ring;/* Backreference to associated Tx ring */ +struct ice_phc *phc;dma_addr_t dma;/* physical address of ring */ struct rcu_head rcu;/* to avoid race on free */ u8 __iomem *tail;
drivers/net/ethernet/intel/ice/ice_adapter.c +47 -0
67 lines not on this page · raw message
drivers/net/ethernet/intel/ice/ice_ptp.c +360 -30
551 lines not on this page · raw message
drivers/net/ethernet/intel/ice/ice_txrx.c +7 -0
@@ -395,6 +395,7 @@ static bool ice_clean_tx_irq(struct ice_tx_ring *tx_ring, int napi_budget)*/ static int ice_alloc_tstamp_ring (struct ice_tx_ring *tx_ring) { +struct ice_pf *pf = tx_ring->vsi->back;struct ice_tstamp_ring *tstamp_ring;/* allocate with kzalloc(), free with kfree_rcu() */
@@ -403,6 +404,7 @@ static int ice_alloc_tstamp_ring(struct ice_tx_ring *tx_ring)return -ENOMEM ; tstamp_ring->tx_ring = tx_ring; + tstamp_ring->phc = &pf->adapter->phc; tx_ring->tstamp_ring = tstamp_ring; tstamp_ring->desc =NULL ; tstamp_ring->count =ice_calc_ts_ring_count (tx_ring);
@@ -1520,6 +1522,7 @@ ice_tx_map(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first,if (ice_is_txtime_cfg (tx_ring)) {struct ice_tstamp_ring *tstamp_ring;struct ice_ts_desc *ts_desc; +struct ice_phc *phc;u32 tstamp_count, j;u64 tstamp;
@@ -1528,10 +1531,14 @@ ice_tx_map(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first,if (unlikely (!tstamp_ring)) goto ring_kick; + phc = tstamp_ring->phc; + tstamp_count = tstamp_ring->count; j = tstamp_ring->next_to_use; tstamp =ktime_to_ns (first->skb->tstamp); +if (phc->use_timecounter) + tstamp -=ice_phc_get_phase (phc); tstamp >>=ICE_TXTIME_CTX_RESOLUTION_128NS ; ts_desc =ICE_TS_DESC (tstamp_ring, j);
drivers/net/ethernet/intel/ice/virt/virtchnl.c +6 -1
@@ -326,7 +326,12 @@ static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)if (vf->driver_caps &VIRTCHNL_VF_OFFLOAD_QOS ) vfres->vf_cap_flags |=VIRTCHNL_VF_OFFLOAD_QOS ; -if (vf->driver_caps &VIRTCHNL_VF_CAP_PTP ) +/* Do not enable VF PTP capability on devices which use a timecounter. +* Proper support for these devices requires extensions to the +* virtchnl interface that have not yet been developed. +*/ +if (vf->driver_caps &VIRTCHNL_VF_CAP_PTP && + !vf->pf->adapter->phc.use_timecounter) vfres->vf_cap_flags |=VIRTCHNL_VF_CAP_PTP ; vfres->num_vsis =1 ;
Signature · 1 line
2.56.0.215.g47fdf2208c5b
[email protected]
AI reviewRe: [PATCH iwl-next v3 4/4] ice: use struct timecounter for E825 and E830
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ice: unreleased phc->lock on error path in ice_ptp_rebuild_owner() - [Medium] ice: premature…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ice: unreleased phc->lock on error path in ice_ptp_rebuild_owner()
- [Medium] ice: premature phc->update_time store in ice_ptp_read_cycles()
--
commit b8fa2e5442cd684f10151ff2ab484f07d9521f1c
Author: Jacob Keller <[email protected]>
ice: use struct timecounter for E825 and E830
This commit switches E825 and E830 devices to use a seqcount_latch-protected timecounter and cyclecounter in struct ice_phc instead of directly adjusting the hardware PHC timers and using the legacy timestamp extension logic.
Quoted 38 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +30
-0@@ -272,6 +272,202 @@ int ice_ptp_read_src_clk_reg(struct ice_pf *pf,return 0 ; } +/** +* ice_ptp_read_cycles - cyclecounter read() callback function +* @cc: the hardware cycle counter structure +* +* Read the PHC time via ice_ptp_read_src_clk_reg() and report it for use +* within timecounter_read() and timecounter_init(). +* +* Although the cycle counter is masked to 32-bits for timestamp extension, +* this still reports the full 64-bit time to enable determining the phase +* difference between the PHC hardware value and the timecounter properly. +* +* Return: The current PHC timer value from the hardware on success. +* Since failure cannot be meaningfully reported, return the previous +* cycle_last value when unable to access the device. +*/ +u64 ice_ptp_read_cycles (struct cyclecounter *cc) +{ +struct ice_adapter *adapter; +struct ice_phc *phc; +struct ice_pf *pf; +u64 time; +int err; + + phc = container_of(cc, struct ice_phc, cc); + adapter = container_of(phc, struct ice_adapter, phc); + +scoped_guard (rcu) { + pf =rcu_dereference (adapter->ctrl_pf); +if (!pf) +return phc->tc[0 ].cycle_last;
drivers/net/ethernet/intel/ice/ice_ptp.c int ice_ptp_read_src_clk_reg(struct ice_pf *pf, · line 314
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +10
-0+ + err =ice_ptp_read_src_clk_reg (pf,NULL , &time); +if (err) +return phc->tc[0 ].cycle_last; + +/* Paired with smp_load_acquire in ice_ptp_extend_32b_ts */ +smp_store_release (&phc->update_time,ktime_get ()); +return time; + } +}
[Severity: Medium]
Can publishing phc->update_time inside ice_ptp_read_cycles() allow ice_ptp_extend_32b_ts() to extend a timestamp against an uninitialized or stale phc->tc[1]?
In ice_phc_init() and ice_phc_refresh(), write_seqcount_latch_begin() sets phc->seq to an odd value (directing readers to phc->tc[1]) before calling timecounter_init() or timecounter_read() on phc->tc[0]:
CPU0 CPU1ice_phc_init()
write_seqcount_latch_begin(&phc->seq)
timecounter_init(&phc->tc[0], ...)
ice_ptp_read_cycles()
smp_store_release(&phc->update_time, ...)
ice_ptp_extend_32b_ts()
smp_load_acquire(&phc->update_time)
ice_phc_cyc2time_with_trace()
seq = read_seqcount_latch(&phc->seq)
/* idx == 1 */
timecounter_cyc2time(&phc->tc[1], ...)
<-- reads uninitialized/stale tc[1]
phc->tc[0].cycle_last = ...
phc->tc[0].nsec = ... write_seqcount_latch(&phc->seq)
phc->tc[1].cycle_last = ...
phc->tc[1].nsec = ...
write_seqcount_latch_end(&phc->seq)
Because read_seqcount_latch_retry() succeeds while phc->seq remains at the same odd value, the reader converts the hardware timestamp using the old phc->tc[1] instead of discarding it or using the updated timecounter. Updating phc->update_time after write_seqcount_latch_end() (or after write_seqcount_latch() once phc->tc[0] is populated and latched) would avoid exposing update_time before a valid timecounter snapshot is visible.
[ ... ]
Quoted 17 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +10
-2@@ -3207,14 +3519,24 @@ static int ice_ptp_rebuild_owner(struct ice_pf *pf)if (phc->update_time) {s64 delta =ktime_sub (ktime_get (), phc->update_time); - ts =ns_to_timespec64 (phc->cached_time + delta); +if (phc->use_timecounter) + ns = phc->tc[0 ].nsec + delta; +else + ns = phc->cached_time + delta; }else { - ts =ktime_to_timespec64 (ktime_get_real ()); + ns =ktime_get_real_ns (); + } + +if (phc->use_timecounter) { +ice_phc_init (phc, ns); + }else {
drivers/net/ethernet/intel/ice/ice_ptp.c static int ice_ptp_rebuild_owner(struct ice_pf *pf) · line 3217
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +5
-3+struct timespec64 ts =ns_to_timespec64 (ns); + + err =ice_ptp_write_init (pf, &ts); +if (err) + goto err_unlock; }mutex_unlock (&phc->lock); - err =ice_ptp_write_init (pf, &ts); -if (err) - goto err_unlock;
[Severity: Medium]
Does jumping to err_unlock when ice_ptp_write_init() fails leave phc->lock held? The err_unlock path only calls ice_ptp_unlock(hw), so any subsequent attempt to acquire phc->lock on a non-timecounter device will deadlock.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH iwl-next v3 4/4] ice: use struct timecounter for E825 and E830
Quoted 95 lines · On 11/17/2026 4:50 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ice: unreleasedphc->lockon error path inice_ptp_rebuild_owner()
- [Medium] ice: prematurephc->update_timestore inice_ptp_read_cycles()
--commit
b8fa2e5442cd684f10151ff2ab484f07d9521f1c
Author: Jacob Keller <[email protected]>ice: use struct timecounter for E825 and E830
This commit switches E825 and E830 devices to use a seqcount_latch-protected timecounter and cyclecounter in struct
ice_phcinstead of directly adjusting the hardware PHC timers and using the legacy timestamp extension logic.Quoted 38 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +30
-0@@ -272,6 +272,202 @@ int ice_ptp_read_src_clk_reg(struct ice_pf *pf,return 0 ; } +/** +* ice_ptp_read_cycles - cyclecounter read() callback function +* @cc: the hardware cycle counter structure +* +* Read the PHC time via ice_ptp_read_src_clk_reg() and report it for use +* within timecounter_read() and timecounter_init(). +* +* Although the cycle counter is masked to 32-bits for timestamp extension, +* this still reports the full 64-bit time to enable determining the phase +* difference between the PHC hardware value and the timecounter properly. +* +* Return: The current PHC timer value from the hardware on success. +* Since failure cannot be meaningfully reported, return the previous +* cycle_last value when unable to access the device. +*/ +u64 ice_ptp_read_cycles (struct cyclecounter *cc) +{ +struct ice_adapter *adapter; +struct ice_phc *phc; +struct ice_pf *pf; +u64 time; +int err; + + phc = container_of(cc, struct ice_phc, cc); + adapter = container_of(phc, struct ice_adapter, phc); + +scoped_guard (rcu) { + pf =rcu_dereference (adapter->ctrl_pf); +if (!pf) +return phc->tc[0 ].cycle_last;drivers/net/ethernet/intel/ice/ice_ptp.c int ice_ptp_read_src_clk_reg(struct ice_pf *pf, · line 314
Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +10
-0+ + err =ice_ptp_read_src_clk_reg (pf,NULL , &time); +if (err) +return phc->tc[0 ].cycle_last; + +/* Paired with smp_load_acquire in ice_ptp_extend_32b_ts */ +smp_store_release (&phc->update_time,ktime_get ()); +return time; + } +}[Severity: Medium]
Can publishingphc->update_timeinsideice_ptp_read_cycles()allowice_ptp_extend_32b_ts()to extend a timestamp against an uninitialized or stale phc->tc[1]?In
ice_phc_init()andice_phc_refresh(),write_seqcount_latch_begin()setsphc->seqto an odd value (directing readers to phc->tc[1]) before callingtimecounter_init()ortimecounter_read()on phc->tc[0]:CPU0 CPU1
ice_phc_init()
write_seqcount_latch_begin(&phc->seq)
timecounter_init(&phc->tc[0], ...)ice_ptp_read_cycles() smp_store_release(&phc->update_time, ...) ice_ptp_extend_32b_ts() smp_load_acquire(&phc->update_time) ice_phc_cyc2time_with_trace() seq = read_seqcount_latch(&phc->seq) /* idx == 1 */ timecounter_cyc2time(&phc->tc[1], ...) <-- reads uninitialized/stale tc[1] phc->tc[0].cycle_last = ... phc->tc[0].nsec = ...
write_seqcount_latch(&phc->seq)
phc->tc[1].cycle_last = ...
phc->tc[1].nsec = ...
write_seqcount_latch_end(&phc->seq)
Quoted 7 lines
Because
read_seqcount_latch_retry()succeeds whilephc->seqremains at the same odd value, the reader converts the hardware timestamp using the old phc->tc[1] instead of discarding it or using the updated timecounter. Updatingphc->update_timeafterwrite_seqcount_latch_end()(or afterwrite_seqcount_latch()once phc->tc[0] is populated and latched) would avoid exposingupdate_timebefore a valid timecounter snapshot is visible.
I'm fixing this by making update_time also protected by the seqcount_latch_t instead of separately, and applying the seqcount to both legacy and new updates. I think the end result will be better and address both concerns.
Quoted 26 lines
[ ... ]
Quoted 24 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +15
-2@@ -3207,14 +3519,24 @@ static int ice_ptp_rebuild_owner(struct ice_pf *pf)if (phc->update_time) {s64 delta =ktime_sub (ktime_get (), phc->update_time); - ts =ns_to_timespec64 (phc->cached_time + delta); +if (phc->use_timecounter) + ns = phc->tc[0 ].nsec + delta; +else + ns = phc->cached_time + delta; }else { - ts =ktime_to_timespec64 (ktime_get_real ()); + ns =ktime_get_real_ns (); + } + +if (phc->use_timecounter) { +ice_phc_init (phc, ns); + }else { +struct timespec64 ts =ns_to_timespec64 (ns); + + err =ice_ptp_write_init (pf, &ts); +if (err) + goto err_unlock; }mutex_unlock (&phc->lock);
Quoted 9 lines
drivers/net/ethernet/intel/ice/ice_ptp.c static int ice_ptp_rebuild_owner(struct ice_pf *pf) · line 3217
Quoted 3 lines
drivers/net/ethernet/intel/ice/ice_ptp.c +0
-3- err =ice_ptp_write_init (pf, &ts); -if (err) - goto err_unlock;[Severity: Medium]
Does jumping toerr_unlockwhenice_ptp_write_init()fails leavephc->lockheld? Theerr_unlockpath only callsice_ptp_unlock(hw), so any subsequent attempt to acquirephc->lockon a non-timecounter device will deadlock.
I'll fix this.