The 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.

To protect readers from concurrent update, use a seqcount_latch_t which
directs the ice_ptp_extend_32b_ts() to read from the secondary copy if it
interrupts a writer. This ensures a consistent read of the update_time and
cached_time, and will be even more necessary for the upcoming introduction
of the timecounter.

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.

Signed-off-by: Jacob Keller <[email protected]>
---
 drivers/net/ethernet/intel/ice/ice_adapter.h |  36 ++++
 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 |   6 +
 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     | 243 ++++++++++++---------------
 7 files changed, 152 insertions(+), 146 deletions(-)

diff --git a/drivers/net/ethernet/intel/ice/ice_adapter.h 
b/drivers/net/ethernet/intel/ice/ice_adapter.h
index ce1fe0846265..6fa4e2a0956b 100644
--- a/drivers/net/ethernet/intel/ice/ice_adapter.h
+++ b/drivers/net/ethernet/intel/ice/ice_adapter.h
@@ -8,6 +8,7 @@
 #include <linux/mutex.h>
 #include <linux/rwsem.h>
 #include <linux/srcu.h>
+#include <linux/seqlock.h>
 #include <linux/spinlock_types.h>
 #include <linux/refcount_types.h>
 
@@ -35,6 +36,38 @@ struct ice_port_list {
        struct srcu_struct srcu;
 };
 
