Add support for the Earliest TxTime First (ETF) offload, also known as
Tx Packet Pacing (SO_TXTIME), on iavf. It is available on E830 adapters
and is negotiated with the PF through the VIRTCHNL_VF_OFFLOAD_TXTIME
extended capability.

When RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP is requested on a Tx queue, the
driver allocates a timestamp ring alongside the Tx descriptor ring. For
each packet, the per-packet launch time is read from the dynamic mbuf Tx
timestamp field and written to the timestamp ring, and a dedicated
timestamp-ring doorbell is rung so that hardware transmits the packet at
the scheduled time. Extra timestamp descriptors are programmed on ring
wrap-around to avoid a malicious driver detection event.

The launch time is encoded from the PHC nanoseconds, rounded to the 128 ns
timestamp resolution. As the hardware treats the half of its 19-bit
timestamp window ahead of the current time as the future, launch times
further than 30 ms ahead are sent at that limit, and packets without a
launch time are sent immediately. The device time is estimated with the
TSC from the last PHC read, which is refreshed every second while Tx Time
is in use, so no PF request is needed in the Tx path.

The timestamp ring address and the per-queue enable flag are passed to
the PF via VIRTCHNL_OP_CONFIG_VSI_QUEUES. The extended VF capabilities
are negotiated via VIRTCHNL_OP_GET_VF_CAPS2.

The PTP timesync dev ops (enable, disable, read Rx timestamp) and
read_clock are also implemented; applications such as testpmd use
read_clock to program the Tx launch times.

Signed-off-by: Soumyadeep Hore <[email protected]>
---
 drivers/net/intel/iavf/iavf.h        |  43 +++++++
 drivers/net/intel/iavf/iavf_ethdev.c | 181 +++++++++++++++++++++++++++
 drivers/net/intel/iavf/iavf_rxtx.c   | 145 ++++++++++++++++++++-
 drivers/net/intel/iavf/iavf_rxtx.h   |  23 ++++
 drivers/net/intel/iavf/iavf_vchnl.c  |  65 ++++++++++
 5 files changed, 456 insertions(+), 1 deletion(-)

diff --git a/drivers/net/intel/iavf/iavf.h b/drivers/net/intel/iavf/iavf.h
index 0c98ae28e1..67dbf21989 100644
--- a/drivers/net/intel/iavf/iavf.h
+++ b/drivers/net/intel/iavf/iavf.h
@@ -8,7 +8,9 @@
 #include <sys/queue.h>
 
 #include <ethdev_driver.h>
+#include <rte_cycles.h>
 #include <rte_kvargs.h>
+#include <rte_seqlock.h>
 #include <rte_tm_driver.h>
 
 #include <iavf_prototype.h>
@@ -80,6 +82,9 @@
 
 #define IAVF_ALARM_INTERVAL 50000 /* us */
 #define IAVF_PHC_SYNC_ALARM_INTERVAL_US 200000
+/* Tx time clock anchor refresh period and age beyond which it is unused */
+#define IAVF_TXTIME_CLOCK_REFRESH_US 1000000
+#define IAVF_TXTIME_CLOCK_MAX_AGE_US 2000000
 
 /* The overhead from MTU to max frame size.
  * Considering QinQ packet, the VLAN tag needs to be counted twice.
@@ -297,6 +302,8 @@ struct iavf_info {
        bool start_pending;
 
        uint32_t ptp_caps;
+       uint64_t vf_cap_flags2; /* extended caps negotiated via GET_VF_CAPS2 */
+       bool stop_txtime; /* request a no-TXTIME reconfigure on teardown */
        rte_spinlock_t phc_time_aq_lock;
 };
 
