blob: 4a2000bfd4aec39e49b777d7128e8366e63a1301 [file] [log] [blame]
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Mark Rustad49425df2016-04-01 12:18:09 -07004 Copyright(c) 1999 - 2016 Intel Corporation.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
Jacob Kellerb89aae72014-02-22 01:23:50 +000023 Linux NICS <linux.nics@intel.com>
Jacob Keller3a6a4ed2012-05-01 05:24:58 +000024 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28#include "ixgbe.h"
Jacob Keller1d1a79b2012-05-22 06:18:08 +000029#include <linux/ptp_classify.h>
Mark Rustada9763f32015-10-27 09:58:07 -070030#include <linux/clocksource.h>
Jacob Keller3a6a4ed2012-05-01 05:24:58 +000031
32/*
33 * The 82599 and the X540 do not have true 64bit nanosecond scale
34 * counter registers. Instead, SYSTIME is defined by a fixed point
35 * system which allows the user to define the scale counter increment
36 * value at every level change of the oscillator driving the SYSTIME
37 * value. For both devices the TIMINCA:IV field defines this
38 * increment. On the X540 device, 31 bits are provided. However on the
39 * 82599 only provides 24 bits. The time unit is determined by the
40 * clock frequency of the oscillator in combination with the TIMINCA
41 * register. When these devices link at 10Gb the oscillator has a
42 * period of 6.4ns. In order to convert the scale counter into
43 * nanoseconds the cyclecounter and timecounter structures are
44 * used. The SYSTIME registers need to be converted to ns values by use
45 * of only a right shift (division by power of 2). The following math
46 * determines the largest incvalue that will fit into the available
47 * bits in the TIMINCA register.
48 *
49 * PeriodWidth: Number of bits to store the clock period
50 * MaxWidth: The maximum width value of the TIMINCA register
51 * Period: The clock period for the oscillator
52 * round(): discard the fractional portion of the calculation
53 *
54 * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
55 *
56 * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
57 * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
58 *
59 * The period also changes based on the link speed:
60 * At 10Gb link or no link, the period remains the same.
61 * At 1Gb link, the period is multiplied by 10. (64ns)
62 * At 100Mb link, the period is multiplied by 100. (640ns)
63 *
64 * The calculated value allows us to right shift the SYSTIME register
65 * value in order to quickly convert it into a nanosecond clock,
66 * while allowing for the maximum possible adjustment value.
67 *
68 * These diagrams are only for the 10Gb link period
69 *
70 * SYSTIMEH SYSTIMEL
71 * +--------------+ +--------------+
72 * X540 | 32 | | 1 | 3 | 28 |
73 * *--------------+ +--------------+
74 * \________ 36 bits ______/ fract
75 *
76 * +--------------+ +--------------+
77 * 82599 | 32 | | 8 | 3 | 21 |
78 * *--------------+ +--------------+
79 * \________ 43 bits ______/ fract
80 *
81 * The 36 bit X540 SYSTIME overflows every
82 * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
83 *
84 * The 43 bit 82599 SYSTIME overflows every
85 * 2^43 * 10^-9 / 3600 = 2.4 hours
86 */
87#define IXGBE_INCVAL_10GB 0x66666666
88#define IXGBE_INCVAL_1GB 0x40000000
89#define IXGBE_INCVAL_100 0x50000000
90
91#define IXGBE_INCVAL_SHIFT_10GB 28
92#define IXGBE_INCVAL_SHIFT_1GB 24
93#define IXGBE_INCVAL_SHIFT_100 21
94
95#define IXGBE_INCVAL_SHIFT_82599 7
96#define IXGBE_INCPER_SHIFT_82599 24
Jacob Keller3a6a4ed2012-05-01 05:24:58 +000097
98#define IXGBE_OVERFLOW_PERIOD (HZ * 30)
Jacob Keller891dc082012-12-05 07:24:46 +000099#define IXGBE_PTP_TX_TIMEOUT (HZ * 15)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000100
Jacob Kellera5a0fc02014-05-28 07:21:47 +0000101/* half of a one second clock period, for use with PPS signal. We have to use
102 * this instead of something pre-defined like IXGBE_PTP_PPS_HALF_SECOND, in
103 * order to force at least 64bits of precision for shifting
104 */
105#define IXGBE_PTP_PPS_HALF_SECOND 500000000ULL
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000106
Mark Rustada9763f32015-10-27 09:58:07 -0700107/* In contrast, the X550 controller has two registers, SYSTIMEH and SYSTIMEL
108 * which contain measurements of seconds and nanoseconds respectively. This
109 * matches the standard linux representation of time in the kernel. In addition,
110 * the X550 also has a SYSTIMER register which represents residue, or
111 * subnanosecond overflow adjustments. To control clock adjustment, the TIMINCA
112 * register is used, but it is unlike the X540 and 82599 devices. TIMINCA
113 * represents units of 2^-32 nanoseconds, and uses 31 bits for this, with the
114 * high bit representing whether the adjustent is positive or negative. Every
115 * clock cycle, the X550 will add 12.5 ns + TIMINCA which can result in a range
116 * of 12 to 13 nanoseconds adjustment. Unlike the 82599 and X540 devices, the
117 * X550's clock for purposes of SYSTIME generation is constant and not dependent
118 * on the link speed.
119 *
120 * SYSTIMEH SYSTIMEL SYSTIMER
121 * +--------------+ +--------------+ +-------------+
122 * X550 | 32 | | 32 | | 32 |
123 * *--------------+ +--------------+ +-------------+
124 * \____seconds___/ \_nanoseconds_/ \__2^-32 ns__/
125 *
126 * This results in a full 96 bits to represent the clock, with 32 bits for
127 * seconds, 32 bits for nanoseconds (largest value is 0d999999999 or just under
128 * 1 second) and an additional 32 bits to measure sub nanosecond adjustments for
129 * underflow of adjustments.
130 *
131 * The 32 bits of seconds for the X550 overflows every
132 * 2^32 / ( 365.25 * 24 * 60 * 60 ) = ~136 years.
133 *
134 * In order to adjust the clock frequency for the X550, the TIMINCA register is
135 * provided. This register represents a + or minus nearly 0.5 ns adjustment to
136 * the base frequency. It is measured in 2^-32 ns units, with the high bit being
137 * the sign bit. This register enables software to calculate frequency
138 * adjustments and apply them directly to the clock rate.
139 *
140 * The math for converting ppb into TIMINCA values is fairly straightforward.
141 * TIMINCA value = ( Base_Frequency * ppb ) / 1000000000ULL
142 *
143 * This assumes that ppb is never high enough to create a value bigger than
144 * TIMINCA's 31 bits can store. This is ensured by the stack. Calculating this
145 * value is also simple.
146 * Max ppb = ( Max Adjustment / Base Frequency ) / 1000000000ULL
147 *
148 * For the X550, the Max adjustment is +/- 0.5 ns, and the base frequency is
149 * 12.5 nanoseconds. This means that the Max ppb is 39999999
150 * Note: We subtract one in order to ensure no overflow, because the TIMINCA
151 * register can only hold slightly under 0.5 nanoseconds.
152 *
153 * Because TIMINCA is measured in 2^-32 ns units, we have to convert 12.5 ns
154 * into 2^-32 units, which is
155 *
156 * 12.5 * 2^32 = C80000000
157 *
158 * Some revisions of hardware have a faster base frequency than the registers
159 * were defined for. To fix this, we use a timecounter structure with the
160 * proper mult and shift to convert the cycles into nanoseconds of time.
161 */
162#define IXGBE_X550_BASE_PERIOD 0xC80000000ULL
163#define INCVALUE_MASK 0x7FFFFFFF
164#define ISGN 0x80000000
165#define MAX_TIMADJ 0x7FFFFFFF
166
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000167/**
Mark Rustada9763f32015-10-27 09:58:07 -0700168 * ixgbe_ptp_setup_sdp_x540
Jacob Keller82083672012-08-01 07:12:25 +0000169 * @hw: the hardware private structure
Jacob Keller82083672012-08-01 07:12:25 +0000170 *
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000171 * this function enables or disables the clock out feature on SDP0 for
172 * the X540 device. It will create a 1second periodic output that can
173 * be used as the PPS (via an interrupt).
Jacob Keller82083672012-08-01 07:12:25 +0000174 *
175 * It calculates when the systime will be on an exact second, and then
176 * aligns the start of the PPS signal to that value. The shift is
177 * necessary because it can change based on the link speed.
178 */
Mark Rustada9763f32015-10-27 09:58:07 -0700179static void ixgbe_ptp_setup_sdp_x540(struct ixgbe_adapter *adapter)
Jacob Keller82083672012-08-01 07:12:25 +0000180{
181 struct ixgbe_hw *hw = &adapter->hw;
Mark Rustada9763f32015-10-27 09:58:07 -0700182 int shift = adapter->hw_cc.shift;
Jacob Keller82083672012-08-01 07:12:25 +0000183 u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
184 u64 ns = 0, clock_edge = 0;
185
Mark Rustada9763f32015-10-27 09:58:07 -0700186 /* disable the pin first */
187 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
188 IXGBE_WRITE_FLUSH(hw);
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000189
Mark Rustada9763f32015-10-27 09:58:07 -0700190 if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
191 return;
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000192
Mark Rustada9763f32015-10-27 09:58:07 -0700193 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
Jacob Keller82083672012-08-01 07:12:25 +0000194
Mark Rustada9763f32015-10-27 09:58:07 -0700195 /* enable the SDP0 pin as output, and connected to the
196 * native function for Timesync (ClockOut)
197 */
198 esdp |= IXGBE_ESDP_SDP0_DIR |
199 IXGBE_ESDP_SDP0_NATIVE;
Jacob Keller82083672012-08-01 07:12:25 +0000200
Mark Rustada9763f32015-10-27 09:58:07 -0700201 /* enable the Clock Out feature on SDP0, and allow
202 * interrupts to occur when the pin changes
203 */
204 tsauxc = IXGBE_TSAUXC_EN_CLK |
205 IXGBE_TSAUXC_SYNCLK |
206 IXGBE_TSAUXC_SDP0_INT;
Jacob Keller82083672012-08-01 07:12:25 +0000207
Mark Rustada9763f32015-10-27 09:58:07 -0700208 /* clock period (or pulse length) */
209 clktiml = (u32)(IXGBE_PTP_PPS_HALF_SECOND << shift);
210 clktimh = (u32)((IXGBE_PTP_PPS_HALF_SECOND << shift) >> 32);
Jacob Keller82083672012-08-01 07:12:25 +0000211
Mark Rustada9763f32015-10-27 09:58:07 -0700212 /* Account for the cyclecounter wrap-around value by
213 * using the converted ns value of the current time to
214 * check for when the next aligned second would occur.
215 */
216 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
217 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
218 ns = timecounter_cyc2time(&adapter->hw_tc, clock_edge);
Jacob Keller82083672012-08-01 07:12:25 +0000219
Mark Rustada9763f32015-10-27 09:58:07 -0700220 div_u64_rem(ns, IXGBE_PTP_PPS_HALF_SECOND, &rem);
221 clock_edge += ((IXGBE_PTP_PPS_HALF_SECOND - (u64)rem) << shift);
Jacob Keller82083672012-08-01 07:12:25 +0000222
Mark Rustada9763f32015-10-27 09:58:07 -0700223 /* specify the initial clock start time */
224 trgttiml = (u32)clock_edge;
225 trgttimh = (u32)(clock_edge >> 32);
Jacob Keller82083672012-08-01 07:12:25 +0000226
Mark Rustada9763f32015-10-27 09:58:07 -0700227 IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
228 IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
229 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
230 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
Jacob Keller82083672012-08-01 07:12:25 +0000231
Mark Rustada9763f32015-10-27 09:58:07 -0700232 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
233 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
Jacob Keller82083672012-08-01 07:12:25 +0000234
Jacob Keller82083672012-08-01 07:12:25 +0000235 IXGBE_WRITE_FLUSH(hw);
236}
237
238/**
Mark Rustada9763f32015-10-27 09:58:07 -0700239 * ixgbe_ptp_read_X550 - read cycle counter value
240 * @hw_cc: cyclecounter structure
241 *
242 * This function reads SYSTIME registers. It is called by the cyclecounter
243 * structure to convert from internal representation into nanoseconds. We need
244 * this for X550 since some skews do not have expected clock frequency and
245 * result of SYSTIME is 32bits of "billions of cycles" and 32 bits of
246 * "cycles", rather than seconds and nanoseconds.
247 */
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100248static u64 ixgbe_ptp_read_X550(const struct cyclecounter *hw_cc)
Mark Rustada9763f32015-10-27 09:58:07 -0700249{
250 struct ixgbe_adapter *adapter =
251 container_of(hw_cc, struct ixgbe_adapter, hw_cc);
252 struct ixgbe_hw *hw = &adapter->hw;
253 struct timespec64 ts;
254
255 /* storage is 32 bits of 'billions of cycles' and 32 bits of 'cycles'.
256 * Some revisions of hardware run at a higher frequency and so the
257 * cycles are not guaranteed to be nanoseconds. The timespec64 created
258 * here is used for its math/conversions but does not necessarily
259 * represent nominal time.
260 *
261 * It should be noted that this cyclecounter will overflow at a
262 * non-bitmask field since we have to convert our billions of cycles
263 * into an actual cycles count. This results in some possible weird
264 * situations at high cycle counter stamps. However given that 32 bits
265 * of "seconds" is ~138 years this isn't a problem. Even at the
266 * increased frequency of some revisions, this is still ~103 years.
267 * Since the SYSTIME values start at 0 and we never write them, it is
268 * highly unlikely for the cyclecounter to overflow in practice.
269 */
270 IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
271 ts.tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
272 ts.tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
273
274 return (u64)timespec64_to_ns(&ts);
275}
276
277/**
278 * ixgbe_ptp_read_82599 - read raw cycle counter (to be used by time counter)
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000279 * @cc: the cyclecounter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000280 *
281 * this function reads the cyclecounter registers and is called by the
282 * cyclecounter structure used to construct a ns counter from the
283 * arbitrary fixed point registers
284 */
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100285static u64 ixgbe_ptp_read_82599(const struct cyclecounter *cc)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000286{
287 struct ixgbe_adapter *adapter =
Mark Rustada9763f32015-10-27 09:58:07 -0700288 container_of(cc, struct ixgbe_adapter, hw_cc);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000289 struct ixgbe_hw *hw = &adapter->hw;
290 u64 stamp = 0;
291
292 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
293 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
294
295 return stamp;
296}
297
298/**
Mark Rustada9763f32015-10-27 09:58:07 -0700299 * ixgbe_ptp_convert_to_hwtstamp - convert register value to hw timestamp
300 * @adapter: private adapter structure
301 * @hwtstamp: stack timestamp structure
302 * @systim: unsigned 64bit system time value
303 *
304 * We need to convert the adapter's RX/TXSTMP registers into a hwtstamp value
305 * which can be used by the stack's ptp functions.
306 *
307 * The lock is used to protect consistency of the cyclecounter and the SYSTIME
308 * registers. However, it does not need to protect against the Rx or Tx
309 * timestamp registers, as there can't be a new timestamp until the old one is
310 * unlatched by reading.
311 *
312 * In addition to the timestamp in hardware, some controllers need a software
313 * overflow cyclecounter, and this function takes this into account as well.
314 **/
315static void ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter *adapter,
316 struct skb_shared_hwtstamps *hwtstamp,
317 u64 timestamp)
318{
319 unsigned long flags;
320 struct timespec64 systime;
321 u64 ns;
322
323 memset(hwtstamp, 0, sizeof(*hwtstamp));
324
325 switch (adapter->hw.mac.type) {
326 /* X550 and later hardware supposedly represent time using a seconds
327 * and nanoseconds counter, instead of raw 64bits nanoseconds. We need
328 * to convert the timestamp into cycles before it can be fed to the
329 * cyclecounter. We need an actual cyclecounter because some revisions
330 * of hardware run at a higher frequency and thus the counter does
331 * not represent seconds/nanoseconds. Instead it can be thought of as
332 * cycles and billions of cycles.
333 */
334 case ixgbe_mac_X550:
335 case ixgbe_mac_X550EM_x:
Mark Rustad49425df2016-04-01 12:18:09 -0700336 case ixgbe_mac_x550em_a:
Mark Rustada9763f32015-10-27 09:58:07 -0700337 /* Upper 32 bits represent billions of cycles, lower 32 bits
338 * represent cycles. However, we use timespec64_to_ns for the
339 * correct math even though the units haven't been corrected
340 * yet.
341 */
342 systime.tv_sec = timestamp >> 32;
343 systime.tv_nsec = timestamp & 0xFFFFFFFF;
344
345 timestamp = timespec64_to_ns(&systime);
346 break;
347 default:
348 break;
349 }
350
351 spin_lock_irqsave(&adapter->tmreg_lock, flags);
352 ns = timecounter_cyc2time(&adapter->hw_tc, timestamp);
353 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
354
355 hwtstamp->hwtstamp = ns_to_ktime(ns);
356}
357
358/**
359 * ixgbe_ptp_adjfreq_82599
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000360 * @ptp: the ptp clock structure
361 * @ppb: parts per billion adjustment from base
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000362 *
363 * adjust the frequency of the ptp cycle counter by the
364 * indicated ppb from the base frequency.
365 */
Mark Rustada9763f32015-10-27 09:58:07 -0700366static int ixgbe_ptp_adjfreq_82599(struct ptp_clock_info *ptp, s32 ppb)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000367{
368 struct ixgbe_adapter *adapter =
369 container_of(ptp, struct ixgbe_adapter, ptp_caps);
370 struct ixgbe_hw *hw = &adapter->hw;
Mark Rustada9763f32015-10-27 09:58:07 -0700371 u64 freq, incval;
372 u32 diff;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000373 int neg_adj = 0;
374
375 if (ppb < 0) {
376 neg_adj = 1;
377 ppb = -ppb;
378 }
379
380 smp_mb();
381 incval = ACCESS_ONCE(adapter->base_incval);
382
383 freq = incval;
384 freq *= ppb;
385 diff = div_u64(freq, 1000000000ULL);
386
387 incval = neg_adj ? (incval - diff) : (incval + diff);
388
389 switch (hw->mac.type) {
390 case ixgbe_mac_X540:
Mark Rustada9763f32015-10-27 09:58:07 -0700391 if (incval > 0xFFFFFFFFULL)
392 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
393 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, (u32)incval);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000394 break;
395 case ixgbe_mac_82599EB:
Mark Rustada9763f32015-10-27 09:58:07 -0700396 if (incval > 0x00FFFFFFULL)
397 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000398 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
Jacob Kellerb4f47a42016-04-13 16:08:22 -0700399 BIT(IXGBE_INCPER_SHIFT_82599) |
Mark Rustada9763f32015-10-27 09:58:07 -0700400 ((u32)incval & 0x00FFFFFFUL));
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000401 break;
402 default:
403 break;
404 }
405
406 return 0;
407}
408
409/**
Mark Rustada9763f32015-10-27 09:58:07 -0700410 * ixgbe_ptp_adjfreq_X550
411 * @ptp: the ptp clock structure
412 * @ppb: parts per billion adjustment from base
413 *
414 * adjust the frequency of the SYSTIME registers by the indicated ppb from base
415 * frequency
416 */
417static int ixgbe_ptp_adjfreq_X550(struct ptp_clock_info *ptp, s32 ppb)
418{
419 struct ixgbe_adapter *adapter =
420 container_of(ptp, struct ixgbe_adapter, ptp_caps);
421 struct ixgbe_hw *hw = &adapter->hw;
422 int neg_adj = 0;
423 u64 rate = IXGBE_X550_BASE_PERIOD;
424 u32 inca;
425
426 if (ppb < 0) {
427 neg_adj = 1;
428 ppb = -ppb;
429 }
430 rate *= ppb;
431 rate = div_u64(rate, 1000000000ULL);
432
433 /* warn if rate is too large */
434 if (rate >= INCVALUE_MASK)
435 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
436
437 inca = rate & INCVALUE_MASK;
438 if (neg_adj)
439 inca |= ISGN;
440
441 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, inca);
442
443 return 0;
444}
445
446/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000447 * ixgbe_ptp_adjtime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000448 * @ptp: the ptp clock structure
449 * @delta: offset to adjust the cycle counter by
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000450 *
451 * adjust the timer by resetting the timecounter structure.
452 */
453static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
454{
455 struct ixgbe_adapter *adapter =
456 container_of(ptp, struct ixgbe_adapter, ptp_caps);
457 unsigned long flags;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000458
459 spin_lock_irqsave(&adapter->tmreg_lock, flags);
Mark Rustada9763f32015-10-27 09:58:07 -0700460 timecounter_adjtime(&adapter->hw_tc, delta);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000461 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000462
Mark Rustada9763f32015-10-27 09:58:07 -0700463 if (adapter->ptp_setup_sdp)
464 adapter->ptp_setup_sdp(adapter);
Jacob Keller82083672012-08-01 07:12:25 +0000465
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000466 return 0;
467}
468
469/**
470 * ixgbe_ptp_gettime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000471 * @ptp: the ptp clock structure
472 * @ts: timespec structure to hold the current time value
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000473 *
474 * read the timecounter and return the correct value on ns,
475 * after converting it into a struct timespec.
476 */
Richard Cochran91432d12015-03-29 23:12:04 +0200477static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000478{
479 struct ixgbe_adapter *adapter =
480 container_of(ptp, struct ixgbe_adapter, ptp_caps);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000481 unsigned long flags;
Mark Rustada9763f32015-10-27 09:58:07 -0700482 u64 ns;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000483
484 spin_lock_irqsave(&adapter->tmreg_lock, flags);
Mark Rustada9763f32015-10-27 09:58:07 -0700485 ns = timecounter_read(&adapter->hw_tc);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000486 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
487
Richard Cochran0704fae2015-03-31 23:08:13 +0200488 *ts = ns_to_timespec64(ns);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000489
490 return 0;
491}
492
493/**
494 * ixgbe_ptp_settime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000495 * @ptp: the ptp clock structure
496 * @ts: the timespec containing the new time for the cycle counter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000497 *
498 * reset the timecounter to use a new base value instead of the kernel
499 * wall timer value.
500 */
501static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
Richard Cochran91432d12015-03-29 23:12:04 +0200502 const struct timespec64 *ts)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000503{
504 struct ixgbe_adapter *adapter =
505 container_of(ptp, struct ixgbe_adapter, ptp_caps);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000506 unsigned long flags;
Mark Rustada9763f32015-10-27 09:58:07 -0700507 u64 ns = timespec64_to_ns(ts);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000508
509 /* reset the timecounter */
510 spin_lock_irqsave(&adapter->tmreg_lock, flags);
Mark Rustada9763f32015-10-27 09:58:07 -0700511 timecounter_init(&adapter->hw_tc, &adapter->hw_cc, ns);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000512 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
513
Mark Rustada9763f32015-10-27 09:58:07 -0700514 if (adapter->ptp_setup_sdp)
515 adapter->ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000516 return 0;
517}
518
519/**
Jacob Keller04c8de82014-05-16 05:12:24 +0000520 * ixgbe_ptp_feature_enable
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000521 * @ptp: the ptp clock structure
522 * @rq: the requested feature to change
523 * @on: whether to enable or disable the feature
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000524 *
525 * enable (or disable) ancillary features of the phc subsystem.
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000526 * our driver only supports the PPS feature on the X540
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000527 */
Jacob Keller04c8de82014-05-16 05:12:24 +0000528static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
529 struct ptp_clock_request *rq, int on)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000530{
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000531 struct ixgbe_adapter *adapter =
532 container_of(ptp, struct ixgbe_adapter, ptp_caps);
533
534 /**
535 * When PPS is enabled, unmask the interrupt for the ClockOut
536 * feature, so that the interrupt handler can send the PPS
537 * event when the clock SDP triggers. Clear mask when PPS is
538 * disabled
539 */
Mark Rustada9763f32015-10-27 09:58:07 -0700540 if (rq->type != PTP_CLK_REQ_PPS || !adapter->ptp_setup_sdp)
541 return -ENOTSUPP;
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000542
Mark Rustada9763f32015-10-27 09:58:07 -0700543 if (on)
544 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
545 else
546 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000547
Mark Rustada9763f32015-10-27 09:58:07 -0700548 adapter->ptp_setup_sdp(adapter);
549 return 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000550}
551
552/**
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000553 * ixgbe_ptp_check_pps_event
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000554 * @adapter: the private adapter structure
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000555 *
556 * This function is called by the interrupt routine when checking for
557 * interrupts. It will check and handle a pps event.
558 */
Mark Rustada9763f32015-10-27 09:58:07 -0700559void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter)
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000560{
561 struct ixgbe_hw *hw = &adapter->hw;
562 struct ptp_clock_event event;
563
Jacob Keller3645adb2012-10-13 05:00:06 +0000564 event.type = PTP_CLOCK_PPS;
565
566 /* this check is necessary in case the interrupt was enabled via some
567 * alternative means (ex. debug_fs). Better to check here than
568 * everywhere that calls this function.
569 */
570 if (!adapter->ptp_clock)
571 return;
572
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000573 switch (hw->mac.type) {
574 case ixgbe_mac_X540:
575 ptp_clock_event(adapter->ptp_clock, &event);
576 break;
577 default:
578 break;
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000579 }
580}
581
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000582/**
Jacob Kellerf2f333872012-12-05 07:24:35 +0000583 * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
584 * @adapter: private adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000585 *
Jacob Kellerf2f333872012-12-05 07:24:35 +0000586 * this watchdog task periodically reads the timecounter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000587 * in order to prevent missing when the system time registers wrap
Jacob Kellerf2f333872012-12-05 07:24:35 +0000588 * around. This needs to be run approximately twice a minute.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000589 */
590void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
591{
Jacob Kellerf2f333872012-12-05 07:24:35 +0000592 bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
593 IXGBE_OVERFLOW_PERIOD);
Richard Cochran91432d12015-03-29 23:12:04 +0200594 struct timespec64 ts;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000595
Jacob Keller891dc082012-12-05 07:24:46 +0000596 if (timeout) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000597 ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
598 adapter->last_overflow_check = jiffies;
599 }
600}
601
602/**
Jacob Keller6cb562d2012-12-05 07:24:41 +0000603 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
604 * @adapter: private network adapter structure
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000605 *
Jacob Keller6cb562d2012-12-05 07:24:41 +0000606 * this watchdog task is scheduled to detect error case where hardware has
607 * dropped an Rx packet that was timestamped when the ring is full. The
608 * particular error is rare but leaves the device in a state unable to timestamp
609 * any future packets.
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000610 */
Jacob Keller6cb562d2012-12-05 07:24:41 +0000611void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000612{
Jacob Keller6cb562d2012-12-05 07:24:41 +0000613 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller6cb562d2012-12-05 07:24:41 +0000614 u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
Mark Rustada9763f32015-10-27 09:58:07 -0700615 struct ixgbe_ring *rx_ring;
Jacob Keller6cb562d2012-12-05 07:24:41 +0000616 unsigned long rx_event;
Mark Rustada9763f32015-10-27 09:58:07 -0700617 int n;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000618
Jacob Keller6cb562d2012-12-05 07:24:41 +0000619 /* if we don't have a valid timestamp in the registers, just update the
620 * timeout counter and exit
621 */
622 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
623 adapter->last_rx_ptp_check = jiffies;
624 return;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000625 }
626
Jacob Keller6cb562d2012-12-05 07:24:41 +0000627 /* determine the most recent watchdog or rx_timestamp event */
628 rx_event = adapter->last_rx_ptp_check;
Mark Rustada9763f32015-10-27 09:58:07 -0700629 for (n = 0; n < adapter->num_rx_queues; n++) {
630 rx_ring = adapter->rx_ring[n];
631 if (time_after(rx_ring->last_rx_timestamp, rx_event))
632 rx_event = rx_ring->last_rx_timestamp;
633 }
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000634
Jacob Keller6cb562d2012-12-05 07:24:41 +0000635 /* only need to read the high RXSTMP register to clear the lock */
Mark Rustada9763f32015-10-27 09:58:07 -0700636 if (time_is_before_jiffies(rx_event + 5 * HZ)) {
Jacob Keller6cb562d2012-12-05 07:24:41 +0000637 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
638 adapter->last_rx_ptp_check = jiffies;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000639
Mark Rustada9763f32015-10-27 09:58:07 -0700640 adapter->rx_hwtstamp_cleared++;
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000641 e_warn(drv, "clearing RX Timestamp hang\n");
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000642 }
643}
644
645/**
Mark Rustada9763f32015-10-27 09:58:07 -0700646 * ixgbe_ptp_clear_tx_timestamp - utility function to clear Tx timestamp state
647 * @adapter: the private adapter structure
648 *
649 * This function should be called whenever the state related to a Tx timestamp
650 * needs to be cleared. This helps ensure that all related bits are reset for
651 * the next Tx timestamp event.
652 */
653static void ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter *adapter)
654{
655 struct ixgbe_hw *hw = &adapter->hw;
656
657 IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
658 if (adapter->ptp_tx_skb) {
659 dev_kfree_skb_any(adapter->ptp_tx_skb);
660 adapter->ptp_tx_skb = NULL;
661 }
662 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
663}
664
665/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000666 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
Jacob Keller891dc082012-12-05 07:24:46 +0000667 * @adapter: the private adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000668 *
669 * if the timestamp is valid, we convert it into the timecounter ns
670 * value, then store that result into the shhwtstamps structure which
671 * is passed up the network stack
672 */
Jacob Keller891dc082012-12-05 07:24:46 +0000673static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000674{
Jacob Kelleraaebaf52017-05-03 10:28:53 -0700675 struct sk_buff *skb = adapter->ptp_tx_skb;
Jacob Keller891dc082012-12-05 07:24:46 +0000676 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000677 struct skb_shared_hwtstamps shhwtstamps;
Mark Rustada9763f32015-10-27 09:58:07 -0700678 u64 regval = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000679
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000680 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
681 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
Mark Rustada9763f32015-10-27 09:58:07 -0700682 ixgbe_ptp_convert_to_hwtstamp(adapter, &shhwtstamps, regval);
Jacob Keller891dc082012-12-05 07:24:46 +0000683
Jacob Kelleraaebaf52017-05-03 10:28:53 -0700684 /* Handle cleanup of the ptp_tx_skb ourselves, and unlock the state
685 * bit prior to notifying the stack via skb_tstamp_tx(). This prevents
686 * well behaved applications from attempting to timestamp again prior
687 * to the lock bit being clear.
688 */
689 adapter->ptp_tx_skb = NULL;
690 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
691
692 /* Notify the stack and then free the skb after we've unlocked */
693 skb_tstamp_tx(skb, &shhwtstamps);
694 dev_kfree_skb_any(skb);
Jacob Keller891dc082012-12-05 07:24:46 +0000695}
696
697/**
698 * ixgbe_ptp_tx_hwtstamp_work
699 * @work: pointer to the work struct
700 *
701 * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
Joe Perchesdbedd442015-03-06 20:49:12 -0800702 * timestamp has been taken for the current skb. It is necessary, because the
Jacob Keller891dc082012-12-05 07:24:46 +0000703 * descriptor's "done" bit does not correlate with the timestamp event.
704 */
705static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
706{
707 struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
708 ptp_tx_work);
709 struct ixgbe_hw *hw = &adapter->hw;
710 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
711 IXGBE_PTP_TX_TIMEOUT);
712 u32 tsynctxctl;
713
Mark Rustada9763f32015-10-27 09:58:07 -0700714 /* we have to have a valid skb to poll for a timestamp */
715 if (!adapter->ptp_tx_skb) {
716 ixgbe_ptp_clear_tx_timestamp(adapter);
Jacob Keller891dc082012-12-05 07:24:46 +0000717 return;
718 }
719
Mark Rustada9763f32015-10-27 09:58:07 -0700720 /* stop polling once we have a valid timestamp */
Jacob Keller891dc082012-12-05 07:24:46 +0000721 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
Mark Rustada9763f32015-10-27 09:58:07 -0700722 if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID) {
Jacob Keller891dc082012-12-05 07:24:46 +0000723 ixgbe_ptp_tx_hwtstamp(adapter);
Mark Rustada9763f32015-10-27 09:58:07 -0700724 return;
725 }
726
727 if (timeout) {
728 ixgbe_ptp_clear_tx_timestamp(adapter);
729 adapter->tx_hwtstamp_timeouts++;
730 e_warn(drv, "clearing Tx Timestamp hang\n");
731 } else {
Jacob Keller891dc082012-12-05 07:24:46 +0000732 /* reschedule to keep checking if it's not available yet */
733 schedule_work(&adapter->ptp_tx_work);
Mark Rustada9763f32015-10-27 09:58:07 -0700734 }
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000735}
736
737/**
Mark Rustada9763f32015-10-27 09:58:07 -0700738 * ixgbe_ptp_rx_pktstamp - utility function to get RX time stamp from buffer
739 * @q_vector: structure containing interrupt and ring information
740 * @skb: the packet
741 *
742 * This function will be called by the Rx routine of the timestamp for this
743 * packet is stored in the buffer. The value is stored in little endian format
744 * starting at the end of the packet data.
745 */
746void ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector *q_vector,
747 struct sk_buff *skb)
748{
749 __le64 regval;
750
751 /* copy the bits out of the skb, and then trim the skb length */
752 skb_copy_bits(skb, skb->len - IXGBE_TS_HDR_LEN, &regval,
753 IXGBE_TS_HDR_LEN);
754 __pskb_trim(skb, skb->len - IXGBE_TS_HDR_LEN);
755
756 /* The timestamp is recorded in little endian format, and is stored at
757 * the end of the packet.
758 *
759 * DWORD: N N + 1 N + 2
760 * Field: End of Packet SYSTIMH SYSTIML
761 */
762 ixgbe_ptp_convert_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
763 le64_to_cpu(regval));
764}
765
766/**
767 * ixgbe_ptp_rx_rgtstamp - utility function which checks for RX time stamp
768 * @q_vector: structure containing interrupt and ring information
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000769 * @skb: particular skb to send timestamp with
770 *
771 * if the timestamp is valid, we convert it into the timecounter ns
772 * value, then store that result into the shhwtstamps structure which
773 * is passed up the network stack
774 */
Mark Rustada9763f32015-10-27 09:58:07 -0700775void ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector *q_vector,
776 struct sk_buff *skb)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000777{
Mark Rustada9763f32015-10-27 09:58:07 -0700778 struct ixgbe_adapter *adapter;
779 struct ixgbe_hw *hw;
780 u64 regval = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000781 u32 tsyncrxctl;
Mark Rustada9763f32015-10-27 09:58:07 -0700782
783 /* we cannot process timestamps on a ring without a q_vector */
784 if (!q_vector || !q_vector->adapter)
785 return;
786
787 adapter = q_vector->adapter;
788 hw = &adapter->hw;
789
790 /* Read the tsyncrxctl register afterwards in order to prevent taking an
791 * I/O hit on every packet.
792 */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000793
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000794 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
Jiri Bencf42df162012-10-25 18:12:05 +0000795 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000796 return;
797
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000798 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
799 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
800
Mark Rustada9763f32015-10-27 09:58:07 -0700801 ixgbe_ptp_convert_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000802}
803
Jacob Keller93501d42014-02-28 15:48:58 -0800804int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
805{
806 struct hwtstamp_config *config = &adapter->tstamp_config;
807
808 return copy_to_user(ifr->ifr_data, config,
809 sizeof(*config)) ? -EFAULT : 0;
810}
811
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000812/**
Jacob Kellera7ef4282014-05-16 05:12:25 +0000813 * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
814 * @adapter: the private ixgbe adapter structure
815 * @config: the hwtstamp configuration requested
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000816 *
817 * Outgoing time stamping can be enabled and disabled. Play nice and
Jacob Keller93501d42014-02-28 15:48:58 -0800818 * disable it when requested, although it shouldn't cause any overhead
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000819 * when no packet needs it. At most one packet in the queue may be
820 * marked for time stamping, otherwise it would be impossible to tell
821 * for sure to which packet the hardware time stamp belongs.
822 *
823 * Incoming time stamping has to be configured via the hardware
824 * filters. Not all combinations are supported, in particular event
825 * type has to be specified. Matching the kind of event packet is
826 * not supported, with the exception of "all V2 events regardless of
827 * level 2 or 4".
Jacob Kellerc19197a2012-05-22 06:08:37 +0000828 *
829 * Since hardware always timestamps Path delay packets when timestamping V2
830 * packets, regardless of the type specified in the register, only use V2
831 * Event mode. This more accurately tells the user what the hardware is going
832 * to do anyways.
Jacob Kellera7ef4282014-05-16 05:12:25 +0000833 *
834 * Note: this may modify the hwtstamp configuration towards a more general
835 * mode, if required to support the specifically requested mode.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000836 */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000837static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
838 struct hwtstamp_config *config)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000839{
840 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000841 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
842 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000843 u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000844 bool is_l2 = false;
845 u32 regval;
846
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000847 /* reserved for future extensions */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000848 if (config->flags)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000849 return -EINVAL;
850
Jacob Kellera7ef4282014-05-16 05:12:25 +0000851 switch (config->tx_type) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000852 case HWTSTAMP_TX_OFF:
853 tsync_tx_ctl = 0;
854 case HWTSTAMP_TX_ON:
855 break;
856 default:
857 return -ERANGE;
858 }
859
Jacob Kellera7ef4282014-05-16 05:12:25 +0000860 switch (config->rx_filter) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000861 case HWTSTAMP_FILTER_NONE:
862 tsync_rx_ctl = 0;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000863 tsync_rx_mtrl = 0;
Mark Rustada9763f32015-10-27 09:58:07 -0700864 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
865 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000866 break;
867 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
868 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
Jacob Kellerb1e50f72012-12-05 07:53:38 +0000869 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
Yusuke Suzukiaeb4c732016-11-21 06:48:45 +0000870 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
871 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000872 break;
873 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
874 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
Jacob Kellerb1e50f72012-12-05 07:53:38 +0000875 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
Yusuke Suzukiaeb4c732016-11-21 06:48:45 +0000876 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
877 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000878 break;
Jacob Kellerc19197a2012-05-22 06:08:37 +0000879 case HWTSTAMP_FILTER_PTP_V2_EVENT:
880 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
881 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000882 case HWTSTAMP_FILTER_PTP_V2_SYNC:
883 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
884 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000885 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
886 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
887 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000888 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000889 is_l2 = true;
Jacob Kellera7ef4282014-05-16 05:12:25 +0000890 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
Yusuke Suzukiaeb4c732016-11-21 06:48:45 +0000891 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
892 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000893 break;
894 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Miroslav Lichvare3412572017-05-19 17:52:36 +0200895 case HWTSTAMP_FILTER_NTP_ALL:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000896 case HWTSTAMP_FILTER_ALL:
Mark Rustada9763f32015-10-27 09:58:07 -0700897 /* The X550 controller is capable of timestamping all packets,
898 * which allows it to accept any filter.
899 */
900 if (hw->mac.type >= ixgbe_mac_X550) {
901 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_ALL;
902 config->rx_filter = HWTSTAMP_FILTER_ALL;
903 adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
904 break;
905 }
906 /* fall through */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000907 default:
908 /*
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000909 * register RXMTRL must be set in order to do V1 packets,
910 * therefore it is not possible to time stamp both V1 Sync and
911 * Delay_Req messages and hardware does not support
912 * timestamping all packets => return error
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000913 */
Mark Rustada9763f32015-10-27 09:58:07 -0700914 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
915 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
Jacob Kellera7ef4282014-05-16 05:12:25 +0000916 config->rx_filter = HWTSTAMP_FILTER_NONE;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000917 return -ERANGE;
918 }
919
920 if (hw->mac.type == ixgbe_mac_82598EB) {
Mark Rustada9763f32015-10-27 09:58:07 -0700921 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
922 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000923 if (tsync_rx_ctl | tsync_tx_ctl)
924 return -ERANGE;
925 return 0;
926 }
927
Mark Rustada9763f32015-10-27 09:58:07 -0700928 /* Per-packet timestamping only works if the filter is set to all
929 * packets. Since this is desired, always timestamp all packets as long
930 * as any Rx filter was configured.
931 */
932 switch (hw->mac.type) {
933 case ixgbe_mac_X550:
934 case ixgbe_mac_X550EM_x:
Mark Rustad49425df2016-04-01 12:18:09 -0700935 case ixgbe_mac_x550em_a:
Mark Rustada9763f32015-10-27 09:58:07 -0700936 /* enable timestamping all packets only if at least some
937 * packets were requested. Otherwise, play nice and disable
938 * timestamping
939 */
940 if (config->rx_filter == HWTSTAMP_FILTER_NONE)
941 break;
942
943 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED |
944 IXGBE_TSYNCRXCTL_TYPE_ALL |
945 IXGBE_TSYNCRXCTL_TSIP_UT_EN;
946 config->rx_filter = HWTSTAMP_FILTER_ALL;
947 adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
948 adapter->flags &= ~IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER;
949 is_l2 = true;
950 break;
951 default:
952 break;
953 }
954
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000955 /* define ethertype filter for timestamping L2 packets */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000956 if (is_l2)
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000957 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000958 (IXGBE_ETQF_FILTER_EN | /* enable filter */
959 IXGBE_ETQF_1588 | /* enable timestamping */
960 ETH_P_1588)); /* 1588 eth protocol type */
961 else
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000962 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000963
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000964 /* enable/disable TX */
965 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
966 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
967 regval |= tsync_tx_ctl;
968 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
969
970 /* enable/disable RX */
971 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
972 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
973 regval |= tsync_rx_ctl;
974 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
975
976 /* define which PTP packets are time stamped */
977 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
978
979 IXGBE_WRITE_FLUSH(hw);
980
981 /* clear TX/RX time stamp registers, just to be sure */
Mark Rustada9763f32015-10-27 09:58:07 -0700982 ixgbe_ptp_clear_tx_timestamp(adapter);
983 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000984
Jacob Kellera7ef4282014-05-16 05:12:25 +0000985 return 0;
986}
987
988/**
989 * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
990 * @adapter: pointer to adapter struct
991 * @ifreq: ioctl data
992 *
993 * Set hardware to requested mode. If unsupported, return an error with no
994 * changes. Otherwise, store the mode for future reference.
995 */
996int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
997{
998 struct hwtstamp_config config;
999 int err;
1000
1001 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1002 return -EFAULT;
1003
1004 err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
1005 if (err)
1006 return err;
1007
Jacob Keller93501d42014-02-28 15:48:58 -08001008 /* save these settings for future reference */
1009 memcpy(&adapter->tstamp_config, &config,
1010 sizeof(adapter->tstamp_config));
1011
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001012 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1013 -EFAULT : 0;
1014}
1015
Mark Rustada9763f32015-10-27 09:58:07 -07001016static void ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter *adapter,
1017 u32 *shift, u32 *incval)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001018{
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001019 /**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001020 * Scale the NIC cycle counter by a large factor so that
1021 * relatively small corrections to the frequency can be added
1022 * or subtracted. The drawbacks of a large factor include
1023 * (a) the clock register overflows more quickly, (b) the cycle
1024 * counter structure must be able to convert the systime value
1025 * to nanoseconds using only a multiplier and a right-shift,
1026 * and (c) the value must fit within the timinca register space
1027 * => math based on internal DMA clock rate and available bits
Jacob Keller1a71ab22012-08-25 03:54:19 +00001028 *
1029 * Note that when there is no link, internal DMA clock is same as when
1030 * link speed is 10Gb. Set the registers correctly even when link is
1031 * down to preserve the clock setting
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001032 */
Jacob Keller1a71ab22012-08-25 03:54:19 +00001033 switch (adapter->link_speed) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001034 case IXGBE_LINK_SPEED_100_FULL:
Mark Rustada9763f32015-10-27 09:58:07 -07001035 *shift = IXGBE_INCVAL_SHIFT_100;
1036 *incval = IXGBE_INCVAL_100;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001037 break;
1038 case IXGBE_LINK_SPEED_1GB_FULL:
Mark Rustada9763f32015-10-27 09:58:07 -07001039 *shift = IXGBE_INCVAL_SHIFT_1GB;
1040 *incval = IXGBE_INCVAL_1GB;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001041 break;
1042 case IXGBE_LINK_SPEED_10GB_FULL:
Jacob Keller1a71ab22012-08-25 03:54:19 +00001043 default:
Mark Rustada9763f32015-10-27 09:58:07 -07001044 *shift = IXGBE_INCVAL_SHIFT_10GB;
1045 *incval = IXGBE_INCVAL_10GB;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001046 break;
1047 }
Mark Rustada9763f32015-10-27 09:58:07 -07001048}
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001049
Mark Rustada9763f32015-10-27 09:58:07 -07001050/**
1051 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
1052 * @adapter: pointer to the adapter structure
1053 *
1054 * This function should be called to set the proper values for the TIMINCA
1055 * register and tell the cyclecounter structure what the tick rate of SYSTIME
1056 * is. It does not directly modify SYSTIME registers or the timecounter
1057 * structure. It should be called whenever a new TIMINCA value is necessary,
1058 * such as during initialization or when the link speed changes.
1059 */
1060void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
1061{
1062 struct ixgbe_hw *hw = &adapter->hw;
1063 struct cyclecounter cc;
1064 unsigned long flags;
1065 u32 incval = 0;
1066 u32 tsauxc = 0;
1067 u32 fuse0 = 0;
1068
1069 /* For some of the boards below this mask is technically incorrect.
1070 * The timestamp mask overflows at approximately 61bits. However the
1071 * particular hardware does not overflow on an even bitmask value.
1072 * Instead, it overflows due to conversion of upper 32bits billions of
1073 * cycles. Timecounters are not really intended for this purpose so
1074 * they do not properly function if the overflow point isn't 2^N-1.
1075 * However, the actual SYSTIME values in question take ~138 years to
1076 * overflow. In practice this means they won't actually overflow. A
1077 * proper fix to this problem would require modification of the
1078 * timecounter delta calculations.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001079 */
Mark Rustada9763f32015-10-27 09:58:07 -07001080 cc.mask = CLOCKSOURCE_MASK(64);
1081 cc.mult = 1;
1082 cc.shift = 0;
1083
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001084 switch (hw->mac.type) {
Mark Rustada9763f32015-10-27 09:58:07 -07001085 case ixgbe_mac_X550EM_x:
1086 /* SYSTIME assumes X550EM_x board frequency is 300Mhz, and is
1087 * designed to represent seconds and nanoseconds when this is
1088 * the case. However, some revisions of hardware have a 400Mhz
1089 * clock and we have to compensate for this frequency
1090 * variation using corrected mult and shift values.
1091 */
1092 fuse0 = IXGBE_READ_REG(hw, IXGBE_FUSES0_GROUP(0));
1093 if (!(fuse0 & IXGBE_FUSES0_300MHZ)) {
1094 cc.mult = 3;
1095 cc.shift = 2;
1096 }
1097 /* fallthrough */
Mark Rustad49425df2016-04-01 12:18:09 -07001098 case ixgbe_mac_x550em_a:
Mark Rustada9763f32015-10-27 09:58:07 -07001099 case ixgbe_mac_X550:
1100 cc.read = ixgbe_ptp_read_X550;
1101
1102 /* enable SYSTIME counter */
1103 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMR, 0);
1104 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0);
1105 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0);
1106 tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC);
1107 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC,
1108 tsauxc & ~IXGBE_TSAUXC_DISABLE_SYSTIME);
1109 IXGBE_WRITE_REG(hw, IXGBE_TSIM, IXGBE_TSIM_TXTS);
1110 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_TIMESYNC);
1111
1112 IXGBE_WRITE_FLUSH(hw);
1113 break;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001114 case ixgbe_mac_X540:
Mark Rustada9763f32015-10-27 09:58:07 -07001115 cc.read = ixgbe_ptp_read_82599;
1116
1117 ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001118 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
1119 break;
1120 case ixgbe_mac_82599EB:
Mark Rustada9763f32015-10-27 09:58:07 -07001121 cc.read = ixgbe_ptp_read_82599;
1122
1123 ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001124 incval >>= IXGBE_INCVAL_SHIFT_82599;
Mark Rustada9763f32015-10-27 09:58:07 -07001125 cc.shift -= IXGBE_INCVAL_SHIFT_82599;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001126 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
Jacob Kellerb4f47a42016-04-13 16:08:22 -07001127 BIT(IXGBE_INCPER_SHIFT_82599) | incval);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001128 break;
1129 default:
1130 /* other devices aren't supported */
1131 return;
1132 }
1133
Jacob Keller1a71ab22012-08-25 03:54:19 +00001134 /* update the base incval used to calculate frequency adjustment */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001135 ACCESS_ONCE(adapter->base_incval) = incval;
1136 smp_mb();
1137
Jacob Keller1a71ab22012-08-25 03:54:19 +00001138 /* need lock to prevent incorrect read while modifying cyclecounter */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001139 spin_lock_irqsave(&adapter->tmreg_lock, flags);
Mark Rustada9763f32015-10-27 09:58:07 -07001140 memcpy(&adapter->hw_cc, &cc, sizeof(adapter->hw_cc));
Jacob Keller1a71ab22012-08-25 03:54:19 +00001141 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
1142}
1143
1144/**
1145 * ixgbe_ptp_reset
1146 * @adapter: the ixgbe private board structure
1147 *
Jacob Kellerd6321402014-05-16 05:12:26 +00001148 * When the MAC resets, all the hardware bits for timesync are reset. This
1149 * function is used to re-enable the device for PTP based on current settings.
1150 * We do lose the current clock time, so just reset the cyclecounter to the
1151 * system real clock time.
1152 *
1153 * This function will maintain hwtstamp_config settings, and resets the SDP
1154 * output if it was enabled.
Jacob Keller1a71ab22012-08-25 03:54:19 +00001155 */
1156void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
1157{
1158 struct ixgbe_hw *hw = &adapter->hw;
1159 unsigned long flags;
1160
Jacob Kellerd6321402014-05-16 05:12:26 +00001161 /* reset the hardware timestamping mode */
1162 ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
Jacob Keller93501d42014-02-28 15:48:58 -08001163
Mark Rustada9763f32015-10-27 09:58:07 -07001164 /* 82598 does not support PTP */
1165 if (hw->mac.type == ixgbe_mac_82598EB)
1166 return;
1167
Jacob Keller1a71ab22012-08-25 03:54:19 +00001168 ixgbe_ptp_start_cyclecounter(adapter);
1169
1170 spin_lock_irqsave(&adapter->tmreg_lock, flags);
Mark Rustada9763f32015-10-27 09:58:07 -07001171 timecounter_init(&adapter->hw_tc, &adapter->hw_cc,
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001172 ktime_to_ns(ktime_get_real()));
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001173 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
Jacob Keller82083672012-08-01 07:12:25 +00001174
Mark Rustada9763f32015-10-27 09:58:07 -07001175 adapter->last_overflow_check = jiffies;
1176
1177 /* Now that the shift has been calculated and the systime
Jacob Keller82083672012-08-01 07:12:25 +00001178 * registers reset, (re-)enable the Clock out feature
1179 */
Mark Rustada9763f32015-10-27 09:58:07 -07001180 if (adapter->ptp_setup_sdp)
1181 adapter->ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001182}
1183
1184/**
Jacob Keller63328ad2014-05-16 05:12:27 +00001185 * ixgbe_ptp_create_clock
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001186 * @adapter: the ixgbe private adapter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001187 *
Jacob Keller63328ad2014-05-16 05:12:27 +00001188 * This function performs setup of the user entry point function table and
1189 * initializes the PTP clock device, which is used to access the clock-like
Mark Rustada9763f32015-10-27 09:58:07 -07001190 * features of the PTP core. It will be called by ixgbe_ptp_init, and may
1191 * reuse a previously initialized clock (such as during a suspend/resume
1192 * cycle).
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001193 */
Mark Rustada9763f32015-10-27 09:58:07 -07001194static long ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001195{
1196 struct net_device *netdev = adapter->netdev;
Jacob Keller63328ad2014-05-16 05:12:27 +00001197 long err;
1198
1199 /* do nothing if we already have a clock device */
1200 if (!IS_ERR_OR_NULL(adapter->ptp_clock))
1201 return 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001202
1203 switch (adapter->hw.mac.type) {
1204 case ixgbe_mac_X540:
Jacob Kellerca324092014-02-25 17:58:54 -08001205 snprintf(adapter->ptp_caps.name,
1206 sizeof(adapter->ptp_caps.name),
1207 "%s", netdev->name);
Jacob E Keller681ae1a2012-05-01 05:24:41 +00001208 adapter->ptp_caps.owner = THIS_MODULE;
1209 adapter->ptp_caps.max_adj = 250000000;
1210 adapter->ptp_caps.n_alarm = 0;
1211 adapter->ptp_caps.n_ext_ts = 0;
1212 adapter->ptp_caps.n_per_out = 0;
1213 adapter->ptp_caps.pps = 1;
Mark Rustada9763f32015-10-27 09:58:07 -07001214 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
Jacob E Keller681ae1a2012-05-01 05:24:41 +00001215 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
Richard Cochran91432d12015-03-29 23:12:04 +02001216 adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
1217 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
Jacob Keller04c8de82014-05-16 05:12:24 +00001218 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
Mark Rustada9763f32015-10-27 09:58:07 -07001219 adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_x540;
Jacob E Keller681ae1a2012-05-01 05:24:41 +00001220 break;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001221 case ixgbe_mac_82599EB:
Jacob Kellerca324092014-02-25 17:58:54 -08001222 snprintf(adapter->ptp_caps.name,
1223 sizeof(adapter->ptp_caps.name),
1224 "%s", netdev->name);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001225 adapter->ptp_caps.owner = THIS_MODULE;
1226 adapter->ptp_caps.max_adj = 250000000;
1227 adapter->ptp_caps.n_alarm = 0;
1228 adapter->ptp_caps.n_ext_ts = 0;
1229 adapter->ptp_caps.n_per_out = 0;
1230 adapter->ptp_caps.pps = 0;
Mark Rustada9763f32015-10-27 09:58:07 -07001231 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001232 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
Richard Cochran91432d12015-03-29 23:12:04 +02001233 adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
1234 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
Jacob Keller04c8de82014-05-16 05:12:24 +00001235 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001236 break;
Mark Rustada9763f32015-10-27 09:58:07 -07001237 case ixgbe_mac_X550:
1238 case ixgbe_mac_X550EM_x:
Mark Rustad49425df2016-04-01 12:18:09 -07001239 case ixgbe_mac_x550em_a:
Mark Rustada9763f32015-10-27 09:58:07 -07001240 snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
1241 adapter->ptp_caps.owner = THIS_MODULE;
1242 adapter->ptp_caps.max_adj = 30000000;
1243 adapter->ptp_caps.n_alarm = 0;
1244 adapter->ptp_caps.n_ext_ts = 0;
1245 adapter->ptp_caps.n_per_out = 0;
1246 adapter->ptp_caps.pps = 0;
1247 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_X550;
1248 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1249 adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
1250 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1251 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1252 adapter->ptp_setup_sdp = NULL;
1253 break;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001254 default:
1255 adapter->ptp_clock = NULL;
Mark Rustada9763f32015-10-27 09:58:07 -07001256 adapter->ptp_setup_sdp = NULL;
Jacob Keller63328ad2014-05-16 05:12:27 +00001257 return -EOPNOTSUPP;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001258 }
1259
Richard Cochran1ef76152012-09-22 07:02:03 +00001260 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
1261 &adapter->pdev->dev);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001262 if (IS_ERR(adapter->ptp_clock)) {
Jacob Keller63328ad2014-05-16 05:12:27 +00001263 err = PTR_ERR(adapter->ptp_clock);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001264 adapter->ptp_clock = NULL;
1265 e_dev_err("ptp_clock_register failed\n");
Jacob Keller63328ad2014-05-16 05:12:27 +00001266 return err;
Nicolas Pitreefee95f2016-09-20 19:25:58 -04001267 } else if (adapter->ptp_clock)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001268 e_dev_info("registered PHC device on %s\n", netdev->name);
1269
Jacob Keller63328ad2014-05-16 05:12:27 +00001270 /* set default timestamp mode to disabled here. We do this in
1271 * create_clock instead of init, because we don't want to override the
1272 * previous settings during a resume cycle.
1273 */
Jacob Kellerd6321402014-05-16 05:12:26 +00001274 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
1275 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
Jacob Keller63328ad2014-05-16 05:12:27 +00001276
1277 return 0;
1278}
1279
1280/**
1281 * ixgbe_ptp_init
1282 * @adapter: the ixgbe private adapter structure
1283 *
1284 * This function performs the required steps for enabling PTP
1285 * support. If PTP support has already been loaded it simply calls the
1286 * cyclecounter init routine and exits.
1287 */
1288void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
1289{
1290 /* initialize the spin lock first since we can't control when a user
1291 * will call the entry functions once we have initialized the clock
1292 * device
1293 */
1294 spin_lock_init(&adapter->tmreg_lock);
1295
1296 /* obtain a PTP device, or re-use an existing device */
1297 if (ixgbe_ptp_create_clock(adapter))
1298 return;
1299
1300 /* we have a clock so we can initialize work now */
1301 INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
1302
1303 /* reset the PTP related hardware bits */
Jacob Keller1a71ab22012-08-25 03:54:19 +00001304 ixgbe_ptp_reset(adapter);
1305
Jacob Keller8fecf672013-06-21 08:14:32 +00001306 /* enter the IXGBE_PTP_RUNNING state */
1307 set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
Jacob Keller1a71ab22012-08-25 03:54:19 +00001308
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001309 return;
1310}
1311
1312/**
Jacob Keller9966d1e2014-05-16 05:12:28 +00001313 * ixgbe_ptp_suspend - stop PTP work items
1314 * @ adapter: pointer to adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001315 *
Jacob Keller9966d1e2014-05-16 05:12:28 +00001316 * this function suspends PTP activity, and prevents more PTP work from being
1317 * generated, but does not destroy the PTP clock device.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001318 */
Jacob Keller9966d1e2014-05-16 05:12:28 +00001319void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001320{
Jacob Keller8fecf672013-06-21 08:14:32 +00001321 /* Leave the IXGBE_PTP_RUNNING state. */
1322 if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
1323 return;
Jacob Kellerdb0677f2012-08-24 07:46:54 +00001324
Mark Rustada9763f32015-10-27 09:58:07 -07001325 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
1326 if (adapter->ptp_setup_sdp)
1327 adapter->ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001328
Jacob Keller9966d1e2014-05-16 05:12:28 +00001329 /* ensure that we cancel any pending PTP Tx work item in progress */
Jacob Keller891dc082012-12-05 07:24:46 +00001330 cancel_work_sync(&adapter->ptp_tx_work);
Mark Rustada9763f32015-10-27 09:58:07 -07001331 ixgbe_ptp_clear_tx_timestamp(adapter);
Jacob Keller9966d1e2014-05-16 05:12:28 +00001332}
Jacob Keller891dc082012-12-05 07:24:46 +00001333
Jacob Keller9966d1e2014-05-16 05:12:28 +00001334/**
1335 * ixgbe_ptp_stop - close the PTP device
1336 * @adapter: pointer to adapter struct
1337 *
1338 * completely destroy the PTP device, should only be called when the device is
1339 * being fully closed.
1340 */
1341void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
1342{
1343 /* first, suspend PTP activity */
1344 ixgbe_ptp_suspend(adapter);
1345
1346 /* disable the PTP clock device */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001347 if (adapter->ptp_clock) {
1348 ptp_clock_unregister(adapter->ptp_clock);
1349 adapter->ptp_clock = NULL;
1350 e_dev_info("removed PHC on %s\n",
1351 adapter->netdev->name);
1352 }
1353}