+/**
+ * struct ice_phc - PTP Hardware Clock fields
+ *
+ * Structure used for handling the timestamp extension logic for this device.
+ *
+ * Legacy timestamp extension fields:
+ * @cached_time: Cached copy of the PHC time for timestamp extension
+ *
+ * General fields:
+ * @update_time: Monotonic time when PHC was laste updated
+ * @seq: seqcount_latch_t used to protect readers, directing them to the
+ *       secondary copy during an update.
+ * @lock: For protecting updates to the PHC fields.
+ *
+ * Readers are lockless and rely on the latched sequence counter for
+ * a consistent view of the most recent update, even if they interrupt
+ * a writer.
+ *
+ * Writers must acquire the lock mutex to ensure that the periodic task cannot
+ * race with clock adjustment callbacks.
+ */
+struct ice_phc {
+       /* Legacy timestamp extension fields */
+       u64 cached_time[2];
+
+       /* General fields */
+       ktime_t update_time[2];
+       seqcount_latch_t seq;
+       /* 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 +79,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 +107,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;
diff --git a/drivers/net/ethernet/intel/ice/ice_ptp.h 
b/drivers/net/ethernet/intel/ice/ice_ptp.h
index 40a0e654c2c6..ab9bf86f7a4c 100644
--- a/drivers/net/ethernet/intel/ice/ice_ptp.h
+++ b/drivers/net/ethernet/intel/ice/ice_ptp.h
@@ -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;
diff --git a/drivers/net/ethernet/intel/ice/ice_txrx.h 
b/drivers/net/ethernet/intel/ice/ice_txrx.h
index 5e517f219379..83a34322e0d3 100644
--- a/drivers/net/ethernet/intel/ice/ice_txrx.h
+++ b/drivers/net/ethernet/intel/ice/ice_txrx.h
@@ -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;
                };
        };
diff --git a/drivers/net/ethernet/intel/ice/ice_adapter.c 
b/drivers/net/ethernet/intel/ice/ice_adapter.c
index 29e089a5a86e..d8e3bb122e76 100644
--- a/drivers/net/ethernet/intel/ice/ice_adapter.c
+++ b/drivers/net/ethernet/intel/ice/ice_adapter.c
@@ -6,6 +6,7 @@
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
+#include <linux/seqlock.h>
 #include <linux/srcu.h>
 #include <linux/xarray.h>
 #include "ice_adapter.h"
@@ -83,6 +84,9 @@ static struct ice_adapter *ice_adapter_new(struct pci_dev 
*pdev)
 
        mutex_init(&adapter->ps_lock);
 
+       seqcount_latch_init(&adapter->phc.seq);
+       mutex_init(&adapter->phc.lock);
+
        return adapter;
 }
 
@@ -98,6 +102,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);
 }
 
diff --git a/drivers/net/ethernet/intel/ice/ice_ethtool.c 
b/drivers/net/ethernet/intel/ice/ice_ethtool.c
index 90fa11b22820..ca5137724b38 100644
--- a/drivers/net/ethernet/intel/ice/ice_ethtool.c
+++ b/drivers/net/ethernet/intel/ice/ice_ethtool.c
@@ -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){ };
diff --git a/drivers/net/ethernet/intel/ice/ice_lib.c 
b/drivers/net/ethernet/intel/ice/ice_lib.c
index 9be3f52bb9d9..f3fefab09f8c 100644
--- a/drivers/net/ethernet/intel/ice/ice_lib.c
+++ b/drivers/net/ethernet/intel/ice/ice_lib.c
@@ -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;
diff --git a/drivers/net/ethernet/intel/ice/ice_ptp.c 
b/drivers/net/ethernet/intel/ice/ice_ptp.c
index 777db22a7c24..a2c7dbd551e9 100644
--- a/drivers/net/ethernet/intel/ice/ice_ptp.c
+++ b/drivers/net/ethernet/intel/ice/ice_ptp.c
@@ -274,8 +274,9 @@ int ice_ptp_read_src_clk_reg(struct ice_pf *pf,
 
 /**
  * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b
- * @cached_phc_time: recently cached copy of PHC time
+ * @phc: PTP Hardware Clock structure
  * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value
+ * @trace_rx: True if this call should trigger an Rx timestamp trace
  *
  * Hardware captures timestamps which contain only 32 bits of nominal
  * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
@@ -309,13 +310,34 @@ 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;
+       unsigned int seq, idx;
+       ktime_t update_time;
+
+       /* Get a consistent copy of the update_time and PHC time */
+       do {
+               seq = read_seqcount_latch(&phc->seq);
+               idx = seq & 0x1;
+
+               update_time = phc->update_time[idx];
+               cached_phc_time = phc->cached_time[idx];
+       } while (read_seqcount_latch_retry(&phc->seq, seq));
+
+       /* Discard the timestamp if update_time isn't initialized */
+       if (!update_time)
+               return 0;
+
+       /* Discard the hardware timestamp if update_time is stale */
+       if (ktime_ms_delta(ktime_get_coarse_boottime(), update_time) > 2000)
+               return 0;
 
-       /* Extract the lower 32 bits of the PHC time */
        phc_time_lo = (u32)cached_phc_time;
 
        /* Calculate the delta between the lower 32bits of the cached PHC
@@ -336,6 +358,9 @@ static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 
in_tstamp)
                ns = cached_phc_time + delta;
        }
 
+       if (trace_rx)
+               ice_trace(rx_hwtstamp, cached_phc_time, in_tstamp, ns);
+
        return ns;
 }
 
@@ -359,7 +384,7 @@ static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 
in_tstamp)
  * calling this function.
  *
  * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
- * time stored in the device private PTP structure as the basis for timestamp
+ * time stored in the adapter PHC structure as the basis for timestamp
  * extension.
  *
  * See ice_ptp_extend_32b_ts for a detailed explanation of the extension
@@ -367,28 +392,17 @@ static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 
in_tstamp)
  */
 static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp)
 {
+       struct ice_phc *phc = &pf->adapter->phc;
        const u64 mask = GENMASK_ULL(31, 0);
-       unsigned long discard_time;
-       u64 cached_time;
+       u64 ns;
 
-       /* Discard the hardware timestamp if the cached PHC time is too old */
-       discard_time = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
-       if (time_is_before_jiffies(discard_time)) {
+       ns = ice_ptp_extend_32b_ts(phc, (in_tstamp >> 8) & mask, false);
+
+       /* A value of 0 indicates the timestamp was discarded */
+       if (!ns)
                pf->ptp.tx_hwtstamp_discarded++;
-               return 0;
-       }
 
-       /* Discard it as well if there is no cached PHC time at all, which is
-        * how ice_ptp_update_cached_phctime() reports that the source clock
-        * has become unreachable.
-        */
-       cached_time = READ_ONCE(pf->ptp.cached_phc_time);
-       if (!cached_time) {
-               pf->ptp.tx_hwtstamp_discarded++;
-               return 0;
-       }
-
-       return ice_ptp_extend_32b_ts(cached_time, (in_tstamp >> 8) & mask);
+       return ns;
 }
 
 /**
@@ -909,6 +923,31 @@ ice_ptp_flush_all_tx_tracker(struct ice_pf *pf)
        srcu_read_unlock(&ports->srcu, srcu_idx);
 }
 
+/**
+ * ice_ptp_mark_all_tx_tracker_stale - Mark all unfinished timestamps stale
+ * @pf: Board private structure
+ *
+ * Mark currently outstanding Tx timestamps for all ports as stale. Called by
+ * the clock owner when a time adjustment is made to prevent accidentally
+ * extending timestamps captured before the adjustment.
+ */
+static void ice_ptp_mark_all_tx_tracker_stale(struct ice_pf *pf)
+{
+       struct ice_port_list *ports = &pf->adapter->ports;
+       struct ice_ptp_port *port;
+       int srcu_idx;
+
+       srcu_idx = srcu_read_lock(&ports->srcu);
+       list_for_each_entry_srcu(port, &ports->list, list_node,
+                                srcu_read_lock_held(&ports->srcu)) {
+               if (!kref_get_unless_zero(&port->ref))
+                       continue;
+               ice_ptp_mark_tx_tracker_stale(&port->tx);
+               kref_put(&port->ref, ice_ptp_release_port_srcu);
+       }
+       srcu_read_unlock(&ports->srcu, srcu_idx);
+}
+
 /**
  * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
  * @pf: Board private structure
@@ -993,81 +1032,54 @@ static int ice_ptp_init_tx(struct ice_pf *pf, struct 
ice_ptp_tx *tx, u8 port)
 }
 
 /**
- * ice_ptp_update_cached_phctime - Update the cached PHC time values
+ * ice_ptp_update_cached_phctime - Update the cached PHC time value
  * @pf: Board specific private structure
  *
- * This function updates the system time values which are cached in the PF
- * structure and the Rx rings.
+ * Update the cached PHC time used for timestamp extension.
  *
- * This function must be called periodically to ensure that the cached value
- * is never more than 2 seconds old.
- *
- * Note that the cached copy in the PF PTP structure is always updated, even
- * if we can't update the copy in the Rx rings.
- *
- * Return:
- * * 0 - OK, successfully updated
- * * -EAGAIN - PF was busy, need to reschedule the update
- * * -ENODEV - no control PF owns the source clock register space
- * * -EOPNOTSUPP - PTP support is not compiled in
+ * This function must be called periodically by the clock owner to ensure that
+ * the timecounter or cached value is never more than 2 seconds old.
  */