@@ -381,6 +388,13 @@ enum iavf_tx_func_type {
 #define IAVF_MBUF_CHECK_F_TX_SEGMENT     (1ULL << 2)
 #define IAVF_MBUF_CHECK_F_TX_OFFLOAD     (1ULL << 3)
 
+/* PHC time paired with the TSC at the middle of the PF request reading it */
+struct iavf_txtime_clock {
+       rte_seqlock_t lock;
+       uint64_t phc;
+       uint64_t tsc;
+};
+
 /* Structure to store private data for each VF instance. */
 struct iavf_adapter {
        struct iavf_hw hw;
@@ -399,6 +413,7 @@ struct iavf_adapter {
        rte_spinlock_t phc_sync_lock;
        uint8_t phc_sync_ticks;
        bool phc_sync_paused;
+       struct iavf_txtime_clock txtime_clk;
        struct iavf_devargs devargs;
        bool mac_primary_set;
        bool adminq_polling; /* Interrupt setup fell back to alarm polling. */
@@ -421,6 +436,33 @@ iavf_get_eth_dev(struct iavf_adapter *adapter)
 #define IAVF_DEV_PRIVATE_TO_IAVF_SECURITY_CTX(adapter) \
        (((struct iavf_adapter *)adapter)->security_ctx)
 
+/* Test an extended VF capability (bit position, e.g. 
VIRTCHNL_VF_OFFLOAD_TXTIME)
+ * negotiated via VIRTCHNL_OP_GET_VF_CAPS2.
+ */
+#define IAVF_VF_CAP2(vf, bit) (((vf)->vf_cap_flags2 & RTE_BIT64(bit)) != 0)
+
+/* PHC time extrapolated with the TSC from the last PF read, 0 if unknown */
+static inline uint64_t
+iavf_txtime_clock_now(struct iavf_adapter *adapter)
+{
+       uint64_t hz = rte_get_tsc_hz();
+       uint64_t phc, tsc, elapsed;
+       uint32_t sn;
+
+       do {
+               sn = rte_seqlock_read_begin(&adapter->txtime_clk.lock);
+               phc = adapter->txtime_clk.phc;
+               tsc = adapter->txtime_clk.tsc;
+       } while (rte_seqlock_read_retry(&adapter->txtime_clk.lock, sn));
+
+       if (tsc == 0)
+               return 0;
+       elapsed = rte_rdtsc() - tsc;
+       if (elapsed > hz / US_PER_S * IAVF_TXTIME_CLOCK_MAX_AGE_US)
+               return 0;
+       return phc + elapsed * NS_PER_S / hz;
+}
+
 /* IAVF_VSI_TO */
 #define IAVF_VSI_TO_HW(vsi) \
        (&(((struct iavf_vsi *)vsi)->adapter->hw))
@@ -529,6 +571,7 @@ int iavf_ipsec_crypto_request(struct iavf_adapter *adapter,
                uint8_t *resp_msg, size_t resp_msg_len);
 extern const struct rte_tm_ops iavf_tm_ops;
 int iavf_get_ptp_cap(struct iavf_adapter *adapter);
+int iavf_get_vf_caps2(struct iavf_adapter *adapter);
 int iavf_get_phc_time(struct ci_rx_queue *rxq);
 int iavf_flow_sub(struct iavf_adapter *adapter,
                  struct iavf_fsub_conf *filter);
diff --git a/drivers/net/intel/iavf/iavf_ethdev.c 
b/drivers/net/intel/iavf/iavf_ethdev.c
index 10069feae2..efd6b2a122 100644
--- a/drivers/net/intel/iavf/iavf_ethdev.c
+++ b/drivers/net/intel/iavf/iavf_ethdev.c
@@ -169,6 +169,12 @@ static uint64_t iavf_get_restore_flags(struct rte_eth_dev 
*dev,
                                       enum rte_eth_dev_operation op);
 static int iavf_post_reset_reconfig(struct rte_eth_dev *dev);
 static int iavf_timesync_read_time(struct rte_eth_dev *dev, struct timespec 
*timestamp);
+static int iavf_read_clock(struct rte_eth_dev *dev, uint64_t *clock);
+static int iavf_timesync_enable(struct rte_eth_dev *dev);
+static int iavf_timesync_disable(struct rte_eth_dev *dev);
+static int iavf_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
+                                          struct timespec *timestamp,
+                                          uint32_t flags);
 
 static const struct rte_pci_id pci_id_iavf_map[] = {
        { RTE_PCI_DEVICE(IAVF_INTEL_VENDOR_ID, IAVF_DEV_ID_ADAPTIVE_VF) },
@@ -270,6 +276,10 @@ static const struct eth_dev_ops iavf_eth_dev_ops = {
        .tm_ops_get                 = iavf_tm_ops_get,
        .get_restore_flags          = iavf_get_restore_flags,
        .timesync_read_time         = iavf_timesync_read_time,
+       .read_clock                 = iavf_read_clock,
+       .timesync_enable            = iavf_timesync_enable,
+       .timesync_disable           = iavf_timesync_disable,
+       .timesync_read_rx_timestamp = iavf_timesync_read_rx_timestamp,
 };
 
 static int
@@ -1134,6 +1144,29 @@ iavf_dev_start(struct rte_eth_dev *dev)
        return -1;
 }
 
+/* Max time HW can hold a Tx time packet: 2^19 x 128 ns launch window. */
+#define IAVF_TXTIME_DRAIN_MS 68
+
+/* Disabling a Tx time queue while packets are still scheduled leaves the
+ * PF scheduler in a state that breaks Tx time in later runs, so give
+ * pending packets one launch window to be sent first.
+ */
+static void
+iavf_txtime_drain(struct rte_eth_dev *dev)
+{
+       uint16_t i;
+
+       for (i = 0; i < dev->data->nb_tx_queues; i++) {
+               struct ci_tx_queue *txq = dev->data->tx_queues[i];
+
+               if (txq != NULL && txq->iavf_tsq != NULL &&
+                   txq->nb_tx_free < txq->nb_tx_desc - 1) {
+                       rte_delay_ms(IAVF_TXTIME_DRAIN_MS);
+                       return;
+               }
+       }
+}
+
 static int
 iavf_dev_stop(struct rte_eth_dev *dev)
 {
@@ -1161,7 +1194,34 @@ iavf_dev_stop(struct rte_eth_dev *dev)
        /* Rx interrupt vector mapping free */
        rte_intr_vec_list_free(intr_handle);
 
+       iavf_txtime_drain(dev);
+
        iavf_stop_queues(dev);
+
+       /* If Tx Time (ETF) was active, reconfigure the queues without the
+        * TXTIME flag so the PF cleanly releases the timestamp scheduler
+        * context. Tearing down while packets are still scheduled otherwise
+        * leaves stale state that breaks Tx Time in later runs.
+        */
+       if (vf->vf_cap_flags2 & RTE_BIT64(VIRTCHNL_VF_OFFLOAD_TXTIME)) {
+               bool txtime_active = false;
+               uint16_t i;
+
+               for (i = 0; i < dev->data->nb_tx_queues; i++) {
+                       struct ci_tx_queue *txq = dev->data->tx_queues[i];
+
+                       if (txq != NULL && txq->iavf_tsq != NULL) {
+                               txtime_active = true;
+                               break;
+                       }
+               }
+               if (txtime_active) {
+                       vf->stop_txtime = true;
+                       if (iavf_configure_queues(adapter, vf->num_queue_pairs) 
!= 0)
+                               PMD_DRV_LOG(WARNING, "failed to disable Tx time 
queues");
+                       vf->stop_txtime = false;
+               }
+       }
        rte_free(vf->qv_map);
        vf->qv_map = NULL;
 
@@ -1225,6 +1285,7 @@ iavf_dev_info_get(struct rte_eth_dev *dev, struct 
rte_eth_dev_info *dev_info)
                 CI_TX_VEC_OFFLOAD_PATH_OFFLOADS | 
CI_TX_VEC_CTX_OFFLOAD_PATH_OFFLOADS) &
                ~(RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM |
                  RTE_ETH_TX_OFFLOAD_QINQ_INSERT |
+                 RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP |
                  RTE_ETH_TX_OFFLOAD_SECURITY);
 
        /* X710 does not support outer udp checksum */
@@ -1238,6 +1299,9 @@ iavf_dev_info_get(struct rte_eth_dev *dev, struct 
rte_eth_dev_info *dev_info)
            vf->ptp_caps & VIRTCHNL_1588_PTP_CAP_RX_TSTAMP)
                dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
 
+       if (IAVF_VF_CAP2(vf, VIRTCHNL_VF_OFFLOAD_TXTIME))
+               dev_info->tx_offload_capa |= 
RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP;
+
        if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN_V2 &&
                        vf->vlan_v2_caps.offloads.insertion_support.inner &&
                        vf->vlan_v2_caps.offloads.insertion_support.outer)
@@ -2726,6 +2790,12 @@ iavf_init_vf(struct rte_eth_dev *dev)
                PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
                goto err_alloc;
        }
