1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 1999 - 2018 Intel Corporation. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include "ixgbe.h"
5*4882a593Smuzhiyun #include <linux/ptp_classify.h>
6*4882a593Smuzhiyun #include <linux/clocksource.h>
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun * The 82599 and the X540 do not have true 64bit nanosecond scale
10*4882a593Smuzhiyun * counter registers. Instead, SYSTIME is defined by a fixed point
11*4882a593Smuzhiyun * system which allows the user to define the scale counter increment
12*4882a593Smuzhiyun * value at every level change of the oscillator driving the SYSTIME
13*4882a593Smuzhiyun * value. For both devices the TIMINCA:IV field defines this
14*4882a593Smuzhiyun * increment. On the X540 device, 31 bits are provided. However on the
15*4882a593Smuzhiyun * 82599 only provides 24 bits. The time unit is determined by the
16*4882a593Smuzhiyun * clock frequency of the oscillator in combination with the TIMINCA
17*4882a593Smuzhiyun * register. When these devices link at 10Gb the oscillator has a
18*4882a593Smuzhiyun * period of 6.4ns. In order to convert the scale counter into
19*4882a593Smuzhiyun * nanoseconds the cyclecounter and timecounter structures are
20*4882a593Smuzhiyun * used. The SYSTIME registers need to be converted to ns values by use
21*4882a593Smuzhiyun * of only a right shift (division by power of 2). The following math
22*4882a593Smuzhiyun * determines the largest incvalue that will fit into the available
23*4882a593Smuzhiyun * bits in the TIMINCA register.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * PeriodWidth: Number of bits to store the clock period
26*4882a593Smuzhiyun * MaxWidth: The maximum width value of the TIMINCA register
27*4882a593Smuzhiyun * Period: The clock period for the oscillator
28*4882a593Smuzhiyun * round(): discard the fractional portion of the calculation
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
33*4882a593Smuzhiyun * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * The period also changes based on the link speed:
36*4882a593Smuzhiyun * At 10Gb link or no link, the period remains the same.
37*4882a593Smuzhiyun * At 1Gb link, the period is multiplied by 10. (64ns)
38*4882a593Smuzhiyun * At 100Mb link, the period is multiplied by 100. (640ns)
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * The calculated value allows us to right shift the SYSTIME register
41*4882a593Smuzhiyun * value in order to quickly convert it into a nanosecond clock,
42*4882a593Smuzhiyun * while allowing for the maximum possible adjustment value.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * These diagrams are only for the 10Gb link period
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * SYSTIMEH SYSTIMEL
47*4882a593Smuzhiyun * +--------------+ +--------------+
48*4882a593Smuzhiyun * X540 | 32 | | 1 | 3 | 28 |
49*4882a593Smuzhiyun * *--------------+ +--------------+
50*4882a593Smuzhiyun * \________ 36 bits ______/ fract
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * +--------------+ +--------------+
53*4882a593Smuzhiyun * 82599 | 32 | | 8 | 3 | 21 |
54*4882a593Smuzhiyun * *--------------+ +--------------+
55*4882a593Smuzhiyun * \________ 43 bits ______/ fract
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * The 36 bit X540 SYSTIME overflows every
58*4882a593Smuzhiyun * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * The 43 bit 82599 SYSTIME overflows every
61*4882a593Smuzhiyun * 2^43 * 10^-9 / 3600 = 2.4 hours
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun #define IXGBE_INCVAL_10GB 0x66666666
64*4882a593Smuzhiyun #define IXGBE_INCVAL_1GB 0x40000000
65*4882a593Smuzhiyun #define IXGBE_INCVAL_100 0x50000000
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #define IXGBE_INCVAL_SHIFT_10GB 28
68*4882a593Smuzhiyun #define IXGBE_INCVAL_SHIFT_1GB 24
69*4882a593Smuzhiyun #define IXGBE_INCVAL_SHIFT_100 21
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #define IXGBE_INCVAL_SHIFT_82599 7
72*4882a593Smuzhiyun #define IXGBE_INCPER_SHIFT_82599 24
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #define IXGBE_OVERFLOW_PERIOD (HZ * 30)
75*4882a593Smuzhiyun #define IXGBE_PTP_TX_TIMEOUT (HZ)
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* We use our own definitions instead of NSEC_PER_SEC because we want to mark
78*4882a593Smuzhiyun * the value as a ULL to force precision when bit shifting.
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun #define NS_PER_SEC 1000000000ULL
81*4882a593Smuzhiyun #define NS_PER_HALF_SEC 500000000ULL
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* In contrast, the X550 controller has two registers, SYSTIMEH and SYSTIMEL
84*4882a593Smuzhiyun * which contain measurements of seconds and nanoseconds respectively. This
85*4882a593Smuzhiyun * matches the standard linux representation of time in the kernel. In addition,
86*4882a593Smuzhiyun * the X550 also has a SYSTIMER register which represents residue, or
87*4882a593Smuzhiyun * subnanosecond overflow adjustments. To control clock adjustment, the TIMINCA
88*4882a593Smuzhiyun * register is used, but it is unlike the X540 and 82599 devices. TIMINCA
89*4882a593Smuzhiyun * represents units of 2^-32 nanoseconds, and uses 31 bits for this, with the
90*4882a593Smuzhiyun * high bit representing whether the adjustent is positive or negative. Every
91*4882a593Smuzhiyun * clock cycle, the X550 will add 12.5 ns + TIMINCA which can result in a range
92*4882a593Smuzhiyun * of 12 to 13 nanoseconds adjustment. Unlike the 82599 and X540 devices, the
93*4882a593Smuzhiyun * X550's clock for purposes of SYSTIME generation is constant and not dependent
94*4882a593Smuzhiyun * on the link speed.
95*4882a593Smuzhiyun *
96*4882a593Smuzhiyun * SYSTIMEH SYSTIMEL SYSTIMER
97*4882a593Smuzhiyun * +--------------+ +--------------+ +-------------+
98*4882a593Smuzhiyun * X550 | 32 | | 32 | | 32 |
99*4882a593Smuzhiyun * *--------------+ +--------------+ +-------------+
100*4882a593Smuzhiyun * \____seconds___/ \_nanoseconds_/ \__2^-32 ns__/
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * This results in a full 96 bits to represent the clock, with 32 bits for
103*4882a593Smuzhiyun * seconds, 32 bits for nanoseconds (largest value is 0d999999999 or just under
104*4882a593Smuzhiyun * 1 second) and an additional 32 bits to measure sub nanosecond adjustments for
105*4882a593Smuzhiyun * underflow of adjustments.
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * The 32 bits of seconds for the X550 overflows every
108*4882a593Smuzhiyun * 2^32 / ( 365.25 * 24 * 60 * 60 ) = ~136 years.
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * In order to adjust the clock frequency for the X550, the TIMINCA register is
111*4882a593Smuzhiyun * provided. This register represents a + or minus nearly 0.5 ns adjustment to
112*4882a593Smuzhiyun * the base frequency. It is measured in 2^-32 ns units, with the high bit being
113*4882a593Smuzhiyun * the sign bit. This register enables software to calculate frequency
114*4882a593Smuzhiyun * adjustments and apply them directly to the clock rate.
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * The math for converting ppb into TIMINCA values is fairly straightforward.
117*4882a593Smuzhiyun * TIMINCA value = ( Base_Frequency * ppb ) / 1000000000ULL
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * This assumes that ppb is never high enough to create a value bigger than
120*4882a593Smuzhiyun * TIMINCA's 31 bits can store. This is ensured by the stack. Calculating this
121*4882a593Smuzhiyun * value is also simple.
122*4882a593Smuzhiyun * Max ppb = ( Max Adjustment / Base Frequency ) / 1000000000ULL
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * For the X550, the Max adjustment is +/- 0.5 ns, and the base frequency is
125*4882a593Smuzhiyun * 12.5 nanoseconds. This means that the Max ppb is 39999999
126*4882a593Smuzhiyun * Note: We subtract one in order to ensure no overflow, because the TIMINCA
127*4882a593Smuzhiyun * register can only hold slightly under 0.5 nanoseconds.
128*4882a593Smuzhiyun *
129*4882a593Smuzhiyun * Because TIMINCA is measured in 2^-32 ns units, we have to convert 12.5 ns
130*4882a593Smuzhiyun * into 2^-32 units, which is
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * 12.5 * 2^32 = C80000000
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * Some revisions of hardware have a faster base frequency than the registers
135*4882a593Smuzhiyun * were defined for. To fix this, we use a timecounter structure with the
136*4882a593Smuzhiyun * proper mult and shift to convert the cycles into nanoseconds of time.
137*4882a593Smuzhiyun */
138*4882a593Smuzhiyun #define IXGBE_X550_BASE_PERIOD 0xC80000000ULL
139*4882a593Smuzhiyun #define INCVALUE_MASK 0x7FFFFFFF
140*4882a593Smuzhiyun #define ISGN 0x80000000
141*4882a593Smuzhiyun #define MAX_TIMADJ 0x7FFFFFFF
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun * ixgbe_ptp_setup_sdp_X540
145*4882a593Smuzhiyun * @adapter: private adapter structure
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun * this function enables or disables the clock out feature on SDP0 for
148*4882a593Smuzhiyun * the X540 device. It will create a 1 second periodic output that can
149*4882a593Smuzhiyun * be used as the PPS (via an interrupt).
150*4882a593Smuzhiyun *
151*4882a593Smuzhiyun * It calculates when the system time will be on an exact second, and then
152*4882a593Smuzhiyun * aligns the start of the PPS signal to that value.
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * This works by using the cycle counter shift and mult values in reverse, and
155*4882a593Smuzhiyun * assumes that the values we're shifting will not overflow.
156*4882a593Smuzhiyun */
ixgbe_ptp_setup_sdp_X540(struct ixgbe_adapter * adapter)157*4882a593Smuzhiyun static void ixgbe_ptp_setup_sdp_X540(struct ixgbe_adapter *adapter)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun struct cyclecounter *cc = &adapter->hw_cc;
160*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
161*4882a593Smuzhiyun u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
162*4882a593Smuzhiyun u64 ns = 0, clock_edge = 0, clock_period;
163*4882a593Smuzhiyun unsigned long flags;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* disable the pin first */
166*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
167*4882a593Smuzhiyun IXGBE_WRITE_FLUSH(hw);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
170*4882a593Smuzhiyun return;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* enable the SDP0 pin as output, and connected to the
175*4882a593Smuzhiyun * native function for Timesync (ClockOut)
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun esdp |= IXGBE_ESDP_SDP0_DIR |
178*4882a593Smuzhiyun IXGBE_ESDP_SDP0_NATIVE;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* enable the Clock Out feature on SDP0, and allow
181*4882a593Smuzhiyun * interrupts to occur when the pin changes
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun tsauxc = (IXGBE_TSAUXC_EN_CLK |
184*4882a593Smuzhiyun IXGBE_TSAUXC_SYNCLK |
185*4882a593Smuzhiyun IXGBE_TSAUXC_SDP0_INT);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /* Determine the clock time period to use. This assumes that the
188*4882a593Smuzhiyun * cycle counter shift is small enough to avoid overflow.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun clock_period = div_u64((NS_PER_HALF_SEC << cc->shift), cc->mult);
191*4882a593Smuzhiyun clktiml = (u32)(clock_period);
192*4882a593Smuzhiyun clktimh = (u32)(clock_period >> 32);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* Read the current clock time, and save the cycle counter value */
195*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
196*4882a593Smuzhiyun ns = timecounter_read(&adapter->hw_tc);
197*4882a593Smuzhiyun clock_edge = adapter->hw_tc.cycle_last;
198*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* Figure out how many seconds to add in order to round up */
201*4882a593Smuzhiyun div_u64_rem(ns, NS_PER_SEC, &rem);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /* Figure out how many nanoseconds to add to round the clock edge up
204*4882a593Smuzhiyun * to the next full second
205*4882a593Smuzhiyun */
206*4882a593Smuzhiyun rem = (NS_PER_SEC - rem);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* Adjust the clock edge to align with the next full second. */
209*4882a593Smuzhiyun clock_edge += div_u64(((u64)rem << cc->shift), cc->mult);
210*4882a593Smuzhiyun trgttiml = (u32)clock_edge;
211*4882a593Smuzhiyun trgttimh = (u32)(clock_edge >> 32);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
214*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
215*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
216*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
219*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun IXGBE_WRITE_FLUSH(hw);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun * ixgbe_ptp_setup_sdp_X550
226*4882a593Smuzhiyun * @adapter: private adapter structure
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * Enable or disable a clock output signal on SDP 0 for X550 hardware.
229*4882a593Smuzhiyun *
230*4882a593Smuzhiyun * Use the target time feature to align the output signal on the next full
231*4882a593Smuzhiyun * second.
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun * This works by using the cycle counter shift and mult values in reverse, and
234*4882a593Smuzhiyun * assumes that the values we're shifting will not overflow.
235*4882a593Smuzhiyun */
ixgbe_ptp_setup_sdp_X550(struct ixgbe_adapter * adapter)236*4882a593Smuzhiyun static void ixgbe_ptp_setup_sdp_X550(struct ixgbe_adapter *adapter)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun u32 esdp, tsauxc, freqout, trgttiml, trgttimh, rem, tssdp;
239*4882a593Smuzhiyun struct cyclecounter *cc = &adapter->hw_cc;
240*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
241*4882a593Smuzhiyun u64 ns = 0, clock_edge = 0;
242*4882a593Smuzhiyun struct timespec64 ts;
243*4882a593Smuzhiyun unsigned long flags;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* disable the pin first */
246*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
247*4882a593Smuzhiyun IXGBE_WRITE_FLUSH(hw);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
250*4882a593Smuzhiyun return;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* enable the SDP0 pin as output, and connected to the
255*4882a593Smuzhiyun * native function for Timesync (ClockOut)
256*4882a593Smuzhiyun */
257*4882a593Smuzhiyun esdp |= IXGBE_ESDP_SDP0_DIR |
258*4882a593Smuzhiyun IXGBE_ESDP_SDP0_NATIVE;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /* enable the Clock Out feature on SDP0, and use Target Time 0 to
261*4882a593Smuzhiyun * enable generation of interrupts on the clock change.
262*4882a593Smuzhiyun */
263*4882a593Smuzhiyun #define IXGBE_TSAUXC_DIS_TS_CLEAR 0x40000000
264*4882a593Smuzhiyun tsauxc = (IXGBE_TSAUXC_EN_CLK | IXGBE_TSAUXC_ST0 |
265*4882a593Smuzhiyun IXGBE_TSAUXC_EN_TT0 | IXGBE_TSAUXC_SDP0_INT |
266*4882a593Smuzhiyun IXGBE_TSAUXC_DIS_TS_CLEAR);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun tssdp = (IXGBE_TSSDP_TS_SDP0_EN |
269*4882a593Smuzhiyun IXGBE_TSSDP_TS_SDP0_CLK0);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* Determine the clock time period to use. This assumes that the
272*4882a593Smuzhiyun * cycle counter shift is small enough to avoid overflowing a 32bit
273*4882a593Smuzhiyun * value.
274*4882a593Smuzhiyun */
275*4882a593Smuzhiyun freqout = div_u64(NS_PER_HALF_SEC << cc->shift, cc->mult);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /* Read the current clock time, and save the cycle counter value */
278*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
279*4882a593Smuzhiyun ns = timecounter_read(&adapter->hw_tc);
280*4882a593Smuzhiyun clock_edge = adapter->hw_tc.cycle_last;
281*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /* Figure out how far past the next second we are */
284*4882a593Smuzhiyun div_u64_rem(ns, NS_PER_SEC, &rem);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun /* Figure out how many nanoseconds to add to round the clock edge up
287*4882a593Smuzhiyun * to the next full second
288*4882a593Smuzhiyun */
289*4882a593Smuzhiyun rem = (NS_PER_SEC - rem);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /* Adjust the clock edge to align with the next full second. */
292*4882a593Smuzhiyun clock_edge += div_u64(((u64)rem << cc->shift), cc->mult);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /* X550 hardware stores the time in 32bits of 'billions of cycles' and
295*4882a593Smuzhiyun * 32bits of 'cycles'. There's no guarantee that cycles represents
296*4882a593Smuzhiyun * nanoseconds. However, we can use the math from a timespec64 to
297*4882a593Smuzhiyun * convert into the hardware representation.
298*4882a593Smuzhiyun *
299*4882a593Smuzhiyun * See ixgbe_ptp_read_X550() for more details.
300*4882a593Smuzhiyun */
301*4882a593Smuzhiyun ts = ns_to_timespec64(clock_edge);
302*4882a593Smuzhiyun trgttiml = (u32)ts.tv_nsec;
303*4882a593Smuzhiyun trgttimh = (u32)ts.tv_sec;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_FREQOUT0, freqout);
306*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
307*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
310*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSSDP, tssdp);
311*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun IXGBE_WRITE_FLUSH(hw);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun /**
317*4882a593Smuzhiyun * ixgbe_ptp_read_X550 - read cycle counter value
318*4882a593Smuzhiyun * @cc: cyclecounter structure
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * This function reads SYSTIME registers. It is called by the cyclecounter
321*4882a593Smuzhiyun * structure to convert from internal representation into nanoseconds. We need
322*4882a593Smuzhiyun * this for X550 since some skews do not have expected clock frequency and
323*4882a593Smuzhiyun * result of SYSTIME is 32bits of "billions of cycles" and 32 bits of
324*4882a593Smuzhiyun * "cycles", rather than seconds and nanoseconds.
325*4882a593Smuzhiyun */
ixgbe_ptp_read_X550(const struct cyclecounter * cc)326*4882a593Smuzhiyun static u64 ixgbe_ptp_read_X550(const struct cyclecounter *cc)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
329*4882a593Smuzhiyun container_of(cc, struct ixgbe_adapter, hw_cc);
330*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
331*4882a593Smuzhiyun struct timespec64 ts;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /* storage is 32 bits of 'billions of cycles' and 32 bits of 'cycles'.
334*4882a593Smuzhiyun * Some revisions of hardware run at a higher frequency and so the
335*4882a593Smuzhiyun * cycles are not guaranteed to be nanoseconds. The timespec64 created
336*4882a593Smuzhiyun * here is used for its math/conversions but does not necessarily
337*4882a593Smuzhiyun * represent nominal time.
338*4882a593Smuzhiyun *
339*4882a593Smuzhiyun * It should be noted that this cyclecounter will overflow at a
340*4882a593Smuzhiyun * non-bitmask field since we have to convert our billions of cycles
341*4882a593Smuzhiyun * into an actual cycles count. This results in some possible weird
342*4882a593Smuzhiyun * situations at high cycle counter stamps. However given that 32 bits
343*4882a593Smuzhiyun * of "seconds" is ~138 years this isn't a problem. Even at the
344*4882a593Smuzhiyun * increased frequency of some revisions, this is still ~103 years.
345*4882a593Smuzhiyun * Since the SYSTIME values start at 0 and we never write them, it is
346*4882a593Smuzhiyun * highly unlikely for the cyclecounter to overflow in practice.
347*4882a593Smuzhiyun */
348*4882a593Smuzhiyun IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
349*4882a593Smuzhiyun ts.tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
350*4882a593Smuzhiyun ts.tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun return (u64)timespec64_to_ns(&ts);
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /**
356*4882a593Smuzhiyun * ixgbe_ptp_read_82599 - read raw cycle counter (to be used by time counter)
357*4882a593Smuzhiyun * @cc: the cyclecounter structure
358*4882a593Smuzhiyun *
359*4882a593Smuzhiyun * this function reads the cyclecounter registers and is called by the
360*4882a593Smuzhiyun * cyclecounter structure used to construct a ns counter from the
361*4882a593Smuzhiyun * arbitrary fixed point registers
362*4882a593Smuzhiyun */
ixgbe_ptp_read_82599(const struct cyclecounter * cc)363*4882a593Smuzhiyun static u64 ixgbe_ptp_read_82599(const struct cyclecounter *cc)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
366*4882a593Smuzhiyun container_of(cc, struct ixgbe_adapter, hw_cc);
367*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
368*4882a593Smuzhiyun u64 stamp = 0;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
371*4882a593Smuzhiyun stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun return stamp;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /**
377*4882a593Smuzhiyun * ixgbe_ptp_convert_to_hwtstamp - convert register value to hw timestamp
378*4882a593Smuzhiyun * @adapter: private adapter structure
379*4882a593Smuzhiyun * @hwtstamp: stack timestamp structure
380*4882a593Smuzhiyun * @timestamp: unsigned 64bit system time value
381*4882a593Smuzhiyun *
382*4882a593Smuzhiyun * We need to convert the adapter's RX/TXSTMP registers into a hwtstamp value
383*4882a593Smuzhiyun * which can be used by the stack's ptp functions.
384*4882a593Smuzhiyun *
385*4882a593Smuzhiyun * The lock is used to protect consistency of the cyclecounter and the SYSTIME
386*4882a593Smuzhiyun * registers. However, it does not need to protect against the Rx or Tx
387*4882a593Smuzhiyun * timestamp registers, as there can't be a new timestamp until the old one is
388*4882a593Smuzhiyun * unlatched by reading.
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * In addition to the timestamp in hardware, some controllers need a software
391*4882a593Smuzhiyun * overflow cyclecounter, and this function takes this into account as well.
392*4882a593Smuzhiyun **/
ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter * adapter,struct skb_shared_hwtstamps * hwtstamp,u64 timestamp)393*4882a593Smuzhiyun static void ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter *adapter,
394*4882a593Smuzhiyun struct skb_shared_hwtstamps *hwtstamp,
395*4882a593Smuzhiyun u64 timestamp)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun unsigned long flags;
398*4882a593Smuzhiyun struct timespec64 systime;
399*4882a593Smuzhiyun u64 ns;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun memset(hwtstamp, 0, sizeof(*hwtstamp));
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun switch (adapter->hw.mac.type) {
404*4882a593Smuzhiyun /* X550 and later hardware supposedly represent time using a seconds
405*4882a593Smuzhiyun * and nanoseconds counter, instead of raw 64bits nanoseconds. We need
406*4882a593Smuzhiyun * to convert the timestamp into cycles before it can be fed to the
407*4882a593Smuzhiyun * cyclecounter. We need an actual cyclecounter because some revisions
408*4882a593Smuzhiyun * of hardware run at a higher frequency and thus the counter does
409*4882a593Smuzhiyun * not represent seconds/nanoseconds. Instead it can be thought of as
410*4882a593Smuzhiyun * cycles and billions of cycles.
411*4882a593Smuzhiyun */
412*4882a593Smuzhiyun case ixgbe_mac_X550:
413*4882a593Smuzhiyun case ixgbe_mac_X550EM_x:
414*4882a593Smuzhiyun case ixgbe_mac_x550em_a:
415*4882a593Smuzhiyun /* Upper 32 bits represent billions of cycles, lower 32 bits
416*4882a593Smuzhiyun * represent cycles. However, we use timespec64_to_ns for the
417*4882a593Smuzhiyun * correct math even though the units haven't been corrected
418*4882a593Smuzhiyun * yet.
419*4882a593Smuzhiyun */
420*4882a593Smuzhiyun systime.tv_sec = timestamp >> 32;
421*4882a593Smuzhiyun systime.tv_nsec = timestamp & 0xFFFFFFFF;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun timestamp = timespec64_to_ns(&systime);
424*4882a593Smuzhiyun break;
425*4882a593Smuzhiyun default:
426*4882a593Smuzhiyun break;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
430*4882a593Smuzhiyun ns = timecounter_cyc2time(&adapter->hw_tc, timestamp);
431*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun hwtstamp->hwtstamp = ns_to_ktime(ns);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun /**
437*4882a593Smuzhiyun * ixgbe_ptp_adjfreq_82599
438*4882a593Smuzhiyun * @ptp: the ptp clock structure
439*4882a593Smuzhiyun * @ppb: parts per billion adjustment from base
440*4882a593Smuzhiyun *
441*4882a593Smuzhiyun * adjust the frequency of the ptp cycle counter by the
442*4882a593Smuzhiyun * indicated ppb from the base frequency.
443*4882a593Smuzhiyun */
ixgbe_ptp_adjfreq_82599(struct ptp_clock_info * ptp,s32 ppb)444*4882a593Smuzhiyun static int ixgbe_ptp_adjfreq_82599(struct ptp_clock_info *ptp, s32 ppb)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
447*4882a593Smuzhiyun container_of(ptp, struct ixgbe_adapter, ptp_caps);
448*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
449*4882a593Smuzhiyun u64 freq, incval;
450*4882a593Smuzhiyun u32 diff;
451*4882a593Smuzhiyun int neg_adj = 0;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun if (ppb < 0) {
454*4882a593Smuzhiyun neg_adj = 1;
455*4882a593Smuzhiyun ppb = -ppb;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun smp_mb();
459*4882a593Smuzhiyun incval = READ_ONCE(adapter->base_incval);
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun freq = incval;
462*4882a593Smuzhiyun freq *= ppb;
463*4882a593Smuzhiyun diff = div_u64(freq, 1000000000ULL);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun incval = neg_adj ? (incval - diff) : (incval + diff);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun switch (hw->mac.type) {
468*4882a593Smuzhiyun case ixgbe_mac_X540:
469*4882a593Smuzhiyun if (incval > 0xFFFFFFFFULL)
470*4882a593Smuzhiyun e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
471*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, (u32)incval);
472*4882a593Smuzhiyun break;
473*4882a593Smuzhiyun case ixgbe_mac_82599EB:
474*4882a593Smuzhiyun if (incval > 0x00FFFFFFULL)
475*4882a593Smuzhiyun e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
476*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
477*4882a593Smuzhiyun BIT(IXGBE_INCPER_SHIFT_82599) |
478*4882a593Smuzhiyun ((u32)incval & 0x00FFFFFFUL));
479*4882a593Smuzhiyun break;
480*4882a593Smuzhiyun default:
481*4882a593Smuzhiyun break;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun return 0;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /**
488*4882a593Smuzhiyun * ixgbe_ptp_adjfreq_X550
489*4882a593Smuzhiyun * @ptp: the ptp clock structure
490*4882a593Smuzhiyun * @ppb: parts per billion adjustment from base
491*4882a593Smuzhiyun *
492*4882a593Smuzhiyun * adjust the frequency of the SYSTIME registers by the indicated ppb from base
493*4882a593Smuzhiyun * frequency
494*4882a593Smuzhiyun */
ixgbe_ptp_adjfreq_X550(struct ptp_clock_info * ptp,s32 ppb)495*4882a593Smuzhiyun static int ixgbe_ptp_adjfreq_X550(struct ptp_clock_info *ptp, s32 ppb)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
498*4882a593Smuzhiyun container_of(ptp, struct ixgbe_adapter, ptp_caps);
499*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
500*4882a593Smuzhiyun int neg_adj = 0;
501*4882a593Smuzhiyun u64 rate = IXGBE_X550_BASE_PERIOD;
502*4882a593Smuzhiyun u32 inca;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun if (ppb < 0) {
505*4882a593Smuzhiyun neg_adj = 1;
506*4882a593Smuzhiyun ppb = -ppb;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun rate *= ppb;
509*4882a593Smuzhiyun rate = div_u64(rate, 1000000000ULL);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /* warn if rate is too large */
512*4882a593Smuzhiyun if (rate >= INCVALUE_MASK)
513*4882a593Smuzhiyun e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun inca = rate & INCVALUE_MASK;
516*4882a593Smuzhiyun if (neg_adj)
517*4882a593Smuzhiyun inca |= ISGN;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, inca);
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun return 0;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /**
525*4882a593Smuzhiyun * ixgbe_ptp_adjtime
526*4882a593Smuzhiyun * @ptp: the ptp clock structure
527*4882a593Smuzhiyun * @delta: offset to adjust the cycle counter by
528*4882a593Smuzhiyun *
529*4882a593Smuzhiyun * adjust the timer by resetting the timecounter structure.
530*4882a593Smuzhiyun */
ixgbe_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)531*4882a593Smuzhiyun static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
534*4882a593Smuzhiyun container_of(ptp, struct ixgbe_adapter, ptp_caps);
535*4882a593Smuzhiyun unsigned long flags;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
538*4882a593Smuzhiyun timecounter_adjtime(&adapter->hw_tc, delta);
539*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun if (adapter->ptp_setup_sdp)
542*4882a593Smuzhiyun adapter->ptp_setup_sdp(adapter);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun return 0;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun /**
548*4882a593Smuzhiyun * ixgbe_ptp_gettimex
549*4882a593Smuzhiyun * @ptp: the ptp clock structure
550*4882a593Smuzhiyun * @ts: timespec to hold the PHC timestamp
551*4882a593Smuzhiyun * @sts: structure to hold the system time before and after reading the PHC
552*4882a593Smuzhiyun *
553*4882a593Smuzhiyun * read the timecounter and return the correct value on ns,
554*4882a593Smuzhiyun * after converting it into a struct timespec.
555*4882a593Smuzhiyun */
ixgbe_ptp_gettimex(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)556*4882a593Smuzhiyun static int ixgbe_ptp_gettimex(struct ptp_clock_info *ptp,
557*4882a593Smuzhiyun struct timespec64 *ts,
558*4882a593Smuzhiyun struct ptp_system_timestamp *sts)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
561*4882a593Smuzhiyun container_of(ptp, struct ixgbe_adapter, ptp_caps);
562*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
563*4882a593Smuzhiyun unsigned long flags;
564*4882a593Smuzhiyun u64 ns, stamp;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun switch (adapter->hw.mac.type) {
569*4882a593Smuzhiyun case ixgbe_mac_X550:
570*4882a593Smuzhiyun case ixgbe_mac_X550EM_x:
571*4882a593Smuzhiyun case ixgbe_mac_x550em_a:
572*4882a593Smuzhiyun /* Upper 32 bits represent billions of cycles, lower 32 bits
573*4882a593Smuzhiyun * represent cycles. However, we use timespec64_to_ns for the
574*4882a593Smuzhiyun * correct math even though the units haven't been corrected
575*4882a593Smuzhiyun * yet.
576*4882a593Smuzhiyun */
577*4882a593Smuzhiyun ptp_read_system_prets(sts);
578*4882a593Smuzhiyun IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
579*4882a593Smuzhiyun ptp_read_system_postts(sts);
580*4882a593Smuzhiyun ts->tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
581*4882a593Smuzhiyun ts->tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
582*4882a593Smuzhiyun stamp = timespec64_to_ns(ts);
583*4882a593Smuzhiyun break;
584*4882a593Smuzhiyun default:
585*4882a593Smuzhiyun ptp_read_system_prets(sts);
586*4882a593Smuzhiyun stamp = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
587*4882a593Smuzhiyun ptp_read_system_postts(sts);
588*4882a593Smuzhiyun stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
589*4882a593Smuzhiyun break;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun ns = timecounter_cyc2time(&adapter->hw_tc, stamp);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun *ts = ns_to_timespec64(ns);
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun return 0;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /**
602*4882a593Smuzhiyun * ixgbe_ptp_settime
603*4882a593Smuzhiyun * @ptp: the ptp clock structure
604*4882a593Smuzhiyun * @ts: the timespec containing the new time for the cycle counter
605*4882a593Smuzhiyun *
606*4882a593Smuzhiyun * reset the timecounter to use a new base value instead of the kernel
607*4882a593Smuzhiyun * wall timer value.
608*4882a593Smuzhiyun */
ixgbe_ptp_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)609*4882a593Smuzhiyun static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
610*4882a593Smuzhiyun const struct timespec64 *ts)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
613*4882a593Smuzhiyun container_of(ptp, struct ixgbe_adapter, ptp_caps);
614*4882a593Smuzhiyun unsigned long flags;
615*4882a593Smuzhiyun u64 ns = timespec64_to_ns(ts);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun /* reset the timecounter */
618*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
619*4882a593Smuzhiyun timecounter_init(&adapter->hw_tc, &adapter->hw_cc, ns);
620*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun if (adapter->ptp_setup_sdp)
623*4882a593Smuzhiyun adapter->ptp_setup_sdp(adapter);
624*4882a593Smuzhiyun return 0;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun /**
628*4882a593Smuzhiyun * ixgbe_ptp_feature_enable
629*4882a593Smuzhiyun * @ptp: the ptp clock structure
630*4882a593Smuzhiyun * @rq: the requested feature to change
631*4882a593Smuzhiyun * @on: whether to enable or disable the feature
632*4882a593Smuzhiyun *
633*4882a593Smuzhiyun * enable (or disable) ancillary features of the phc subsystem.
634*4882a593Smuzhiyun * our driver only supports the PPS feature on the X540
635*4882a593Smuzhiyun */
ixgbe_ptp_feature_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)636*4882a593Smuzhiyun static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
637*4882a593Smuzhiyun struct ptp_clock_request *rq, int on)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun struct ixgbe_adapter *adapter =
640*4882a593Smuzhiyun container_of(ptp, struct ixgbe_adapter, ptp_caps);
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun /**
643*4882a593Smuzhiyun * When PPS is enabled, unmask the interrupt for the ClockOut
644*4882a593Smuzhiyun * feature, so that the interrupt handler can send the PPS
645*4882a593Smuzhiyun * event when the clock SDP triggers. Clear mask when PPS is
646*4882a593Smuzhiyun * disabled
647*4882a593Smuzhiyun */
648*4882a593Smuzhiyun if (rq->type != PTP_CLK_REQ_PPS || !adapter->ptp_setup_sdp)
649*4882a593Smuzhiyun return -ENOTSUPP;
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun if (on)
652*4882a593Smuzhiyun adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
653*4882a593Smuzhiyun else
654*4882a593Smuzhiyun adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun adapter->ptp_setup_sdp(adapter);
657*4882a593Smuzhiyun return 0;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun /**
661*4882a593Smuzhiyun * ixgbe_ptp_check_pps_event
662*4882a593Smuzhiyun * @adapter: the private adapter structure
663*4882a593Smuzhiyun *
664*4882a593Smuzhiyun * This function is called by the interrupt routine when checking for
665*4882a593Smuzhiyun * interrupts. It will check and handle a pps event.
666*4882a593Smuzhiyun */
ixgbe_ptp_check_pps_event(struct ixgbe_adapter * adapter)667*4882a593Smuzhiyun void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
670*4882a593Smuzhiyun struct ptp_clock_event event;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun event.type = PTP_CLOCK_PPS;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /* this check is necessary in case the interrupt was enabled via some
675*4882a593Smuzhiyun * alternative means (ex. debug_fs). Better to check here than
676*4882a593Smuzhiyun * everywhere that calls this function.
677*4882a593Smuzhiyun */
678*4882a593Smuzhiyun if (!adapter->ptp_clock)
679*4882a593Smuzhiyun return;
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun switch (hw->mac.type) {
682*4882a593Smuzhiyun case ixgbe_mac_X540:
683*4882a593Smuzhiyun ptp_clock_event(adapter->ptp_clock, &event);
684*4882a593Smuzhiyun break;
685*4882a593Smuzhiyun default:
686*4882a593Smuzhiyun break;
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /**
691*4882a593Smuzhiyun * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
692*4882a593Smuzhiyun * @adapter: private adapter struct
693*4882a593Smuzhiyun *
694*4882a593Smuzhiyun * this watchdog task periodically reads the timecounter
695*4882a593Smuzhiyun * in order to prevent missing when the system time registers wrap
696*4882a593Smuzhiyun * around. This needs to be run approximately twice a minute.
697*4882a593Smuzhiyun */
ixgbe_ptp_overflow_check(struct ixgbe_adapter * adapter)698*4882a593Smuzhiyun void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
701*4882a593Smuzhiyun IXGBE_OVERFLOW_PERIOD);
702*4882a593Smuzhiyun unsigned long flags;
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun if (timeout) {
705*4882a593Smuzhiyun /* Update the timecounter */
706*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
707*4882a593Smuzhiyun timecounter_read(&adapter->hw_tc);
708*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun adapter->last_overflow_check = jiffies;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun /**
715*4882a593Smuzhiyun * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
716*4882a593Smuzhiyun * @adapter: private network adapter structure
717*4882a593Smuzhiyun *
718*4882a593Smuzhiyun * this watchdog task is scheduled to detect error case where hardware has
719*4882a593Smuzhiyun * dropped an Rx packet that was timestamped when the ring is full. The
720*4882a593Smuzhiyun * particular error is rare but leaves the device in a state unable to timestamp
721*4882a593Smuzhiyun * any future packets.
722*4882a593Smuzhiyun */
ixgbe_ptp_rx_hang(struct ixgbe_adapter * adapter)723*4882a593Smuzhiyun void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
726*4882a593Smuzhiyun u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
727*4882a593Smuzhiyun struct ixgbe_ring *rx_ring;
728*4882a593Smuzhiyun unsigned long rx_event;
729*4882a593Smuzhiyun int n;
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun /* if we don't have a valid timestamp in the registers, just update the
732*4882a593Smuzhiyun * timeout counter and exit
733*4882a593Smuzhiyun */
734*4882a593Smuzhiyun if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
735*4882a593Smuzhiyun adapter->last_rx_ptp_check = jiffies;
736*4882a593Smuzhiyun return;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun /* determine the most recent watchdog or rx_timestamp event */
740*4882a593Smuzhiyun rx_event = adapter->last_rx_ptp_check;
741*4882a593Smuzhiyun for (n = 0; n < adapter->num_rx_queues; n++) {
742*4882a593Smuzhiyun rx_ring = adapter->rx_ring[n];
743*4882a593Smuzhiyun if (time_after(rx_ring->last_rx_timestamp, rx_event))
744*4882a593Smuzhiyun rx_event = rx_ring->last_rx_timestamp;
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun /* only need to read the high RXSTMP register to clear the lock */
748*4882a593Smuzhiyun if (time_is_before_jiffies(rx_event + 5 * HZ)) {
749*4882a593Smuzhiyun IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
750*4882a593Smuzhiyun adapter->last_rx_ptp_check = jiffies;
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun adapter->rx_hwtstamp_cleared++;
753*4882a593Smuzhiyun e_warn(drv, "clearing RX Timestamp hang\n");
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /**
758*4882a593Smuzhiyun * ixgbe_ptp_clear_tx_timestamp - utility function to clear Tx timestamp state
759*4882a593Smuzhiyun * @adapter: the private adapter structure
760*4882a593Smuzhiyun *
761*4882a593Smuzhiyun * This function should be called whenever the state related to a Tx timestamp
762*4882a593Smuzhiyun * needs to be cleared. This helps ensure that all related bits are reset for
763*4882a593Smuzhiyun * the next Tx timestamp event.
764*4882a593Smuzhiyun */
ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter * adapter)765*4882a593Smuzhiyun static void ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter *adapter)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
770*4882a593Smuzhiyun if (adapter->ptp_tx_skb) {
771*4882a593Smuzhiyun dev_kfree_skb_any(adapter->ptp_tx_skb);
772*4882a593Smuzhiyun adapter->ptp_tx_skb = NULL;
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun /**
778*4882a593Smuzhiyun * ixgbe_ptp_tx_hang - detect error case where Tx timestamp never finishes
779*4882a593Smuzhiyun * @adapter: private network adapter structure
780*4882a593Smuzhiyun */
ixgbe_ptp_tx_hang(struct ixgbe_adapter * adapter)781*4882a593Smuzhiyun void ixgbe_ptp_tx_hang(struct ixgbe_adapter *adapter)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
784*4882a593Smuzhiyun IXGBE_PTP_TX_TIMEOUT);
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun if (!adapter->ptp_tx_skb)
787*4882a593Smuzhiyun return;
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun if (!test_bit(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state))
790*4882a593Smuzhiyun return;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun /* If we haven't received a timestamp within the timeout, it is
793*4882a593Smuzhiyun * reasonable to assume that it will never occur, so we can unlock the
794*4882a593Smuzhiyun * timestamp bit when this occurs.
795*4882a593Smuzhiyun */
796*4882a593Smuzhiyun if (timeout) {
797*4882a593Smuzhiyun cancel_work_sync(&adapter->ptp_tx_work);
798*4882a593Smuzhiyun ixgbe_ptp_clear_tx_timestamp(adapter);
799*4882a593Smuzhiyun adapter->tx_hwtstamp_timeouts++;
800*4882a593Smuzhiyun e_warn(drv, "clearing Tx timestamp hang\n");
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun /**
805*4882a593Smuzhiyun * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
806*4882a593Smuzhiyun * @adapter: the private adapter struct
807*4882a593Smuzhiyun *
808*4882a593Smuzhiyun * if the timestamp is valid, we convert it into the timecounter ns
809*4882a593Smuzhiyun * value, then store that result into the shhwtstamps structure which
810*4882a593Smuzhiyun * is passed up the network stack
811*4882a593Smuzhiyun */
ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter * adapter)812*4882a593Smuzhiyun static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
813*4882a593Smuzhiyun {
814*4882a593Smuzhiyun struct sk_buff *skb = adapter->ptp_tx_skb;
815*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
816*4882a593Smuzhiyun struct skb_shared_hwtstamps shhwtstamps;
817*4882a593Smuzhiyun u64 regval = 0;
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
820*4882a593Smuzhiyun regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
821*4882a593Smuzhiyun ixgbe_ptp_convert_to_hwtstamp(adapter, &shhwtstamps, regval);
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun /* Handle cleanup of the ptp_tx_skb ourselves, and unlock the state
824*4882a593Smuzhiyun * bit prior to notifying the stack via skb_tstamp_tx(). This prevents
825*4882a593Smuzhiyun * well behaved applications from attempting to timestamp again prior
826*4882a593Smuzhiyun * to the lock bit being clear.
827*4882a593Smuzhiyun */
828*4882a593Smuzhiyun adapter->ptp_tx_skb = NULL;
829*4882a593Smuzhiyun clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun /* Notify the stack and then free the skb after we've unlocked */
832*4882a593Smuzhiyun skb_tstamp_tx(skb, &shhwtstamps);
833*4882a593Smuzhiyun dev_kfree_skb_any(skb);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun /**
837*4882a593Smuzhiyun * ixgbe_ptp_tx_hwtstamp_work
838*4882a593Smuzhiyun * @work: pointer to the work struct
839*4882a593Smuzhiyun *
840*4882a593Smuzhiyun * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
841*4882a593Smuzhiyun * timestamp has been taken for the current skb. It is necessary, because the
842*4882a593Smuzhiyun * descriptor's "done" bit does not correlate with the timestamp event.
843*4882a593Smuzhiyun */
ixgbe_ptp_tx_hwtstamp_work(struct work_struct * work)844*4882a593Smuzhiyun static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
847*4882a593Smuzhiyun ptp_tx_work);
848*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
849*4882a593Smuzhiyun bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
850*4882a593Smuzhiyun IXGBE_PTP_TX_TIMEOUT);
851*4882a593Smuzhiyun u32 tsynctxctl;
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun /* we have to have a valid skb to poll for a timestamp */
854*4882a593Smuzhiyun if (!adapter->ptp_tx_skb) {
855*4882a593Smuzhiyun ixgbe_ptp_clear_tx_timestamp(adapter);
856*4882a593Smuzhiyun return;
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun /* stop polling once we have a valid timestamp */
860*4882a593Smuzhiyun tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
861*4882a593Smuzhiyun if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID) {
862*4882a593Smuzhiyun ixgbe_ptp_tx_hwtstamp(adapter);
863*4882a593Smuzhiyun return;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun if (timeout) {
867*4882a593Smuzhiyun ixgbe_ptp_clear_tx_timestamp(adapter);
868*4882a593Smuzhiyun adapter->tx_hwtstamp_timeouts++;
869*4882a593Smuzhiyun e_warn(drv, "clearing Tx Timestamp hang\n");
870*4882a593Smuzhiyun } else {
871*4882a593Smuzhiyun /* reschedule to keep checking if it's not available yet */
872*4882a593Smuzhiyun schedule_work(&adapter->ptp_tx_work);
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun /**
877*4882a593Smuzhiyun * ixgbe_ptp_rx_pktstamp - utility function to get RX time stamp from buffer
878*4882a593Smuzhiyun * @q_vector: structure containing interrupt and ring information
879*4882a593Smuzhiyun * @skb: the packet
880*4882a593Smuzhiyun *
881*4882a593Smuzhiyun * This function will be called by the Rx routine of the timestamp for this
882*4882a593Smuzhiyun * packet is stored in the buffer. The value is stored in little endian format
883*4882a593Smuzhiyun * starting at the end of the packet data.
884*4882a593Smuzhiyun */
ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector * q_vector,struct sk_buff * skb)885*4882a593Smuzhiyun void ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector *q_vector,
886*4882a593Smuzhiyun struct sk_buff *skb)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun __le64 regval;
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun /* copy the bits out of the skb, and then trim the skb length */
891*4882a593Smuzhiyun skb_copy_bits(skb, skb->len - IXGBE_TS_HDR_LEN, ®val,
892*4882a593Smuzhiyun IXGBE_TS_HDR_LEN);
893*4882a593Smuzhiyun __pskb_trim(skb, skb->len - IXGBE_TS_HDR_LEN);
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /* The timestamp is recorded in little endian format, and is stored at
896*4882a593Smuzhiyun * the end of the packet.
897*4882a593Smuzhiyun *
898*4882a593Smuzhiyun * DWORD: N N + 1 N + 2
899*4882a593Smuzhiyun * Field: End of Packet SYSTIMH SYSTIML
900*4882a593Smuzhiyun */
901*4882a593Smuzhiyun ixgbe_ptp_convert_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
902*4882a593Smuzhiyun le64_to_cpu(regval));
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /**
906*4882a593Smuzhiyun * ixgbe_ptp_rx_rgtstamp - utility function which checks for RX time stamp
907*4882a593Smuzhiyun * @q_vector: structure containing interrupt and ring information
908*4882a593Smuzhiyun * @skb: particular skb to send timestamp with
909*4882a593Smuzhiyun *
910*4882a593Smuzhiyun * if the timestamp is valid, we convert it into the timecounter ns
911*4882a593Smuzhiyun * value, then store that result into the shhwtstamps structure which
912*4882a593Smuzhiyun * is passed up the network stack
913*4882a593Smuzhiyun */
ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector * q_vector,struct sk_buff * skb)914*4882a593Smuzhiyun void ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector *q_vector,
915*4882a593Smuzhiyun struct sk_buff *skb)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun struct ixgbe_adapter *adapter;
918*4882a593Smuzhiyun struct ixgbe_hw *hw;
919*4882a593Smuzhiyun u64 regval = 0;
920*4882a593Smuzhiyun u32 tsyncrxctl;
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun /* we cannot process timestamps on a ring without a q_vector */
923*4882a593Smuzhiyun if (!q_vector || !q_vector->adapter)
924*4882a593Smuzhiyun return;
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun adapter = q_vector->adapter;
927*4882a593Smuzhiyun hw = &adapter->hw;
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun /* Read the tsyncrxctl register afterwards in order to prevent taking an
930*4882a593Smuzhiyun * I/O hit on every packet.
931*4882a593Smuzhiyun */
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
934*4882a593Smuzhiyun if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
935*4882a593Smuzhiyun return;
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
938*4882a593Smuzhiyun regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun ixgbe_ptp_convert_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun /**
944*4882a593Smuzhiyun * ixgbe_ptp_get_ts_config - get current hardware timestamping configuration
945*4882a593Smuzhiyun * @adapter: pointer to adapter structure
946*4882a593Smuzhiyun * @ifr: ioctl data
947*4882a593Smuzhiyun *
948*4882a593Smuzhiyun * This function returns the current timestamping settings. Rather than
949*4882a593Smuzhiyun * attempt to deconstruct registers to fill in the values, simply keep a copy
950*4882a593Smuzhiyun * of the old settings around, and return a copy when requested.
951*4882a593Smuzhiyun */
ixgbe_ptp_get_ts_config(struct ixgbe_adapter * adapter,struct ifreq * ifr)952*4882a593Smuzhiyun int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun struct hwtstamp_config *config = &adapter->tstamp_config;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun return copy_to_user(ifr->ifr_data, config,
957*4882a593Smuzhiyun sizeof(*config)) ? -EFAULT : 0;
958*4882a593Smuzhiyun }
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun /**
961*4882a593Smuzhiyun * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
962*4882a593Smuzhiyun * @adapter: the private ixgbe adapter structure
963*4882a593Smuzhiyun * @config: the hwtstamp configuration requested
964*4882a593Smuzhiyun *
965*4882a593Smuzhiyun * Outgoing time stamping can be enabled and disabled. Play nice and
966*4882a593Smuzhiyun * disable it when requested, although it shouldn't cause any overhead
967*4882a593Smuzhiyun * when no packet needs it. At most one packet in the queue may be
968*4882a593Smuzhiyun * marked for time stamping, otherwise it would be impossible to tell
969*4882a593Smuzhiyun * for sure to which packet the hardware time stamp belongs.
970*4882a593Smuzhiyun *
971*4882a593Smuzhiyun * Incoming time stamping has to be configured via the hardware
972*4882a593Smuzhiyun * filters. Not all combinations are supported, in particular event
973*4882a593Smuzhiyun * type has to be specified. Matching the kind of event packet is
974*4882a593Smuzhiyun * not supported, with the exception of "all V2 events regardless of
975*4882a593Smuzhiyun * level 2 or 4".
976*4882a593Smuzhiyun *
977*4882a593Smuzhiyun * Since hardware always timestamps Path delay packets when timestamping V2
978*4882a593Smuzhiyun * packets, regardless of the type specified in the register, only use V2
979*4882a593Smuzhiyun * Event mode. This more accurately tells the user what the hardware is going
980*4882a593Smuzhiyun * to do anyways.
981*4882a593Smuzhiyun *
982*4882a593Smuzhiyun * Note: this may modify the hwtstamp configuration towards a more general
983*4882a593Smuzhiyun * mode, if required to support the specifically requested mode.
984*4882a593Smuzhiyun */
ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter * adapter,struct hwtstamp_config * config)985*4882a593Smuzhiyun static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
986*4882a593Smuzhiyun struct hwtstamp_config *config)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
989*4882a593Smuzhiyun u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
990*4882a593Smuzhiyun u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
991*4882a593Smuzhiyun u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
992*4882a593Smuzhiyun bool is_l2 = false;
993*4882a593Smuzhiyun u32 regval;
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun /* reserved for future extensions */
996*4882a593Smuzhiyun if (config->flags)
997*4882a593Smuzhiyun return -EINVAL;
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun switch (config->tx_type) {
1000*4882a593Smuzhiyun case HWTSTAMP_TX_OFF:
1001*4882a593Smuzhiyun tsync_tx_ctl = 0;
1002*4882a593Smuzhiyun case HWTSTAMP_TX_ON:
1003*4882a593Smuzhiyun break;
1004*4882a593Smuzhiyun default:
1005*4882a593Smuzhiyun return -ERANGE;
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun switch (config->rx_filter) {
1009*4882a593Smuzhiyun case HWTSTAMP_FILTER_NONE:
1010*4882a593Smuzhiyun tsync_rx_ctl = 0;
1011*4882a593Smuzhiyun tsync_rx_mtrl = 0;
1012*4882a593Smuzhiyun adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1013*4882a593Smuzhiyun IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1014*4882a593Smuzhiyun break;
1015*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1016*4882a593Smuzhiyun tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
1017*4882a593Smuzhiyun tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
1018*4882a593Smuzhiyun adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1019*4882a593Smuzhiyun IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1020*4882a593Smuzhiyun break;
1021*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1022*4882a593Smuzhiyun tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
1023*4882a593Smuzhiyun tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
1024*4882a593Smuzhiyun adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1025*4882a593Smuzhiyun IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1026*4882a593Smuzhiyun break;
1027*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_EVENT:
1028*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1029*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1030*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_SYNC:
1031*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1032*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1033*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1034*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1035*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1036*4882a593Smuzhiyun tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
1037*4882a593Smuzhiyun is_l2 = true;
1038*4882a593Smuzhiyun config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1039*4882a593Smuzhiyun adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1040*4882a593Smuzhiyun IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1041*4882a593Smuzhiyun break;
1042*4882a593Smuzhiyun case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1043*4882a593Smuzhiyun case HWTSTAMP_FILTER_NTP_ALL:
1044*4882a593Smuzhiyun case HWTSTAMP_FILTER_ALL:
1045*4882a593Smuzhiyun /* The X550 controller is capable of timestamping all packets,
1046*4882a593Smuzhiyun * which allows it to accept any filter.
1047*4882a593Smuzhiyun */
1048*4882a593Smuzhiyun if (hw->mac.type >= ixgbe_mac_X550) {
1049*4882a593Smuzhiyun tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_ALL;
1050*4882a593Smuzhiyun config->rx_filter = HWTSTAMP_FILTER_ALL;
1051*4882a593Smuzhiyun adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
1052*4882a593Smuzhiyun break;
1053*4882a593Smuzhiyun }
1054*4882a593Smuzhiyun fallthrough;
1055*4882a593Smuzhiyun default:
1056*4882a593Smuzhiyun /*
1057*4882a593Smuzhiyun * register RXMTRL must be set in order to do V1 packets,
1058*4882a593Smuzhiyun * therefore it is not possible to time stamp both V1 Sync and
1059*4882a593Smuzhiyun * Delay_Req messages and hardware does not support
1060*4882a593Smuzhiyun * timestamping all packets => return error
1061*4882a593Smuzhiyun */
1062*4882a593Smuzhiyun adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1063*4882a593Smuzhiyun IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1064*4882a593Smuzhiyun config->rx_filter = HWTSTAMP_FILTER_NONE;
1065*4882a593Smuzhiyun return -ERANGE;
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun if (hw->mac.type == ixgbe_mac_82598EB) {
1069*4882a593Smuzhiyun adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1070*4882a593Smuzhiyun IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1071*4882a593Smuzhiyun if (tsync_rx_ctl | tsync_tx_ctl)
1072*4882a593Smuzhiyun return -ERANGE;
1073*4882a593Smuzhiyun return 0;
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun /* Per-packet timestamping only works if the filter is set to all
1077*4882a593Smuzhiyun * packets. Since this is desired, always timestamp all packets as long
1078*4882a593Smuzhiyun * as any Rx filter was configured.
1079*4882a593Smuzhiyun */
1080*4882a593Smuzhiyun switch (hw->mac.type) {
1081*4882a593Smuzhiyun case ixgbe_mac_X550:
1082*4882a593Smuzhiyun case ixgbe_mac_X550EM_x:
1083*4882a593Smuzhiyun case ixgbe_mac_x550em_a:
1084*4882a593Smuzhiyun /* enable timestamping all packets only if at least some
1085*4882a593Smuzhiyun * packets were requested. Otherwise, play nice and disable
1086*4882a593Smuzhiyun * timestamping
1087*4882a593Smuzhiyun */
1088*4882a593Smuzhiyun if (config->rx_filter == HWTSTAMP_FILTER_NONE)
1089*4882a593Smuzhiyun break;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED |
1092*4882a593Smuzhiyun IXGBE_TSYNCRXCTL_TYPE_ALL |
1093*4882a593Smuzhiyun IXGBE_TSYNCRXCTL_TSIP_UT_EN;
1094*4882a593Smuzhiyun config->rx_filter = HWTSTAMP_FILTER_ALL;
1095*4882a593Smuzhiyun adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
1096*4882a593Smuzhiyun adapter->flags &= ~IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER;
1097*4882a593Smuzhiyun is_l2 = true;
1098*4882a593Smuzhiyun break;
1099*4882a593Smuzhiyun default:
1100*4882a593Smuzhiyun break;
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun /* define ethertype filter for timestamping L2 packets */
1104*4882a593Smuzhiyun if (is_l2)
1105*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
1106*4882a593Smuzhiyun (IXGBE_ETQF_FILTER_EN | /* enable filter */
1107*4882a593Smuzhiyun IXGBE_ETQF_1588 | /* enable timestamping */
1108*4882a593Smuzhiyun ETH_P_1588)); /* 1588 eth protocol type */
1109*4882a593Smuzhiyun else
1110*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun /* enable/disable TX */
1113*4882a593Smuzhiyun regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
1114*4882a593Smuzhiyun regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
1115*4882a593Smuzhiyun regval |= tsync_tx_ctl;
1116*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun /* enable/disable RX */
1119*4882a593Smuzhiyun regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
1120*4882a593Smuzhiyun regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
1121*4882a593Smuzhiyun regval |= tsync_rx_ctl;
1122*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun /* define which PTP packets are time stamped */
1125*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun IXGBE_WRITE_FLUSH(hw);
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun /* clear TX/RX time stamp registers, just to be sure */
1130*4882a593Smuzhiyun ixgbe_ptp_clear_tx_timestamp(adapter);
1131*4882a593Smuzhiyun IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
1132*4882a593Smuzhiyun
1133*4882a593Smuzhiyun return 0;
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun /**
1137*4882a593Smuzhiyun * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
1138*4882a593Smuzhiyun * @adapter: pointer to adapter struct
1139*4882a593Smuzhiyun * @ifr: ioctl data
1140*4882a593Smuzhiyun *
1141*4882a593Smuzhiyun * Set hardware to requested mode. If unsupported, return an error with no
1142*4882a593Smuzhiyun * changes. Otherwise, store the mode for future reference.
1143*4882a593Smuzhiyun */
ixgbe_ptp_set_ts_config(struct ixgbe_adapter * adapter,struct ifreq * ifr)1144*4882a593Smuzhiyun int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
1145*4882a593Smuzhiyun {
1146*4882a593Smuzhiyun struct hwtstamp_config config;
1147*4882a593Smuzhiyun int err;
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1150*4882a593Smuzhiyun return -EFAULT;
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
1153*4882a593Smuzhiyun if (err)
1154*4882a593Smuzhiyun return err;
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun /* save these settings for future reference */
1157*4882a593Smuzhiyun memcpy(&adapter->tstamp_config, &config,
1158*4882a593Smuzhiyun sizeof(adapter->tstamp_config));
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1161*4882a593Smuzhiyun -EFAULT : 0;
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun
ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter * adapter,u32 * shift,u32 * incval)1164*4882a593Smuzhiyun static void ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter *adapter,
1165*4882a593Smuzhiyun u32 *shift, u32 *incval)
1166*4882a593Smuzhiyun {
1167*4882a593Smuzhiyun /**
1168*4882a593Smuzhiyun * Scale the NIC cycle counter by a large factor so that
1169*4882a593Smuzhiyun * relatively small corrections to the frequency can be added
1170*4882a593Smuzhiyun * or subtracted. The drawbacks of a large factor include
1171*4882a593Smuzhiyun * (a) the clock register overflows more quickly, (b) the cycle
1172*4882a593Smuzhiyun * counter structure must be able to convert the systime value
1173*4882a593Smuzhiyun * to nanoseconds using only a multiplier and a right-shift,
1174*4882a593Smuzhiyun * and (c) the value must fit within the timinca register space
1175*4882a593Smuzhiyun * => math based on internal DMA clock rate and available bits
1176*4882a593Smuzhiyun *
1177*4882a593Smuzhiyun * Note that when there is no link, internal DMA clock is same as when
1178*4882a593Smuzhiyun * link speed is 10Gb. Set the registers correctly even when link is
1179*4882a593Smuzhiyun * down to preserve the clock setting
1180*4882a593Smuzhiyun */
1181*4882a593Smuzhiyun switch (adapter->link_speed) {
1182*4882a593Smuzhiyun case IXGBE_LINK_SPEED_100_FULL:
1183*4882a593Smuzhiyun *shift = IXGBE_INCVAL_SHIFT_100;
1184*4882a593Smuzhiyun *incval = IXGBE_INCVAL_100;
1185*4882a593Smuzhiyun break;
1186*4882a593Smuzhiyun case IXGBE_LINK_SPEED_1GB_FULL:
1187*4882a593Smuzhiyun *shift = IXGBE_INCVAL_SHIFT_1GB;
1188*4882a593Smuzhiyun *incval = IXGBE_INCVAL_1GB;
1189*4882a593Smuzhiyun break;
1190*4882a593Smuzhiyun case IXGBE_LINK_SPEED_10GB_FULL:
1191*4882a593Smuzhiyun default:
1192*4882a593Smuzhiyun *shift = IXGBE_INCVAL_SHIFT_10GB;
1193*4882a593Smuzhiyun *incval = IXGBE_INCVAL_10GB;
1194*4882a593Smuzhiyun break;
1195*4882a593Smuzhiyun }
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun /**
1199*4882a593Smuzhiyun * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
1200*4882a593Smuzhiyun * @adapter: pointer to the adapter structure
1201*4882a593Smuzhiyun *
1202*4882a593Smuzhiyun * This function should be called to set the proper values for the TIMINCA
1203*4882a593Smuzhiyun * register and tell the cyclecounter structure what the tick rate of SYSTIME
1204*4882a593Smuzhiyun * is. It does not directly modify SYSTIME registers or the timecounter
1205*4882a593Smuzhiyun * structure. It should be called whenever a new TIMINCA value is necessary,
1206*4882a593Smuzhiyun * such as during initialization or when the link speed changes.
1207*4882a593Smuzhiyun */
ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter * adapter)1208*4882a593Smuzhiyun void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
1209*4882a593Smuzhiyun {
1210*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
1211*4882a593Smuzhiyun struct cyclecounter cc;
1212*4882a593Smuzhiyun unsigned long flags;
1213*4882a593Smuzhiyun u32 incval = 0;
1214*4882a593Smuzhiyun u32 fuse0 = 0;
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun /* For some of the boards below this mask is technically incorrect.
1217*4882a593Smuzhiyun * The timestamp mask overflows at approximately 61bits. However the
1218*4882a593Smuzhiyun * particular hardware does not overflow on an even bitmask value.
1219*4882a593Smuzhiyun * Instead, it overflows due to conversion of upper 32bits billions of
1220*4882a593Smuzhiyun * cycles. Timecounters are not really intended for this purpose so
1221*4882a593Smuzhiyun * they do not properly function if the overflow point isn't 2^N-1.
1222*4882a593Smuzhiyun * However, the actual SYSTIME values in question take ~138 years to
1223*4882a593Smuzhiyun * overflow. In practice this means they won't actually overflow. A
1224*4882a593Smuzhiyun * proper fix to this problem would require modification of the
1225*4882a593Smuzhiyun * timecounter delta calculations.
1226*4882a593Smuzhiyun */
1227*4882a593Smuzhiyun cc.mask = CLOCKSOURCE_MASK(64);
1228*4882a593Smuzhiyun cc.mult = 1;
1229*4882a593Smuzhiyun cc.shift = 0;
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun switch (hw->mac.type) {
1232*4882a593Smuzhiyun case ixgbe_mac_X550EM_x:
1233*4882a593Smuzhiyun /* SYSTIME assumes X550EM_x board frequency is 300Mhz, and is
1234*4882a593Smuzhiyun * designed to represent seconds and nanoseconds when this is
1235*4882a593Smuzhiyun * the case. However, some revisions of hardware have a 400Mhz
1236*4882a593Smuzhiyun * clock and we have to compensate for this frequency
1237*4882a593Smuzhiyun * variation using corrected mult and shift values.
1238*4882a593Smuzhiyun */
1239*4882a593Smuzhiyun fuse0 = IXGBE_READ_REG(hw, IXGBE_FUSES0_GROUP(0));
1240*4882a593Smuzhiyun if (!(fuse0 & IXGBE_FUSES0_300MHZ)) {
1241*4882a593Smuzhiyun cc.mult = 3;
1242*4882a593Smuzhiyun cc.shift = 2;
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun fallthrough;
1245*4882a593Smuzhiyun case ixgbe_mac_x550em_a:
1246*4882a593Smuzhiyun case ixgbe_mac_X550:
1247*4882a593Smuzhiyun cc.read = ixgbe_ptp_read_X550;
1248*4882a593Smuzhiyun break;
1249*4882a593Smuzhiyun case ixgbe_mac_X540:
1250*4882a593Smuzhiyun cc.read = ixgbe_ptp_read_82599;
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
1253*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
1254*4882a593Smuzhiyun break;
1255*4882a593Smuzhiyun case ixgbe_mac_82599EB:
1256*4882a593Smuzhiyun cc.read = ixgbe_ptp_read_82599;
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
1259*4882a593Smuzhiyun incval >>= IXGBE_INCVAL_SHIFT_82599;
1260*4882a593Smuzhiyun cc.shift -= IXGBE_INCVAL_SHIFT_82599;
1261*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
1262*4882a593Smuzhiyun BIT(IXGBE_INCPER_SHIFT_82599) | incval);
1263*4882a593Smuzhiyun break;
1264*4882a593Smuzhiyun default:
1265*4882a593Smuzhiyun /* other devices aren't supported */
1266*4882a593Smuzhiyun return;
1267*4882a593Smuzhiyun }
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun /* update the base incval used to calculate frequency adjustment */
1270*4882a593Smuzhiyun WRITE_ONCE(adapter->base_incval, incval);
1271*4882a593Smuzhiyun smp_mb();
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun /* need lock to prevent incorrect read while modifying cyclecounter */
1274*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
1275*4882a593Smuzhiyun memcpy(&adapter->hw_cc, &cc, sizeof(adapter->hw_cc));
1276*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
1277*4882a593Smuzhiyun }
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun /**
1280*4882a593Smuzhiyun * ixgbe_ptp_init_systime - Initialize SYSTIME registers
1281*4882a593Smuzhiyun * @adapter: the ixgbe private board structure
1282*4882a593Smuzhiyun *
1283*4882a593Smuzhiyun * Initialize and start the SYSTIME registers.
1284*4882a593Smuzhiyun */
ixgbe_ptp_init_systime(struct ixgbe_adapter * adapter)1285*4882a593Smuzhiyun static void ixgbe_ptp_init_systime(struct ixgbe_adapter *adapter)
1286*4882a593Smuzhiyun {
1287*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
1288*4882a593Smuzhiyun u32 tsauxc;
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun switch (hw->mac.type) {
1291*4882a593Smuzhiyun case ixgbe_mac_X550EM_x:
1292*4882a593Smuzhiyun case ixgbe_mac_x550em_a:
1293*4882a593Smuzhiyun case ixgbe_mac_X550:
1294*4882a593Smuzhiyun tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC);
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun /* Reset SYSTIME registers to 0 */
1297*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_SYSTIMR, 0);
1298*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0);
1299*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0);
1300*4882a593Smuzhiyun
1301*4882a593Smuzhiyun /* Reset interrupt settings */
1302*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSIM, IXGBE_TSIM_TXTS);
1303*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_TIMESYNC);
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun /* Activate the SYSTIME counter */
1306*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_TSAUXC,
1307*4882a593Smuzhiyun tsauxc & ~IXGBE_TSAUXC_DISABLE_SYSTIME);
1308*4882a593Smuzhiyun break;
1309*4882a593Smuzhiyun case ixgbe_mac_X540:
1310*4882a593Smuzhiyun case ixgbe_mac_82599EB:
1311*4882a593Smuzhiyun /* Reset SYSTIME registers to 0 */
1312*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0);
1313*4882a593Smuzhiyun IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0);
1314*4882a593Smuzhiyun break;
1315*4882a593Smuzhiyun default:
1316*4882a593Smuzhiyun /* Other devices aren't supported */
1317*4882a593Smuzhiyun return;
1318*4882a593Smuzhiyun };
1319*4882a593Smuzhiyun
1320*4882a593Smuzhiyun IXGBE_WRITE_FLUSH(hw);
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun /**
1324*4882a593Smuzhiyun * ixgbe_ptp_reset
1325*4882a593Smuzhiyun * @adapter: the ixgbe private board structure
1326*4882a593Smuzhiyun *
1327*4882a593Smuzhiyun * When the MAC resets, all the hardware bits for timesync are reset. This
1328*4882a593Smuzhiyun * function is used to re-enable the device for PTP based on current settings.
1329*4882a593Smuzhiyun * We do lose the current clock time, so just reset the cyclecounter to the
1330*4882a593Smuzhiyun * system real clock time.
1331*4882a593Smuzhiyun *
1332*4882a593Smuzhiyun * This function will maintain hwtstamp_config settings, and resets the SDP
1333*4882a593Smuzhiyun * output if it was enabled.
1334*4882a593Smuzhiyun */
ixgbe_ptp_reset(struct ixgbe_adapter * adapter)1335*4882a593Smuzhiyun void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun struct ixgbe_hw *hw = &adapter->hw;
1338*4882a593Smuzhiyun unsigned long flags;
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun /* reset the hardware timestamping mode */
1341*4882a593Smuzhiyun ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun /* 82598 does not support PTP */
1344*4882a593Smuzhiyun if (hw->mac.type == ixgbe_mac_82598EB)
1345*4882a593Smuzhiyun return;
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun ixgbe_ptp_start_cyclecounter(adapter);
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun ixgbe_ptp_init_systime(adapter);
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun spin_lock_irqsave(&adapter->tmreg_lock, flags);
1352*4882a593Smuzhiyun timecounter_init(&adapter->hw_tc, &adapter->hw_cc,
1353*4882a593Smuzhiyun ktime_to_ns(ktime_get_real()));
1354*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun adapter->last_overflow_check = jiffies;
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun /* Now that the shift has been calculated and the systime
1359*4882a593Smuzhiyun * registers reset, (re-)enable the Clock out feature
1360*4882a593Smuzhiyun */
1361*4882a593Smuzhiyun if (adapter->ptp_setup_sdp)
1362*4882a593Smuzhiyun adapter->ptp_setup_sdp(adapter);
1363*4882a593Smuzhiyun }
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun /**
1366*4882a593Smuzhiyun * ixgbe_ptp_create_clock
1367*4882a593Smuzhiyun * @adapter: the ixgbe private adapter structure
1368*4882a593Smuzhiyun *
1369*4882a593Smuzhiyun * This function performs setup of the user entry point function table and
1370*4882a593Smuzhiyun * initializes the PTP clock device, which is used to access the clock-like
1371*4882a593Smuzhiyun * features of the PTP core. It will be called by ixgbe_ptp_init, and may
1372*4882a593Smuzhiyun * reuse a previously initialized clock (such as during a suspend/resume
1373*4882a593Smuzhiyun * cycle).
1374*4882a593Smuzhiyun */
ixgbe_ptp_create_clock(struct ixgbe_adapter * adapter)1375*4882a593Smuzhiyun static long ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
1376*4882a593Smuzhiyun {
1377*4882a593Smuzhiyun struct net_device *netdev = adapter->netdev;
1378*4882a593Smuzhiyun long err;
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun /* do nothing if we already have a clock device */
1381*4882a593Smuzhiyun if (!IS_ERR_OR_NULL(adapter->ptp_clock))
1382*4882a593Smuzhiyun return 0;
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun switch (adapter->hw.mac.type) {
1385*4882a593Smuzhiyun case ixgbe_mac_X540:
1386*4882a593Smuzhiyun snprintf(adapter->ptp_caps.name,
1387*4882a593Smuzhiyun sizeof(adapter->ptp_caps.name),
1388*4882a593Smuzhiyun "%s", netdev->name);
1389*4882a593Smuzhiyun adapter->ptp_caps.owner = THIS_MODULE;
1390*4882a593Smuzhiyun adapter->ptp_caps.max_adj = 250000000;
1391*4882a593Smuzhiyun adapter->ptp_caps.n_alarm = 0;
1392*4882a593Smuzhiyun adapter->ptp_caps.n_ext_ts = 0;
1393*4882a593Smuzhiyun adapter->ptp_caps.n_per_out = 0;
1394*4882a593Smuzhiyun adapter->ptp_caps.pps = 1;
1395*4882a593Smuzhiyun adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
1396*4882a593Smuzhiyun adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1397*4882a593Smuzhiyun adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
1398*4882a593Smuzhiyun adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1399*4882a593Smuzhiyun adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1400*4882a593Smuzhiyun adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X540;
1401*4882a593Smuzhiyun break;
1402*4882a593Smuzhiyun case ixgbe_mac_82599EB:
1403*4882a593Smuzhiyun snprintf(adapter->ptp_caps.name,
1404*4882a593Smuzhiyun sizeof(adapter->ptp_caps.name),
1405*4882a593Smuzhiyun "%s", netdev->name);
1406*4882a593Smuzhiyun adapter->ptp_caps.owner = THIS_MODULE;
1407*4882a593Smuzhiyun adapter->ptp_caps.max_adj = 250000000;
1408*4882a593Smuzhiyun adapter->ptp_caps.n_alarm = 0;
1409*4882a593Smuzhiyun adapter->ptp_caps.n_ext_ts = 0;
1410*4882a593Smuzhiyun adapter->ptp_caps.n_per_out = 0;
1411*4882a593Smuzhiyun adapter->ptp_caps.pps = 0;
1412*4882a593Smuzhiyun adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
1413*4882a593Smuzhiyun adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1414*4882a593Smuzhiyun adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
1415*4882a593Smuzhiyun adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1416*4882a593Smuzhiyun adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1417*4882a593Smuzhiyun break;
1418*4882a593Smuzhiyun case ixgbe_mac_X550:
1419*4882a593Smuzhiyun case ixgbe_mac_X550EM_x:
1420*4882a593Smuzhiyun case ixgbe_mac_x550em_a:
1421*4882a593Smuzhiyun snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
1422*4882a593Smuzhiyun adapter->ptp_caps.owner = THIS_MODULE;
1423*4882a593Smuzhiyun adapter->ptp_caps.max_adj = 30000000;
1424*4882a593Smuzhiyun adapter->ptp_caps.n_alarm = 0;
1425*4882a593Smuzhiyun adapter->ptp_caps.n_ext_ts = 0;
1426*4882a593Smuzhiyun adapter->ptp_caps.n_per_out = 0;
1427*4882a593Smuzhiyun adapter->ptp_caps.pps = 1;
1428*4882a593Smuzhiyun adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_X550;
1429*4882a593Smuzhiyun adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1430*4882a593Smuzhiyun adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
1431*4882a593Smuzhiyun adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1432*4882a593Smuzhiyun adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1433*4882a593Smuzhiyun adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X550;
1434*4882a593Smuzhiyun break;
1435*4882a593Smuzhiyun default:
1436*4882a593Smuzhiyun adapter->ptp_clock = NULL;
1437*4882a593Smuzhiyun adapter->ptp_setup_sdp = NULL;
1438*4882a593Smuzhiyun return -EOPNOTSUPP;
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
1442*4882a593Smuzhiyun &adapter->pdev->dev);
1443*4882a593Smuzhiyun if (IS_ERR(adapter->ptp_clock)) {
1444*4882a593Smuzhiyun err = PTR_ERR(adapter->ptp_clock);
1445*4882a593Smuzhiyun adapter->ptp_clock = NULL;
1446*4882a593Smuzhiyun e_dev_err("ptp_clock_register failed\n");
1447*4882a593Smuzhiyun return err;
1448*4882a593Smuzhiyun } else if (adapter->ptp_clock)
1449*4882a593Smuzhiyun e_dev_info("registered PHC device on %s\n", netdev->name);
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun /* set default timestamp mode to disabled here. We do this in
1452*4882a593Smuzhiyun * create_clock instead of init, because we don't want to override the
1453*4882a593Smuzhiyun * previous settings during a resume cycle.
1454*4882a593Smuzhiyun */
1455*4882a593Smuzhiyun adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
1456*4882a593Smuzhiyun adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun return 0;
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun /**
1462*4882a593Smuzhiyun * ixgbe_ptp_init
1463*4882a593Smuzhiyun * @adapter: the ixgbe private adapter structure
1464*4882a593Smuzhiyun *
1465*4882a593Smuzhiyun * This function performs the required steps for enabling PTP
1466*4882a593Smuzhiyun * support. If PTP support has already been loaded it simply calls the
1467*4882a593Smuzhiyun * cyclecounter init routine and exits.
1468*4882a593Smuzhiyun */
ixgbe_ptp_init(struct ixgbe_adapter * adapter)1469*4882a593Smuzhiyun void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
1470*4882a593Smuzhiyun {
1471*4882a593Smuzhiyun /* initialize the spin lock first since we can't control when a user
1472*4882a593Smuzhiyun * will call the entry functions once we have initialized the clock
1473*4882a593Smuzhiyun * device
1474*4882a593Smuzhiyun */
1475*4882a593Smuzhiyun spin_lock_init(&adapter->tmreg_lock);
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun /* obtain a PTP device, or re-use an existing device */
1478*4882a593Smuzhiyun if (ixgbe_ptp_create_clock(adapter))
1479*4882a593Smuzhiyun return;
1480*4882a593Smuzhiyun
1481*4882a593Smuzhiyun /* we have a clock so we can initialize work now */
1482*4882a593Smuzhiyun INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun /* reset the PTP related hardware bits */
1485*4882a593Smuzhiyun ixgbe_ptp_reset(adapter);
1486*4882a593Smuzhiyun
1487*4882a593Smuzhiyun /* enter the IXGBE_PTP_RUNNING state */
1488*4882a593Smuzhiyun set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun return;
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun /**
1494*4882a593Smuzhiyun * ixgbe_ptp_suspend - stop PTP work items
1495*4882a593Smuzhiyun * @adapter: pointer to adapter struct
1496*4882a593Smuzhiyun *
1497*4882a593Smuzhiyun * this function suspends PTP activity, and prevents more PTP work from being
1498*4882a593Smuzhiyun * generated, but does not destroy the PTP clock device.
1499*4882a593Smuzhiyun */
ixgbe_ptp_suspend(struct ixgbe_adapter * adapter)1500*4882a593Smuzhiyun void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
1501*4882a593Smuzhiyun {
1502*4882a593Smuzhiyun /* Leave the IXGBE_PTP_RUNNING state. */
1503*4882a593Smuzhiyun if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
1504*4882a593Smuzhiyun return;
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
1507*4882a593Smuzhiyun if (adapter->ptp_setup_sdp)
1508*4882a593Smuzhiyun adapter->ptp_setup_sdp(adapter);
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun /* ensure that we cancel any pending PTP Tx work item in progress */
1511*4882a593Smuzhiyun cancel_work_sync(&adapter->ptp_tx_work);
1512*4882a593Smuzhiyun ixgbe_ptp_clear_tx_timestamp(adapter);
1513*4882a593Smuzhiyun }
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun /**
1516*4882a593Smuzhiyun * ixgbe_ptp_stop - close the PTP device
1517*4882a593Smuzhiyun * @adapter: pointer to adapter struct
1518*4882a593Smuzhiyun *
1519*4882a593Smuzhiyun * completely destroy the PTP device, should only be called when the device is
1520*4882a593Smuzhiyun * being fully closed.
1521*4882a593Smuzhiyun */
ixgbe_ptp_stop(struct ixgbe_adapter * adapter)1522*4882a593Smuzhiyun void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
1523*4882a593Smuzhiyun {
1524*4882a593Smuzhiyun /* first, suspend PTP activity */
1525*4882a593Smuzhiyun ixgbe_ptp_suspend(adapter);
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun /* disable the PTP clock device */
1528*4882a593Smuzhiyun if (adapter->ptp_clock) {
1529*4882a593Smuzhiyun ptp_clock_unregister(adapter->ptp_clock);
1530*4882a593Smuzhiyun adapter->ptp_clock = NULL;
1531*4882a593Smuzhiyun e_dev_info("removed PHC on %s\n",
1532*4882a593Smuzhiyun adapter->netdev->name);
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun }
1535