-static int ice_ptp_update_cached_phctime(struct ice_pf *pf)
+static void ice_ptp_update_cached_phctime(struct ice_pf *pf)
 {
+       struct ice_phc *phc = &pf->adapter->phc;
        struct device *dev = ice_pf_to_dev(pf);
-       unsigned long update_before;
+       ktime_t now;
        u64 systime;
-       int err, i;
+       s64 delta;
+       int err;
 
-       update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
-       if (pf->ptp.cached_phc_time &&
-           time_is_before_jiffies(update_before)) {
-               unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies;
+       mutex_lock(&phc->lock);
 
-               dev_warn(dev, "%u msecs passed between update to cached PHC 
time\n",
-                        jiffies_to_msecs(time_taken));
+       now = ktime_get_boottime();
+       delta = ktime_ms_delta(now, phc->update_time[0]);
+       if (phc->update_time[0] && delta > 2000) {
+               dev_warn(dev, "%lld msecs passed between update to cached PHC 
time\n",
+                        delta);
                pf->ptp.late_cached_phc_updates++;
        }
 
+       write_seqcount_latch_begin(&phc->seq);
+
        /* Read the current PHC time */
        err = ice_ptp_read_src_clk_reg(pf, NULL, &systime);
        if (err) {
                /* The source clock is gone, so the cache can no longer be
                 * refreshed. Publish zero instead of leaving the last good
-                * value behind: neither ice_ptp_get_rx_hwts() nor
-                * ice_ptp_extend_40b_ts() may extend against a value that no
-                * longer tracks the PHC, and both treat a zero cache as
-                * "no timestamp".
+                * value behind.
                 */
                systime = 0;
-               WRITE_ONCE(pf->ptp.cached_phc_time, 0);
-       } else {
-               /* Update the cached PHC time stored in the PF structure */
-               WRITE_ONCE(pf->ptp.cached_phc_time, systime);
-               WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies);
+               now = ns_to_ktime(0);
        }
 
-       if (test_and_set_bit(ICE_CFG_BUSY, pf->state))
-               return -EAGAIN;
+       phc->cached_time[0] = systime;
+       phc->update_time[0] = now;
+       write_seqcount_latch(&phc->seq);
+       phc->cached_time[1] = systime;
+       phc->update_time[1] = now;
+       write_seqcount_latch_end(&phc->seq);
 
-       ice_for_each_vsi(pf, i) {
-               struct ice_vsi *vsi = pf->vsi[i];
-               int j;
-
-               if (!vsi)
-                       continue;
-
-               if (vsi->type != ICE_VSI_PF)
-                       continue;
-
-               ice_for_each_rxq(vsi, j) {
-                       if (!vsi->rx_rings[j])
-                               continue;
-                       WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
-               }
-       }
-       clear_bit(ICE_CFG_BUSY, pf->state);
-
-       return err;
+       mutex_unlock(&phc->lock);
 }
 
 /**
@@ -1076,41 +1088,18 @@ static int ice_ptp_update_cached_phctime(struct ice_pf 
*pf)
  *
  * This function must be called when the cached PHC time is no longer valid,
  * such as after a time adjustment. It marks any currently outstanding Tx
- * timestamps as stale and updates the cached PHC time for both the PF and Rx
- * rings.
- *
- * If updating the PHC time cannot be done immediately, a warning message is
- * logged and the work item is scheduled immediately to minimize the window
- * with a wrong cached timestamp.
+ * timestamps as stale and updates the cached PHC time.
  */
 static void ice_ptp_reset_cached_phctime(struct ice_pf *pf)
 {
-       struct device *dev = ice_pf_to_dev(pf);
-       int err;
-
-       /* Update the cached PHC time immediately if possible, otherwise
-        * schedule the work item to execute soon.
-        */
-       err = ice_ptp_update_cached_phctime(pf);
-       if (err) {
-               /* If another thread is updating the Rx rings, we won't
-                * properly reset them here. This could lead to reporting of
-                * invalid timestamps, but there isn't much we can do.
-                */
-               dev_warn(dev, "%s: unable to immediately update cached PHC 
time, err %d\n",
-                        __func__, err);
-
-               /* Queue the work item to update the Rx rings when possible */
-               kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work,
-                                          msecs_to_jiffies(10));
-       }
+       ice_ptp_update_cached_phctime(pf);
 
        /* Mark any outstanding timestamps as stale, since they might have
         * been captured in hardware before the time update. This could lead
         * to us extending them with the wrong cached value resulting in
         * incorrect timestamp values.
         */