+
+       /* Negotiate extended capabilities (e.g. TXTIME) if the PF supports it. 
*/
+       if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAPS2) {
+               if (iavf_get_vf_caps2(adapter) != 0)
+                       PMD_INIT_LOG(WARNING, "failed to get VF extended 
capabilities");
+       }
        /* Allocate memort for RSS info */
        if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
                vf->rss_key = rte_zmalloc("rss_key",
@@ -2921,6 +2991,29 @@ iavf_phc_sync_tick(struct rte_eth_dev *dev)
        rte_spinlock_unlock(&adapter->phc_sync_lock);
 }
 
+/* Keep the Tx time clock anchor fresh while Tx time is used */
+static void
+iavf_txtime_clock_tick(struct rte_eth_dev *dev)
+{
+       struct iavf_adapter *adapter =
+               IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
+       struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
+       uint64_t refresh = rte_get_tsc_hz() / US_PER_S * 
IAVF_TXTIME_CLOCK_REFRESH_US;
+       uint64_t phc;
+
+       if (!dev->data->dev_started || adapter->closed ||
+           !(dev->data->dev_conf.txmode.offloads & 
RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP) ||
+           !IAVF_VF_CAP2(vf, VIRTCHNL_VF_OFFLOAD_TXTIME) ||
+           !(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_PTP) ||
+           !(vf->ptp_caps & VIRTCHNL_1588_PTP_CAP_READ_PHC))
+               return;
+
+       if (rte_rdtsc() - adapter->txtime_clk.tsc < refresh)
+               return;
+
+       iavf_phc_get_time(adapter, &phc);
+}
+
 void
 iavf_dev_alarm_handler(void *param)
 {
@@ -2948,6 +3041,7 @@ iavf_dev_alarm_handler(void *param)
        }
 
        iavf_phc_sync_tick(dev);
+       iavf_txtime_clock_tick(dev);
 
        rte_eal_alarm_set(IAVF_ALARM_INTERVAL,
                          iavf_dev_alarm_handler, dev);
@@ -3084,6 +3178,7 @@ iavf_dev_init(struct rte_eth_dev *eth_dev)
        adapter->tpid = RTE_ETHER_TYPE_VLAN; /* VLAN TPID set to 0x8100 by 
default */
        rte_spinlock_init(&adapter->phc_sync_lock);
        rte_spinlock_init(&vf->watchdog_lock);
+       rte_seqlock_init(&adapter->txtime_clk.lock);
 
        if (!rte_atomic_load_explicit(&vf->in_reset_recovery, 
rte_memory_order_relaxed) &&
            iavf_dev_event_handler_init())
@@ -3799,6 +3894,92 @@ iavf_timesync_read_time(struct rte_eth_dev *dev, struct 
timespec *timestamp)
        return 0;
 }
 
