18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2013 - 2018 Intel Corporation. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "i40e.h"
58c2ecf20Sopenharmony_ci#include <linux/ptp_classify.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/* The XL710 timesync is very much like Intel's 82599 design when it comes to
88c2ecf20Sopenharmony_ci * the fundamental clock design. However, the clock operations are much simpler
98c2ecf20Sopenharmony_ci * in the XL710 because the device supports a full 64 bits of nanoseconds.
108c2ecf20Sopenharmony_ci * Because the field is so wide, we can forgo the cycle counter and just
118c2ecf20Sopenharmony_ci * operate with the nanosecond field directly without fear of overflow.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Much like the 82599, the update period is dependent upon the link speed:
148c2ecf20Sopenharmony_ci * At 40Gb, 25Gb, or no link, the period is 1.6ns.
158c2ecf20Sopenharmony_ci * At 10Gb or 5Gb link, the period is multiplied by 2. (3.2ns)
168c2ecf20Sopenharmony_ci * At 1Gb link, the period is multiplied by 20. (32ns)
178c2ecf20Sopenharmony_ci * 1588 functionality is not supported at 100Mbps.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci#define I40E_PTP_40GB_INCVAL		0x0199999999ULL
208c2ecf20Sopenharmony_ci#define I40E_PTP_10GB_INCVAL_MULT	2
218c2ecf20Sopenharmony_ci#define I40E_PTP_5GB_INCVAL_MULT	2
228c2ecf20Sopenharmony_ci#define I40E_PTP_1GB_INCVAL_MULT	20
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define I40E_PRTTSYN_CTL1_TSYNTYPE_V1  BIT(I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
258c2ecf20Sopenharmony_ci#define I40E_PRTTSYN_CTL1_TSYNTYPE_V2  (2 << \
268c2ecf20Sopenharmony_ci					I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/**
298c2ecf20Sopenharmony_ci * i40e_ptp_read - Read the PHC time from the device
308c2ecf20Sopenharmony_ci * @pf: Board private structure
318c2ecf20Sopenharmony_ci * @ts: timespec structure to hold the current time value
328c2ecf20Sopenharmony_ci * @sts: structure to hold the system time before and after reading the PHC
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * This function reads the PRTTSYN_TIME registers and stores them in a
358c2ecf20Sopenharmony_ci * timespec. However, since the registers are 64 bits of nanoseconds, we must
368c2ecf20Sopenharmony_ci * convert the result to a timespec before we can return.
378c2ecf20Sopenharmony_ci **/
388c2ecf20Sopenharmony_cistatic void i40e_ptp_read(struct i40e_pf *pf, struct timespec64 *ts,
398c2ecf20Sopenharmony_ci			  struct ptp_system_timestamp *sts)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
428c2ecf20Sopenharmony_ci	u32 hi, lo;
438c2ecf20Sopenharmony_ci	u64 ns;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* The timer latches on the lowest register read. */
468c2ecf20Sopenharmony_ci	ptp_read_system_prets(sts);
478c2ecf20Sopenharmony_ci	lo = rd32(hw, I40E_PRTTSYN_TIME_L);
488c2ecf20Sopenharmony_ci	ptp_read_system_postts(sts);
498c2ecf20Sopenharmony_ci	hi = rd32(hw, I40E_PRTTSYN_TIME_H);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	ns = (((u64)hi) << 32) | lo;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	*ts = ns_to_timespec64(ns);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/**
578c2ecf20Sopenharmony_ci * i40e_ptp_write - Write the PHC time to the device
588c2ecf20Sopenharmony_ci * @pf: Board private structure
598c2ecf20Sopenharmony_ci * @ts: timespec structure that holds the new time value
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * This function writes the PRTTSYN_TIME registers with the user value. Since
628c2ecf20Sopenharmony_ci * we receive a timespec from the stack, we must convert that timespec into
638c2ecf20Sopenharmony_ci * nanoseconds before programming the registers.
648c2ecf20Sopenharmony_ci **/
658c2ecf20Sopenharmony_cistatic void i40e_ptp_write(struct i40e_pf *pf, const struct timespec64 *ts)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
688c2ecf20Sopenharmony_ci	u64 ns = timespec64_to_ns(ts);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/* The timer will not update until the high register is written, so
718c2ecf20Sopenharmony_ci	 * write the low register first.
728c2ecf20Sopenharmony_ci	 */
738c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_TIME_L, ns & 0xFFFFFFFF);
748c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_TIME_H, ns >> 32);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/**
788c2ecf20Sopenharmony_ci * i40e_ptp_convert_to_hwtstamp - Convert device clock to system time
798c2ecf20Sopenharmony_ci * @hwtstamps: Timestamp structure to update
808c2ecf20Sopenharmony_ci * @timestamp: Timestamp from the hardware
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * We need to convert the NIC clock value into a hwtstamp which can be used by
838c2ecf20Sopenharmony_ci * the upper level timestamping functions. Since the timestamp is simply a 64-
848c2ecf20Sopenharmony_ci * bit nanosecond value, we can call ns_to_ktime directly to handle this.
858c2ecf20Sopenharmony_ci **/
868c2ecf20Sopenharmony_cistatic void i40e_ptp_convert_to_hwtstamp(struct skb_shared_hwtstamps *hwtstamps,
878c2ecf20Sopenharmony_ci					 u64 timestamp)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	memset(hwtstamps, 0, sizeof(*hwtstamps));
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	hwtstamps->hwtstamp = ns_to_ktime(timestamp);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/**
958c2ecf20Sopenharmony_ci * i40e_ptp_adjfreq - Adjust the PHC frequency
968c2ecf20Sopenharmony_ci * @ptp: The PTP clock structure
978c2ecf20Sopenharmony_ci * @ppb: Parts per billion adjustment from the base
988c2ecf20Sopenharmony_ci *
998c2ecf20Sopenharmony_ci * Adjust the frequency of the PHC by the indicated parts per billion from the
1008c2ecf20Sopenharmony_ci * base frequency.
1018c2ecf20Sopenharmony_ci **/
1028c2ecf20Sopenharmony_cistatic int i40e_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
1058c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
1068c2ecf20Sopenharmony_ci	u64 adj, freq, diff;
1078c2ecf20Sopenharmony_ci	int neg_adj = 0;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (ppb < 0) {
1108c2ecf20Sopenharmony_ci		neg_adj = 1;
1118c2ecf20Sopenharmony_ci		ppb = -ppb;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	freq = I40E_PTP_40GB_INCVAL;
1158c2ecf20Sopenharmony_ci	freq *= ppb;
1168c2ecf20Sopenharmony_ci	diff = div_u64(freq, 1000000000ULL);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (neg_adj)
1198c2ecf20Sopenharmony_ci		adj = I40E_PTP_40GB_INCVAL - diff;
1208c2ecf20Sopenharmony_ci	else
1218c2ecf20Sopenharmony_ci		adj = I40E_PTP_40GB_INCVAL + diff;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* At some link speeds, the base incval is so large that directly
1248c2ecf20Sopenharmony_ci	 * multiplying by ppb would result in arithmetic overflow even when
1258c2ecf20Sopenharmony_ci	 * using a u64. Avoid this by instead calculating the new incval
1268c2ecf20Sopenharmony_ci	 * always in terms of the 40GbE clock rate and then multiplying by the
1278c2ecf20Sopenharmony_ci	 * link speed factor afterwards. This does result in slightly lower
1288c2ecf20Sopenharmony_ci	 * precision at lower link speeds, but it is fairly minor.
1298c2ecf20Sopenharmony_ci	 */
1308c2ecf20Sopenharmony_ci	smp_mb(); /* Force any pending update before accessing. */
1318c2ecf20Sopenharmony_ci	adj *= READ_ONCE(pf->ptp_adj_mult);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
1348c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/**
1408c2ecf20Sopenharmony_ci * i40e_ptp_adjtime - Adjust the PHC time
1418c2ecf20Sopenharmony_ci * @ptp: The PTP clock structure
1428c2ecf20Sopenharmony_ci * @delta: Offset in nanoseconds to adjust the PHC time by
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * Adjust the current clock time by a delta specified in nanoseconds.
1458c2ecf20Sopenharmony_ci **/
1468c2ecf20Sopenharmony_cistatic int i40e_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
1498c2ecf20Sopenharmony_ci	struct timespec64 now, then;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	then = ns_to_timespec64(delta);
1528c2ecf20Sopenharmony_ci	mutex_lock(&pf->tmreg_lock);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	i40e_ptp_read(pf, &now, NULL);
1558c2ecf20Sopenharmony_ci	now = timespec64_add(now, then);
1568c2ecf20Sopenharmony_ci	i40e_ptp_write(pf, (const struct timespec64 *)&now);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	mutex_unlock(&pf->tmreg_lock);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return 0;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/**
1648c2ecf20Sopenharmony_ci * i40e_ptp_gettimex - Get the time of the PHC
1658c2ecf20Sopenharmony_ci * @ptp: The PTP clock structure
1668c2ecf20Sopenharmony_ci * @ts: timespec structure to hold the current time value
1678c2ecf20Sopenharmony_ci * @sts: structure to hold the system time before and after reading the PHC
1688c2ecf20Sopenharmony_ci *
1698c2ecf20Sopenharmony_ci * Read the device clock and return the correct value on ns, after converting it
1708c2ecf20Sopenharmony_ci * into a timespec struct.
1718c2ecf20Sopenharmony_ci **/
1728c2ecf20Sopenharmony_cistatic int i40e_ptp_gettimex(struct ptp_clock_info *ptp, struct timespec64 *ts,
1738c2ecf20Sopenharmony_ci			     struct ptp_system_timestamp *sts)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	mutex_lock(&pf->tmreg_lock);
1788c2ecf20Sopenharmony_ci	i40e_ptp_read(pf, ts, sts);
1798c2ecf20Sopenharmony_ci	mutex_unlock(&pf->tmreg_lock);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return 0;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/**
1858c2ecf20Sopenharmony_ci * i40e_ptp_settime - Set the time of the PHC
1868c2ecf20Sopenharmony_ci * @ptp: The PTP clock structure
1878c2ecf20Sopenharmony_ci * @ts: timespec structure that holds the new time value
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * Set the device clock to the user input value. The conversion from timespec
1908c2ecf20Sopenharmony_ci * to ns happens in the write function.
1918c2ecf20Sopenharmony_ci **/
1928c2ecf20Sopenharmony_cistatic int i40e_ptp_settime(struct ptp_clock_info *ptp,
1938c2ecf20Sopenharmony_ci			    const struct timespec64 *ts)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	mutex_lock(&pf->tmreg_lock);
1988c2ecf20Sopenharmony_ci	i40e_ptp_write(pf, ts);
1998c2ecf20Sopenharmony_ci	mutex_unlock(&pf->tmreg_lock);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return 0;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/**
2058c2ecf20Sopenharmony_ci * i40e_ptp_feature_enable - Enable/disable ancillary features of the PHC subsystem
2068c2ecf20Sopenharmony_ci * @ptp: The PTP clock structure
2078c2ecf20Sopenharmony_ci * @rq: The requested feature to change
2088c2ecf20Sopenharmony_ci * @on: Enable/disable flag
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci * The XL710 does not support any of the ancillary features of the PHC
2118c2ecf20Sopenharmony_ci * subsystem, so this function may just return.
2128c2ecf20Sopenharmony_ci **/
2138c2ecf20Sopenharmony_cistatic int i40e_ptp_feature_enable(struct ptp_clock_info *ptp,
2148c2ecf20Sopenharmony_ci				   struct ptp_clock_request *rq, int on)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/**
2208c2ecf20Sopenharmony_ci * i40e_ptp_update_latch_events - Read I40E_PRTTSYN_STAT_1 and latch events
2218c2ecf20Sopenharmony_ci * @pf: the PF data structure
2228c2ecf20Sopenharmony_ci *
2238c2ecf20Sopenharmony_ci * This function reads I40E_PRTTSYN_STAT_1 and updates the corresponding timers
2248c2ecf20Sopenharmony_ci * for noticed latch events. This allows the driver to keep track of the first
2258c2ecf20Sopenharmony_ci * time a latch event was noticed which will be used to help clear out Rx
2268c2ecf20Sopenharmony_ci * timestamps for packets that got dropped or lost.
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci * This function will return the current value of I40E_PRTTSYN_STAT_1 and is
2298c2ecf20Sopenharmony_ci * expected to be called only while under the ptp_rx_lock.
2308c2ecf20Sopenharmony_ci **/
2318c2ecf20Sopenharmony_cistatic u32 i40e_ptp_get_rx_events(struct i40e_pf *pf)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
2348c2ecf20Sopenharmony_ci	u32 prttsyn_stat, new_latch_events;
2358c2ecf20Sopenharmony_ci	int  i;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_1);
2388c2ecf20Sopenharmony_ci	new_latch_events = prttsyn_stat & ~pf->latch_event_flags;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/* Update the jiffies time for any newly latched timestamp. This
2418c2ecf20Sopenharmony_ci	 * ensures that we store the time that we first discovered a timestamp
2428c2ecf20Sopenharmony_ci	 * was latched by the hardware. The service task will later determine
2438c2ecf20Sopenharmony_ci	 * if we should free the latch and drop that timestamp should too much
2448c2ecf20Sopenharmony_ci	 * time pass. This flow ensures that we only update jiffies for new
2458c2ecf20Sopenharmony_ci	 * events latched since the last time we checked, and not all events
2468c2ecf20Sopenharmony_ci	 * currently latched, so that the service task accounting remains
2478c2ecf20Sopenharmony_ci	 * accurate.
2488c2ecf20Sopenharmony_ci	 */
2498c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
2508c2ecf20Sopenharmony_ci		if (new_latch_events & BIT(i))
2518c2ecf20Sopenharmony_ci			pf->latch_events[i] = jiffies;
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/* Finally, we store the current status of the Rx timestamp latches */
2558c2ecf20Sopenharmony_ci	pf->latch_event_flags = prttsyn_stat;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return prttsyn_stat;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/**
2618c2ecf20Sopenharmony_ci * i40e_ptp_rx_hang - Detect error case when Rx timestamp registers are hung
2628c2ecf20Sopenharmony_ci * @pf: The PF private data structure
2638c2ecf20Sopenharmony_ci *
2648c2ecf20Sopenharmony_ci * This watchdog task is scheduled to detect error case where hardware has
2658c2ecf20Sopenharmony_ci * dropped an Rx packet that was timestamped when the ring is full. The
2668c2ecf20Sopenharmony_ci * particular error is rare but leaves the device in a state unable to timestamp
2678c2ecf20Sopenharmony_ci * any future packets.
2688c2ecf20Sopenharmony_ci **/
2698c2ecf20Sopenharmony_civoid i40e_ptp_rx_hang(struct i40e_pf *pf)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
2728c2ecf20Sopenharmony_ci	unsigned int i, cleared = 0;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Since we cannot turn off the Rx timestamp logic if the device is
2758c2ecf20Sopenharmony_ci	 * configured for Tx timestamping, we check if Rx timestamping is
2768c2ecf20Sopenharmony_ci	 * configured. We don't want to spuriously warn about Rx timestamp
2778c2ecf20Sopenharmony_ci	 * hangs if we don't care about the timestamps.
2788c2ecf20Sopenharmony_ci	 */
2798c2ecf20Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
2808c2ecf20Sopenharmony_ci		return;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	spin_lock_bh(&pf->ptp_rx_lock);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Update current latch times for Rx events */
2858c2ecf20Sopenharmony_ci	i40e_ptp_get_rx_events(pf);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* Check all the currently latched Rx events and see whether they have
2888c2ecf20Sopenharmony_ci	 * been latched for over a second. It is assumed that any timestamp
2898c2ecf20Sopenharmony_ci	 * should have been cleared within this time, or else it was captured
2908c2ecf20Sopenharmony_ci	 * for a dropped frame that the driver never received. Thus, we will
2918c2ecf20Sopenharmony_ci	 * clear any timestamp that has been latched for over 1 second.
2928c2ecf20Sopenharmony_ci	 */
2938c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
2948c2ecf20Sopenharmony_ci		if ((pf->latch_event_flags & BIT(i)) &&
2958c2ecf20Sopenharmony_ci		    time_is_before_jiffies(pf->latch_events[i] + HZ)) {
2968c2ecf20Sopenharmony_ci			rd32(hw, I40E_PRTTSYN_RXTIME_H(i));
2978c2ecf20Sopenharmony_ci			pf->latch_event_flags &= ~BIT(i);
2988c2ecf20Sopenharmony_ci			cleared++;
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	spin_unlock_bh(&pf->ptp_rx_lock);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* Log a warning if more than 2 timestamps got dropped in the same
3058c2ecf20Sopenharmony_ci	 * check. We don't want to warn about all drops because it can occur
3068c2ecf20Sopenharmony_ci	 * in normal scenarios such as PTP frames on multicast addresses we
3078c2ecf20Sopenharmony_ci	 * aren't listening to. However, administrator should know if this is
3088c2ecf20Sopenharmony_ci	 * the reason packets aren't receiving timestamps.
3098c2ecf20Sopenharmony_ci	 */
3108c2ecf20Sopenharmony_ci	if (cleared > 2)
3118c2ecf20Sopenharmony_ci		dev_dbg(&pf->pdev->dev,
3128c2ecf20Sopenharmony_ci			"Dropped %d missed RXTIME timestamp events\n",
3138c2ecf20Sopenharmony_ci			cleared);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/* Finally, update the rx_hwtstamp_cleared counter */
3168c2ecf20Sopenharmony_ci	pf->rx_hwtstamp_cleared += cleared;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci/**
3208c2ecf20Sopenharmony_ci * i40e_ptp_tx_hang - Detect error case when Tx timestamp register is hung
3218c2ecf20Sopenharmony_ci * @pf: The PF private data structure
3228c2ecf20Sopenharmony_ci *
3238c2ecf20Sopenharmony_ci * This watchdog task is run periodically to make sure that we clear the Tx
3248c2ecf20Sopenharmony_ci * timestamp logic if we don't obtain a timestamp in a reasonable amount of
3258c2ecf20Sopenharmony_ci * time. It is unexpected in the normal case but if it occurs it results in
3268c2ecf20Sopenharmony_ci * permanently preventing timestamps of future packets.
3278c2ecf20Sopenharmony_ci **/
3288c2ecf20Sopenharmony_civoid i40e_ptp_tx_hang(struct i40e_pf *pf)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	struct sk_buff *skb;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
3338c2ecf20Sopenharmony_ci		return;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* Nothing to do if we're not already waiting for a timestamp */
3368c2ecf20Sopenharmony_ci	if (!test_bit(__I40E_PTP_TX_IN_PROGRESS, pf->state))
3378c2ecf20Sopenharmony_ci		return;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/* We already have a handler routine which is run when we are notified
3408c2ecf20Sopenharmony_ci	 * of a Tx timestamp in the hardware. If we don't get an interrupt
3418c2ecf20Sopenharmony_ci	 * within a second it is reasonable to assume that we never will.
3428c2ecf20Sopenharmony_ci	 */
3438c2ecf20Sopenharmony_ci	if (time_is_before_jiffies(pf->ptp_tx_start + HZ)) {
3448c2ecf20Sopenharmony_ci		skb = pf->ptp_tx_skb;
3458c2ecf20Sopenharmony_ci		pf->ptp_tx_skb = NULL;
3468c2ecf20Sopenharmony_ci		clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci		/* Free the skb after we clear the bitlock */
3498c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
3508c2ecf20Sopenharmony_ci		pf->tx_hwtstamp_timeouts++;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci/**
3558c2ecf20Sopenharmony_ci * i40e_ptp_tx_hwtstamp - Utility function which returns the Tx timestamp
3568c2ecf20Sopenharmony_ci * @pf: Board private structure
3578c2ecf20Sopenharmony_ci *
3588c2ecf20Sopenharmony_ci * Read the value of the Tx timestamp from the registers, convert it into a
3598c2ecf20Sopenharmony_ci * value consumable by the stack, and store that result into the shhwtstamps
3608c2ecf20Sopenharmony_ci * struct before returning it up the stack.
3618c2ecf20Sopenharmony_ci **/
3628c2ecf20Sopenharmony_civoid i40e_ptp_tx_hwtstamp(struct i40e_pf *pf)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct skb_shared_hwtstamps shhwtstamps;
3658c2ecf20Sopenharmony_ci	struct sk_buff *skb = pf->ptp_tx_skb;
3668c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
3678c2ecf20Sopenharmony_ci	u32 hi, lo;
3688c2ecf20Sopenharmony_ci	u64 ns;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
3718c2ecf20Sopenharmony_ci		return;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/* don't attempt to timestamp if we don't have an skb */
3748c2ecf20Sopenharmony_ci	if (!pf->ptp_tx_skb)
3758c2ecf20Sopenharmony_ci		return;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	lo = rd32(hw, I40E_PRTTSYN_TXTIME_L);
3788c2ecf20Sopenharmony_ci	hi = rd32(hw, I40E_PRTTSYN_TXTIME_H);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	ns = (((u64)hi) << 32) | lo;
3818c2ecf20Sopenharmony_ci	i40e_ptp_convert_to_hwtstamp(&shhwtstamps, ns);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/* Clear the bit lock as soon as possible after reading the register,
3848c2ecf20Sopenharmony_ci	 * and prior to notifying the stack via skb_tstamp_tx(). Otherwise
3858c2ecf20Sopenharmony_ci	 * applications might wake up and attempt to request another transmit
3868c2ecf20Sopenharmony_ci	 * timestamp prior to the bit lock being cleared.
3878c2ecf20Sopenharmony_ci	 */
3888c2ecf20Sopenharmony_ci	pf->ptp_tx_skb = NULL;
3898c2ecf20Sopenharmony_ci	clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	/* Notify the stack and free the skb after we've unlocked */
3928c2ecf20Sopenharmony_ci	skb_tstamp_tx(skb, &shhwtstamps);
3938c2ecf20Sopenharmony_ci	dev_kfree_skb_any(skb);
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/**
3978c2ecf20Sopenharmony_ci * i40e_ptp_rx_hwtstamp - Utility function which checks for an Rx timestamp
3988c2ecf20Sopenharmony_ci * @pf: Board private structure
3998c2ecf20Sopenharmony_ci * @skb: Particular skb to send timestamp with
4008c2ecf20Sopenharmony_ci * @index: Index into the receive timestamp registers for the timestamp
4018c2ecf20Sopenharmony_ci *
4028c2ecf20Sopenharmony_ci * The XL710 receives a notification in the receive descriptor with an offset
4038c2ecf20Sopenharmony_ci * into the set of RXTIME registers where the timestamp is for that skb. This
4048c2ecf20Sopenharmony_ci * function goes and fetches the receive timestamp from that offset, if a valid
4058c2ecf20Sopenharmony_ci * one exists. The RXTIME registers are in ns, so we must convert the result
4068c2ecf20Sopenharmony_ci * first.
4078c2ecf20Sopenharmony_ci **/
4088c2ecf20Sopenharmony_civoid i40e_ptp_rx_hwtstamp(struct i40e_pf *pf, struct sk_buff *skb, u8 index)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	u32 prttsyn_stat, hi, lo;
4118c2ecf20Sopenharmony_ci	struct i40e_hw *hw;
4128c2ecf20Sopenharmony_ci	u64 ns;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Since we cannot turn off the Rx timestamp logic if the device is
4158c2ecf20Sopenharmony_ci	 * doing Tx timestamping, check if Rx timestamping is configured.
4168c2ecf20Sopenharmony_ci	 */
4178c2ecf20Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
4188c2ecf20Sopenharmony_ci		return;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	hw = &pf->hw;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	spin_lock_bh(&pf->ptp_rx_lock);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	/* Get current Rx events and update latch times */
4258c2ecf20Sopenharmony_ci	prttsyn_stat = i40e_ptp_get_rx_events(pf);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* TODO: Should we warn about missing Rx timestamp event? */
4288c2ecf20Sopenharmony_ci	if (!(prttsyn_stat & BIT(index))) {
4298c2ecf20Sopenharmony_ci		spin_unlock_bh(&pf->ptp_rx_lock);
4308c2ecf20Sopenharmony_ci		return;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/* Clear the latched event since we're about to read its register */
4348c2ecf20Sopenharmony_ci	pf->latch_event_flags &= ~BIT(index);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	lo = rd32(hw, I40E_PRTTSYN_RXTIME_L(index));
4378c2ecf20Sopenharmony_ci	hi = rd32(hw, I40E_PRTTSYN_RXTIME_H(index));
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	spin_unlock_bh(&pf->ptp_rx_lock);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	ns = (((u64)hi) << 32) | lo;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	i40e_ptp_convert_to_hwtstamp(skb_hwtstamps(skb), ns);
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci/**
4478c2ecf20Sopenharmony_ci * i40e_ptp_set_increment - Utility function to update clock increment rate
4488c2ecf20Sopenharmony_ci * @pf: Board private structure
4498c2ecf20Sopenharmony_ci *
4508c2ecf20Sopenharmony_ci * During a link change, the DMA frequency that drives the 1588 logic will
4518c2ecf20Sopenharmony_ci * change. In order to keep the PRTTSYN_TIME registers in units of nanoseconds,
4528c2ecf20Sopenharmony_ci * we must update the increment value per clock tick.
4538c2ecf20Sopenharmony_ci **/
4548c2ecf20Sopenharmony_civoid i40e_ptp_set_increment(struct i40e_pf *pf)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	struct i40e_link_status *hw_link_info;
4578c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
4588c2ecf20Sopenharmony_ci	u64 incval;
4598c2ecf20Sopenharmony_ci	u32 mult;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	hw_link_info = &hw->phy.link_info;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	switch (hw_link_info->link_speed) {
4668c2ecf20Sopenharmony_ci	case I40E_LINK_SPEED_10GB:
4678c2ecf20Sopenharmony_ci		mult = I40E_PTP_10GB_INCVAL_MULT;
4688c2ecf20Sopenharmony_ci		break;
4698c2ecf20Sopenharmony_ci	case I40E_LINK_SPEED_5GB:
4708c2ecf20Sopenharmony_ci		mult = I40E_PTP_5GB_INCVAL_MULT;
4718c2ecf20Sopenharmony_ci		break;
4728c2ecf20Sopenharmony_ci	case I40E_LINK_SPEED_1GB:
4738c2ecf20Sopenharmony_ci		mult = I40E_PTP_1GB_INCVAL_MULT;
4748c2ecf20Sopenharmony_ci		break;
4758c2ecf20Sopenharmony_ci	case I40E_LINK_SPEED_100MB:
4768c2ecf20Sopenharmony_ci	{
4778c2ecf20Sopenharmony_ci		static int warn_once;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci		if (!warn_once) {
4808c2ecf20Sopenharmony_ci			dev_warn(&pf->pdev->dev,
4818c2ecf20Sopenharmony_ci				 "1588 functionality is not supported at 100 Mbps. Stopping the PHC.\n");
4828c2ecf20Sopenharmony_ci			warn_once++;
4838c2ecf20Sopenharmony_ci		}
4848c2ecf20Sopenharmony_ci		mult = 0;
4858c2ecf20Sopenharmony_ci		break;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci	case I40E_LINK_SPEED_40GB:
4888c2ecf20Sopenharmony_ci	default:
4898c2ecf20Sopenharmony_ci		mult = 1;
4908c2ecf20Sopenharmony_ci		break;
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* The increment value is calculated by taking the base 40GbE incvalue
4948c2ecf20Sopenharmony_ci	 * and multiplying it by a factor based on the link speed.
4958c2ecf20Sopenharmony_ci	 */
4968c2ecf20Sopenharmony_ci	incval = I40E_PTP_40GB_INCVAL * mult;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	/* Write the new increment value into the increment register. The
4998c2ecf20Sopenharmony_ci	 * hardware will not update the clock until both registers have been
5008c2ecf20Sopenharmony_ci	 * written.
5018c2ecf20Sopenharmony_ci	 */
5028c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_L, incval & 0xFFFFFFFF);
5038c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_H, incval >> 32);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	/* Update the base adjustement value. */
5068c2ecf20Sopenharmony_ci	WRITE_ONCE(pf->ptp_adj_mult, mult);
5078c2ecf20Sopenharmony_ci	smp_mb(); /* Force the above update. */
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci/**
5118c2ecf20Sopenharmony_ci * i40e_ptp_get_ts_config - ioctl interface to read the HW timestamping
5128c2ecf20Sopenharmony_ci * @pf: Board private structure
5138c2ecf20Sopenharmony_ci * @ifr: ioctl data
5148c2ecf20Sopenharmony_ci *
5158c2ecf20Sopenharmony_ci * Obtain the current hardware timestamping settigs as requested. To do this,
5168c2ecf20Sopenharmony_ci * keep a shadow copy of the timestamp settings rather than attempting to
5178c2ecf20Sopenharmony_ci * deconstruct it from the registers.
5188c2ecf20Sopenharmony_ci **/
5198c2ecf20Sopenharmony_ciint i40e_ptp_get_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct hwtstamp_config *config = &pf->tstamp_config;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP))
5248c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
5278c2ecf20Sopenharmony_ci		-EFAULT : 0;
5288c2ecf20Sopenharmony_ci}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci/**
5318c2ecf20Sopenharmony_ci * i40e_ptp_set_timestamp_mode - setup hardware for requested timestamp mode
5328c2ecf20Sopenharmony_ci * @pf: Board private structure
5338c2ecf20Sopenharmony_ci * @config: hwtstamp settings requested or saved
5348c2ecf20Sopenharmony_ci *
5358c2ecf20Sopenharmony_ci * Control hardware registers to enter the specific mode requested by the
5368c2ecf20Sopenharmony_ci * user. Also used during reset path to ensure that timestamp settings are
5378c2ecf20Sopenharmony_ci * maintained.
5388c2ecf20Sopenharmony_ci *
5398c2ecf20Sopenharmony_ci * Note: modifies config in place, and may update the requested mode to be
5408c2ecf20Sopenharmony_ci * more broad if the specific filter is not directly supported.
5418c2ecf20Sopenharmony_ci **/
5428c2ecf20Sopenharmony_cistatic int i40e_ptp_set_timestamp_mode(struct i40e_pf *pf,
5438c2ecf20Sopenharmony_ci				       struct hwtstamp_config *config)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
5468c2ecf20Sopenharmony_ci	u32 tsyntype, regval;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* Reserved for future extensions. */
5498c2ecf20Sopenharmony_ci	if (config->flags)
5508c2ecf20Sopenharmony_ci		return -EINVAL;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	switch (config->tx_type) {
5538c2ecf20Sopenharmony_ci	case HWTSTAMP_TX_OFF:
5548c2ecf20Sopenharmony_ci		pf->ptp_tx = false;
5558c2ecf20Sopenharmony_ci		break;
5568c2ecf20Sopenharmony_ci	case HWTSTAMP_TX_ON:
5578c2ecf20Sopenharmony_ci		pf->ptp_tx = true;
5588c2ecf20Sopenharmony_ci		break;
5598c2ecf20Sopenharmony_ci	default:
5608c2ecf20Sopenharmony_ci		return -ERANGE;
5618c2ecf20Sopenharmony_ci	}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	switch (config->rx_filter) {
5648c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_NONE:
5658c2ecf20Sopenharmony_ci		pf->ptp_rx = false;
5668c2ecf20Sopenharmony_ci		/* We set the type to V1, but do not enable UDP packet
5678c2ecf20Sopenharmony_ci		 * recognition. In this way, we should be as close to
5688c2ecf20Sopenharmony_ci		 * disabling PTP Rx timestamps as possible since V1 packets
5698c2ecf20Sopenharmony_ci		 * are always UDP, since L2 packets are a V2 feature.
5708c2ecf20Sopenharmony_ci		 */
5718c2ecf20Sopenharmony_ci		tsyntype = I40E_PRTTSYN_CTL1_TSYNTYPE_V1;
5728c2ecf20Sopenharmony_ci		break;
5738c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
5748c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
5758c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
5768c2ecf20Sopenharmony_ci		if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
5778c2ecf20Sopenharmony_ci			return -ERANGE;
5788c2ecf20Sopenharmony_ci		pf->ptp_rx = true;
5798c2ecf20Sopenharmony_ci		tsyntype = I40E_PRTTSYN_CTL1_V1MESSTYPE0_MASK |
5808c2ecf20Sopenharmony_ci			   I40E_PRTTSYN_CTL1_TSYNTYPE_V1 |
5818c2ecf20Sopenharmony_ci			   I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
5828c2ecf20Sopenharmony_ci		config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
5838c2ecf20Sopenharmony_ci		break;
5848c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_EVENT:
5858c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
5868c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_SYNC:
5878c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
5888c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
5898c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
5908c2ecf20Sopenharmony_ci		if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
5918c2ecf20Sopenharmony_ci			return -ERANGE;
5928c2ecf20Sopenharmony_ci		fallthrough;
5938c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
5948c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
5958c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
5968c2ecf20Sopenharmony_ci		pf->ptp_rx = true;
5978c2ecf20Sopenharmony_ci		tsyntype = I40E_PRTTSYN_CTL1_V2MESSTYPE0_MASK |
5988c2ecf20Sopenharmony_ci			   I40E_PRTTSYN_CTL1_TSYNTYPE_V2;
5998c2ecf20Sopenharmony_ci		if (pf->hw_features & I40E_HW_PTP_L4_CAPABLE) {
6008c2ecf20Sopenharmony_ci			tsyntype |= I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
6018c2ecf20Sopenharmony_ci			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
6028c2ecf20Sopenharmony_ci		} else {
6038c2ecf20Sopenharmony_ci			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
6048c2ecf20Sopenharmony_ci		}
6058c2ecf20Sopenharmony_ci		break;
6068c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_NTP_ALL:
6078c2ecf20Sopenharmony_ci	case HWTSTAMP_FILTER_ALL:
6088c2ecf20Sopenharmony_ci	default:
6098c2ecf20Sopenharmony_ci		return -ERANGE;
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	/* Clear out all 1588-related registers to clear and unlatch them. */
6138c2ecf20Sopenharmony_ci	spin_lock_bh(&pf->ptp_rx_lock);
6148c2ecf20Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_STAT_0);
6158c2ecf20Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_TXTIME_H);
6168c2ecf20Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(0));
6178c2ecf20Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(1));
6188c2ecf20Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(2));
6198c2ecf20Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(3));
6208c2ecf20Sopenharmony_ci	pf->latch_event_flags = 0;
6218c2ecf20Sopenharmony_ci	spin_unlock_bh(&pf->ptp_rx_lock);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	/* Enable/disable the Tx timestamp interrupt based on user input. */
6248c2ecf20Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_CTL0);
6258c2ecf20Sopenharmony_ci	if (pf->ptp_tx)
6268c2ecf20Sopenharmony_ci		regval |= I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
6278c2ecf20Sopenharmony_ci	else
6288c2ecf20Sopenharmony_ci		regval &= ~I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
6298c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_CTL0, regval);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	regval = rd32(hw, I40E_PFINT_ICR0_ENA);
6328c2ecf20Sopenharmony_ci	if (pf->ptp_tx)
6338c2ecf20Sopenharmony_ci		regval |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
6348c2ecf20Sopenharmony_ci	else
6358c2ecf20Sopenharmony_ci		regval &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
6368c2ecf20Sopenharmony_ci	wr32(hw, I40E_PFINT_ICR0_ENA, regval);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/* Although there is no simple on/off switch for Rx, we "disable" Rx
6398c2ecf20Sopenharmony_ci	 * timestamps by setting to V1 only mode and clear the UDP
6408c2ecf20Sopenharmony_ci	 * recognition. This ought to disable all PTP Rx timestamps as V1
6418c2ecf20Sopenharmony_ci	 * packets are always over UDP. Note that software is configured to
6428c2ecf20Sopenharmony_ci	 * ignore Rx timestamps via the pf->ptp_rx flag.
6438c2ecf20Sopenharmony_ci	 */
6448c2ecf20Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_CTL1);
6458c2ecf20Sopenharmony_ci	/* clear everything but the enable bit */
6468c2ecf20Sopenharmony_ci	regval &= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
6478c2ecf20Sopenharmony_ci	/* now enable bits for desired Rx timestamps */
6488c2ecf20Sopenharmony_ci	regval |= tsyntype;
6498c2ecf20Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_CTL1, regval);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	return 0;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci/**
6558c2ecf20Sopenharmony_ci * i40e_ptp_set_ts_config - ioctl interface to control the HW timestamping
6568c2ecf20Sopenharmony_ci * @pf: Board private structure
6578c2ecf20Sopenharmony_ci * @ifr: ioctl data
6588c2ecf20Sopenharmony_ci *
6598c2ecf20Sopenharmony_ci * Respond to the user filter requests and make the appropriate hardware
6608c2ecf20Sopenharmony_ci * changes here. The XL710 cannot support splitting of the Tx/Rx timestamping
6618c2ecf20Sopenharmony_ci * logic, so keep track in software of whether to indicate these timestamps
6628c2ecf20Sopenharmony_ci * or not.
6638c2ecf20Sopenharmony_ci *
6648c2ecf20Sopenharmony_ci * It is permissible to "upgrade" the user request to a broader filter, as long
6658c2ecf20Sopenharmony_ci * as the user receives the timestamps they care about and the user is notified
6668c2ecf20Sopenharmony_ci * the filter has been broadened.
6678c2ecf20Sopenharmony_ci **/
6688c2ecf20Sopenharmony_ciint i40e_ptp_set_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct hwtstamp_config config;
6718c2ecf20Sopenharmony_ci	int err;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP))
6748c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
6778c2ecf20Sopenharmony_ci		return -EFAULT;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	err = i40e_ptp_set_timestamp_mode(pf, &config);
6808c2ecf20Sopenharmony_ci	if (err)
6818c2ecf20Sopenharmony_ci		return err;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	/* save these settings for future reference */
6848c2ecf20Sopenharmony_ci	pf->tstamp_config = config;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
6878c2ecf20Sopenharmony_ci		-EFAULT : 0;
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci/**
6918c2ecf20Sopenharmony_ci * i40e_ptp_create_clock - Create PTP clock device for userspace
6928c2ecf20Sopenharmony_ci * @pf: Board private structure
6938c2ecf20Sopenharmony_ci *
6948c2ecf20Sopenharmony_ci * This function creates a new PTP clock device. It only creates one if we
6958c2ecf20Sopenharmony_ci * don't already have one, so it is safe to call. Will return error if it
6968c2ecf20Sopenharmony_ci * can't create one, but success if we already have a device. Should be used
6978c2ecf20Sopenharmony_ci * by i40e_ptp_init to create clock initially, and prevent global resets from
6988c2ecf20Sopenharmony_ci * creating new clock devices.
6998c2ecf20Sopenharmony_ci **/
7008c2ecf20Sopenharmony_cistatic long i40e_ptp_create_clock(struct i40e_pf *pf)
7018c2ecf20Sopenharmony_ci{
7028c2ecf20Sopenharmony_ci	/* no need to create a clock device if we already have one */
7038c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(pf->ptp_clock))
7048c2ecf20Sopenharmony_ci		return 0;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	strlcpy(pf->ptp_caps.name, i40e_driver_name,
7078c2ecf20Sopenharmony_ci		sizeof(pf->ptp_caps.name) - 1);
7088c2ecf20Sopenharmony_ci	pf->ptp_caps.owner = THIS_MODULE;
7098c2ecf20Sopenharmony_ci	pf->ptp_caps.max_adj = 999999999;
7108c2ecf20Sopenharmony_ci	pf->ptp_caps.n_ext_ts = 0;
7118c2ecf20Sopenharmony_ci	pf->ptp_caps.pps = 0;
7128c2ecf20Sopenharmony_ci	pf->ptp_caps.adjfreq = i40e_ptp_adjfreq;
7138c2ecf20Sopenharmony_ci	pf->ptp_caps.adjtime = i40e_ptp_adjtime;
7148c2ecf20Sopenharmony_ci	pf->ptp_caps.gettimex64 = i40e_ptp_gettimex;
7158c2ecf20Sopenharmony_ci	pf->ptp_caps.settime64 = i40e_ptp_settime;
7168c2ecf20Sopenharmony_ci	pf->ptp_caps.enable = i40e_ptp_feature_enable;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	/* Attempt to register the clock before enabling the hardware. */
7198c2ecf20Sopenharmony_ci	pf->ptp_clock = ptp_clock_register(&pf->ptp_caps, &pf->pdev->dev);
7208c2ecf20Sopenharmony_ci	if (IS_ERR(pf->ptp_clock))
7218c2ecf20Sopenharmony_ci		return PTR_ERR(pf->ptp_clock);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	/* clear the hwtstamp settings here during clock create, instead of
7248c2ecf20Sopenharmony_ci	 * during regular init, so that we can maintain settings across a
7258c2ecf20Sopenharmony_ci	 * reset or suspend.
7268c2ecf20Sopenharmony_ci	 */
7278c2ecf20Sopenharmony_ci	pf->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
7288c2ecf20Sopenharmony_ci	pf->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	/* Set the previous "reset" time to the current Kernel clock time */
7318c2ecf20Sopenharmony_ci	ktime_get_real_ts64(&pf->ptp_prev_hw_time);
7328c2ecf20Sopenharmony_ci	pf->ptp_reset_start = ktime_get();
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	return 0;
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci/**
7388c2ecf20Sopenharmony_ci * i40e_ptp_save_hw_time - Save the current PTP time as ptp_prev_hw_time
7398c2ecf20Sopenharmony_ci * @pf: Board private structure
7408c2ecf20Sopenharmony_ci *
7418c2ecf20Sopenharmony_ci * Read the current PTP time and save it into pf->ptp_prev_hw_time. This should
7428c2ecf20Sopenharmony_ci * be called at the end of preparing to reset, just before hardware reset
7438c2ecf20Sopenharmony_ci * occurs, in order to preserve the PTP time as close as possible across
7448c2ecf20Sopenharmony_ci * resets.
7458c2ecf20Sopenharmony_ci */
7468c2ecf20Sopenharmony_civoid i40e_ptp_save_hw_time(struct i40e_pf *pf)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	/* don't try to access the PTP clock if it's not enabled */
7498c2ecf20Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP))
7508c2ecf20Sopenharmony_ci		return;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	i40e_ptp_gettimex(&pf->ptp_caps, &pf->ptp_prev_hw_time, NULL);
7538c2ecf20Sopenharmony_ci	/* Get a monotonic starting time for this reset */
7548c2ecf20Sopenharmony_ci	pf->ptp_reset_start = ktime_get();
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci/**
7588c2ecf20Sopenharmony_ci * i40e_ptp_restore_hw_time - Restore the ptp_prev_hw_time + delta to PTP regs
7598c2ecf20Sopenharmony_ci * @pf: Board private structure
7608c2ecf20Sopenharmony_ci *
7618c2ecf20Sopenharmony_ci * Restore the PTP hardware clock registers. We previously cached the PTP
7628c2ecf20Sopenharmony_ci * hardware time as pf->ptp_prev_hw_time. To be as accurate as possible,
7638c2ecf20Sopenharmony_ci * update this value based on the time delta since the time was saved, using
7648c2ecf20Sopenharmony_ci * CLOCK_MONOTONIC (via ktime_get()) to calculate the time difference.
7658c2ecf20Sopenharmony_ci *
7668c2ecf20Sopenharmony_ci * This ensures that the hardware clock is restored to nearly what it should
7678c2ecf20Sopenharmony_ci * have been if a reset had not occurred.
7688c2ecf20Sopenharmony_ci */
7698c2ecf20Sopenharmony_civoid i40e_ptp_restore_hw_time(struct i40e_pf *pf)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	ktime_t delta = ktime_sub(ktime_get(), pf->ptp_reset_start);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	/* Update the previous HW time with the ktime delta */
7748c2ecf20Sopenharmony_ci	timespec64_add_ns(&pf->ptp_prev_hw_time, ktime_to_ns(delta));
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	/* Restore the hardware clock registers */
7778c2ecf20Sopenharmony_ci	i40e_ptp_settime(&pf->ptp_caps, &pf->ptp_prev_hw_time);
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci/**
7818c2ecf20Sopenharmony_ci * i40e_ptp_init - Initialize the 1588 support after device probe or reset
7828c2ecf20Sopenharmony_ci * @pf: Board private structure
7838c2ecf20Sopenharmony_ci *
7848c2ecf20Sopenharmony_ci * This function sets device up for 1588 support. The first time it is run, it
7858c2ecf20Sopenharmony_ci * will create a PHC clock device. It does not create a clock device if one
7868c2ecf20Sopenharmony_ci * already exists. It also reconfigures the device after a reset.
7878c2ecf20Sopenharmony_ci *
7888c2ecf20Sopenharmony_ci * The first time a clock is created, i40e_ptp_create_clock will set
7898c2ecf20Sopenharmony_ci * pf->ptp_prev_hw_time to the current system time. During resets, it is
7908c2ecf20Sopenharmony_ci * expected that this timespec will be set to the last known PTP clock time,
7918c2ecf20Sopenharmony_ci * in order to preserve the clock time as close as possible across a reset.
7928c2ecf20Sopenharmony_ci **/
7938c2ecf20Sopenharmony_civoid i40e_ptp_init(struct i40e_pf *pf)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	struct net_device *netdev = pf->vsi[pf->lan_vsi]->netdev;
7968c2ecf20Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
7978c2ecf20Sopenharmony_ci	u32 pf_id;
7988c2ecf20Sopenharmony_ci	long err;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	/* Only one PF is assigned to control 1588 logic per port. Do not
8018c2ecf20Sopenharmony_ci	 * enable any support for PFs not assigned via PRTTSYN_CTL0.PF_ID
8028c2ecf20Sopenharmony_ci	 */
8038c2ecf20Sopenharmony_ci	pf_id = (rd32(hw, I40E_PRTTSYN_CTL0) & I40E_PRTTSYN_CTL0_PF_ID_MASK) >>
8048c2ecf20Sopenharmony_ci		I40E_PRTTSYN_CTL0_PF_ID_SHIFT;
8058c2ecf20Sopenharmony_ci	if (hw->pf_id != pf_id) {
8068c2ecf20Sopenharmony_ci		pf->flags &= ~I40E_FLAG_PTP;
8078c2ecf20Sopenharmony_ci		dev_info(&pf->pdev->dev, "%s: PTP not supported on %s\n",
8088c2ecf20Sopenharmony_ci			 __func__,
8098c2ecf20Sopenharmony_ci			 netdev->name);
8108c2ecf20Sopenharmony_ci		return;
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	mutex_init(&pf->tmreg_lock);
8148c2ecf20Sopenharmony_ci	spin_lock_init(&pf->ptp_rx_lock);
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	/* ensure we have a clock device */
8178c2ecf20Sopenharmony_ci	err = i40e_ptp_create_clock(pf);
8188c2ecf20Sopenharmony_ci	if (err) {
8198c2ecf20Sopenharmony_ci		pf->ptp_clock = NULL;
8208c2ecf20Sopenharmony_ci		dev_err(&pf->pdev->dev, "%s: ptp_clock_register failed\n",
8218c2ecf20Sopenharmony_ci			__func__);
8228c2ecf20Sopenharmony_ci	} else if (pf->ptp_clock) {
8238c2ecf20Sopenharmony_ci		u32 regval;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci		if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8268c2ecf20Sopenharmony_ci			dev_info(&pf->pdev->dev, "PHC enabled\n");
8278c2ecf20Sopenharmony_ci		pf->flags |= I40E_FLAG_PTP;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci		/* Ensure the clocks are running. */
8308c2ecf20Sopenharmony_ci		regval = rd32(hw, I40E_PRTTSYN_CTL0);
8318c2ecf20Sopenharmony_ci		regval |= I40E_PRTTSYN_CTL0_TSYNENA_MASK;
8328c2ecf20Sopenharmony_ci		wr32(hw, I40E_PRTTSYN_CTL0, regval);
8338c2ecf20Sopenharmony_ci		regval = rd32(hw, I40E_PRTTSYN_CTL1);
8348c2ecf20Sopenharmony_ci		regval |= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
8358c2ecf20Sopenharmony_ci		wr32(hw, I40E_PRTTSYN_CTL1, regval);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci		/* Set the increment value per clock tick. */
8388c2ecf20Sopenharmony_ci		i40e_ptp_set_increment(pf);
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci		/* reset timestamping mode */
8418c2ecf20Sopenharmony_ci		i40e_ptp_set_timestamp_mode(pf, &pf->tstamp_config);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci		/* Restore the clock time based on last known value */
8448c2ecf20Sopenharmony_ci		i40e_ptp_restore_hw_time(pf);
8458c2ecf20Sopenharmony_ci	}
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci/**
8498c2ecf20Sopenharmony_ci * i40e_ptp_stop - Disable the driver/hardware support and unregister the PHC
8508c2ecf20Sopenharmony_ci * @pf: Board private structure
8518c2ecf20Sopenharmony_ci *
8528c2ecf20Sopenharmony_ci * This function handles the cleanup work required from the initialization by
8538c2ecf20Sopenharmony_ci * clearing out the important information and unregistering the PHC.
8548c2ecf20Sopenharmony_ci **/
8558c2ecf20Sopenharmony_civoid i40e_ptp_stop(struct i40e_pf *pf)
8568c2ecf20Sopenharmony_ci{
8578c2ecf20Sopenharmony_ci	pf->flags &= ~I40E_FLAG_PTP;
8588c2ecf20Sopenharmony_ci	pf->ptp_tx = false;
8598c2ecf20Sopenharmony_ci	pf->ptp_rx = false;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	if (pf->ptp_tx_skb) {
8628c2ecf20Sopenharmony_ci		struct sk_buff *skb = pf->ptp_tx_skb;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci		pf->ptp_tx_skb = NULL;
8658c2ecf20Sopenharmony_ci		clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
8668c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	if (pf->ptp_clock) {
8708c2ecf20Sopenharmony_ci		ptp_clock_unregister(pf->ptp_clock);
8718c2ecf20Sopenharmony_ci		pf->ptp_clock = NULL;
8728c2ecf20Sopenharmony_ci		dev_info(&pf->pdev->dev, "%s: removed PHC on %s\n", __func__,
8738c2ecf20Sopenharmony_ci			 pf->vsi[pf->lan_vsi]->netdev->name);
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci}
876