-       ice_ptp_mark_tx_tracker_stale(&pf->ptp.port.tx);
+       ice_ptp_mark_all_tx_tracker_stale(pf);
 }
 
 /**
@@ -2484,28 +2473,14 @@ int ice_ptp_hwtstamp_set(struct net_device *netdev,
 u64 ice_ptp_get_rx_hwts(const union ice_32b_rx_flex_desc *rx_desc,
                        const struct ice_pkt_ctx *pkt_ctx)
 {
-       u64 ts_ns, cached_time;
        u32 ts_high;
+       u64 ts_ns;
 
        if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID))
                return 0;
 
-       cached_time = READ_ONCE(pkt_ctx->cached_phctime);
-
-       /* Do not report a timestamp if we don't have a cached PHC time */
-       if (!cached_time)
-               return 0;
-
-       /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached
-        * PHC value, rather than accessing the PF. This also allows us to
-        * simply pass the upper 32bits of nanoseconds directly. Calling
-        * ice_ptp_extend_40b_ts is unnecessary as it would just discard these
-        * bits itself.
-        */
        ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
-       ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high);
-
-       ice_trace(rx_hwtstamp, cached_time, ts_high, ts_ns);
+       ts_ns = ice_ptp_extend_32b_ts(pkt_ctx->phc, ts_high, true);
 
        return ts_ns;
 }