+static int
+iavf_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
+{
+       struct timespec ts;
+       int ret;
+
+       ret = iavf_timesync_read_time(dev, &ts);
+       if (ret)
+               return ret;
+
+       *clock = (uint64_t)ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
+
+       return 0;
+}
+
+static int
+iavf_timesync_enable(struct rte_eth_dev *dev)
+{
+       struct iavf_adapter *adapter =
+               IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
+       struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
+
+       if (adapter->closed)
+               return -EIO;
+
+       if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_PTP) ||
+           !(vf->ptp_caps & VIRTCHNL_1588_PTP_CAP_RX_TSTAMP))
+               return -ENOTSUP;
+
+       /* Rx timestamping is driven by the RTE_ETH_RX_OFFLOAD_TIMESTAMP offload
+        * programmed at queue setup; there is nothing else to enable on the VF.
+        */
+       if (dev->data->dev_started &&
+           !(dev->data->dev_conf.rxmode.offloads & 
RTE_ETH_RX_OFFLOAD_TIMESTAMP)) {
+               PMD_DRV_LOG(ERR, "Rx timestamp offload not configured");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int
+iavf_timesync_disable(struct rte_eth_dev *dev)
+{
+       struct iavf_adapter *adapter =
+               IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
+
+       if (adapter->closed)
+               return -EIO;
+
+       /* Nothing VF-specific to tear down; disabling the Rx timestamp offload
+        * is handled through the normal queue reconfiguration path.
+        */
+       return 0;
+}
+
+static int
+iavf_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
+                               struct timespec *timestamp, uint32_t flags)
+{
+       struct iavf_adapter *adapter =
+               IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
+       struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
+       struct ci_rx_queue *rxq;
+
+       if (adapter->closed)
+               return -EIO;
+
+       if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_PTP) ||
+           !(vf->ptp_caps & VIRTCHNL_1588_PTP_CAP_RX_TSTAMP))
+               return -ENOTSUP;
+
+       /* The 'flags' argument carries the Rx queue index to read from. */
+       if (flags >= dev->data->nb_rx_queues)
+               return -EINVAL;
+
+       rxq = dev->data->rx_queues[flags];
+       if (rxq == NULL)
+               return -EINVAL;
+
+       /* The Rx path stores the most recent converted 64-bit timestamp. */
+       *timestamp = rte_ns_to_timespec(rxq->phc_time);
+
+       return 0;
+}
+
 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
diff --git a/drivers/net/intel/iavf/iavf_rxtx.c 
b/drivers/net/intel/iavf/iavf_rxtx.c
index 8c84991b86..af35ea538c 100644
--- a/drivers/net/intel/iavf/iavf_rxtx.c
+++ b/drivers/net/intel/iavf/iavf_rxtx.c
@@ -16,6 +16,7 @@
 #include <rte_string_fns.h>
 #include <rte_memzone.h>
 #include <rte_mbuf.h>
+#include <rte_mbuf_dyn.h>
 #include <rte_malloc.h>
 #include <rte_ether.h>
 #include <ethdev_driver.h>
