151dce24bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0
251dce24bSJeff Kirsher /* Copyright(c) 1999 - 2018 Intel Corporation. */
33a6a4edaSJacob Keller
43a6a4edaSJacob Keller #include "ixgbe.h"
51d1a79b5SJacob Keller #include <linux/ptp_classify.h>
6a9763f3cSMark Rustad #include <linux/clocksource.h>
73a6a4edaSJacob Keller
83a6a4edaSJacob Keller /*
93a6a4edaSJacob Keller * The 82599 and the X540 do not have true 64bit nanosecond scale
103a6a4edaSJacob Keller * counter registers. Instead, SYSTIME is defined by a fixed point
113a6a4edaSJacob Keller * system which allows the user to define the scale counter increment
123a6a4edaSJacob Keller * value at every level change of the oscillator driving the SYSTIME
133a6a4edaSJacob Keller * value. For both devices the TIMINCA:IV field defines this
143a6a4edaSJacob Keller * increment. On the X540 device, 31 bits are provided. However on the
153a6a4edaSJacob Keller * 82599 only provides 24 bits. The time unit is determined by the
163a6a4edaSJacob Keller * clock frequency of the oscillator in combination with the TIMINCA
173a6a4edaSJacob Keller * register. When these devices link at 10Gb the oscillator has a
183a6a4edaSJacob Keller * period of 6.4ns. In order to convert the scale counter into
193a6a4edaSJacob Keller * nanoseconds the cyclecounter and timecounter structures are
203a6a4edaSJacob Keller * used. The SYSTIME registers need to be converted to ns values by use
213a6a4edaSJacob Keller * of only a right shift (division by power of 2). The following math
223a6a4edaSJacob Keller * determines the largest incvalue that will fit into the available
233a6a4edaSJacob Keller * bits in the TIMINCA register.
243a6a4edaSJacob Keller *
253a6a4edaSJacob Keller * PeriodWidth: Number of bits to store the clock period
263a6a4edaSJacob Keller * MaxWidth: The maximum width value of the TIMINCA register
273a6a4edaSJacob Keller * Period: The clock period for the oscillator
283a6a4edaSJacob Keller * round(): discard the fractional portion of the calculation
293a6a4edaSJacob Keller *
303a6a4edaSJacob Keller * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
313a6a4edaSJacob Keller *
323a6a4edaSJacob Keller * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
333a6a4edaSJacob Keller * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
343a6a4edaSJacob Keller *
353a6a4edaSJacob Keller * The period also changes based on the link speed:
363a6a4edaSJacob Keller * At 10Gb link or no link, the period remains the same.
373a6a4edaSJacob Keller * At 1Gb link, the period is multiplied by 10. (64ns)
383a6a4edaSJacob Keller * At 100Mb link, the period is multiplied by 100. (640ns)
393a6a4edaSJacob Keller *
403a6a4edaSJacob Keller * The calculated value allows us to right shift the SYSTIME register
413a6a4edaSJacob Keller * value in order to quickly convert it into a nanosecond clock,
423a6a4edaSJacob Keller * while allowing for the maximum possible adjustment value.
433a6a4edaSJacob Keller *
443a6a4edaSJacob Keller * These diagrams are only for the 10Gb link period
453a6a4edaSJacob Keller *
463a6a4edaSJacob Keller * SYSTIMEH SYSTIMEL
473a6a4edaSJacob Keller * +--------------+ +--------------+
483a6a4edaSJacob Keller * X540 | 32 | | 1 | 3 | 28 |
493a6a4edaSJacob Keller * *--------------+ +--------------+
503a6a4edaSJacob Keller * \________ 36 bits ______/ fract
513a6a4edaSJacob Keller *
523a6a4edaSJacob Keller * +--------------+ +--------------+
533a6a4edaSJacob Keller * 82599 | 32 | | 8 | 3 | 21 |
543a6a4edaSJacob Keller * *--------------+ +--------------+
553a6a4edaSJacob Keller * \________ 43 bits ______/ fract
563a6a4edaSJacob Keller *
573a6a4edaSJacob Keller * The 36 bit X540 SYSTIME overflows every
583a6a4edaSJacob Keller * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
593a6a4edaSJacob Keller *
603a6a4edaSJacob Keller * The 43 bit 82599 SYSTIME overflows every
613a6a4edaSJacob Keller * 2^43 * 10^-9 / 3600 = 2.4 hours
623a6a4edaSJacob Keller */
633a6a4edaSJacob Keller #define IXGBE_INCVAL_10GB 0x66666666
643a6a4edaSJacob Keller #define IXGBE_INCVAL_1GB 0x40000000
653a6a4edaSJacob Keller #define IXGBE_INCVAL_100 0x50000000
663a6a4edaSJacob Keller
673a6a4edaSJacob Keller #define IXGBE_INCVAL_SHIFT_10GB 28
683a6a4edaSJacob Keller #define IXGBE_INCVAL_SHIFT_1GB 24
693a6a4edaSJacob Keller #define IXGBE_INCVAL_SHIFT_100 21
703a6a4edaSJacob Keller
713a6a4edaSJacob Keller #define IXGBE_INCVAL_SHIFT_82599 7
723a6a4edaSJacob Keller #define IXGBE_INCPER_SHIFT_82599 24
733a6a4edaSJacob Keller
743a6a4edaSJacob Keller #define IXGBE_OVERFLOW_PERIOD (HZ * 30)
758fd70994SJacob Keller #define IXGBE_PTP_TX_TIMEOUT (HZ)
763a6a4edaSJacob Keller
7768d9676fSJacob Keller /* We use our own definitions instead of NSEC_PER_SEC because we want to mark
7868d9676fSJacob Keller * the value as a ULL to force precision when bit shifting.
79a5a0fc04SJacob Keller */
8068d9676fSJacob Keller #define NS_PER_SEC 1000000000ULL
8168d9676fSJacob Keller #define NS_PER_HALF_SEC 500000000ULL
82681ae1adSJacob E Keller
83a9763f3cSMark Rustad /* In contrast, the X550 controller has two registers, SYSTIMEH and SYSTIMEL
84a9763f3cSMark Rustad * which contain measurements of seconds and nanoseconds respectively. This
85a9763f3cSMark Rustad * matches the standard linux representation of time in the kernel. In addition,
86a9763f3cSMark Rustad * the X550 also has a SYSTIMER register which represents residue, or
87a9763f3cSMark Rustad * subnanosecond overflow adjustments. To control clock adjustment, the TIMINCA
88a9763f3cSMark Rustad * register is used, but it is unlike the X540 and 82599 devices. TIMINCA
89a9763f3cSMark Rustad * represents units of 2^-32 nanoseconds, and uses 31 bits for this, with the
90a9763f3cSMark Rustad * high bit representing whether the adjustent is positive or negative. Every
91a9763f3cSMark Rustad * clock cycle, the X550 will add 12.5 ns + TIMINCA which can result in a range
92a9763f3cSMark Rustad * of 12 to 13 nanoseconds adjustment. Unlike the 82599 and X540 devices, the
93a9763f3cSMark Rustad * X550's clock for purposes of SYSTIME generation is constant and not dependent
94a9763f3cSMark Rustad * on the link speed.
95a9763f3cSMark Rustad *
96a9763f3cSMark Rustad * SYSTIMEH SYSTIMEL SYSTIMER
97a9763f3cSMark Rustad * +--------------+ +--------------+ +-------------+
98a9763f3cSMark Rustad * X550 | 32 | | 32 | | 32 |
99a9763f3cSMark Rustad * *--------------+ +--------------+ +-------------+
100a9763f3cSMark Rustad * \____seconds___/ \_nanoseconds_/ \__2^-32 ns__/
101a9763f3cSMark Rustad *
102a9763f3cSMark Rustad * This results in a full 96 bits to represent the clock, with 32 bits for
103a9763f3cSMark Rustad * seconds, 32 bits for nanoseconds (largest value is 0d999999999 or just under
104a9763f3cSMark Rustad * 1 second) and an additional 32 bits to measure sub nanosecond adjustments for
105a9763f3cSMark Rustad * underflow of adjustments.
106a9763f3cSMark Rustad *
107a9763f3cSMark Rustad * The 32 bits of seconds for the X550 overflows every
108a9763f3cSMark Rustad * 2^32 / ( 365.25 * 24 * 60 * 60 ) = ~136 years.
109a9763f3cSMark Rustad *
110a9763f3cSMark Rustad * In order to adjust the clock frequency for the X550, the TIMINCA register is
111a9763f3cSMark Rustad * provided. This register represents a + or minus nearly 0.5 ns adjustment to
112a9763f3cSMark Rustad * the base frequency. It is measured in 2^-32 ns units, with the high bit being
113a9763f3cSMark Rustad * the sign bit. This register enables software to calculate frequency
114a9763f3cSMark Rustad * adjustments and apply them directly to the clock rate.
115a9763f3cSMark Rustad *
1165a554232SJacob Keller * The math for converting scaled_ppm into TIMINCA values is fairly
1175a554232SJacob Keller * straightforward.
118a9763f3cSMark Rustad *
1195a554232SJacob Keller * TIMINCA value = ( Base_Frequency * scaled_ppm ) / 1000000ULL << 16
1205a554232SJacob Keller *
1215a554232SJacob Keller * To avoid overflow, we simply use mul_u64_u64_div_u64.
1225a554232SJacob Keller *
1235a554232SJacob Keller * This assumes that scaled_ppm is never high enough to create a value bigger
1245a554232SJacob Keller * than TIMINCA's 31 bits can store. This is ensured by the stack, and is
1255a554232SJacob Keller * measured in parts per billion. Calculating this value is also simple.
126a9763f3cSMark Rustad * Max ppb = ( Max Adjustment / Base Frequency ) / 1000000000ULL
127a9763f3cSMark Rustad *
128a9763f3cSMark Rustad * For the X550, the Max adjustment is +/- 0.5 ns, and the base frequency is
129a9763f3cSMark Rustad * 12.5 nanoseconds. This means that the Max ppb is 39999999
130a9763f3cSMark Rustad * Note: We subtract one in order to ensure no overflow, because the TIMINCA
131a9763f3cSMark Rustad * register can only hold slightly under 0.5 nanoseconds.
132a9763f3cSMark Rustad *
133a9763f3cSMark Rustad * Because TIMINCA is measured in 2^-32 ns units, we have to convert 12.5 ns
134a9763f3cSMark Rustad * into 2^-32 units, which is
135a9763f3cSMark Rustad *
136a9763f3cSMark Rustad * 12.5 * 2^32 = C80000000
137a9763f3cSMark Rustad *
138a9763f3cSMark Rustad * Some revisions of hardware have a faster base frequency than the registers
139a9763f3cSMark Rustad * were defined for. To fix this, we use a timecounter structure with the
140a9763f3cSMark Rustad * proper mult and shift to convert the cycles into nanoseconds of time.
141a9763f3cSMark Rustad */
142a9763f3cSMark Rustad #define IXGBE_X550_BASE_PERIOD 0xC80000000ULL
143a9763f3cSMark Rustad #define INCVALUE_MASK 0x7FFFFFFF
144a9763f3cSMark Rustad #define ISGN 0x80000000
145a9763f3cSMark Rustad
1463a6a4edaSJacob Keller /**
14768d9676fSJacob Keller * ixgbe_ptp_setup_sdp_X540
1485ba643c6STony Nguyen * @adapter: private adapter structure
14982083673SJacob Keller *
150db0677faSJacob Keller * this function enables or disables the clock out feature on SDP0 for
151db0677faSJacob Keller * the X540 device. It will create a 1 second periodic output that can
152db0677faSJacob Keller * be used as the PPS (via an interrupt).
15382083673SJacob Keller *
15468d9676fSJacob Keller * It calculates when the system time will be on an exact second, and then
15568d9676fSJacob Keller * aligns the start of the PPS signal to that value.
15668d9676fSJacob Keller *
15768d9676fSJacob Keller * This works by using the cycle counter shift and mult values in reverse, and
15868d9676fSJacob Keller * assumes that the values we're shifting will not overflow.
15982083673SJacob Keller */
ixgbe_ptp_setup_sdp_X540(struct ixgbe_adapter * adapter)16068d9676fSJacob Keller static void ixgbe_ptp_setup_sdp_X540(struct ixgbe_adapter *adapter)
16182083673SJacob Keller {
16268d9676fSJacob Keller struct cyclecounter *cc = &adapter->hw_cc;
16382083673SJacob Keller struct ixgbe_hw *hw = &adapter->hw;
16482083673SJacob Keller u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
16568d9676fSJacob Keller u64 ns = 0, clock_edge = 0, clock_period;
16668d9676fSJacob Keller unsigned long flags;
16782083673SJacob Keller
168db0677faSJacob Keller /* disable the pin first */
169db0677faSJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
170db0677faSJacob Keller IXGBE_WRITE_FLUSH(hw);
171db0677faSJacob Keller
172a9763f3cSMark Rustad if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
173a9763f3cSMark Rustad return;
174a9763f3cSMark Rustad
17582083673SJacob Keller esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
17682083673SJacob Keller
177a9763f3cSMark Rustad /* enable the SDP0 pin as output, and connected to the
178db0677faSJacob Keller * native function for Timesync (ClockOut)
17982083673SJacob Keller */
180a9763f3cSMark Rustad esdp |= IXGBE_ESDP_SDP0_DIR |
181a9763f3cSMark Rustad IXGBE_ESDP_SDP0_NATIVE;
18282083673SJacob Keller
183a9763f3cSMark Rustad /* enable the Clock Out feature on SDP0, and allow
184db0677faSJacob Keller * interrupts to occur when the pin changes
18582083673SJacob Keller */
18668d9676fSJacob Keller tsauxc = (IXGBE_TSAUXC_EN_CLK |
18782083673SJacob Keller IXGBE_TSAUXC_SYNCLK |
18868d9676fSJacob Keller IXGBE_TSAUXC_SDP0_INT);
18982083673SJacob Keller
19068d9676fSJacob Keller /* Determine the clock time period to use. This assumes that the
19168d9676fSJacob Keller * cycle counter shift is small enough to avoid overflow.
19282083673SJacob Keller */
19368d9676fSJacob Keller clock_period = div_u64((NS_PER_HALF_SEC << cc->shift), cc->mult);
19468d9676fSJacob Keller clktiml = (u32)(clock_period);
19568d9676fSJacob Keller clktimh = (u32)(clock_period >> 32);
19682083673SJacob Keller
19768d9676fSJacob Keller /* Read the current clock time, and save the cycle counter value */
19868d9676fSJacob Keller spin_lock_irqsave(&adapter->tmreg_lock, flags);
19968d9676fSJacob Keller ns = timecounter_read(&adapter->hw_tc);
20068d9676fSJacob Keller clock_edge = adapter->hw_tc.cycle_last;
20168d9676fSJacob Keller spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
20282083673SJacob Keller
20368d9676fSJacob Keller /* Figure out how many seconds to add in order to round up */
20468d9676fSJacob Keller div_u64_rem(ns, NS_PER_SEC, &rem);
20568d9676fSJacob Keller
20668d9676fSJacob Keller /* Figure out how many nanoseconds to add to round the clock edge up
20768d9676fSJacob Keller * to the next full second
20868d9676fSJacob Keller */
20968d9676fSJacob Keller rem = (NS_PER_SEC - rem);
21068d9676fSJacob Keller
211b97c0b52SColin Ian King /* Adjust the clock edge to align with the next full second. */
212b97c0b52SColin Ian King clock_edge += div_u64(((u64)rem << cc->shift), cc->mult);
21382083673SJacob Keller trgttiml = (u32)clock_edge;
21482083673SJacob Keller trgttimh = (u32)(clock_edge >> 32);
21582083673SJacob Keller
21682083673SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
21782083673SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
21882083673SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
21982083673SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
22082083673SJacob Keller
22182083673SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
22282083673SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
22382083673SJacob Keller
22482083673SJacob Keller IXGBE_WRITE_FLUSH(hw);
22582083673SJacob Keller }
22682083673SJacob Keller
22782083673SJacob Keller /**
228cd458320SJacob Keller * ixgbe_ptp_setup_sdp_X550
229cd458320SJacob Keller * @adapter: private adapter structure
230cd458320SJacob Keller *
231cd458320SJacob Keller * Enable or disable a clock output signal on SDP 0 for X550 hardware.
232cd458320SJacob Keller *
233cd458320SJacob Keller * Use the target time feature to align the output signal on the next full
234cd458320SJacob Keller * second.
235cd458320SJacob Keller *
236cd458320SJacob Keller * This works by using the cycle counter shift and mult values in reverse, and
237cd458320SJacob Keller * assumes that the values we're shifting will not overflow.
238cd458320SJacob Keller */
ixgbe_ptp_setup_sdp_X550(struct ixgbe_adapter * adapter)239cd458320SJacob Keller static void ixgbe_ptp_setup_sdp_X550(struct ixgbe_adapter *adapter)
240cd458320SJacob Keller {
241cd458320SJacob Keller u32 esdp, tsauxc, freqout, trgttiml, trgttimh, rem, tssdp;
242cd458320SJacob Keller struct cyclecounter *cc = &adapter->hw_cc;
243cd458320SJacob Keller struct ixgbe_hw *hw = &adapter->hw;
244cd458320SJacob Keller u64 ns = 0, clock_edge = 0;
245cd458320SJacob Keller struct timespec64 ts;
246cd458320SJacob Keller unsigned long flags;
247cd458320SJacob Keller
248cd458320SJacob Keller /* disable the pin first */
249cd458320SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
250cd458320SJacob Keller IXGBE_WRITE_FLUSH(hw);
251cd458320SJacob Keller
252cd458320SJacob Keller if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
253cd458320SJacob Keller return;
254cd458320SJacob Keller
255cd458320SJacob Keller esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
256cd458320SJacob Keller
257cd458320SJacob Keller /* enable the SDP0 pin as output, and connected to the
258cd458320SJacob Keller * native function for Timesync (ClockOut)
259cd458320SJacob Keller */
260cd458320SJacob Keller esdp |= IXGBE_ESDP_SDP0_DIR |
261cd458320SJacob Keller IXGBE_ESDP_SDP0_NATIVE;
262cd458320SJacob Keller
263cd458320SJacob Keller /* enable the Clock Out feature on SDP0, and use Target Time 0 to
264cd458320SJacob Keller * enable generation of interrupts on the clock change.
265cd458320SJacob Keller */
266cd458320SJacob Keller #define IXGBE_TSAUXC_DIS_TS_CLEAR 0x40000000
267cd458320SJacob Keller tsauxc = (IXGBE_TSAUXC_EN_CLK | IXGBE_TSAUXC_ST0 |
268cd458320SJacob Keller IXGBE_TSAUXC_EN_TT0 | IXGBE_TSAUXC_SDP0_INT |
269cd458320SJacob Keller IXGBE_TSAUXC_DIS_TS_CLEAR);
270cd458320SJacob Keller
271cd458320SJacob Keller tssdp = (IXGBE_TSSDP_TS_SDP0_EN |
272cd458320SJacob Keller IXGBE_TSSDP_TS_SDP0_CLK0);
273cd458320SJacob Keller
274cd458320SJacob Keller /* Determine the clock time period to use. This assumes that the
275cd458320SJacob Keller * cycle counter shift is small enough to avoid overflowing a 32bit
276cd458320SJacob Keller * value.
277cd458320SJacob Keller */
278cd458320SJacob Keller freqout = div_u64(NS_PER_HALF_SEC << cc->shift, cc->mult);
279cd458320SJacob Keller
280cd458320SJacob Keller /* Read the current clock time, and save the cycle counter value */
281cd458320SJacob Keller spin_lock_irqsave(&adapter->tmreg_lock, flags);
282cd458320SJacob Keller ns = timecounter_read(&adapter->hw_tc);
283cd458320SJacob Keller clock_edge = adapter->hw_tc.cycle_last;
284cd458320SJacob Keller spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
285cd458320SJacob Keller
286cd458320SJacob Keller /* Figure out how far past the next second we are */
287cd458320SJacob Keller div_u64_rem(ns, NS_PER_SEC, &rem);
288cd458320SJacob Keller
289cd458320SJacob Keller /* Figure out how many nanoseconds to add to round the clock edge up
290cd458320SJacob Keller * to the next full second
291cd458320SJacob Keller */
292cd458320SJacob Keller rem = (NS_PER_SEC - rem);
293cd458320SJacob Keller
294b97c0b52SColin Ian King /* Adjust the clock edge to align with the next full second. */
295b97c0b52SColin Ian King clock_edge += div_u64(((u64)rem << cc->shift), cc->mult);
296cd458320SJacob Keller
297cd458320SJacob Keller /* X550 hardware stores the time in 32bits of 'billions of cycles' and
298cd458320SJacob Keller * 32bits of 'cycles'. There's no guarantee that cycles represents
299cd458320SJacob Keller * nanoseconds. However, we can use the math from a timespec64 to
300cd458320SJacob Keller * convert into the hardware representation.
301cd458320SJacob Keller *
302cd458320SJacob Keller * See ixgbe_ptp_read_X550() for more details.
303cd458320SJacob Keller */
304cd458320SJacob Keller ts = ns_to_timespec64(clock_edge);
305cd458320SJacob Keller trgttiml = (u32)ts.tv_nsec;
306cd458320SJacob Keller trgttimh = (u32)ts.tv_sec;
307cd458320SJacob Keller
308cd458320SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_FREQOUT0, freqout);
309cd458320SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
310cd458320SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
311cd458320SJacob Keller
312cd458320SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
313cd458320SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSSDP, tssdp);
314cd458320SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
315cd458320SJacob Keller
316cd458320SJacob Keller IXGBE_WRITE_FLUSH(hw);
317cd458320SJacob Keller }
318cd458320SJacob Keller
319cd458320SJacob Keller /**
320a9763f3cSMark Rustad * ixgbe_ptp_read_X550 - read cycle counter value
3218b057955SJacob Keller * @cc: cyclecounter structure
322a9763f3cSMark Rustad *
323a9763f3cSMark Rustad * This function reads SYSTIME registers. It is called by the cyclecounter
324a9763f3cSMark Rustad * structure to convert from internal representation into nanoseconds. We need
325a9763f3cSMark Rustad * this for X550 since some skews do not have expected clock frequency and
326a9763f3cSMark Rustad * result of SYSTIME is 32bits of "billions of cycles" and 32 bits of
327a9763f3cSMark Rustad * "cycles", rather than seconds and nanoseconds.
328a9763f3cSMark Rustad */
ixgbe_ptp_read_X550(const struct cyclecounter * cc)3298b057955SJacob Keller static u64 ixgbe_ptp_read_X550(const struct cyclecounter *cc)
330a9763f3cSMark Rustad {
331a9763f3cSMark Rustad struct ixgbe_adapter *adapter =
3328b057955SJacob Keller container_of(cc, struct ixgbe_adapter, hw_cc);
333a9763f3cSMark Rustad struct ixgbe_hw *hw = &adapter->hw;
334a9763f3cSMark Rustad struct timespec64 ts;
335a9763f3cSMark Rustad
336a9763f3cSMark Rustad /* storage is 32 bits of 'billions of cycles' and 32 bits of 'cycles'.
337a9763f3cSMark Rustad * Some revisions of hardware run at a higher frequency and so the
338a9763f3cSMark Rustad * cycles are not guaranteed to be nanoseconds. The timespec64 created
339a9763f3cSMark Rustad * here is used for its math/conversions but does not necessarily
340a9763f3cSMark Rustad * represent nominal time.
341a9763f3cSMark Rustad *
342a9763f3cSMark Rustad * It should be noted that this cyclecounter will overflow at a
343a9763f3cSMark Rustad * non-bitmask field since we have to convert our billions of cycles
344a9763f3cSMark Rustad * into an actual cycles count. This results in some possible weird
345a9763f3cSMark Rustad * situations at high cycle counter stamps. However given that 32 bits
346a9763f3cSMark Rustad * of "seconds" is ~138 years this isn't a problem. Even at the
347a9763f3cSMark Rustad * increased frequency of some revisions, this is still ~103 years.
348a9763f3cSMark Rustad * Since the SYSTIME values start at 0 and we never write them, it is
349a9763f3cSMark Rustad * highly unlikely for the cyclecounter to overflow in practice.
350a9763f3cSMark Rustad */
351a9763f3cSMark Rustad IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
352a9763f3cSMark Rustad ts.tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
353a9763f3cSMark Rustad ts.tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
354a9763f3cSMark Rustad
355a9763f3cSMark Rustad return (u64)timespec64_to_ns(&ts);
356a9763f3cSMark Rustad }
357a9763f3cSMark Rustad
358a9763f3cSMark Rustad /**
359a9763f3cSMark Rustad * ixgbe_ptp_read_82599 - read raw cycle counter (to be used by time counter)
36049ce9c2cSBen Hutchings * @cc: the cyclecounter structure
3613a6a4edaSJacob Keller *
3623a6a4edaSJacob Keller * this function reads the cyclecounter registers and is called by the
3633a6a4edaSJacob Keller * cyclecounter structure used to construct a ns counter from the
3643a6a4edaSJacob Keller * arbitrary fixed point registers
3653a6a4edaSJacob Keller */
ixgbe_ptp_read_82599(const struct cyclecounter * cc)366a5a1d1c2SThomas Gleixner static u64 ixgbe_ptp_read_82599(const struct cyclecounter *cc)
3673a6a4edaSJacob Keller {
3683a6a4edaSJacob Keller struct ixgbe_adapter *adapter =
369a9763f3cSMark Rustad container_of(cc, struct ixgbe_adapter, hw_cc);
3703a6a4edaSJacob Keller struct ixgbe_hw *hw = &adapter->hw;
3713a6a4edaSJacob Keller u64 stamp = 0;
3723a6a4edaSJacob Keller
3733a6a4edaSJacob Keller stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
3743a6a4edaSJacob Keller stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
3753a6a4edaSJacob Keller
3763a6a4edaSJacob Keller return stamp;
3773a6a4edaSJacob Keller }
3783a6a4edaSJacob Keller
3793a6a4edaSJacob Keller /**
380a9763f3cSMark Rustad * ixgbe_ptp_convert_to_hwtstamp - convert register value to hw timestamp
381a9763f3cSMark Rustad * @adapter: private adapter structure
382a9763f3cSMark Rustad * @hwtstamp: stack timestamp structure
3835ba643c6STony Nguyen * @timestamp: unsigned 64bit system time value
384a9763f3cSMark Rustad *
385a9763f3cSMark Rustad * We need to convert the adapter's RX/TXSTMP registers into a hwtstamp value
386a9763f3cSMark Rustad * which can be used by the stack's ptp functions.
387a9763f3cSMark Rustad *
388a9763f3cSMark Rustad * The lock is used to protect consistency of the cyclecounter and the SYSTIME
389a9763f3cSMark Rustad * registers. However, it does not need to protect against the Rx or Tx
390a9763f3cSMark Rustad * timestamp registers, as there can't be a new timestamp until the old one is
391a9763f3cSMark Rustad * unlatched by reading.
392a9763f3cSMark Rustad *
393a9763f3cSMark Rustad * In addition to the timestamp in hardware, some controllers need a software
394a9763f3cSMark Rustad * overflow cyclecounter, and this function takes this into account as well.
395a9763f3cSMark Rustad **/
ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter * adapter,struct skb_shared_hwtstamps * hwtstamp,u64 timestamp)396a9763f3cSMark Rustad static void ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter *adapter,
397a9763f3cSMark Rustad struct skb_shared_hwtstamps *hwtstamp,
398a9763f3cSMark Rustad u64 timestamp)
399a9763f3cSMark Rustad {
400a9763f3cSMark Rustad unsigned long flags;
401a9763f3cSMark Rustad struct timespec64 systime;
402a9763f3cSMark Rustad u64 ns;
403a9763f3cSMark Rustad
404a9763f3cSMark Rustad memset(hwtstamp, 0, sizeof(*hwtstamp));
405a9763f3cSMark Rustad
406a9763f3cSMark Rustad switch (adapter->hw.mac.type) {
407a9763f3cSMark Rustad /* X550 and later hardware supposedly represent time using a seconds
408a9763f3cSMark Rustad * and nanoseconds counter, instead of raw 64bits nanoseconds. We need
409a9763f3cSMark Rustad * to convert the timestamp into cycles before it can be fed to the
410a9763f3cSMark Rustad * cyclecounter. We need an actual cyclecounter because some revisions
411a9763f3cSMark Rustad * of hardware run at a higher frequency and thus the counter does
412a9763f3cSMark Rustad * not represent seconds/nanoseconds. Instead it can be thought of as
413a9763f3cSMark Rustad * cycles and billions of cycles.
414a9763f3cSMark Rustad */
415a9763f3cSMark Rustad case ixgbe_mac_X550:
416a9763f3cSMark Rustad case ixgbe_mac_X550EM_x:
41749425dfcSMark Rustad case ixgbe_mac_x550em_a:
418a9763f3cSMark Rustad /* Upper 32 bits represent billions of cycles, lower 32 bits
419a9763f3cSMark Rustad * represent cycles. However, we use timespec64_to_ns for the
420a9763f3cSMark Rustad * correct math even though the units haven't been corrected
421a9763f3cSMark Rustad * yet.
422a9763f3cSMark Rustad */
423a9763f3cSMark Rustad systime.tv_sec = timestamp >> 32;
424a9763f3cSMark Rustad systime.tv_nsec = timestamp & 0xFFFFFFFF;
425a9763f3cSMark Rustad
426a9763f3cSMark Rustad timestamp = timespec64_to_ns(&systime);
427a9763f3cSMark Rustad break;
428a9763f3cSMark Rustad default:
429a9763f3cSMark Rustad break;
430a9763f3cSMark Rustad }
431a9763f3cSMark Rustad
432a9763f3cSMark Rustad spin_lock_irqsave(&adapter->tmreg_lock, flags);
433a9763f3cSMark Rustad ns = timecounter_cyc2time(&adapter->hw_tc, timestamp);
434a9763f3cSMark Rustad spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
435a9763f3cSMark Rustad
436a9763f3cSMark Rustad hwtstamp->hwtstamp = ns_to_ktime(ns);
437a9763f3cSMark Rustad }
438a9763f3cSMark Rustad
439a9763f3cSMark Rustad /**
4405a554232SJacob Keller * ixgbe_ptp_adjfine_82599
44149ce9c2cSBen Hutchings * @ptp: the ptp clock structure
4425a554232SJacob Keller * @scaled_ppm: scaled parts per million adjustment from base
4433a6a4edaSJacob Keller *
4445a554232SJacob Keller * Adjust the frequency of the ptp cycle counter by the
4455a554232SJacob Keller * indicated scaled_ppm from the base frequency.
4465a554232SJacob Keller *
4475a554232SJacob Keller * Scaled parts per million is ppm with a 16-bit binary fractional field.
4483a6a4edaSJacob Keller */
ixgbe_ptp_adjfine_82599(struct ptp_clock_info * ptp,long scaled_ppm)4495a554232SJacob Keller static int ixgbe_ptp_adjfine_82599(struct ptp_clock_info *ptp, long scaled_ppm)
4503a6a4edaSJacob Keller {
4513a6a4edaSJacob Keller struct ixgbe_adapter *adapter =
4523a6a4edaSJacob Keller container_of(ptp, struct ixgbe_adapter, ptp_caps);
4533a6a4edaSJacob Keller struct ixgbe_hw *hw = &adapter->hw;
4541060707eSJacob Keller u64 incval;
4553a6a4edaSJacob Keller
4563a6a4edaSJacob Keller smp_mb();
4576aa7de05SMark Rutland incval = READ_ONCE(adapter->base_incval);
4581060707eSJacob Keller incval = adjust_by_scaled_ppm(incval, scaled_ppm);
4593a6a4edaSJacob Keller
4603a6a4edaSJacob Keller switch (hw->mac.type) {
4613a6a4edaSJacob Keller case ixgbe_mac_X540:
462a9763f3cSMark Rustad if (incval > 0xFFFFFFFFULL)
4635a554232SJacob Keller e_dev_warn("PTP scaled_ppm adjusted SYSTIME rate overflowed!\n");
464a9763f3cSMark Rustad IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, (u32)incval);
4653a6a4edaSJacob Keller break;
4663a6a4edaSJacob Keller case ixgbe_mac_82599EB:
467a9763f3cSMark Rustad if (incval > 0x00FFFFFFULL)
4685a554232SJacob Keller e_dev_warn("PTP scaled_ppm adjusted SYSTIME rate overflowed!\n");
4693a6a4edaSJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
470b4f47a48SJacob Keller BIT(IXGBE_INCPER_SHIFT_82599) |
471a9763f3cSMark Rustad ((u32)incval & 0x00FFFFFFUL));
4723a6a4edaSJacob Keller break;
4733a6a4edaSJacob Keller default:
4743a6a4edaSJacob Keller break;
4753a6a4edaSJacob Keller }
4763a6a4edaSJacob Keller
4773a6a4edaSJacob Keller return 0;
4783a6a4edaSJacob Keller }
4793a6a4edaSJacob Keller
4803a6a4edaSJacob Keller /**
4815a554232SJacob Keller * ixgbe_ptp_adjfine_X550
482a9763f3cSMark Rustad * @ptp: the ptp clock structure
4835a554232SJacob Keller * @scaled_ppm: scaled parts per million adjustment from base
484a9763f3cSMark Rustad *
4855a554232SJacob Keller * Adjust the frequency of the SYSTIME registers by the indicated scaled_ppm
4865a554232SJacob Keller * from base frequency.
4875a554232SJacob Keller *
4885a554232SJacob Keller * Scaled parts per million is ppm with a 16-bit binary fractional field.
489a9763f3cSMark Rustad */
ixgbe_ptp_adjfine_X550(struct ptp_clock_info * ptp,long scaled_ppm)4905a554232SJacob Keller static int ixgbe_ptp_adjfine_X550(struct ptp_clock_info *ptp, long scaled_ppm)
491a9763f3cSMark Rustad {
492a9763f3cSMark Rustad struct ixgbe_adapter *adapter =
493a9763f3cSMark Rustad container_of(ptp, struct ixgbe_adapter, ptp_caps);
494a9763f3cSMark Rustad struct ixgbe_hw *hw = &adapter->hw;
4951060707eSJacob Keller bool neg_adj;
4965a554232SJacob Keller u64 rate;
497a9763f3cSMark Rustad u32 inca;
498a9763f3cSMark Rustad
4991060707eSJacob Keller neg_adj = diff_by_scaled_ppm(IXGBE_X550_BASE_PERIOD, scaled_ppm, &rate);
500a9763f3cSMark Rustad
501a9763f3cSMark Rustad /* warn if rate is too large */
502a9763f3cSMark Rustad if (rate >= INCVALUE_MASK)
5035a554232SJacob Keller e_dev_warn("PTP scaled_ppm adjusted SYSTIME rate overflowed!\n");
504a9763f3cSMark Rustad
505a9763f3cSMark Rustad inca = rate & INCVALUE_MASK;
506a9763f3cSMark Rustad if (neg_adj)
507a9763f3cSMark Rustad inca |= ISGN;
508a9763f3cSMark Rustad
509a9763f3cSMark Rustad IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, inca);
510a9763f3cSMark Rustad
511a9763f3cSMark Rustad return 0;
512a9763f3cSMark Rustad }
513a9763f3cSMark Rustad
514a9763f3cSMark Rustad /**
5153a6a4edaSJacob Keller * ixgbe_ptp_adjtime
51649ce9c2cSBen Hutchings * @ptp: the ptp clock structure
51749ce9c2cSBen Hutchings * @delta: offset to adjust the cycle counter by
5183a6a4edaSJacob Keller *
5193a6a4edaSJacob Keller * adjust the timer by resetting the timecounter structure.
5203a6a4edaSJacob Keller */
ixgbe_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)5213a6a4edaSJacob Keller static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5223a6a4edaSJacob Keller {
5233a6a4edaSJacob Keller struct ixgbe_adapter *adapter =
5243a6a4edaSJacob Keller container_of(ptp, struct ixgbe_adapter, ptp_caps);
5253a6a4edaSJacob Keller unsigned long flags;
5263a6a4edaSJacob Keller
5273a6a4edaSJacob Keller spin_lock_irqsave(&adapter->tmreg_lock, flags);
528a9763f3cSMark Rustad timecounter_adjtime(&adapter->hw_tc, delta);
5293a6a4edaSJacob Keller spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
530db0677faSJacob Keller
531a9763f3cSMark Rustad if (adapter->ptp_setup_sdp)
532a9763f3cSMark Rustad adapter->ptp_setup_sdp(adapter);
53382083673SJacob Keller
5343a6a4edaSJacob Keller return 0;
5353a6a4edaSJacob Keller }
5363a6a4edaSJacob Keller
5373a6a4edaSJacob Keller /**
538018ed23dSMiroslav Lichvar * ixgbe_ptp_gettimex
53949ce9c2cSBen Hutchings * @ptp: the ptp clock structure
540018ed23dSMiroslav Lichvar * @ts: timespec to hold the PHC timestamp
541018ed23dSMiroslav Lichvar * @sts: structure to hold the system time before and after reading the PHC
5423a6a4edaSJacob Keller *
5433a6a4edaSJacob Keller * read the timecounter and return the correct value on ns,
5443a6a4edaSJacob Keller * after converting it into a struct timespec.
5453a6a4edaSJacob Keller */
ixgbe_ptp_gettimex(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)546018ed23dSMiroslav Lichvar static int ixgbe_ptp_gettimex(struct ptp_clock_info *ptp,
547018ed23dSMiroslav Lichvar struct timespec64 *ts,
548018ed23dSMiroslav Lichvar struct ptp_system_timestamp *sts)
5493a6a4edaSJacob Keller {
5503a6a4edaSJacob Keller struct ixgbe_adapter *adapter =
5513a6a4edaSJacob Keller container_of(ptp, struct ixgbe_adapter, ptp_caps);
552018ed23dSMiroslav Lichvar struct ixgbe_hw *hw = &adapter->hw;
5533a6a4edaSJacob Keller unsigned long flags;
554018ed23dSMiroslav Lichvar u64 ns, stamp;
5553a6a4edaSJacob Keller
5563a6a4edaSJacob Keller spin_lock_irqsave(&adapter->tmreg_lock, flags);
557018ed23dSMiroslav Lichvar
558018ed23dSMiroslav Lichvar switch (adapter->hw.mac.type) {
559018ed23dSMiroslav Lichvar case ixgbe_mac_X550:
560018ed23dSMiroslav Lichvar case ixgbe_mac_X550EM_x:
561018ed23dSMiroslav Lichvar case ixgbe_mac_x550em_a:
562018ed23dSMiroslav Lichvar /* Upper 32 bits represent billions of cycles, lower 32 bits
563018ed23dSMiroslav Lichvar * represent cycles. However, we use timespec64_to_ns for the
564018ed23dSMiroslav Lichvar * correct math even though the units haven't been corrected
565018ed23dSMiroslav Lichvar * yet.
566018ed23dSMiroslav Lichvar */
567018ed23dSMiroslav Lichvar ptp_read_system_prets(sts);
568018ed23dSMiroslav Lichvar IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
569018ed23dSMiroslav Lichvar ptp_read_system_postts(sts);
570018ed23dSMiroslav Lichvar ts->tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
571018ed23dSMiroslav Lichvar ts->tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
572018ed23dSMiroslav Lichvar stamp = timespec64_to_ns(ts);
573018ed23dSMiroslav Lichvar break;
574018ed23dSMiroslav Lichvar default:
575018ed23dSMiroslav Lichvar ptp_read_system_prets(sts);
576018ed23dSMiroslav Lichvar stamp = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
577018ed23dSMiroslav Lichvar ptp_read_system_postts(sts);
578018ed23dSMiroslav Lichvar stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
579018ed23dSMiroslav Lichvar break;
580018ed23dSMiroslav Lichvar }
581018ed23dSMiroslav Lichvar
582018ed23dSMiroslav Lichvar ns = timecounter_cyc2time(&adapter->hw_tc, stamp);
583018ed23dSMiroslav Lichvar
5843a6a4edaSJacob Keller spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
5853a6a4edaSJacob Keller
5860704fae3SRichard Cochran *ts = ns_to_timespec64(ns);
5873a6a4edaSJacob Keller
5883a6a4edaSJacob Keller return 0;
5893a6a4edaSJacob Keller }
5903a6a4edaSJacob Keller
5913a6a4edaSJacob Keller /**
5923a6a4edaSJacob Keller * ixgbe_ptp_settime
59349ce9c2cSBen Hutchings * @ptp: the ptp clock structure
59449ce9c2cSBen Hutchings * @ts: the timespec containing the new time for the cycle counter
5953a6a4edaSJacob Keller *
5963a6a4edaSJacob Keller * reset the timecounter to use a new base value instead of the kernel
5973a6a4edaSJacob Keller * wall timer value.
5983a6a4edaSJacob Keller */
ixgbe_ptp_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)5993a6a4edaSJacob Keller static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
60091432d18SRichard Cochran const struct timespec64 *ts)
6013a6a4edaSJacob Keller {
6023a6a4edaSJacob Keller struct ixgbe_adapter *adapter =
6033a6a4edaSJacob Keller container_of(ptp, struct ixgbe_adapter, ptp_caps);
6043a6a4edaSJacob Keller unsigned long flags;
605a9763f3cSMark Rustad u64 ns = timespec64_to_ns(ts);
6063a6a4edaSJacob Keller
6073a6a4edaSJacob Keller /* reset the timecounter */
6083a6a4edaSJacob Keller spin_lock_irqsave(&adapter->tmreg_lock, flags);
609a9763f3cSMark Rustad timecounter_init(&adapter->hw_tc, &adapter->hw_cc, ns);
6103a6a4edaSJacob Keller spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
6113a6a4edaSJacob Keller
612a9763f3cSMark Rustad if (adapter->ptp_setup_sdp)
613a9763f3cSMark Rustad adapter->ptp_setup_sdp(adapter);
6143a6a4edaSJacob Keller return 0;
6153a6a4edaSJacob Keller }
6163a6a4edaSJacob Keller
6173a6a4edaSJacob Keller /**
61804c8de8eSJacob Keller * ixgbe_ptp_feature_enable
61949ce9c2cSBen Hutchings * @ptp: the ptp clock structure
62049ce9c2cSBen Hutchings * @rq: the requested feature to change
62149ce9c2cSBen Hutchings * @on: whether to enable or disable the feature
6223a6a4edaSJacob Keller *
6233a6a4edaSJacob Keller * enable (or disable) ancillary features of the phc subsystem.
624681ae1adSJacob E Keller * our driver only supports the PPS feature on the X540
6253a6a4edaSJacob Keller */
ixgbe_ptp_feature_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)62604c8de8eSJacob Keller static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
6273a6a4edaSJacob Keller struct ptp_clock_request *rq, int on)
6283a6a4edaSJacob Keller {
629681ae1adSJacob E Keller struct ixgbe_adapter *adapter =
630681ae1adSJacob E Keller container_of(ptp, struct ixgbe_adapter, ptp_caps);
631681ae1adSJacob E Keller
632681ae1adSJacob E Keller /**
633681ae1adSJacob E Keller * When PPS is enabled, unmask the interrupt for the ClockOut
634681ae1adSJacob E Keller * feature, so that the interrupt handler can send the PPS
635681ae1adSJacob E Keller * event when the clock SDP triggers. Clear mask when PPS is
636681ae1adSJacob E Keller * disabled
637681ae1adSJacob E Keller */
638a9763f3cSMark Rustad if (rq->type != PTP_CLK_REQ_PPS || !adapter->ptp_setup_sdp)
639a9763f3cSMark Rustad return -ENOTSUPP;
640a9763f3cSMark Rustad
641681ae1adSJacob E Keller if (on)
642681ae1adSJacob E Keller adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
643681ae1adSJacob E Keller else
644db0677faSJacob Keller adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
645db0677faSJacob Keller
646a9763f3cSMark Rustad adapter->ptp_setup_sdp(adapter);
647681ae1adSJacob E Keller return 0;
6483a6a4edaSJacob Keller }
6493a6a4edaSJacob Keller
6503a6a4edaSJacob Keller /**
651681ae1adSJacob E Keller * ixgbe_ptp_check_pps_event
65249ce9c2cSBen Hutchings * @adapter: the private adapter structure
653681ae1adSJacob E Keller *
654681ae1adSJacob E Keller * This function is called by the interrupt routine when checking for
655681ae1adSJacob E Keller * interrupts. It will check and handle a pps event.
656681ae1adSJacob E Keller */
ixgbe_ptp_check_pps_event(struct ixgbe_adapter * adapter)657a9763f3cSMark Rustad void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter)
658681ae1adSJacob E Keller {
659681ae1adSJacob E Keller struct ixgbe_hw *hw = &adapter->hw;
660681ae1adSJacob E Keller struct ptp_clock_event event;
661681ae1adSJacob E Keller
6623645adbbSJacob Keller event.type = PTP_CLOCK_PPS;
6633645adbbSJacob Keller
6643645adbbSJacob Keller /* this check is necessary in case the interrupt was enabled via some
6653645adbbSJacob Keller * alternative means (ex. debug_fs). Better to check here than
6663645adbbSJacob Keller * everywhere that calls this function.
6673645adbbSJacob Keller */
6683645adbbSJacob Keller if (!adapter->ptp_clock)
6693645adbbSJacob Keller return;
6703645adbbSJacob Keller
671681ae1adSJacob E Keller switch (hw->mac.type) {
672681ae1adSJacob E Keller case ixgbe_mac_X540:
673681ae1adSJacob E Keller ptp_clock_event(adapter->ptp_clock, &event);
674681ae1adSJacob E Keller break;
675681ae1adSJacob E Keller default:
676681ae1adSJacob E Keller break;
677681ae1adSJacob E Keller }
678681ae1adSJacob E Keller }
679681ae1adSJacob E Keller
680681ae1adSJacob E Keller /**
681f2f33387SJacob Keller * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
682f2f33387SJacob Keller * @adapter: private adapter struct
6833a6a4edaSJacob Keller *
684f2f33387SJacob Keller * this watchdog task periodically reads the timecounter
6853a6a4edaSJacob Keller * in order to prevent missing when the system time registers wrap
686f2f33387SJacob Keller * around. This needs to be run approximately twice a minute.
6873a6a4edaSJacob Keller */
ixgbe_ptp_overflow_check(struct ixgbe_adapter * adapter)6883a6a4edaSJacob Keller void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
6893a6a4edaSJacob Keller {
690f2f33387SJacob Keller bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
691f2f33387SJacob Keller IXGBE_OVERFLOW_PERIOD);
692018ed23dSMiroslav Lichvar unsigned long flags;
6933a6a4edaSJacob Keller
694891dc082SJacob Keller if (timeout) {
695018ed23dSMiroslav Lichvar /* Update the timecounter */
696018ed23dSMiroslav Lichvar spin_lock_irqsave(&adapter->tmreg_lock, flags);
697018ed23dSMiroslav Lichvar timecounter_read(&adapter->hw_tc);
698018ed23dSMiroslav Lichvar spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
699018ed23dSMiroslav Lichvar
7003a6a4edaSJacob Keller adapter->last_overflow_check = jiffies;
7013a6a4edaSJacob Keller }
7023a6a4edaSJacob Keller }
7033a6a4edaSJacob Keller
7043a6a4edaSJacob Keller /**
7056cb562d6SJacob Keller * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
7066cb562d6SJacob Keller * @adapter: private network adapter structure
7071d1a79b5SJacob Keller *
7086cb562d6SJacob Keller * this watchdog task is scheduled to detect error case where hardware has
7096cb562d6SJacob Keller * dropped an Rx packet that was timestamped when the ring is full. The
7106cb562d6SJacob Keller * particular error is rare but leaves the device in a state unable to timestamp
7116cb562d6SJacob Keller * any future packets.
7121d1a79b5SJacob Keller */
ixgbe_ptp_rx_hang(struct ixgbe_adapter * adapter)7136cb562d6SJacob Keller void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
7141d1a79b5SJacob Keller {
7156cb562d6SJacob Keller struct ixgbe_hw *hw = &adapter->hw;
7166cb562d6SJacob Keller u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
717a9763f3cSMark Rustad struct ixgbe_ring *rx_ring;
7186cb562d6SJacob Keller unsigned long rx_event;
719a9763f3cSMark Rustad int n;
7201d1a79b5SJacob Keller
7216cb562d6SJacob Keller /* if we don't have a valid timestamp in the registers, just update the
7226cb562d6SJacob Keller * timeout counter and exit
7236cb562d6SJacob Keller */
7246cb562d6SJacob Keller if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
7256cb562d6SJacob Keller adapter->last_rx_ptp_check = jiffies;
7266cb562d6SJacob Keller return;
7271d1a79b5SJacob Keller }
7281d1a79b5SJacob Keller
7296cb562d6SJacob Keller /* determine the most recent watchdog or rx_timestamp event */
7306cb562d6SJacob Keller rx_event = adapter->last_rx_ptp_check;
731a9763f3cSMark Rustad for (n = 0; n < adapter->num_rx_queues; n++) {
732a9763f3cSMark Rustad rx_ring = adapter->rx_ring[n];
733a9763f3cSMark Rustad if (time_after(rx_ring->last_rx_timestamp, rx_event))
734a9763f3cSMark Rustad rx_event = rx_ring->last_rx_timestamp;
735a9763f3cSMark Rustad }
7361d1a79b5SJacob Keller
7376cb562d6SJacob Keller /* only need to read the high RXSTMP register to clear the lock */
7386cb562d6SJacob Keller if (time_is_before_jiffies(rx_event + 5 * HZ)) {
7396cb562d6SJacob Keller IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
7406cb562d6SJacob Keller adapter->last_rx_ptp_check = jiffies;
7411d1a79b5SJacob Keller
742a9763f3cSMark Rustad adapter->rx_hwtstamp_cleared++;
743c5ffe7e1SJakub Kicinski e_warn(drv, "clearing RX Timestamp hang\n");
7441d1a79b5SJacob Keller }
7451d1a79b5SJacob Keller }
7461d1a79b5SJacob Keller
7471d1a79b5SJacob Keller /**
748a9763f3cSMark Rustad * ixgbe_ptp_clear_tx_timestamp - utility function to clear Tx timestamp state
749a9763f3cSMark Rustad * @adapter: the private adapter structure
750a9763f3cSMark Rustad *
751a9763f3cSMark Rustad * This function should be called whenever the state related to a Tx timestamp
752a9763f3cSMark Rustad * needs to be cleared. This helps ensure that all related bits are reset for
753a9763f3cSMark Rustad * the next Tx timestamp event.
754a9763f3cSMark Rustad */
ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter * adapter)755a9763f3cSMark Rustad static void ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter *adapter)
756a9763f3cSMark Rustad {
757a9763f3cSMark Rustad struct ixgbe_hw *hw = &adapter->hw;
758a9763f3cSMark Rustad
759a9763f3cSMark Rustad IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
760a9763f3cSMark Rustad if (adapter->ptp_tx_skb) {
761a9763f3cSMark Rustad dev_kfree_skb_any(adapter->ptp_tx_skb);
762a9763f3cSMark Rustad adapter->ptp_tx_skb = NULL;
763a9763f3cSMark Rustad }
764a9763f3cSMark Rustad clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
765a9763f3cSMark Rustad }
766a9763f3cSMark Rustad
767a9763f3cSMark Rustad /**
768622a2ef5SJacob Keller * ixgbe_ptp_tx_hang - detect error case where Tx timestamp never finishes
769622a2ef5SJacob Keller * @adapter: private network adapter structure
770622a2ef5SJacob Keller */
ixgbe_ptp_tx_hang(struct ixgbe_adapter * adapter)771622a2ef5SJacob Keller void ixgbe_ptp_tx_hang(struct ixgbe_adapter *adapter)
772622a2ef5SJacob Keller {
773622a2ef5SJacob Keller bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
774622a2ef5SJacob Keller IXGBE_PTP_TX_TIMEOUT);
775622a2ef5SJacob Keller
776622a2ef5SJacob Keller if (!adapter->ptp_tx_skb)
777622a2ef5SJacob Keller return;
778622a2ef5SJacob Keller
779622a2ef5SJacob Keller if (!test_bit(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state))
780622a2ef5SJacob Keller return;
781622a2ef5SJacob Keller
782622a2ef5SJacob Keller /* If we haven't received a timestamp within the timeout, it is
783622a2ef5SJacob Keller * reasonable to assume that it will never occur, so we can unlock the
784622a2ef5SJacob Keller * timestamp bit when this occurs.
785622a2ef5SJacob Keller */
786622a2ef5SJacob Keller if (timeout) {
787622a2ef5SJacob Keller cancel_work_sync(&adapter->ptp_tx_work);
788622a2ef5SJacob Keller ixgbe_ptp_clear_tx_timestamp(adapter);
789622a2ef5SJacob Keller adapter->tx_hwtstamp_timeouts++;
790622a2ef5SJacob Keller e_warn(drv, "clearing Tx timestamp hang\n");
791622a2ef5SJacob Keller }
792622a2ef5SJacob Keller }
793622a2ef5SJacob Keller
794622a2ef5SJacob Keller /**
7953a6a4edaSJacob Keller * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
796891dc082SJacob Keller * @adapter: the private adapter struct
7973a6a4edaSJacob Keller *
7983a6a4edaSJacob Keller * if the timestamp is valid, we convert it into the timecounter ns
7993a6a4edaSJacob Keller * value, then store that result into the shhwtstamps structure which
8003a6a4edaSJacob Keller * is passed up the network stack
8013a6a4edaSJacob Keller */
ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter * adapter)802891dc082SJacob Keller static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
8033a6a4edaSJacob Keller {
804aaebaf50SJacob Keller struct sk_buff *skb = adapter->ptp_tx_skb;
805891dc082SJacob Keller struct ixgbe_hw *hw = &adapter->hw;
8063a6a4edaSJacob Keller struct skb_shared_hwtstamps shhwtstamps;
807a9763f3cSMark Rustad u64 regval = 0;
8083a6a4edaSJacob Keller
8093a6a4edaSJacob Keller regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
8103a6a4edaSJacob Keller regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
811a9763f3cSMark Rustad ixgbe_ptp_convert_to_hwtstamp(adapter, &shhwtstamps, regval);
812891dc082SJacob Keller
813aaebaf50SJacob Keller /* Handle cleanup of the ptp_tx_skb ourselves, and unlock the state
814aaebaf50SJacob Keller * bit prior to notifying the stack via skb_tstamp_tx(). This prevents
815aaebaf50SJacob Keller * well behaved applications from attempting to timestamp again prior
816aaebaf50SJacob Keller * to the lock bit being clear.
817aaebaf50SJacob Keller */
818aaebaf50SJacob Keller adapter->ptp_tx_skb = NULL;
819aaebaf50SJacob Keller clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
820aaebaf50SJacob Keller
821aaebaf50SJacob Keller /* Notify the stack and then free the skb after we've unlocked */
822aaebaf50SJacob Keller skb_tstamp_tx(skb, &shhwtstamps);
823aaebaf50SJacob Keller dev_kfree_skb_any(skb);
824891dc082SJacob Keller }
825891dc082SJacob Keller
826891dc082SJacob Keller /**
827891dc082SJacob Keller * ixgbe_ptp_tx_hwtstamp_work
828891dc082SJacob Keller * @work: pointer to the work struct
829891dc082SJacob Keller *
830891dc082SJacob Keller * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
831dbedd44eSJoe Perches * timestamp has been taken for the current skb. It is necessary, because the
832891dc082SJacob Keller * descriptor's "done" bit does not correlate with the timestamp event.
833891dc082SJacob Keller */
ixgbe_ptp_tx_hwtstamp_work(struct work_struct * work)834891dc082SJacob Keller static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
835891dc082SJacob Keller {
836891dc082SJacob Keller struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
837891dc082SJacob Keller ptp_tx_work);
838891dc082SJacob Keller struct ixgbe_hw *hw = &adapter->hw;
839891dc082SJacob Keller bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
840891dc082SJacob Keller IXGBE_PTP_TX_TIMEOUT);
841891dc082SJacob Keller u32 tsynctxctl;
842891dc082SJacob Keller
843a9763f3cSMark Rustad /* we have to have a valid skb to poll for a timestamp */
844a9763f3cSMark Rustad if (!adapter->ptp_tx_skb) {
845a9763f3cSMark Rustad ixgbe_ptp_clear_tx_timestamp(adapter);
846891dc082SJacob Keller return;
847891dc082SJacob Keller }
848891dc082SJacob Keller
849a9763f3cSMark Rustad /* stop polling once we have a valid timestamp */
850891dc082SJacob Keller tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
851a9763f3cSMark Rustad if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID) {
852891dc082SJacob Keller ixgbe_ptp_tx_hwtstamp(adapter);
853a9763f3cSMark Rustad return;
854a9763f3cSMark Rustad }
855a9763f3cSMark Rustad
856a9763f3cSMark Rustad if (timeout) {
857a9763f3cSMark Rustad ixgbe_ptp_clear_tx_timestamp(adapter);
858a9763f3cSMark Rustad adapter->tx_hwtstamp_timeouts++;
859a9763f3cSMark Rustad e_warn(drv, "clearing Tx Timestamp hang\n");
860a9763f3cSMark Rustad } else {
861891dc082SJacob Keller /* reschedule to keep checking if it's not available yet */
862891dc082SJacob Keller schedule_work(&adapter->ptp_tx_work);
8633a6a4edaSJacob Keller }
864a9763f3cSMark Rustad }
8653a6a4edaSJacob Keller
8663a6a4edaSJacob Keller /**
867a9763f3cSMark Rustad * ixgbe_ptp_rx_pktstamp - utility function to get RX time stamp from buffer
868a9763f3cSMark Rustad * @q_vector: structure containing interrupt and ring information
869a9763f3cSMark Rustad * @skb: the packet
870a9763f3cSMark Rustad *
871a9763f3cSMark Rustad * This function will be called by the Rx routine of the timestamp for this
872a9763f3cSMark Rustad * packet is stored in the buffer. The value is stored in little endian format
873a9763f3cSMark Rustad * starting at the end of the packet data.
874a9763f3cSMark Rustad */
ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector * q_vector,struct sk_buff * skb)875a9763f3cSMark Rustad void ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector *q_vector,
876a9763f3cSMark Rustad struct sk_buff *skb)
877a9763f3cSMark Rustad {
878a9763f3cSMark Rustad __le64 regval;
879a9763f3cSMark Rustad
880a9763f3cSMark Rustad /* copy the bits out of the skb, and then trim the skb length */
881a9763f3cSMark Rustad skb_copy_bits(skb, skb->len - IXGBE_TS_HDR_LEN, ®val,
882a9763f3cSMark Rustad IXGBE_TS_HDR_LEN);
883a9763f3cSMark Rustad __pskb_trim(skb, skb->len - IXGBE_TS_HDR_LEN);
884a9763f3cSMark Rustad
885a9763f3cSMark Rustad /* The timestamp is recorded in little endian format, and is stored at
886a9763f3cSMark Rustad * the end of the packet.
887a9763f3cSMark Rustad *
888a9763f3cSMark Rustad * DWORD: N N + 1 N + 2
889a9763f3cSMark Rustad * Field: End of Packet SYSTIMH SYSTIML
890a9763f3cSMark Rustad */
891a9763f3cSMark Rustad ixgbe_ptp_convert_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
892a9763f3cSMark Rustad le64_to_cpu(regval));
893a9763f3cSMark Rustad }
894a9763f3cSMark Rustad
895a9763f3cSMark Rustad /**
896a9763f3cSMark Rustad * ixgbe_ptp_rx_rgtstamp - utility function which checks for RX time stamp
897a9763f3cSMark Rustad * @q_vector: structure containing interrupt and ring information
8983a6a4edaSJacob Keller * @skb: particular skb to send timestamp with
8993a6a4edaSJacob Keller *
9003a6a4edaSJacob Keller * if the timestamp is valid, we convert it into the timecounter ns
9013a6a4edaSJacob Keller * value, then store that result into the shhwtstamps structure which
9023a6a4edaSJacob Keller * is passed up the network stack
9033a6a4edaSJacob Keller */
ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector * q_vector,struct sk_buff * skb)904a9763f3cSMark Rustad void ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector *q_vector,
905a9763f3cSMark Rustad struct sk_buff *skb)
9063a6a4edaSJacob Keller {
907a9763f3cSMark Rustad struct ixgbe_adapter *adapter;
908a9763f3cSMark Rustad struct ixgbe_hw *hw;
909a9763f3cSMark Rustad u64 regval = 0;
9103a6a4edaSJacob Keller u32 tsyncrxctl;
911a9763f3cSMark Rustad
912a9763f3cSMark Rustad /* we cannot process timestamps on a ring without a q_vector */
913a9763f3cSMark Rustad if (!q_vector || !q_vector->adapter)
914a9763f3cSMark Rustad return;
915a9763f3cSMark Rustad
916a9763f3cSMark Rustad adapter = q_vector->adapter;
917a9763f3cSMark Rustad hw = &adapter->hw;
918a9763f3cSMark Rustad
919a9763f3cSMark Rustad /* Read the tsyncrxctl register afterwards in order to prevent taking an
920a9763f3cSMark Rustad * I/O hit on every packet.
921a9763f3cSMark Rustad */
9223a6a4edaSJacob Keller
9233a6a4edaSJacob Keller tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
924f42df167SJiri Benc if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
9251d1a79b5SJacob Keller return;
9261d1a79b5SJacob Keller
9273a6a4edaSJacob Keller regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
9283a6a4edaSJacob Keller regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
9293a6a4edaSJacob Keller
930a9763f3cSMark Rustad ixgbe_ptp_convert_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
9313a6a4edaSJacob Keller }
9323a6a4edaSJacob Keller
933c3e9297cSJacob Keller /**
934c3e9297cSJacob Keller * ixgbe_ptp_get_ts_config - get current hardware timestamping configuration
935c3e9297cSJacob Keller * @adapter: pointer to adapter structure
936c3e9297cSJacob Keller * @ifr: ioctl data
937c3e9297cSJacob Keller *
938c3e9297cSJacob Keller * This function returns the current timestamping settings. Rather than
939c3e9297cSJacob Keller * attempt to deconstruct registers to fill in the values, simply keep a copy
940c3e9297cSJacob Keller * of the old settings around, and return a copy when requested.
941c3e9297cSJacob Keller */
ixgbe_ptp_get_ts_config(struct ixgbe_adapter * adapter,struct ifreq * ifr)94293501d48SJacob Keller int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
94393501d48SJacob Keller {
94493501d48SJacob Keller struct hwtstamp_config *config = &adapter->tstamp_config;
94593501d48SJacob Keller
94693501d48SJacob Keller return copy_to_user(ifr->ifr_data, config,
94793501d48SJacob Keller sizeof(*config)) ? -EFAULT : 0;
94893501d48SJacob Keller }
94993501d48SJacob Keller
9503a6a4edaSJacob Keller /**
951a7ef4286SJacob Keller * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
952a7ef4286SJacob Keller * @adapter: the private ixgbe adapter structure
953a7ef4286SJacob Keller * @config: the hwtstamp configuration requested
9543a6a4edaSJacob Keller *
9553a6a4edaSJacob Keller * Outgoing time stamping can be enabled and disabled. Play nice and
95693501d48SJacob Keller * disable it when requested, although it shouldn't cause any overhead
9573a6a4edaSJacob Keller * when no packet needs it. At most one packet in the queue may be
9583a6a4edaSJacob Keller * marked for time stamping, otherwise it would be impossible to tell
9593a6a4edaSJacob Keller * for sure to which packet the hardware time stamp belongs.
9603a6a4edaSJacob Keller *
9613a6a4edaSJacob Keller * Incoming time stamping has to be configured via the hardware
9623a6a4edaSJacob Keller * filters. Not all combinations are supported, in particular event
9633a6a4edaSJacob Keller * type has to be specified. Matching the kind of event packet is
9643a6a4edaSJacob Keller * not supported, with the exception of "all V2 events regardless of
9653a6a4edaSJacob Keller * level 2 or 4".
966c19197a7SJacob Keller *
967c19197a7SJacob Keller * Since hardware always timestamps Path delay packets when timestamping V2
968c19197a7SJacob Keller * packets, regardless of the type specified in the register, only use V2
969c19197a7SJacob Keller * Event mode. This more accurately tells the user what the hardware is going
970c19197a7SJacob Keller * to do anyways.
971a7ef4286SJacob Keller *
972a7ef4286SJacob Keller * Note: this may modify the hwtstamp configuration towards a more general
973a7ef4286SJacob Keller * mode, if required to support the specifically requested mode.
9743a6a4edaSJacob Keller */
ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter * adapter,struct hwtstamp_config * config)975a7ef4286SJacob Keller static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
976a7ef4286SJacob Keller struct hwtstamp_config *config)
9773a6a4edaSJacob Keller {
9783a6a4edaSJacob Keller struct ixgbe_hw *hw = &adapter->hw;
9793a6a4edaSJacob Keller u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
9803a6a4edaSJacob Keller u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
981f3444d8bSJacob Keller u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
982*3c44191dSVadim Fedorenko u32 aflags = adapter->flags;
9833a6a4edaSJacob Keller bool is_l2 = false;
9843a6a4edaSJacob Keller u32 regval;
9853a6a4edaSJacob Keller
986a7ef4286SJacob Keller switch (config->tx_type) {
9873a6a4edaSJacob Keller case HWTSTAMP_TX_OFF:
9883a6a4edaSJacob Keller tsync_tx_ctl = 0;
98927e40255SGustavo A. R. Silva break;
9903a6a4edaSJacob Keller case HWTSTAMP_TX_ON:
9913a6a4edaSJacob Keller break;
9923a6a4edaSJacob Keller default:
9933a6a4edaSJacob Keller return -ERANGE;
9943a6a4edaSJacob Keller }
9953a6a4edaSJacob Keller
996a7ef4286SJacob Keller switch (config->rx_filter) {
9973a6a4edaSJacob Keller case HWTSTAMP_FILTER_NONE:
9983a6a4edaSJacob Keller tsync_rx_ctl = 0;
999f3444d8bSJacob Keller tsync_rx_mtrl = 0;
1000*3c44191dSVadim Fedorenko aflags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1001a9763f3cSMark Rustad IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
10023a6a4edaSJacob Keller break;
10033a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
10043a6a4edaSJacob Keller tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
1005b1e50f7aSJacob Keller tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
1006*3c44191dSVadim Fedorenko aflags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1007a9763f3cSMark Rustad IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
10083a6a4edaSJacob Keller break;
10093a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
10103a6a4edaSJacob Keller tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
1011b1e50f7aSJacob Keller tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
1012*3c44191dSVadim Fedorenko aflags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1013a9763f3cSMark Rustad IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
10143a6a4edaSJacob Keller break;
1015c19197a7SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT:
1016c19197a7SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1017c19197a7SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
10183a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC:
10193a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
10203a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
10213a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
10223a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
10233a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
10243a6a4edaSJacob Keller tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
10253a6a4edaSJacob Keller is_l2 = true;
1026a7ef4286SJacob Keller config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1027*3c44191dSVadim Fedorenko aflags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1028a9763f3cSMark Rustad IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
10293a6a4edaSJacob Keller break;
10303a6a4edaSJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1031e3412575SMiroslav Lichvar case HWTSTAMP_FILTER_NTP_ALL:
10323a6a4edaSJacob Keller case HWTSTAMP_FILTER_ALL:
1033a9763f3cSMark Rustad /* The X550 controller is capable of timestamping all packets,
1034a9763f3cSMark Rustad * which allows it to accept any filter.
1035a9763f3cSMark Rustad */
1036a9763f3cSMark Rustad if (hw->mac.type >= ixgbe_mac_X550) {
1037a9763f3cSMark Rustad tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_ALL;
1038a9763f3cSMark Rustad config->rx_filter = HWTSTAMP_FILTER_ALL;
1039*3c44191dSVadim Fedorenko aflags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
1040a9763f3cSMark Rustad break;
1041a9763f3cSMark Rustad }
10425463fce6SJeff Kirsher fallthrough;
10433a6a4edaSJacob Keller default:
10443a6a4edaSJacob Keller /*
10451d1a79b5SJacob Keller * register RXMTRL must be set in order to do V1 packets,
10461d1a79b5SJacob Keller * therefore it is not possible to time stamp both V1 Sync and
10471d1a79b5SJacob Keller * Delay_Req messages and hardware does not support
10481d1a79b5SJacob Keller * timestamping all packets => return error
10493a6a4edaSJacob Keller */
1050a7ef4286SJacob Keller config->rx_filter = HWTSTAMP_FILTER_NONE;
10513a6a4edaSJacob Keller return -ERANGE;
10523a6a4edaSJacob Keller }
10533a6a4edaSJacob Keller
10543a6a4edaSJacob Keller if (hw->mac.type == ixgbe_mac_82598EB) {
1055a9763f3cSMark Rustad adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1056a9763f3cSMark Rustad IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
10573a6a4edaSJacob Keller if (tsync_rx_ctl | tsync_tx_ctl)
10583a6a4edaSJacob Keller return -ERANGE;
10593a6a4edaSJacob Keller return 0;
10603a6a4edaSJacob Keller }
10613a6a4edaSJacob Keller
1062a9763f3cSMark Rustad /* Per-packet timestamping only works if the filter is set to all
1063a9763f3cSMark Rustad * packets. Since this is desired, always timestamp all packets as long
1064a9763f3cSMark Rustad * as any Rx filter was configured.
1065a9763f3cSMark Rustad */
1066a9763f3cSMark Rustad switch (hw->mac.type) {
1067a9763f3cSMark Rustad case ixgbe_mac_X550:
1068a9763f3cSMark Rustad case ixgbe_mac_X550EM_x:
106949425dfcSMark Rustad case ixgbe_mac_x550em_a:
1070a9763f3cSMark Rustad /* enable timestamping all packets only if at least some
1071a9763f3cSMark Rustad * packets were requested. Otherwise, play nice and disable
1072a9763f3cSMark Rustad * timestamping
1073a9763f3cSMark Rustad */
1074a9763f3cSMark Rustad if (config->rx_filter == HWTSTAMP_FILTER_NONE)
1075a9763f3cSMark Rustad break;
1076a9763f3cSMark Rustad
1077a9763f3cSMark Rustad tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED |
1078a9763f3cSMark Rustad IXGBE_TSYNCRXCTL_TYPE_ALL |
1079a9763f3cSMark Rustad IXGBE_TSYNCRXCTL_TSIP_UT_EN;
1080a9763f3cSMark Rustad config->rx_filter = HWTSTAMP_FILTER_ALL;
1081*3c44191dSVadim Fedorenko aflags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
1082*3c44191dSVadim Fedorenko aflags &= ~IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER;
1083a9763f3cSMark Rustad is_l2 = true;
1084a9763f3cSMark Rustad break;
1085a9763f3cSMark Rustad default:
1086a9763f3cSMark Rustad break;
1087a9763f3cSMark Rustad }
1088a9763f3cSMark Rustad
10896ccf7a57SJacob Keller /* define ethertype filter for timestamping L2 packets */
10903a6a4edaSJacob Keller if (is_l2)
10916ccf7a57SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
10923a6a4edaSJacob Keller (IXGBE_ETQF_FILTER_EN | /* enable filter */
10933a6a4edaSJacob Keller IXGBE_ETQF_1588 | /* enable timestamping */
10943a6a4edaSJacob Keller ETH_P_1588)); /* 1588 eth protocol type */
10953a6a4edaSJacob Keller else
10966ccf7a57SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
10973a6a4edaSJacob Keller
10983a6a4edaSJacob Keller /* enable/disable TX */
10993a6a4edaSJacob Keller regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
11003a6a4edaSJacob Keller regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
11013a6a4edaSJacob Keller regval |= tsync_tx_ctl;
11023a6a4edaSJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
11033a6a4edaSJacob Keller
11043a6a4edaSJacob Keller /* enable/disable RX */
11053a6a4edaSJacob Keller regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
11063a6a4edaSJacob Keller regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
11073a6a4edaSJacob Keller regval |= tsync_rx_ctl;
11083a6a4edaSJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
11093a6a4edaSJacob Keller
11103a6a4edaSJacob Keller /* define which PTP packets are time stamped */
11113a6a4edaSJacob Keller IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
11123a6a4edaSJacob Keller
11133a6a4edaSJacob Keller IXGBE_WRITE_FLUSH(hw);
11143a6a4edaSJacob Keller
1115*3c44191dSVadim Fedorenko /* configure adapter flags only when HW is actually configured */
1116*3c44191dSVadim Fedorenko adapter->flags = aflags;
1117*3c44191dSVadim Fedorenko
11183a6a4edaSJacob Keller /* clear TX/RX time stamp registers, just to be sure */
1119a9763f3cSMark Rustad ixgbe_ptp_clear_tx_timestamp(adapter);
1120a9763f3cSMark Rustad IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
11213a6a4edaSJacob Keller
1122a7ef4286SJacob Keller return 0;
1123a7ef4286SJacob Keller }
1124a7ef4286SJacob Keller
1125a7ef4286SJacob Keller /**
1126a7ef4286SJacob Keller * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
1127a7ef4286SJacob Keller * @adapter: pointer to adapter struct
11285ba643c6STony Nguyen * @ifr: ioctl data
1129a7ef4286SJacob Keller *
1130a7ef4286SJacob Keller * Set hardware to requested mode. If unsupported, return an error with no
1131a7ef4286SJacob Keller * changes. Otherwise, store the mode for future reference.
1132a7ef4286SJacob Keller */
ixgbe_ptp_set_ts_config(struct ixgbe_adapter * adapter,struct ifreq * ifr)1133a7ef4286SJacob Keller int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
1134a7ef4286SJacob Keller {
1135a7ef4286SJacob Keller struct hwtstamp_config config;
1136a7ef4286SJacob Keller int err;
1137a7ef4286SJacob Keller
1138a7ef4286SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1139a7ef4286SJacob Keller return -EFAULT;
1140a7ef4286SJacob Keller
1141a7ef4286SJacob Keller err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
1142a7ef4286SJacob Keller if (err)
1143a7ef4286SJacob Keller return err;
1144a7ef4286SJacob Keller
114593501d48SJacob Keller /* save these settings for future reference */
114693501d48SJacob Keller memcpy(&adapter->tstamp_config, &config,
114793501d48SJacob Keller sizeof(adapter->tstamp_config));
114893501d48SJacob Keller
11493a6a4edaSJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
11503a6a4edaSJacob Keller -EFAULT : 0;
11513a6a4edaSJacob Keller }
11523a6a4edaSJacob Keller
ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter * adapter,u32 * shift,u32 * incval)1153a9763f3cSMark Rustad static void ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter *adapter,
1154a9763f3cSMark Rustad u32 *shift, u32 *incval)
11553a6a4edaSJacob Keller {
11563a6a4edaSJacob Keller /**
11573a6a4edaSJacob Keller * Scale the NIC cycle counter by a large factor so that
11583a6a4edaSJacob Keller * relatively small corrections to the frequency can be added
11593a6a4edaSJacob Keller * or subtracted. The drawbacks of a large factor include
11603a6a4edaSJacob Keller * (a) the clock register overflows more quickly, (b) the cycle
11613a6a4edaSJacob Keller * counter structure must be able to convert the systime value
11623a6a4edaSJacob Keller * to nanoseconds using only a multiplier and a right-shift,
11633a6a4edaSJacob Keller * and (c) the value must fit within the timinca register space
11643a6a4edaSJacob Keller * => math based on internal DMA clock rate and available bits
11651a71ab24SJacob Keller *
11661a71ab24SJacob Keller * Note that when there is no link, internal DMA clock is same as when
11671a71ab24SJacob Keller * link speed is 10Gb. Set the registers correctly even when link is
11681a71ab24SJacob Keller * down to preserve the clock setting
11693a6a4edaSJacob Keller */
11701a71ab24SJacob Keller switch (adapter->link_speed) {
11713a6a4edaSJacob Keller case IXGBE_LINK_SPEED_100_FULL:
1172a9763f3cSMark Rustad *shift = IXGBE_INCVAL_SHIFT_100;
1173a9763f3cSMark Rustad *incval = IXGBE_INCVAL_100;
11743a6a4edaSJacob Keller break;
11753a6a4edaSJacob Keller case IXGBE_LINK_SPEED_1GB_FULL:
1176a9763f3cSMark Rustad *shift = IXGBE_INCVAL_SHIFT_1GB;
1177a9763f3cSMark Rustad *incval = IXGBE_INCVAL_1GB;
11783a6a4edaSJacob Keller break;
11793a6a4edaSJacob Keller case IXGBE_LINK_SPEED_10GB_FULL:
11801a71ab24SJacob Keller default:
1181a9763f3cSMark Rustad *shift = IXGBE_INCVAL_SHIFT_10GB;
1182a9763f3cSMark Rustad *incval = IXGBE_INCVAL_10GB;
11833a6a4edaSJacob Keller break;
11843a6a4edaSJacob Keller }
1185a9763f3cSMark Rustad }
11863a6a4edaSJacob Keller
11873a6a4edaSJacob Keller /**
1188a9763f3cSMark Rustad * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
1189a9763f3cSMark Rustad * @adapter: pointer to the adapter structure
1190a9763f3cSMark Rustad *
1191a9763f3cSMark Rustad * This function should be called to set the proper values for the TIMINCA
1192a9763f3cSMark Rustad * register and tell the cyclecounter structure what the tick rate of SYSTIME
1193a9763f3cSMark Rustad * is. It does not directly modify SYSTIME registers or the timecounter
1194a9763f3cSMark Rustad * structure. It should be called whenever a new TIMINCA value is necessary,
1195a9763f3cSMark Rustad * such as during initialization or when the link speed changes.
11963a6a4edaSJacob Keller */
ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter * adapter)1197a9763f3cSMark Rustad void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
1198a9763f3cSMark Rustad {
1199a9763f3cSMark Rustad struct ixgbe_hw *hw = &adapter->hw;
1200a9763f3cSMark Rustad struct cyclecounter cc;
1201a9763f3cSMark Rustad unsigned long flags;
1202a9763f3cSMark Rustad u32 incval = 0;
1203a9763f3cSMark Rustad u32 fuse0 = 0;
1204a9763f3cSMark Rustad
1205a9763f3cSMark Rustad /* For some of the boards below this mask is technically incorrect.
1206a9763f3cSMark Rustad * The timestamp mask overflows at approximately 61bits. However the
1207a9763f3cSMark Rustad * particular hardware does not overflow on an even bitmask value.
1208a9763f3cSMark Rustad * Instead, it overflows due to conversion of upper 32bits billions of
1209a9763f3cSMark Rustad * cycles. Timecounters are not really intended for this purpose so
1210a9763f3cSMark Rustad * they do not properly function if the overflow point isn't 2^N-1.
1211a9763f3cSMark Rustad * However, the actual SYSTIME values in question take ~138 years to
1212a9763f3cSMark Rustad * overflow. In practice this means they won't actually overflow. A
1213a9763f3cSMark Rustad * proper fix to this problem would require modification of the
1214a9763f3cSMark Rustad * timecounter delta calculations.
1215a9763f3cSMark Rustad */
1216a9763f3cSMark Rustad cc.mask = CLOCKSOURCE_MASK(64);
1217a9763f3cSMark Rustad cc.mult = 1;
1218a9763f3cSMark Rustad cc.shift = 0;
1219a9763f3cSMark Rustad
12203a6a4edaSJacob Keller switch (hw->mac.type) {
1221a9763f3cSMark Rustad case ixgbe_mac_X550EM_x:
1222a9763f3cSMark Rustad /* SYSTIME assumes X550EM_x board frequency is 300Mhz, and is
1223a9763f3cSMark Rustad * designed to represent seconds and nanoseconds when this is
1224a9763f3cSMark Rustad * the case. However, some revisions of hardware have a 400Mhz
1225a9763f3cSMark Rustad * clock and we have to compensate for this frequency
1226a9763f3cSMark Rustad * variation using corrected mult and shift values.
1227a9763f3cSMark Rustad */
1228a9763f3cSMark Rustad fuse0 = IXGBE_READ_REG(hw, IXGBE_FUSES0_GROUP(0));
1229a9763f3cSMark Rustad if (!(fuse0 & IXGBE_FUSES0_300MHZ)) {
1230a9763f3cSMark Rustad cc.mult = 3;
1231a9763f3cSMark Rustad cc.shift = 2;
1232a9763f3cSMark Rustad }
12335463fce6SJeff Kirsher fallthrough;
123449425dfcSMark Rustad case ixgbe_mac_x550em_a:
1235a9763f3cSMark Rustad case ixgbe_mac_X550:
1236a9763f3cSMark Rustad cc.read = ixgbe_ptp_read_X550;
1237a9763f3cSMark Rustad break;
12383a6a4edaSJacob Keller case ixgbe_mac_X540:
1239a9763f3cSMark Rustad cc.read = ixgbe_ptp_read_82599;
1240a9763f3cSMark Rustad
1241a9763f3cSMark Rustad ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
12423a6a4edaSJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
12433a6a4edaSJacob Keller break;
12443a6a4edaSJacob Keller case ixgbe_mac_82599EB:
1245a9763f3cSMark Rustad cc.read = ixgbe_ptp_read_82599;
1246a9763f3cSMark Rustad
1247a9763f3cSMark Rustad ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
12483a6a4edaSJacob Keller incval >>= IXGBE_INCVAL_SHIFT_82599;
1249a9763f3cSMark Rustad cc.shift -= IXGBE_INCVAL_SHIFT_82599;
12503a6a4edaSJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
1251b4f47a48SJacob Keller BIT(IXGBE_INCPER_SHIFT_82599) | incval);
12523a6a4edaSJacob Keller break;
12533a6a4edaSJacob Keller default:
12543a6a4edaSJacob Keller /* other devices aren't supported */
12553a6a4edaSJacob Keller return;
12563a6a4edaSJacob Keller }
12573a6a4edaSJacob Keller
12581a71ab24SJacob Keller /* update the base incval used to calculate frequency adjustment */
12596aa7de05SMark Rutland WRITE_ONCE(adapter->base_incval, incval);
12603a6a4edaSJacob Keller smp_mb();
12613a6a4edaSJacob Keller
12621a71ab24SJacob Keller /* need lock to prevent incorrect read while modifying cyclecounter */
12633a6a4edaSJacob Keller spin_lock_irqsave(&adapter->tmreg_lock, flags);
1264a9763f3cSMark Rustad memcpy(&adapter->hw_cc, &cc, sizeof(adapter->hw_cc));
12651a71ab24SJacob Keller spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
12661a71ab24SJacob Keller }
12671a71ab24SJacob Keller
12681a71ab24SJacob Keller /**
126925d7a5f5SJacob Keller * ixgbe_ptp_init_systime - Initialize SYSTIME registers
127025d7a5f5SJacob Keller * @adapter: the ixgbe private board structure
127125d7a5f5SJacob Keller *
127225d7a5f5SJacob Keller * Initialize and start the SYSTIME registers.
127325d7a5f5SJacob Keller */
ixgbe_ptp_init_systime(struct ixgbe_adapter * adapter)127425d7a5f5SJacob Keller static void ixgbe_ptp_init_systime(struct ixgbe_adapter *adapter)
127525d7a5f5SJacob Keller {
127625d7a5f5SJacob Keller struct ixgbe_hw *hw = &adapter->hw;
127725d7a5f5SJacob Keller u32 tsauxc;
127825d7a5f5SJacob Keller
127925d7a5f5SJacob Keller switch (hw->mac.type) {
128025d7a5f5SJacob Keller case ixgbe_mac_X550EM_x:
128125d7a5f5SJacob Keller case ixgbe_mac_x550em_a:
128225d7a5f5SJacob Keller case ixgbe_mac_X550:
128325d7a5f5SJacob Keller tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC);
128425d7a5f5SJacob Keller
128525d7a5f5SJacob Keller /* Reset SYSTIME registers to 0 */
128625d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_SYSTIMR, 0);
128725d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0);
128825d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0);
128925d7a5f5SJacob Keller
129025d7a5f5SJacob Keller /* Reset interrupt settings */
129125d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSIM, IXGBE_TSIM_TXTS);
129225d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_TIMESYNC);
129325d7a5f5SJacob Keller
129425d7a5f5SJacob Keller /* Activate the SYSTIME counter */
129525d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_TSAUXC,
129625d7a5f5SJacob Keller tsauxc & ~IXGBE_TSAUXC_DISABLE_SYSTIME);
129725d7a5f5SJacob Keller break;
129825d7a5f5SJacob Keller case ixgbe_mac_X540:
129925d7a5f5SJacob Keller case ixgbe_mac_82599EB:
130025d7a5f5SJacob Keller /* Reset SYSTIME registers to 0 */
130125d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0);
130225d7a5f5SJacob Keller IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0);
130325d7a5f5SJacob Keller break;
130425d7a5f5SJacob Keller default:
130525d7a5f5SJacob Keller /* Other devices aren't supported */
130625d7a5f5SJacob Keller return;
1307f49fafa5SYang Li }
130825d7a5f5SJacob Keller
130925d7a5f5SJacob Keller IXGBE_WRITE_FLUSH(hw);
131025d7a5f5SJacob Keller }
131125d7a5f5SJacob Keller
131225d7a5f5SJacob Keller /**
13131a71ab24SJacob Keller * ixgbe_ptp_reset
13141a71ab24SJacob Keller * @adapter: the ixgbe private board structure
13151a71ab24SJacob Keller *
1316d6321407SJacob Keller * When the MAC resets, all the hardware bits for timesync are reset. This
1317d6321407SJacob Keller * function is used to re-enable the device for PTP based on current settings.
1318d6321407SJacob Keller * We do lose the current clock time, so just reset the cyclecounter to the
1319d6321407SJacob Keller * system real clock time.
1320d6321407SJacob Keller *
1321d6321407SJacob Keller * This function will maintain hwtstamp_config settings, and resets the SDP
1322d6321407SJacob Keller * output if it was enabled.
13231a71ab24SJacob Keller */
ixgbe_ptp_reset(struct ixgbe_adapter * adapter)13241a71ab24SJacob Keller void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
13251a71ab24SJacob Keller {
13261a71ab24SJacob Keller struct ixgbe_hw *hw = &adapter->hw;
13271a71ab24SJacob Keller unsigned long flags;
13281a71ab24SJacob Keller
1329d6321407SJacob Keller /* reset the hardware timestamping mode */
1330d6321407SJacob Keller ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
133193501d48SJacob Keller
1332a9763f3cSMark Rustad /* 82598 does not support PTP */
1333a9763f3cSMark Rustad if (hw->mac.type == ixgbe_mac_82598EB)
1334a9763f3cSMark Rustad return;
1335a9763f3cSMark Rustad
13361a71ab24SJacob Keller ixgbe_ptp_start_cyclecounter(adapter);
13371a71ab24SJacob Keller
133825d7a5f5SJacob Keller ixgbe_ptp_init_systime(adapter);
133925d7a5f5SJacob Keller
13401a71ab24SJacob Keller spin_lock_irqsave(&adapter->tmreg_lock, flags);
1341a9763f3cSMark Rustad timecounter_init(&adapter->hw_tc, &adapter->hw_cc,
13423a6a4edaSJacob Keller ktime_to_ns(ktime_get_real()));
13433a6a4edaSJacob Keller spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
134482083673SJacob Keller
1345a9763f3cSMark Rustad adapter->last_overflow_check = jiffies;
1346a9763f3cSMark Rustad
1347a9763f3cSMark Rustad /* Now that the shift has been calculated and the systime
134882083673SJacob Keller * registers reset, (re-)enable the Clock out feature
134982083673SJacob Keller */
1350a9763f3cSMark Rustad if (adapter->ptp_setup_sdp)
1351a9763f3cSMark Rustad adapter->ptp_setup_sdp(adapter);
13523a6a4edaSJacob Keller }
13533a6a4edaSJacob Keller
13543a6a4edaSJacob Keller /**
135563328adaSJacob Keller * ixgbe_ptp_create_clock
135649ce9c2cSBen Hutchings * @adapter: the ixgbe private adapter structure
13573a6a4edaSJacob Keller *
135863328adaSJacob Keller * This function performs setup of the user entry point function table and
135963328adaSJacob Keller * initializes the PTP clock device, which is used to access the clock-like
1360a9763f3cSMark Rustad * features of the PTP core. It will be called by ixgbe_ptp_init, and may
1361a9763f3cSMark Rustad * reuse a previously initialized clock (such as during a suspend/resume
1362a9763f3cSMark Rustad * cycle).
13633a6a4edaSJacob Keller */
ixgbe_ptp_create_clock(struct ixgbe_adapter * adapter)1364a9763f3cSMark Rustad static long ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
13653a6a4edaSJacob Keller {
13663a6a4edaSJacob Keller struct net_device *netdev = adapter->netdev;
136763328adaSJacob Keller long err;
136863328adaSJacob Keller
136963328adaSJacob Keller /* do nothing if we already have a clock device */
137063328adaSJacob Keller if (!IS_ERR_OR_NULL(adapter->ptp_clock))
137163328adaSJacob Keller return 0;
13723a6a4edaSJacob Keller
13733a6a4edaSJacob Keller switch (adapter->hw.mac.type) {
13743a6a4edaSJacob Keller case ixgbe_mac_X540:
1375ca324099SJacob Keller snprintf(adapter->ptp_caps.name,
1376ca324099SJacob Keller sizeof(adapter->ptp_caps.name),
1377ca324099SJacob Keller "%s", netdev->name);
1378681ae1adSJacob E Keller adapter->ptp_caps.owner = THIS_MODULE;
1379681ae1adSJacob E Keller adapter->ptp_caps.max_adj = 250000000;
1380681ae1adSJacob E Keller adapter->ptp_caps.n_alarm = 0;
1381681ae1adSJacob E Keller adapter->ptp_caps.n_ext_ts = 0;
1382681ae1adSJacob E Keller adapter->ptp_caps.n_per_out = 0;
1383681ae1adSJacob E Keller adapter->ptp_caps.pps = 1;
13845a554232SJacob Keller adapter->ptp_caps.adjfine = ixgbe_ptp_adjfine_82599;
1385681ae1adSJacob E Keller adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1386018ed23dSMiroslav Lichvar adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
138791432d18SRichard Cochran adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
138804c8de8eSJacob Keller adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
138968d9676fSJacob Keller adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X540;
1390681ae1adSJacob E Keller break;
13913a6a4edaSJacob Keller case ixgbe_mac_82599EB:
1392ca324099SJacob Keller snprintf(adapter->ptp_caps.name,
1393ca324099SJacob Keller sizeof(adapter->ptp_caps.name),
1394ca324099SJacob Keller "%s", netdev->name);
13953a6a4edaSJacob Keller adapter->ptp_caps.owner = THIS_MODULE;
13963a6a4edaSJacob Keller adapter->ptp_caps.max_adj = 250000000;
13973a6a4edaSJacob Keller adapter->ptp_caps.n_alarm = 0;
13983a6a4edaSJacob Keller adapter->ptp_caps.n_ext_ts = 0;
13993a6a4edaSJacob Keller adapter->ptp_caps.n_per_out = 0;
14003a6a4edaSJacob Keller adapter->ptp_caps.pps = 0;
14015a554232SJacob Keller adapter->ptp_caps.adjfine = ixgbe_ptp_adjfine_82599;
14023a6a4edaSJacob Keller adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1403018ed23dSMiroslav Lichvar adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
140491432d18SRichard Cochran adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
140504c8de8eSJacob Keller adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
14063a6a4edaSJacob Keller break;
1407a9763f3cSMark Rustad case ixgbe_mac_X550:
1408a9763f3cSMark Rustad case ixgbe_mac_X550EM_x:
140949425dfcSMark Rustad case ixgbe_mac_x550em_a:
1410a9763f3cSMark Rustad snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
1411a9763f3cSMark Rustad adapter->ptp_caps.owner = THIS_MODULE;
1412a9763f3cSMark Rustad adapter->ptp_caps.max_adj = 30000000;
1413a9763f3cSMark Rustad adapter->ptp_caps.n_alarm = 0;
1414a9763f3cSMark Rustad adapter->ptp_caps.n_ext_ts = 0;
1415a9763f3cSMark Rustad adapter->ptp_caps.n_per_out = 0;
1416cd458320SJacob Keller adapter->ptp_caps.pps = 1;
14175a554232SJacob Keller adapter->ptp_caps.adjfine = ixgbe_ptp_adjfine_X550;
1418a9763f3cSMark Rustad adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1419018ed23dSMiroslav Lichvar adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
1420a9763f3cSMark Rustad adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1421a9763f3cSMark Rustad adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1422cd458320SJacob Keller adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X550;
1423a9763f3cSMark Rustad break;
14243a6a4edaSJacob Keller default:
14253a6a4edaSJacob Keller adapter->ptp_clock = NULL;
1426a9763f3cSMark Rustad adapter->ptp_setup_sdp = NULL;
142763328adaSJacob Keller return -EOPNOTSUPP;
14283a6a4edaSJacob Keller }
14293a6a4edaSJacob Keller
14301ef76158SRichard Cochran adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
14311ef76158SRichard Cochran &adapter->pdev->dev);
14323a6a4edaSJacob Keller if (IS_ERR(adapter->ptp_clock)) {
143363328adaSJacob Keller err = PTR_ERR(adapter->ptp_clock);
14343a6a4edaSJacob Keller adapter->ptp_clock = NULL;
14353a6a4edaSJacob Keller e_dev_err("ptp_clock_register failed\n");
143663328adaSJacob Keller return err;
1437efee95f4SNicolas Pitre } else if (adapter->ptp_clock)
14383a6a4edaSJacob Keller e_dev_info("registered PHC device on %s\n", netdev->name);
14393a6a4edaSJacob Keller
144063328adaSJacob Keller /* set default timestamp mode to disabled here. We do this in
144163328adaSJacob Keller * create_clock instead of init, because we don't want to override the
144263328adaSJacob Keller * previous settings during a resume cycle.
144363328adaSJacob Keller */
1444d6321407SJacob Keller adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
1445d6321407SJacob Keller adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
144663328adaSJacob Keller
144763328adaSJacob Keller return 0;
144863328adaSJacob Keller }
144963328adaSJacob Keller
145063328adaSJacob Keller /**
145163328adaSJacob Keller * ixgbe_ptp_init
145263328adaSJacob Keller * @adapter: the ixgbe private adapter structure
145363328adaSJacob Keller *
145463328adaSJacob Keller * This function performs the required steps for enabling PTP
145563328adaSJacob Keller * support. If PTP support has already been loaded it simply calls the
145663328adaSJacob Keller * cyclecounter init routine and exits.
145763328adaSJacob Keller */
ixgbe_ptp_init(struct ixgbe_adapter * adapter)145863328adaSJacob Keller void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
145963328adaSJacob Keller {
146063328adaSJacob Keller /* initialize the spin lock first since we can't control when a user
146163328adaSJacob Keller * will call the entry functions once we have initialized the clock
146263328adaSJacob Keller * device
146363328adaSJacob Keller */
146463328adaSJacob Keller spin_lock_init(&adapter->tmreg_lock);
146563328adaSJacob Keller
146663328adaSJacob Keller /* obtain a PTP device, or re-use an existing device */
146763328adaSJacob Keller if (ixgbe_ptp_create_clock(adapter))
146863328adaSJacob Keller return;
146963328adaSJacob Keller
147063328adaSJacob Keller /* we have a clock so we can initialize work now */
147163328adaSJacob Keller INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
147263328adaSJacob Keller
147363328adaSJacob Keller /* reset the PTP related hardware bits */
14741a71ab24SJacob Keller ixgbe_ptp_reset(adapter);
14751a71ab24SJacob Keller
14768fecf67cSJacob Keller /* enter the IXGBE_PTP_RUNNING state */
14778fecf67cSJacob Keller set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
14781a71ab24SJacob Keller
14793a6a4edaSJacob Keller return;
14803a6a4edaSJacob Keller }
14813a6a4edaSJacob Keller
14823a6a4edaSJacob Keller /**
14839966d1eeSJacob Keller * ixgbe_ptp_suspend - stop PTP work items
14843a6a4edaSJacob Keller * @adapter: pointer to adapter struct
14853a6a4edaSJacob Keller *
14869966d1eeSJacob Keller * this function suspends PTP activity, and prevents more PTP work from being
14879966d1eeSJacob Keller * generated, but does not destroy the PTP clock device.
14883a6a4edaSJacob Keller */
ixgbe_ptp_suspend(struct ixgbe_adapter * adapter)14899966d1eeSJacob Keller void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
14903a6a4edaSJacob Keller {
14918fecf67cSJacob Keller /* Leave the IXGBE_PTP_RUNNING state. */
14928fecf67cSJacob Keller if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
14938fecf67cSJacob Keller return;
1494db0677faSJacob Keller
1495a9763f3cSMark Rustad adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
1496a9763f3cSMark Rustad if (adapter->ptp_setup_sdp)
1497a9763f3cSMark Rustad adapter->ptp_setup_sdp(adapter);
14983a6a4edaSJacob Keller
14999966d1eeSJacob Keller /* ensure that we cancel any pending PTP Tx work item in progress */
1500891dc082SJacob Keller cancel_work_sync(&adapter->ptp_tx_work);
1501a9763f3cSMark Rustad ixgbe_ptp_clear_tx_timestamp(adapter);
15029966d1eeSJacob Keller }
1503891dc082SJacob Keller
15049966d1eeSJacob Keller /**
15059966d1eeSJacob Keller * ixgbe_ptp_stop - close the PTP device
15069966d1eeSJacob Keller * @adapter: pointer to adapter struct
15079966d1eeSJacob Keller *
15089966d1eeSJacob Keller * completely destroy the PTP device, should only be called when the device is
15099966d1eeSJacob Keller * being fully closed.
15109966d1eeSJacob Keller */
ixgbe_ptp_stop(struct ixgbe_adapter * adapter)15119966d1eeSJacob Keller void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
15129966d1eeSJacob Keller {
15139966d1eeSJacob Keller /* first, suspend PTP activity */
15149966d1eeSJacob Keller ixgbe_ptp_suspend(adapter);
15159966d1eeSJacob Keller
15169966d1eeSJacob Keller /* disable the PTP clock device */
15173a6a4edaSJacob Keller if (adapter->ptp_clock) {
15183a6a4edaSJacob Keller ptp_clock_unregister(adapter->ptp_clock);
15193a6a4edaSJacob Keller adapter->ptp_clock = NULL;
15203a6a4edaSJacob Keller e_dev_info("removed PHC on %s\n",
15213a6a4edaSJacob Keller adapter->netdev->name);
15223a6a4edaSJacob Keller }
15233a6a4edaSJacob Keller }
1524