@@ -3104,24 +3079,20 @@ static void ice_ptp_periodic_work(struct kthread_work 
*work)
 {
        struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
        struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
-       int err;
 
        if (pf->ptp.state != ICE_PTP_READY)
                return;
 
        ice_ptp_tspll_monitor(pf);
 
-       err = ice_ptp_update_cached_phctime(pf);
+       if (ice_pf_src_tmr_owned(pf))
+               ice_ptp_update_cached_phctime(pf);
 
        ice_ptp_maybe_trigger_tx_interrupt(pf);
 
-       /* Run twice a second, or retry soon if another thread was busy
-        * updating the Rx rings. Other errors, such as a missing control PF,
-        * do not clear up on their own and must not turn the periodic worker
-        * into a busy loop.
-        */
+       /* Run twice a second */
        kthread_queue_delayed_work(ptp->kworker, &ptp->work,
-                                  msecs_to_jiffies(err == -EAGAIN ? 10 : 500));
+                                  msecs_to_jiffies(500));
 }
 
 /**
@@ -3213,9 +3184,6 @@ void ice_ptp_prepare_for_reset(struct ice_pf *pf, enum 
ice_reset_req reset_type)
 
        /* Disable source clock */
        wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M);
-
-       /* Acquire PHC and system timer to restore after reset */
-       ptp->reset_time = ktime_get_real_ns();
 }
 
 /**
@@ -3227,10 +3195,10 @@ void ice_ptp_prepare_for_reset(struct ice_pf *pf, enum 
ice_reset_req reset_type)
  */
 static int ice_ptp_rebuild_owner(struct ice_pf *pf)
 {
+       struct ice_phc *phc = &pf->adapter->phc;
        struct ice_ptp *ptp = &pf->ptp;
        struct ice_hw *hw = &pf->hw;
        struct timespec64 ts;
-       u64 time_diff;
        int err;
 
        err = ice_ptp_init_phc(hw);
@@ -3255,16 +3223,19 @@ static int ice_ptp_rebuild_owner(struct ice_pf *pf)
        if (err)
                goto err_unlock;
 
-       /* Write the initial Time value to PHY and LAN using the cached PHC
-        * time before the reset and time difference between stopping and
-        * starting the clock.
+       /* Write the initial Time value to PHY and LAN using the last known
+        * cached PHC time and the monotonic time difference since that
+        * update.
         */
-       if (ptp->cached_phc_time) {
-               time_diff = ktime_get_real_ns() - ptp->reset_time;
-               ts = ns_to_timespec64(ptp->cached_phc_time + time_diff);
+       mutex_lock(&phc->lock);
+       if (phc->update_time[0]) {
+               s64 delta = ktime_sub(ktime_get_boottime(), 
phc->update_time[0]);
+
+               ts = ns_to_timespec64(phc->cached_time[0] + delta);
        } else {
                ts = ktime_to_timespec64(ktime_get_real());
        }
+       mutex_unlock(&phc->lock);
        err = ice_ptp_write_init(pf, &ts);
        if (err)
                goto err_unlock;

-- 
2.56.0.215.g47fdf2208c5b

Reply via email to