@@ -26,6 +27,7 @@
 #include <rte_net.h>
 #include <rte_vect.h>
 #include <rte_bitops.h>
+#include <rte_cycles.h>
 #include <rte_vxlan.h>
 #include <rte_gtp.h>
 #include <rte_geneve.h>
@@ -864,6 +866,64 @@ iavf_dev_tx_queue_setup(struct rte_eth_dev *dev,
        dev->data->tx_queues[queue_idx] = txq;
        txq->qtx_tail = hw->hw_addr + IAVF_QTX_TAIL1(queue_idx);
 
+       /* Earliest TxTime First (SO_TXTIME) offload: allocate a timestamp ring
+        * alongside the Tx descriptor ring. HW reads the per-packet transmit
+        * time from this ring to schedule the packet.
+        */
+       if (offloads & RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP) {
+               struct iavf_txtime *tsq;
+               const struct rte_memzone *ts_mz;
+               uint32_t ts_ring_size;
+
+               if (!(IAVF_VF_CAP2(vf, VIRTCHNL_VF_OFFLOAD_TXTIME))) {
+                       PMD_INIT_LOG(ERR, "Tx Time offload is not supported by 
the PF");
+                       iavf_dev_tx_queue_release(dev, queue_idx);
+                       dev->data->tx_queues[queue_idx] = NULL;
+                       return -ENOTSUP;
+               }
+
+               tsq = rte_zmalloc_socket("iavf txtime queue", sizeof(*tsq),
+                                        RTE_CACHE_LINE_SIZE, socket_id);
+               if (!tsq) {
+                       PMD_INIT_LOG(ERR, "Failed to allocate memory for Tx 
time queue");
+                       iavf_dev_tx_queue_release(dev, queue_idx);
+                       dev->data->tx_queues[queue_idx] = NULL;
+                       return -ENOMEM;
+               }
+               txq->iavf_tsq = tsq;
+
+               if (rte_mbuf_dyn_tx_timestamp_register(&tsq->ts_offset,
+                                                      &tsq->ts_flag)) {
+                       PMD_INIT_LOG(ERR, "Cannot register Tx mbuf field/flag 
for timestamp");
+                       iavf_dev_tx_queue_release(dev, queue_idx);
+                       dev->data->tx_queues[queue_idx] = NULL;
+                       return -EINVAL;
+               }
+
+               /* Extra descriptors are needed to handle ring wrap-around. */
+               tsq->nb_ts_desc = nb_desc + VIRTCHNL_MAX_TXTIME_FETCH_TX_DESC;
+               ts_ring_size = sizeof(struct iavf_ts_desc) * tsq->nb_ts_desc;
+               ts_ring_size = RTE_ALIGN(ts_ring_size, IAVF_DMA_MEM_ALIGN);
+               ts_mz = rte_eth_dma_zone_reserve(dev, "iavf_tstamp_ring", 
queue_idx,
+                                                ts_ring_size, 
IAVF_RING_BASE_ALIGN,
+                                                socket_id);
+               if (!ts_mz) {
+                       PMD_INIT_LOG(ERR, "Failed to reserve DMA memory for Tx 
timestamp ring");
+                       iavf_dev_tx_queue_release(dev, queue_idx);
+                       dev->data->tx_queues[queue_idx] = NULL;
+                       return -ENOMEM;
+               }
+               tsq->ts_mz = ts_mz;
+               tsq->ts_ring = ts_mz->addr;
+               tsq->ts_tail = 0;
+
+               /* Tx Time uses a dedicated timestamp-ring doorbell register. 
The
+                * normal qtx_tail is kept so queue init writes the standard 
tail;
+                * the TS doorbell is only rung from the xmit path.
+                */
+               tsq->tail = hw->hw_addr + 
IAVF_GLQTX_TXTIME_DBELL_LSB(queue_idx);
+       }
+
        if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_QOS &&
            vf->tm_conf.committed) {
                int tc;
@@ -956,6 +1016,16 @@ iavf_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t 
tx_queue_id)
 
        txq = dev->data->tx_queues[tx_queue_id];
 
+       /* For a Tx Time queue, start from a clean timestamp ring and tail so a
+        * stale state left by a previous, non-drained run is not fetched by
+        * hardware once the queue is re-enabled.
+        */
+       if (txq->iavf_tsq != NULL) {
+               memset((void *)(uintptr_t)txq->iavf_tsq->ts_ring, 0,
+                      txq->iavf_tsq->nb_ts_desc * sizeof(struct iavf_ts_desc));
+               txq->iavf_tsq->ts_tail = 0;
+       }
+
        /* Init the RX tail register. */
        IAVF_PCI_REG_WRITE(txq->qtx_tail, 0);
        IAVF_WRITE_FLUSH(hw);
@@ -1068,6 +1138,11 @@ iavf_dev_tx_queue_release(struct rte_eth_dev *dev, 
uint16_t qid)
        ci_txq_release_all_mbufs(q, q->use_ctx);
        rte_free(q->sw_ring);
        rte_free(q->rs_last_id);
+       if (q->iavf_tsq) {
+               rte_memzone_free(q->iavf_tsq->ts_mz);
+               rte_free(q->iavf_tsq);
+               q->iavf_tsq = NULL;
+       }
        rte_memzone_free(q->mz);
        rte_free(q);
 }
@@ -2591,6 +2666,62 @@ iavf_get_context_desc(uint64_t ol_flags, const struct 
rte_mbuf *mbuf,
        return 1; /* One context descriptor needed */
 }
 
+/* Tx Time (SO_TXTIME / ETF) timestamp-queue callbacks for ci_xmit_pkts */
+static uint16_t
+iavf_get_ts_tail(struct ci_tx_queue *txq)
+{
+       return txq->iavf_tsq->ts_tail;
+}
+
+static uint16_t
+iavf_write_ts_desc(struct ci_tx_queue *txq, struct rte_mbuf *tx_pkt,
+                  uint16_t tx_id, uint16_t ts_id)
+{
+       struct iavf_txtime *tsq = txq->iavf_tsq;
+       uint64_t txtime = *RTE_MBUF_DYNFIELD(tx_pkt, tsq->ts_offset, uint64_t 
*);
+       const uint32_t desc_tx_id = (tx_id == 0) ? txq->nb_tx_desc : tx_id;
+       uint32_t tstamp = 0;
+       rte_le32_t ts_desc;
+
+       /* Timestamp 0 means not paced: send unscheduled and long late packets 
now */
+       if ((tx_pkt->ol_flags & tsq->ts_flag) &&
+           (tsq->now == 0 || txtime + IAVF_TXTIME_HORIZON_NS >= tsq->now)) {
+               /* The clock estimate is coarse, only keep far times in the 
window */
+               if (tsq->now != 0 && txtime > tsq->now + IAVF_TXTIME_HORIZON_NS)
+                       txtime = tsq->now + IAVF_TXTIME_HORIZON_NS;
+               /* The hardware compares against the free running PHC, not ns 
in second */
+               tstamp = (uint32_t)((txtime + IAVF_TXTIME_ROUND_NS) >>
+                                   IAVF_TXTIME_RESOLUTION_128NS);
+               tstamp &= IAVF_TXTIME_STAMP_M >> IAVF_TXTIME_STAMP_S;
+               if (tstamp == 0)
+                       tstamp = 1;
+       }
+       ts_desc = rte_cpu_to_le_32((desc_tx_id & IAVF_TXTIME_TX_DESC_IDX_M) |
+                       ((tstamp << IAVF_TXTIME_STAMP_S) & 
IAVF_TXTIME_STAMP_M));
+
+       tsq->ts_ring[ts_id].tx_desc_idx_tstamp = ts_desc;
+       ts_id++;
+
+       /* To prevent an MDD, when wrapping the timestamp ring create additional
+        * TS descriptors equal to the number of fetch TS descriptors. HW merges
+        * TS descriptors with the same timestamp value into a single 
descriptor.
+        */
+       if (ts_id == tsq->nb_ts_desc) {
+               uint16_t fetch = tsq->nb_ts_desc - txq->nb_tx_desc;
+               ts_id = 0;
+               for (; ts_id < fetch; ts_id++)
+                       tsq->ts_ring[ts_id].tx_desc_idx_tstamp = ts_desc;
+       }
+       return ts_id;
+}
+
+static void
+iavf_write_ts_tail(struct ci_tx_queue *txq, uint16_t ts_id)
+{
+       IAVF_PCI_REG_WRITE(txq->iavf_tsq->tail, ts_id);
+       txq->iavf_tsq->ts_tail = ts_id;
+}
+
 /* TX function */
 uint16_t
 iavf_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
@@ -2605,7 +2736,19 @@ iavf_xmit_pkts(void *tx_queue, struct rte_mbuf 
**tx_pkts, uint16_t nb_pkts)
                .calc_segment_len = iavf_calc_ipsec_segment_len,
        };
 
-       /* IAVF does not support timestamp queues, so pass NULL for ts_fns */
+       /* If Tx Time offload is enabled, drive the timestamp ring doorbell. */
+       if (txq->iavf_tsq != NULL && txq->iavf_tsq->ts_flag > 0) {
+               const struct ci_timestamp_queue_fns ts_fns = {
+                       .get_ts_tail = iavf_get_ts_tail,
+                       .write_ts_desc = iavf_write_ts_desc,
+                       .write_ts_tail = iavf_write_ts_tail,
+               };
+
+               txq->iavf_tsq->now = 
iavf_txtime_clock_now(txq->iavf_vsi->adapter);
+               return ci_xmit_pkts(txq, tx_pkts, nb_pkts, vlan_pos, vlan_pos,
+                                   iavf_get_context_desc, &ipsec_ops, &ts_fns);
+       }
+
        return ci_xmit_pkts(txq, tx_pkts, nb_pkts, vlan_pos, vlan_pos,
                            iavf_get_context_desc, &ipsec_ops, NULL);
 }
diff --git a/drivers/net/intel/iavf/iavf_rxtx.h 
b/drivers/net/intel/iavf/iavf_rxtx.h
index 812d7e23ee..5efa920974 100644
--- a/drivers/net/intel/iavf/iavf_rxtx.h
+++ b/drivers/net/intel/iavf/iavf_rxtx.h
@@ -21,6 +21,28 @@
 /* Base address of the HW descriptor ring should be 128B aligned. */
 #define IAVF_RING_BASE_ALIGN      128
 
+/* Tx Time (Earliest TxTime First / SO_TXTIME) offload definitions */
+#define IAVF_TXTIME_TX_DESC_IDX_M      RTE_GENMASK32(12, 0)
+#define IAVF_TXTIME_STAMP_S            13
+#define IAVF_TXTIME_STAMP_M            RTE_GENMASK32(31, 13)
+/* Tx time ring timestamp resolution is 128ns */
+#define IAVF_TXTIME_RESOLUTION_128NS   7
+#define IAVF_TXTIME_ROUND_NS           (1 << (IAVF_TXTIME_RESOLUTION_128NS - 
1))
+/* Furthest launch time accepted, inside the ~33.5 ms future half of the 
window */
+#define IAVF_TXTIME_HORIZON_NS         30000000ULL
+
+/* Structure associated with a Tx Time based queue */
+struct iavf_txtime {
+       volatile struct iavf_ts_desc *ts_ring; /* Tx time ring virtual address 
*/
+       volatile uint8_t *tail; /* timestamp ring doorbell register address */
+       uint16_t nb_ts_desc; /* number of Tx Time descriptors */
+       uint16_t ts_tail; /* current value of tail register */
+       int ts_offset; /* dynamic mbuf Tx timestamp field offset */
+       uint64_t ts_flag; /* dynamic mbuf Tx timestamp flag */
+       uint64_t now; /* estimated PHC time at the start of a Tx burst, 0 if 
unknown */
+       const struct rte_memzone *ts_mz;
+};
+
 /* used for Rx Bulk Allocate */
 #define IAVF_RX_MAX_BURST         CI_RX_MAX_BURST
 
