1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2021-2021 Hisilicon Limited.
5 #include <ethdev_pci.h>
9 #include "hns3_ethdev.h"
10 #include "hns3_regs.h"
11 #include "hns3_logs.h"
13 uint64_t hns3_timestamp_rx_dynflag;
14 int hns3_timestamp_dynfield_offset = -1;
17 hns3_mbuf_dyn_rx_timestamp_register(struct rte_eth_dev *dev,
18 struct rte_eth_conf *conf)
20 struct hns3_adapter *hns = dev->data->dev_private;
21 struct hns3_hw *hw = &hns->hw;
24 if (!(conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TIMESTAMP))
27 ret = rte_mbuf_dyn_rx_timestamp_register
28 (&hns3_timestamp_dynfield_offset,
29 &hns3_timestamp_rx_dynflag);
32 "failed to register Rx timestamp field/flag");
40 hns3_ptp_int_en(struct hns3_hw *hw, bool en)
42 struct hns3_ptp_int_cmd *req;
43 struct hns3_cmd_desc desc;
46 req = (struct hns3_ptp_int_cmd *)desc.data;
47 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PTP_INT_EN, false);
48 req->int_en = en ? 1 : 0;
50 ret = hns3_cmd_send(hw, &desc, 1);
53 "failed to %s ptp interrupt, ret = %d\n",
54 en ? "enable" : "disable", ret);
60 hns3_ptp_init(struct hns3_hw *hw)
64 if (!hns3_dev_get_support(hw, PTP))
67 ret = hns3_ptp_int_en(hw, true);
72 hns3_write_dev(hw, HNS3_CFG_TIME_CYC_EN, 1);
78 hns3_timesync_configure(struct hns3_adapter *hns, bool en)
80 struct hns3_ptp_mode_cfg_cmd *req;
81 struct hns3_hw *hw = &hns->hw;
82 struct hns3_pf *pf = &hns->pf;
83 struct hns3_cmd_desc desc;
87 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PTP_MODE, false);
89 req = (struct hns3_ptp_mode_cfg_cmd *)desc.data;
92 hns3_set_bit(req->enable, HNS3_PTP_ENABLE_B, val);
93 hns3_set_bit(req->enable, HNS3_PTP_TX_ENABLE_B, val);
94 hns3_set_bit(req->enable, HNS3_PTP_RX_ENABLE_B, val);
97 hns3_set_field(req->ptp_type, HNS3_PTP_TYPE_M, HNS3_PTP_TYPE_S,
99 hns3_set_field(req->v2_message_type_1, HNS3_PTP_MESSAGE_TYPE_M,
100 HNS3_PTP_MESSAGE_TYPE_S, ALL_PTP_V2_TYPE);
103 ret = hns3_cmd_send(hw, &desc, 1);
105 hns3_err(hw, "configure PTP time failed, en = %d, ret = %d",
116 hns3_timesync_enable(struct rte_eth_dev *dev)
118 struct hns3_adapter *hns = dev->data->dev_private;
119 struct hns3_hw *hw = &hns->hw;
120 struct hns3_pf *pf = &hns->pf;
123 if (!hns3_dev_get_support(hw, PTP))
128 hns3_warn(hw, "note: please ensure Rx/Tx burst mode is simple or common when enabling PTP!");
130 rte_spinlock_lock(&hw->lock);
131 ret = hns3_timesync_configure(hns, true);
132 rte_spinlock_unlock(&hw->lock);
137 hns3_timesync_disable(struct rte_eth_dev *dev)
139 struct hns3_adapter *hns = dev->data->dev_private;
140 struct hns3_hw *hw = &hns->hw;
141 struct hns3_pf *pf = &hns->pf;
144 if (!hns3_dev_get_support(hw, PTP))
150 rte_spinlock_lock(&hw->lock);
151 ret = hns3_timesync_configure(hns, false);
152 rte_spinlock_unlock(&hw->lock);
158 hns3_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
159 struct timespec *timestamp,
160 uint32_t flags __rte_unused)
162 #define TIME_RX_STAMP_NS_MASK 0x3FFFFFFF
163 struct hns3_adapter *hns = dev->data->dev_private;
164 struct hns3_hw *hw = &hns->hw;
165 struct hns3_pf *pf = &hns->pf;
168 if (!hns3_dev_get_support(hw, PTP))
171 ns = pf->rx_timestamp & TIME_RX_STAMP_NS_MASK;
172 sec = upper_32_bits(pf->rx_timestamp);
174 ns += sec * NSEC_PER_SEC;
175 *timestamp = rte_ns_to_timespec(ns);
181 hns3_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
182 struct timespec *timestamp)
184 #define TIME_TX_STAMP_NS_MASK 0x3FFFFFFF
185 #define TIME_TX_STAMP_VALID 24
186 #define TIME_TX_STAMP_CNT_MASK 0x7
187 struct hns3_adapter *hns = dev->data->dev_private;
188 struct hns3_hw *hw = &hns->hw;
194 if (!hns3_dev_get_support(hw, PTP))
197 ts_cnt = hns3_read_dev(hw, HNS3_TX_1588_BACK_TSP_CNT) &
198 TIME_TX_STAMP_CNT_MASK;
202 ns = hns3_read_dev(hw, HNS3_TX_1588_TSP_BACK_0) & TIME_TX_STAMP_NS_MASK;
203 sec = hns3_read_dev(hw, HNS3_TX_1588_TSP_BACK_1);
204 tmp = hns3_read_dev(hw, HNS3_TX_1588_TSP_BACK_2) & 0xFFFF;
205 sec = (tmp << 32) | sec;
207 ns += sec * NSEC_PER_SEC;
209 *timestamp = rte_ns_to_timespec(ns);
211 /* Clear current timestamp hardware stores */
212 hns3_read_dev(hw, HNS3_TX_1588_SEQID_BACK);
218 hns3_timesync_read_time(struct rte_eth_dev *dev, struct timespec *ts)
220 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
223 if (!hns3_dev_get_support(hw, PTP))
226 sec = hns3_read_dev(hw, HNS3_CURR_TIME_OUT_L);
227 sec |= (uint64_t)(hns3_read_dev(hw, HNS3_CURR_TIME_OUT_H) & 0xFFFF)
230 ns = hns3_read_dev(hw, HNS3_CURR_TIME_OUT_NS);
231 ns += sec * NSEC_PER_SEC;
232 *ts = rte_ns_to_timespec(ns);
238 hns3_timesync_write_time(struct rte_eth_dev *dev, const struct timespec *ts)
240 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
241 uint64_t sec = ts->tv_sec;
242 uint64_t ns = ts->tv_nsec;
244 if (!hns3_dev_get_support(hw, PTP))
247 /* Set the timecounters to a new value. */
248 hns3_write_dev(hw, HNS3_CFG_TIME_SYNC_H, upper_32_bits(sec));
249 hns3_write_dev(hw, HNS3_CFG_TIME_SYNC_M, lower_32_bits(sec));
250 hns3_write_dev(hw, HNS3_CFG_TIME_SYNC_L, lower_32_bits(ns));
251 hns3_write_dev(hw, HNS3_CFG_TIME_SYNC_RDY, 1);
257 hns3_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta)
259 #define TIME_SYNC_L_MASK 0x7FFFFFFF
260 #define SYMBOL_BIT_OFFSET 31
261 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
262 struct timespec cur_time;
265 if (!hns3_dev_get_support(hw, PTP))
268 (void)hns3_timesync_read_time(dev, &cur_time);
269 ns = rte_timespec_to_ns((const struct timespec *)&cur_time);
270 cur_time = rte_ns_to_timespec(ns + delta);
271 (void)hns3_timesync_write_time(dev, (const struct timespec *)&cur_time);
277 hns3_restore_ptp(struct hns3_adapter *hns)
279 struct hns3_pf *pf = &hns->pf;
280 struct hns3_hw *hw = &hns->hw;
281 bool en = pf->ptp_enable;
284 if (!hns3_dev_get_support(hw, PTP))
287 ret = hns3_timesync_configure(hns, en);
289 hns3_err(hw, "restore PTP enable state(%d) failed, ret = %d",