@@ -2,4 +2,4 @@
obj-$(CONFIG_GVE) += gve.o
gve-objs := gve_main.o gve_tx.o gve_tx_dqo.o gve_rx.o gve_rx_dqo.o gve_ethtool.o gve_adminq.o gve_utils.o gve_flow_rule.o \
- gve_buffer_mgmt_dqo.o
+ gve_buffer_mgmt_dqo.o gve_clock.o
@@ -873,7 +873,12 @@ struct gve_priv {
struct gve_rss_config rss_config;
/* True if the device supports reading the nic clock */
+ struct workqueue_struct *gve_ts_wq;
bool nic_timestamp_supported;
+ struct delayed_work nic_ts_sync_task;
+ struct gve_nic_ts_report *nic_ts_report;
+ dma_addr_t nic_ts_report_bus;
+ u64 last_sync_nic_counter; /* Clock counter from last NIC TS report */
};
enum gve_service_task_flags_bit {
@@ -1255,6 +1260,9 @@ int gve_flow_rules_reset(struct gve_priv *priv);
int gve_init_rss_config(struct gve_priv *priv, u16 num_queues);
/* report stats handling */
void gve_handle_report_stats(struct gve_priv *priv);
+/* Timestamping */
+int gve_init_clock(struct gve_priv *priv);
+void gve_teardown_clock(struct gve_priv *priv);
/* exported by ethtool.c */
extern const struct ethtool_ops gve_ethtool_ops;
/* needed by ethtool */
new file mode 100644
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: (GPL-2.0 OR MIT)
+/* Google virtual Ethernet (gve) driver
+ *
+ * Copyright (C) 2015-2025 Google LLC
+ */
+
+#include "gve.h"
+#include "gve_adminq.h"
+
+/* Interval to schedule a nic timestamp calibration, 250ms. */
+#define GVE_NIC_TS_SYNC_INTERVAL_MS 250
+
+/* Read the nic timestamp from hardware via the admin queue. */
+static int gve_clock_nic_ts_read(struct gve_priv *priv)
+{
+ u64 nic_raw;
+ int err;
+
+ err = gve_adminq_report_nic_ts(priv, priv->nic_ts_report_bus);
+ if (err)
+ return err;
+
+ nic_raw = be64_to_cpu(priv->nic_ts_report->nic_timestamp);
+ WRITE_ONCE(priv->last_sync_nic_counter, nic_raw);
+
+ return 0;
+}
+
+static void gve_nic_ts_sync_task(struct work_struct *work)
+{
+ struct gve_priv *priv = container_of(work, struct gve_priv,
+ nic_ts_sync_task.work);
+ int err;
+
+ if (gve_get_reset_in_progress(priv) || !gve_get_admin_queue_ok(priv))
+ goto out;
+
+ err = gve_clock_nic_ts_read(priv);
+ if (err && net_ratelimit())
+ dev_err(&priv->pdev->dev,
+ "%s read err %d\n", __func__, err);
+
+out:
+ queue_delayed_work(priv->gve_wq, &priv->nic_ts_sync_task,
+ msecs_to_jiffies(GVE_NIC_TS_SYNC_INTERVAL_MS));
+}
+
+int gve_init_clock(struct gve_priv *priv)
+{
+ int err;
+
+ if (!priv->nic_timestamp_supported)
+ return -EPERM;
+
+ priv->nic_ts_report =
+ dma_alloc_coherent(&priv->pdev->dev,
+ sizeof(struct gve_nic_ts_report),
+ &priv->nic_ts_report_bus,
+ GFP_KERNEL);
+ if (!priv->nic_ts_report) {
+ dev_err(&priv->pdev->dev, "%s dma alloc error\n", __func__);
+ return -ENOMEM;
+ }
+
+ err = gve_clock_nic_ts_read(priv);
+ if (err) {
+ dev_err(&priv->pdev->dev, "%s read error %d\n", __func__, err);
+ goto free_nic_ts_report;
+ }
+
+ priv->gve_ts_wq = alloc_ordered_workqueue("gve-ts", 0);
+ if (!priv->gve_ts_wq) {
+ dev_err(&priv->pdev->dev, "%s Could not allocate workqueue\n",
+ __func__);
+ err = -ENOMEM;
+ goto free_nic_ts_report;
+ }
+ INIT_DELAYED_WORK(&priv->nic_ts_sync_task, gve_nic_ts_sync_task);
+ queue_delayed_work(priv->gve_ts_wq, &priv->nic_ts_sync_task,
+ msecs_to_jiffies(GVE_NIC_TS_SYNC_INTERVAL_MS));
+
+ return 0;
+
+free_nic_ts_report:
+ dma_free_coherent(&priv->pdev->dev,
+ sizeof(struct gve_nic_ts_report),
+ priv->nic_ts_report, priv->nic_ts_report_bus);
+ priv->nic_ts_report = NULL;
+
+ return err;
+}
+
+void gve_teardown_clock(struct gve_priv *priv)
+{
+ if (priv->nic_ts_report) {
+ cancel_delayed_work_sync(&priv->nic_ts_sync_task);
+ destroy_workqueue(priv->gve_ts_wq);
+ dma_free_coherent(&priv->pdev->dev,
+ sizeof(struct gve_nic_ts_report),
+ priv->nic_ts_report, priv->nic_ts_report_bus);
+ priv->nic_ts_report = NULL;
+ }
+}