@@ -51,6 +73,7 @@
                RTE_ETH_TX_OFFLOAD_MULTI_SEGS |         \
                RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE |     \
                RTE_ETH_TX_OFFLOAD_SECURITY |           \
+               RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP |  \
                RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM)
 
 /* basic scalar path */
diff --git a/drivers/net/intel/iavf/iavf_vchnl.c 
b/drivers/net/intel/iavf/iavf_vchnl.c
index 64ca93c20b..57a6644896 100644
--- a/drivers/net/intel/iavf/iavf_vchnl.c
+++ b/drivers/net/intel/iavf/iavf_vchnl.c
@@ -856,6 +856,7 @@ iavf_get_vf_resource(struct iavf_adapter *adapter)
                VIRTCHNL_VF_LARGE_NUM_QPAIRS |
                VIRTCHNL_VF_OFFLOAD_QOS |
                VIRTCHNL_VF_OFFLOAD_INLINE_IPSEC_CRYPTO |
+               VIRTCHNL_VF_CAPS2 |
                VIRTCHNL_VF_CAP_PTP;
 
        args.in_args = (uint8_t *)&caps;
@@ -1489,6 +1490,17 @@ iavf_configure_queue_pair(struct iavf_adapter *adapter,
        if (q_idx < adapter->dev_data->nb_tx_queues) {
                vc_qp->txq.ring_len = txq[q_idx]->nb_tx_desc;
                vc_qp->txq.dma_ring_addr = txq[q_idx]->tx_ring_dma;
+
+               /* Tx Time (SO_TXTIME / ETF) offload: pass the timestamp ring
+                * address and request the PF to enable TXTIME on this queue.
+                * On teardown stop_txtime is set so the queue is reconfigured
+                * without the flag, releasing the PF scheduler context.
+                */
+               if (txq[q_idx]->iavf_tsq != NULL && !vf->stop_txtime) {
+                       vc_qp->txq.flags |= VIRTCHNL_TXQ_FLAG_TXTIME_ENABLED;
+                       vc_qp->txq.dma_txtime_addr =
+                               txq[q_idx]->iavf_tsq->ts_mz->iova;
+               }
        }
 
        /* is this rxq active? */
@@ -2568,6 +2580,51 @@ iavf_get_ptp_cap(struct iavf_adapter *adapter)
        return 0;
 }
 
+int
+iavf_get_vf_caps2(struct iavf_adapter *adapter)
+{
+       struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
+       uint8_t msg_buf[IAVF_AQ_BUF_SZ] = {0};
+       /* flags_len words cover all bits up to VIRTCHNL_VF_CAPS_MAX */
+       const uint32_t flags_len =
+               (VIRTCHNL_VF_CAPS_MAX + 31) / 32;
+       uint8_t req_buf[sizeof(struct virtchnl_vf_caps2) +
+                       ((VIRTCHNL_VF_CAPS_MAX + 31) / 32) * sizeof(uint32_t)] 
= {0};
+       struct virtchnl_vf_caps2 *caps2 = (struct virtchnl_vf_caps2 *)req_buf;
+       struct virtchnl_vf_caps2 *resp;
+       struct iavf_cmd_info args;
+       uint32_t i;
+       int err;
+
+       /* Request the already-granted base caps plus the extended TXTIME bit. 
*/
+       caps2->flags_len = flags_len;
+       caps2->vf_cap_flags[0] = vf->vf_res->vf_cap_flags;
+       caps2->vf_cap_flags[VIRTCHNL_VF_OFFLOAD_TXTIME / 32] |=
+               RTE_BIT32(VIRTCHNL_VF_OFFLOAD_TXTIME % 32);
+
+       args.ops = VIRTCHNL_OP_GET_VF_CAPS2;
+       args.in_args = req_buf;
+       args.in_args_size = sizeof(struct virtchnl_vf_caps2) +
+                           flags_len * sizeof(uint32_t);
+       args.out_buffer = msg_buf;
+       args.out_size = IAVF_AQ_BUF_SZ;
+
+       err = iavf_execute_vf_cmd_safe(adapter, &args);
+       if (err) {
+               PMD_DRV_LOG(ERR,
+                           "Failed to execute command of 
VIRTCHNL_OP_GET_VF_CAPS2");
+               return err;
+       }
+
+       /* The PF replies with the bitmap of enabled extended capabilities. */
+       resp = (struct virtchnl_vf_caps2 *)args.out_buffer;
+       vf->vf_cap_flags2 = 0;
+       for (i = 0; i < resp->flags_len && i < 2; i++)
+               vf->vf_cap_flags2 |= (uint64_t)resp->vf_cap_flags[i] << (32 * 
i);
+
+       return 0;
+}
+
 int
 iavf_phc_get_time(struct iavf_adapter *adapter, uint64_t *time)
 {
@@ -2575,6 +2632,7 @@ iavf_phc_get_time(struct iavf_adapter *adapter, uint64_t 
*time)
        uint8_t msg_buf[IAVF_AQ_BUF_SZ] = {0};
        struct virtchnl_phc_time phc_time;
        struct iavf_cmd_info args;
+       uint64_t tsc_start;
        int err = 0;
 
        args.ops = VIRTCHNL_OP_1588_PTP_GET_TIME;
@@ -2584,6 +2642,7 @@ iavf_phc_get_time(struct iavf_adapter *adapter, uint64_t 
*time)
        args.out_size = IAVF_AQ_BUF_SZ;
 
        rte_spinlock_lock(&vf->phc_time_aq_lock);
+       tsc_start = rte_rdtsc();
        err = iavf_execute_vf_cmd_safe(adapter, &args);
        if (err) {
                PMD_DRV_LOG(ERR,
@@ -2592,6 +2651,12 @@ iavf_phc_get_time(struct iavf_adapter *adapter, uint64_t 
*time)
        }
        *time = ((struct virtchnl_phc_time *)args.out_buffer)->time;
 
+       /* anchor the Tx time clock estimate to the middle of the request */
+       rte_seqlock_write_lock(&adapter->txtime_clk.lock);
+       adapter->txtime_clk.phc = *time;
+       adapter->txtime_clk.tsc = tsc_start + (rte_rdtsc() - tsc_start) / 2;
+       rte_seqlock_write_unlock(&adapter->txtime_clk.lock);
+
 out:
        rte_spinlock_unlock(&vf->phc_time_aq_lock);
        return err;
-- 
2.43.0

Reply via email to