blob: e5ba04025e2b9dc76d58090cd5a46a563fbf22e4 [file] [log] [blame]
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Don Skidmore434c5e32013-01-08 05:02:28 +00004 Copyright(c) 1999 - 2013 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>
Jacob Keller3a6a4ed2012-05-01 05:24:58 +000030
31/*
32 * The 82599 and the X540 do not have true 64bit nanosecond scale
33 * counter registers. Instead, SYSTIME is defined by a fixed point
34 * system which allows the user to define the scale counter increment
35 * value at every level change of the oscillator driving the SYSTIME
36 * value. For both devices the TIMINCA:IV field defines this
37 * increment. On the X540 device, 31 bits are provided. However on the
38 * 82599 only provides 24 bits. The time unit is determined by the
39 * clock frequency of the oscillator in combination with the TIMINCA
40 * register. When these devices link at 10Gb the oscillator has a
41 * period of 6.4ns. In order to convert the scale counter into
42 * nanoseconds the cyclecounter and timecounter structures are
43 * used. The SYSTIME registers need to be converted to ns values by use
44 * of only a right shift (division by power of 2). The following math
45 * determines the largest incvalue that will fit into the available
46 * bits in the TIMINCA register.
47 *
48 * PeriodWidth: Number of bits to store the clock period
49 * MaxWidth: The maximum width value of the TIMINCA register
50 * Period: The clock period for the oscillator
51 * round(): discard the fractional portion of the calculation
52 *
53 * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
54 *
55 * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
56 * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
57 *
58 * The period also changes based on the link speed:
59 * At 10Gb link or no link, the period remains the same.
60 * At 1Gb link, the period is multiplied by 10. (64ns)
61 * At 100Mb link, the period is multiplied by 100. (640ns)
62 *
63 * The calculated value allows us to right shift the SYSTIME register
64 * value in order to quickly convert it into a nanosecond clock,
65 * while allowing for the maximum possible adjustment value.
66 *
67 * These diagrams are only for the 10Gb link period
68 *
69 * SYSTIMEH SYSTIMEL
70 * +--------------+ +--------------+
71 * X540 | 32 | | 1 | 3 | 28 |
72 * *--------------+ +--------------+
73 * \________ 36 bits ______/ fract
74 *
75 * +--------------+ +--------------+
76 * 82599 | 32 | | 8 | 3 | 21 |
77 * *--------------+ +--------------+
78 * \________ 43 bits ______/ fract
79 *
80 * The 36 bit X540 SYSTIME overflows every
81 * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
82 *
83 * The 43 bit 82599 SYSTIME overflows every
84 * 2^43 * 10^-9 / 3600 = 2.4 hours
85 */
86#define IXGBE_INCVAL_10GB 0x66666666
87#define IXGBE_INCVAL_1GB 0x40000000
88#define IXGBE_INCVAL_100 0x50000000
89
90#define IXGBE_INCVAL_SHIFT_10GB 28
91#define IXGBE_INCVAL_SHIFT_1GB 24
92#define IXGBE_INCVAL_SHIFT_100 21
93
94#define IXGBE_INCVAL_SHIFT_82599 7
95#define IXGBE_INCPER_SHIFT_82599 24
96#define IXGBE_MAX_TIMEADJ_VALUE 0x7FFFFFFFFFFFFFFFULL
97
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
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000107/**
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000108 * ixgbe_ptp_setup_sdp
Jacob Keller82083672012-08-01 07:12:25 +0000109 * @hw: the hardware private structure
Jacob Keller82083672012-08-01 07:12:25 +0000110 *
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000111 * this function enables or disables the clock out feature on SDP0 for
112 * the X540 device. It will create a 1second periodic output that can
113 * be used as the PPS (via an interrupt).
Jacob Keller82083672012-08-01 07:12:25 +0000114 *
115 * It calculates when the systime will be on an exact second, and then
116 * aligns the start of the PPS signal to that value. The shift is
117 * necessary because it can change based on the link speed.
118 */
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000119static void ixgbe_ptp_setup_sdp(struct ixgbe_adapter *adapter)
Jacob Keller82083672012-08-01 07:12:25 +0000120{
121 struct ixgbe_hw *hw = &adapter->hw;
122 int shift = adapter->cc.shift;
123 u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
124 u64 ns = 0, clock_edge = 0;
125
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000126 if ((adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED) &&
127 (hw->mac.type == ixgbe_mac_X540)) {
128
129 /* disable the pin first */
130 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
131 IXGBE_WRITE_FLUSH(hw);
132
Jacob Keller82083672012-08-01 07:12:25 +0000133 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
134
135 /*
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000136 * enable the SDP0 pin as output, and connected to the
137 * native function for Timesync (ClockOut)
Jacob Keller82083672012-08-01 07:12:25 +0000138 */
139 esdp |= (IXGBE_ESDP_SDP0_DIR |
140 IXGBE_ESDP_SDP0_NATIVE);
141
142 /*
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000143 * enable the Clock Out feature on SDP0, and allow
144 * interrupts to occur when the pin changes
Jacob Keller82083672012-08-01 07:12:25 +0000145 */
146 tsauxc = (IXGBE_TSAUXC_EN_CLK |
147 IXGBE_TSAUXC_SYNCLK |
148 IXGBE_TSAUXC_SDP0_INT);
149
150 /* clock period (or pulse length) */
Jacob Kellera5a0fc02014-05-28 07:21:47 +0000151 clktiml = (u32)(IXGBE_PTP_PPS_HALF_SECOND << shift);
152 clktimh = (u32)((IXGBE_PTP_PPS_HALF_SECOND << shift) >> 32);
Jacob Keller82083672012-08-01 07:12:25 +0000153
154 /*
155 * Account for the cyclecounter wrap-around value by
156 * using the converted ns value of the current time to
157 * check for when the next aligned second would occur.
158 */
159 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
160 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
161 ns = timecounter_cyc2time(&adapter->tc, clock_edge);
162
Jacob Kellera5a0fc02014-05-28 07:21:47 +0000163 div_u64_rem(ns, IXGBE_PTP_PPS_HALF_SECOND, &rem);
164 clock_edge += ((IXGBE_PTP_PPS_HALF_SECOND - (u64)rem) << shift);
Jacob Keller82083672012-08-01 07:12:25 +0000165
166 /* specify the initial clock start time */
167 trgttiml = (u32)clock_edge;
168 trgttimh = (u32)(clock_edge >> 32);
169
170 IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
171 IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
172 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
173 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
174
175 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
176 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000177 } else {
178 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
Jacob Keller82083672012-08-01 07:12:25 +0000179 }
Jacob Keller82083672012-08-01 07:12:25 +0000180
Jacob Keller82083672012-08-01 07:12:25 +0000181 IXGBE_WRITE_FLUSH(hw);
182}
183
184/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000185 * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000186 * @cc: the cyclecounter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000187 *
188 * this function reads the cyclecounter registers and is called by the
189 * cyclecounter structure used to construct a ns counter from the
190 * arbitrary fixed point registers
191 */
192static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
193{
194 struct ixgbe_adapter *adapter =
195 container_of(cc, struct ixgbe_adapter, cc);
196 struct ixgbe_hw *hw = &adapter->hw;
197 u64 stamp = 0;
198
199 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
200 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
201
202 return stamp;
203}
204
205/**
206 * ixgbe_ptp_adjfreq
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000207 * @ptp: the ptp clock structure
208 * @ppb: parts per billion adjustment from base
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000209 *
210 * adjust the frequency of the ptp cycle counter by the
211 * indicated ppb from the base frequency.
212 */
213static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
214{
215 struct ixgbe_adapter *adapter =
216 container_of(ptp, struct ixgbe_adapter, ptp_caps);
217 struct ixgbe_hw *hw = &adapter->hw;
218 u64 freq;
219 u32 diff, incval;
220 int neg_adj = 0;
221
222 if (ppb < 0) {
223 neg_adj = 1;
224 ppb = -ppb;
225 }
226
227 smp_mb();
228 incval = ACCESS_ONCE(adapter->base_incval);
229
230 freq = incval;
231 freq *= ppb;
232 diff = div_u64(freq, 1000000000ULL);
233
234 incval = neg_adj ? (incval - diff) : (incval + diff);
235
236 switch (hw->mac.type) {
237 case ixgbe_mac_X540:
238 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
239 break;
240 case ixgbe_mac_82599EB:
241 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
242 (1 << IXGBE_INCPER_SHIFT_82599) |
243 incval);
244 break;
245 default:
246 break;
247 }
248
249 return 0;
250}
251
252/**
253 * ixgbe_ptp_adjtime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000254 * @ptp: the ptp clock structure
255 * @delta: offset to adjust the cycle counter by
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000256 *
257 * adjust the timer by resetting the timecounter structure.
258 */
259static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
260{
261 struct ixgbe_adapter *adapter =
262 container_of(ptp, struct ixgbe_adapter, ptp_caps);
263 unsigned long flags;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000264
265 spin_lock_irqsave(&adapter->tmreg_lock, flags);
Richard Cochran826ef902014-12-21 19:47:03 +0100266 timecounter_adjtime(&adapter->tc, delta);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000267 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000268
269 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller82083672012-08-01 07:12:25 +0000270
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000271 return 0;
272}
273
274/**
275 * ixgbe_ptp_gettime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000276 * @ptp: the ptp clock structure
277 * @ts: timespec structure to hold the current time value
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000278 *
279 * read the timecounter and return the correct value on ns,
280 * after converting it into a struct timespec.
281 */
Richard Cochran91432d12015-03-29 23:12:04 +0200282static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000283{
284 struct ixgbe_adapter *adapter =
285 container_of(ptp, struct ixgbe_adapter, ptp_caps);
286 u64 ns;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000287 unsigned long flags;
288
289 spin_lock_irqsave(&adapter->tmreg_lock, flags);
290 ns = timecounter_read(&adapter->tc);
291 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
292
Richard Cochran0704fae2015-03-31 23:08:13 +0200293 *ts = ns_to_timespec64(ns);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000294
295 return 0;
296}
297
298/**
299 * ixgbe_ptp_settime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000300 * @ptp: the ptp clock structure
301 * @ts: the timespec containing the new time for the cycle counter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000302 *
303 * reset the timecounter to use a new base value instead of the kernel
304 * wall timer value.
305 */
306static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
Richard Cochran91432d12015-03-29 23:12:04 +0200307 const struct timespec64 *ts)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000308{
309 struct ixgbe_adapter *adapter =
310 container_of(ptp, struct ixgbe_adapter, ptp_caps);
311 u64 ns;
312 unsigned long flags;
313
Richard Cochran0704fae2015-03-31 23:08:13 +0200314 ns = timespec64_to_ns(ts);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000315
316 /* reset the timecounter */
317 spin_lock_irqsave(&adapter->tmreg_lock, flags);
318 timecounter_init(&adapter->tc, &adapter->cc, ns);
319 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
320
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000321 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000322 return 0;
323}
324
325/**
Jacob Keller04c8de82014-05-16 05:12:24 +0000326 * ixgbe_ptp_feature_enable
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000327 * @ptp: the ptp clock structure
328 * @rq: the requested feature to change
329 * @on: whether to enable or disable the feature
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000330 *
331 * enable (or disable) ancillary features of the phc subsystem.
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000332 * our driver only supports the PPS feature on the X540
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000333 */
Jacob Keller04c8de82014-05-16 05:12:24 +0000334static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
335 struct ptp_clock_request *rq, int on)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000336{
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000337 struct ixgbe_adapter *adapter =
338 container_of(ptp, struct ixgbe_adapter, ptp_caps);
339
340 /**
341 * When PPS is enabled, unmask the interrupt for the ClockOut
342 * feature, so that the interrupt handler can send the PPS
343 * event when the clock SDP triggers. Clear mask when PPS is
344 * disabled
345 */
346 if (rq->type == PTP_CLK_REQ_PPS) {
347 switch (adapter->hw.mac.type) {
348 case ixgbe_mac_X540:
349 if (on)
350 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
351 else
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000352 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
353
354 ixgbe_ptp_setup_sdp(adapter);
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000355 return 0;
356 default:
357 break;
358 }
359 }
360
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000361 return -ENOTSUPP;
362}
363
364/**
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000365 * ixgbe_ptp_check_pps_event
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000366 * @adapter: the private adapter structure
367 * @eicr: the interrupt cause register value
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000368 *
369 * This function is called by the interrupt routine when checking for
370 * interrupts. It will check and handle a pps event.
371 */
372void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
373{
374 struct ixgbe_hw *hw = &adapter->hw;
375 struct ptp_clock_event event;
376
Jacob Keller3645adb2012-10-13 05:00:06 +0000377 event.type = PTP_CLOCK_PPS;
378
379 /* this check is necessary in case the interrupt was enabled via some
380 * alternative means (ex. debug_fs). Better to check here than
381 * everywhere that calls this function.
382 */
383 if (!adapter->ptp_clock)
384 return;
385
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000386 switch (hw->mac.type) {
387 case ixgbe_mac_X540:
388 ptp_clock_event(adapter->ptp_clock, &event);
389 break;
390 default:
391 break;
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000392 }
393}
394
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000395/**
Jacob Kellerf2f333872012-12-05 07:24:35 +0000396 * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
397 * @adapter: private adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000398 *
Jacob Kellerf2f333872012-12-05 07:24:35 +0000399 * this watchdog task periodically reads the timecounter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000400 * in order to prevent missing when the system time registers wrap
Jacob Kellerf2f333872012-12-05 07:24:35 +0000401 * around. This needs to be run approximately twice a minute.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000402 */
403void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
404{
Jacob Kellerf2f333872012-12-05 07:24:35 +0000405 bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
406 IXGBE_OVERFLOW_PERIOD);
Richard Cochran91432d12015-03-29 23:12:04 +0200407 struct timespec64 ts;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000408
Jacob Keller891dc082012-12-05 07:24:46 +0000409 if (timeout) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000410 ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
411 adapter->last_overflow_check = jiffies;
412 }
413}
414
415/**
Jacob Keller6cb562d2012-12-05 07:24:41 +0000416 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
417 * @adapter: private network adapter structure
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000418 *
Jacob Keller6cb562d2012-12-05 07:24:41 +0000419 * this watchdog task is scheduled to detect error case where hardware has
420 * dropped an Rx packet that was timestamped when the ring is full. The
421 * particular error is rare but leaves the device in a state unable to timestamp
422 * any future packets.
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000423 */
Jacob Keller6cb562d2012-12-05 07:24:41 +0000424void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000425{
Jacob Keller6cb562d2012-12-05 07:24:41 +0000426 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller6cb562d2012-12-05 07:24:41 +0000427 u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
428 unsigned long rx_event;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000429
Jacob Keller6cb562d2012-12-05 07:24:41 +0000430 /* if we don't have a valid timestamp in the registers, just update the
431 * timeout counter and exit
432 */
433 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
434 adapter->last_rx_ptp_check = jiffies;
435 return;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000436 }
437
Jacob Keller6cb562d2012-12-05 07:24:41 +0000438 /* determine the most recent watchdog or rx_timestamp event */
439 rx_event = adapter->last_rx_ptp_check;
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000440 if (time_after(adapter->last_rx_timestamp, rx_event))
441 rx_event = adapter->last_rx_timestamp;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000442
Jacob Keller6cb562d2012-12-05 07:24:41 +0000443 /* only need to read the high RXSTMP register to clear the lock */
444 if (time_is_before_jiffies(rx_event + 5*HZ)) {
445 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
446 adapter->last_rx_ptp_check = jiffies;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000447
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000448 e_warn(drv, "clearing RX Timestamp hang\n");
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000449 }
450}
451
452/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000453 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
Jacob Keller891dc082012-12-05 07:24:46 +0000454 * @adapter: the private adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000455 *
456 * if the timestamp is valid, we convert it into the timecounter ns
457 * value, then store that result into the shhwtstamps structure which
458 * is passed up the network stack
459 */
Jacob Keller891dc082012-12-05 07:24:46 +0000460static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000461{
Jacob Keller891dc082012-12-05 07:24:46 +0000462 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000463 struct skb_shared_hwtstamps shhwtstamps;
464 u64 regval = 0, ns;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000465 unsigned long flags;
466
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000467 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
468 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
469
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000470 spin_lock_irqsave(&adapter->tmreg_lock, flags);
471 ns = timecounter_cyc2time(&adapter->tc, regval);
472 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
473
474 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
475 shhwtstamps.hwtstamp = ns_to_ktime(ns);
Jacob Keller891dc082012-12-05 07:24:46 +0000476 skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
477
478 dev_kfree_skb_any(adapter->ptp_tx_skb);
479 adapter->ptp_tx_skb = NULL;
Jakub Kicinski151b260c2014-03-15 14:55:21 +0000480 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
Jacob Keller891dc082012-12-05 07:24:46 +0000481}
482
483/**
484 * ixgbe_ptp_tx_hwtstamp_work
485 * @work: pointer to the work struct
486 *
487 * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
Joe Perchesdbedd442015-03-06 20:49:12 -0800488 * timestamp has been taken for the current skb. It is necessary, because the
Jacob Keller891dc082012-12-05 07:24:46 +0000489 * descriptor's "done" bit does not correlate with the timestamp event.
490 */
491static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
492{
493 struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
494 ptp_tx_work);
495 struct ixgbe_hw *hw = &adapter->hw;
496 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
497 IXGBE_PTP_TX_TIMEOUT);
498 u32 tsynctxctl;
499
Jacob Keller891dc082012-12-05 07:24:46 +0000500 if (timeout) {
501 dev_kfree_skb_any(adapter->ptp_tx_skb);
502 adapter->ptp_tx_skb = NULL;
Jakub Kicinski151b260c2014-03-15 14:55:21 +0000503 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000504 e_warn(drv, "clearing Tx Timestamp hang\n");
Jacob Keller891dc082012-12-05 07:24:46 +0000505 return;
506 }
507
508 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
509 if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID)
510 ixgbe_ptp_tx_hwtstamp(adapter);
511 else
512 /* reschedule to keep checking if it's not available yet */
513 schedule_work(&adapter->ptp_tx_work);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000514}
515
516/**
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000517 * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
518 * @adapter: pointer to adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000519 * @skb: particular skb to send timestamp with
520 *
521 * if the timestamp is valid, we convert it into the timecounter ns
522 * value, then store that result into the shhwtstamps structure which
523 * is passed up the network stack
524 */
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000525void ixgbe_ptp_rx_hwtstamp(struct ixgbe_adapter *adapter, struct sk_buff *skb)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000526{
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000527 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000528 struct skb_shared_hwtstamps *shhwtstamps;
529 u64 regval = 0, ns;
530 u32 tsyncrxctl;
531 unsigned long flags;
532
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000533 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
Jiri Bencf42df162012-10-25 18:12:05 +0000534 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000535 return;
536
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000537 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
538 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
539
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000540 spin_lock_irqsave(&adapter->tmreg_lock, flags);
541 ns = timecounter_cyc2time(&adapter->tc, regval);
542 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
543
544 shhwtstamps = skb_hwtstamps(skb);
545 shhwtstamps->hwtstamp = ns_to_ktime(ns);
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000546
547 /* Update the last_rx_timestamp timer in order to enable watchdog check
548 * for error case of latched timestamp on a dropped packet.
549 */
550 adapter->last_rx_timestamp = jiffies;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000551}
552
Jacob Keller93501d42014-02-28 15:48:58 -0800553int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
554{
555 struct hwtstamp_config *config = &adapter->tstamp_config;
556
557 return copy_to_user(ifr->ifr_data, config,
558 sizeof(*config)) ? -EFAULT : 0;
559}
560
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000561/**
Jacob Kellera7ef4282014-05-16 05:12:25 +0000562 * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
563 * @adapter: the private ixgbe adapter structure
564 * @config: the hwtstamp configuration requested
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000565 *
566 * Outgoing time stamping can be enabled and disabled. Play nice and
Jacob Keller93501d42014-02-28 15:48:58 -0800567 * disable it when requested, although it shouldn't cause any overhead
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000568 * when no packet needs it. At most one packet in the queue may be
569 * marked for time stamping, otherwise it would be impossible to tell
570 * for sure to which packet the hardware time stamp belongs.
571 *
572 * Incoming time stamping has to be configured via the hardware
573 * filters. Not all combinations are supported, in particular event
574 * type has to be specified. Matching the kind of event packet is
575 * not supported, with the exception of "all V2 events regardless of
576 * level 2 or 4".
Jacob Kellerc19197a2012-05-22 06:08:37 +0000577 *
578 * Since hardware always timestamps Path delay packets when timestamping V2
579 * packets, regardless of the type specified in the register, only use V2
580 * Event mode. This more accurately tells the user what the hardware is going
581 * to do anyways.
Jacob Kellera7ef4282014-05-16 05:12:25 +0000582 *
583 * Note: this may modify the hwtstamp configuration towards a more general
584 * mode, if required to support the specifically requested mode.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000585 */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000586static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
587 struct hwtstamp_config *config)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000588{
589 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000590 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
591 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000592 u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000593 bool is_l2 = false;
594 u32 regval;
595
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000596 /* reserved for future extensions */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000597 if (config->flags)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000598 return -EINVAL;
599
Jacob Kellera7ef4282014-05-16 05:12:25 +0000600 switch (config->tx_type) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000601 case HWTSTAMP_TX_OFF:
602 tsync_tx_ctl = 0;
603 case HWTSTAMP_TX_ON:
604 break;
605 default:
606 return -ERANGE;
607 }
608
Jacob Kellera7ef4282014-05-16 05:12:25 +0000609 switch (config->rx_filter) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000610 case HWTSTAMP_FILTER_NONE:
611 tsync_rx_ctl = 0;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000612 tsync_rx_mtrl = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000613 break;
614 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
615 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
Jacob Kellerb1e50f72012-12-05 07:53:38 +0000616 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000617 break;
618 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
619 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
Jacob Kellerb1e50f72012-12-05 07:53:38 +0000620 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000621 break;
Jacob Kellerc19197a2012-05-22 06:08:37 +0000622 case HWTSTAMP_FILTER_PTP_V2_EVENT:
623 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
624 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000625 case HWTSTAMP_FILTER_PTP_V2_SYNC:
626 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
627 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000628 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
629 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
630 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000631 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000632 is_l2 = true;
Jacob Kellera7ef4282014-05-16 05:12:25 +0000633 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000634 break;
635 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
636 case HWTSTAMP_FILTER_ALL:
637 default:
638 /*
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000639 * register RXMTRL must be set in order to do V1 packets,
640 * therefore it is not possible to time stamp both V1 Sync and
641 * Delay_Req messages and hardware does not support
642 * timestamping all packets => return error
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000643 */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000644 config->rx_filter = HWTSTAMP_FILTER_NONE;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000645 return -ERANGE;
646 }
647
648 if (hw->mac.type == ixgbe_mac_82598EB) {
649 if (tsync_rx_ctl | tsync_tx_ctl)
650 return -ERANGE;
651 return 0;
652 }
653
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000654 /* define ethertype filter for timestamping L2 packets */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000655 if (is_l2)
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000656 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000657 (IXGBE_ETQF_FILTER_EN | /* enable filter */
658 IXGBE_ETQF_1588 | /* enable timestamping */
659 ETH_P_1588)); /* 1588 eth protocol type */
660 else
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000661 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000662
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000663 /* enable/disable TX */
664 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
665 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
666 regval |= tsync_tx_ctl;
667 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
668
669 /* enable/disable RX */
670 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
671 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
672 regval |= tsync_rx_ctl;
673 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
674
675 /* define which PTP packets are time stamped */
676 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
677
678 IXGBE_WRITE_FLUSH(hw);
679
680 /* clear TX/RX time stamp registers, just to be sure */
681 regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
682 regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
683
Jacob Kellera7ef4282014-05-16 05:12:25 +0000684 return 0;
685}
686
687/**
688 * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
689 * @adapter: pointer to adapter struct
690 * @ifreq: ioctl data
691 *
692 * Set hardware to requested mode. If unsupported, return an error with no
693 * changes. Otherwise, store the mode for future reference.
694 */
695int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
696{
697 struct hwtstamp_config config;
698 int err;
699
700 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
701 return -EFAULT;
702
703 err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
704 if (err)
705 return err;
706
Jacob Keller93501d42014-02-28 15:48:58 -0800707 /* save these settings for future reference */
708 memcpy(&adapter->tstamp_config, &config,
709 sizeof(adapter->tstamp_config));
710
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000711 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
712 -EFAULT : 0;
713}
714
715/**
716 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000717 * @adapter: pointer to the adapter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000718 *
Jacob Keller1a71ab22012-08-25 03:54:19 +0000719 * This function should be called to set the proper values for the TIMINCA
720 * register and tell the cyclecounter structure what the tick rate of SYSTIME
721 * is. It does not directly modify SYSTIME registers or the timecounter
722 * structure. It should be called whenever a new TIMINCA value is necessary,
723 * such as during initialization or when the link speed changes.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000724 */
725void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
726{
727 struct ixgbe_hw *hw = &adapter->hw;
728 u32 incval = 0;
729 u32 shift = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000730 unsigned long flags;
731
732 /**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000733 * Scale the NIC cycle counter by a large factor so that
734 * relatively small corrections to the frequency can be added
735 * or subtracted. The drawbacks of a large factor include
736 * (a) the clock register overflows more quickly, (b) the cycle
737 * counter structure must be able to convert the systime value
738 * to nanoseconds using only a multiplier and a right-shift,
739 * and (c) the value must fit within the timinca register space
740 * => math based on internal DMA clock rate and available bits
Jacob Keller1a71ab22012-08-25 03:54:19 +0000741 *
742 * Note that when there is no link, internal DMA clock is same as when
743 * link speed is 10Gb. Set the registers correctly even when link is
744 * down to preserve the clock setting
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000745 */
Jacob Keller1a71ab22012-08-25 03:54:19 +0000746 switch (adapter->link_speed) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000747 case IXGBE_LINK_SPEED_100_FULL:
748 incval = IXGBE_INCVAL_100;
749 shift = IXGBE_INCVAL_SHIFT_100;
750 break;
751 case IXGBE_LINK_SPEED_1GB_FULL:
752 incval = IXGBE_INCVAL_1GB;
753 shift = IXGBE_INCVAL_SHIFT_1GB;
754 break;
755 case IXGBE_LINK_SPEED_10GB_FULL:
Jacob Keller1a71ab22012-08-25 03:54:19 +0000756 default:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000757 incval = IXGBE_INCVAL_10GB;
758 shift = IXGBE_INCVAL_SHIFT_10GB;
759 break;
760 }
761
762 /**
763 * Modify the calculated values to fit within the correct
764 * number of bits specified by the hardware. The 82599 doesn't
765 * have the same space as the X540, so bitshift the calculated
766 * values to fit.
767 */
768 switch (hw->mac.type) {
769 case ixgbe_mac_X540:
770 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
771 break;
772 case ixgbe_mac_82599EB:
773 incval >>= IXGBE_INCVAL_SHIFT_82599;
774 shift -= IXGBE_INCVAL_SHIFT_82599;
775 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
776 (1 << IXGBE_INCPER_SHIFT_82599) |
777 incval);
778 break;
779 default:
780 /* other devices aren't supported */
781 return;
782 }
783
Jacob Keller1a71ab22012-08-25 03:54:19 +0000784 /* update the base incval used to calculate frequency adjustment */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000785 ACCESS_ONCE(adapter->base_incval) = incval;
786 smp_mb();
787
Jacob Keller1a71ab22012-08-25 03:54:19 +0000788 /* need lock to prevent incorrect read while modifying cyclecounter */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000789 spin_lock_irqsave(&adapter->tmreg_lock, flags);
790
791 memset(&adapter->cc, 0, sizeof(adapter->cc));
792 adapter->cc.read = ixgbe_ptp_read;
Richard Cochrand312da22015-01-02 20:22:07 +0100793 adapter->cc.mask = CYCLECOUNTER_MASK(64);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000794 adapter->cc.shift = shift;
795 adapter->cc.mult = 1;
796
Jacob Keller1a71ab22012-08-25 03:54:19 +0000797 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
798}
799
800/**
801 * ixgbe_ptp_reset
802 * @adapter: the ixgbe private board structure
803 *
Jacob Kellerd6321402014-05-16 05:12:26 +0000804 * When the MAC resets, all the hardware bits for timesync are reset. This
805 * function is used to re-enable the device for PTP based on current settings.
806 * We do lose the current clock time, so just reset the cyclecounter to the
807 * system real clock time.
808 *
809 * This function will maintain hwtstamp_config settings, and resets the SDP
810 * output if it was enabled.
Jacob Keller1a71ab22012-08-25 03:54:19 +0000811 */
812void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
813{
814 struct ixgbe_hw *hw = &adapter->hw;
815 unsigned long flags;
816
817 /* set SYSTIME registers to 0 just in case */
818 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
819 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
820 IXGBE_WRITE_FLUSH(hw);
821
Jacob Kellerd6321402014-05-16 05:12:26 +0000822 /* reset the hardware timestamping mode */
823 ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
Jacob Keller93501d42014-02-28 15:48:58 -0800824
Jacob Keller1a71ab22012-08-25 03:54:19 +0000825 ixgbe_ptp_start_cyclecounter(adapter);
826
827 spin_lock_irqsave(&adapter->tmreg_lock, flags);
828
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000829 /* reset the ns time counter */
830 timecounter_init(&adapter->tc, &adapter->cc,
831 ktime_to_ns(ktime_get_real()));
832
833 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
Jacob Keller82083672012-08-01 07:12:25 +0000834
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000835 /*
836 * Now that the shift has been calculated and the systime
Jacob Keller82083672012-08-01 07:12:25 +0000837 * registers reset, (re-)enable the Clock out feature
838 */
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000839 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000840}
841
842/**
Jacob Keller63328ad2014-05-16 05:12:27 +0000843 * ixgbe_ptp_create_clock
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000844 * @adapter: the ixgbe private adapter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000845 *
Jacob Keller63328ad2014-05-16 05:12:27 +0000846 * This function performs setup of the user entry point function table and
847 * initializes the PTP clock device, which is used to access the clock-like
848 * features of the PTP core. It will be called by ixgbe_ptp_init, only if
849 * there isn't already a clock device (such as after a suspend/resume cycle,
850 * where the clock device wasn't destroyed).
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000851 */
Jacob Keller63328ad2014-05-16 05:12:27 +0000852static int ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000853{
854 struct net_device *netdev = adapter->netdev;
Jacob Keller63328ad2014-05-16 05:12:27 +0000855 long err;
856
857 /* do nothing if we already have a clock device */
858 if (!IS_ERR_OR_NULL(adapter->ptp_clock))
859 return 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000860
861 switch (adapter->hw.mac.type) {
862 case ixgbe_mac_X540:
Jacob Kellerca324092014-02-25 17:58:54 -0800863 snprintf(adapter->ptp_caps.name,
864 sizeof(adapter->ptp_caps.name),
865 "%s", netdev->name);
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000866 adapter->ptp_caps.owner = THIS_MODULE;
867 adapter->ptp_caps.max_adj = 250000000;
868 adapter->ptp_caps.n_alarm = 0;
869 adapter->ptp_caps.n_ext_ts = 0;
870 adapter->ptp_caps.n_per_out = 0;
871 adapter->ptp_caps.pps = 1;
872 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
873 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
Richard Cochran91432d12015-03-29 23:12:04 +0200874 adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
875 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
Jacob Keller04c8de82014-05-16 05:12:24 +0000876 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000877 break;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000878 case ixgbe_mac_82599EB:
Jacob Kellerca324092014-02-25 17:58:54 -0800879 snprintf(adapter->ptp_caps.name,
880 sizeof(adapter->ptp_caps.name),
881 "%s", netdev->name);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000882 adapter->ptp_caps.owner = THIS_MODULE;
883 adapter->ptp_caps.max_adj = 250000000;
884 adapter->ptp_caps.n_alarm = 0;
885 adapter->ptp_caps.n_ext_ts = 0;
886 adapter->ptp_caps.n_per_out = 0;
887 adapter->ptp_caps.pps = 0;
888 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
889 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
Richard Cochran91432d12015-03-29 23:12:04 +0200890 adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
891 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
Jacob Keller04c8de82014-05-16 05:12:24 +0000892 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000893 break;
894 default:
895 adapter->ptp_clock = NULL;
Jacob Keller63328ad2014-05-16 05:12:27 +0000896 return -EOPNOTSUPP;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000897 }
898
Richard Cochran1ef76152012-09-22 07:02:03 +0000899 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
900 &adapter->pdev->dev);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000901 if (IS_ERR(adapter->ptp_clock)) {
Jacob Keller63328ad2014-05-16 05:12:27 +0000902 err = PTR_ERR(adapter->ptp_clock);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000903 adapter->ptp_clock = NULL;
904 e_dev_err("ptp_clock_register failed\n");
Jacob Keller63328ad2014-05-16 05:12:27 +0000905 return err;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000906 } else
907 e_dev_info("registered PHC device on %s\n", netdev->name);
908
Jacob Keller63328ad2014-05-16 05:12:27 +0000909 /* set default timestamp mode to disabled here. We do this in
910 * create_clock instead of init, because we don't want to override the
911 * previous settings during a resume cycle.
912 */
Jacob Kellerd6321402014-05-16 05:12:26 +0000913 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
914 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
Jacob Keller63328ad2014-05-16 05:12:27 +0000915
916 return 0;
917}
918
919/**
920 * ixgbe_ptp_init
921 * @adapter: the ixgbe private adapter structure
922 *
923 * This function performs the required steps for enabling PTP
924 * support. If PTP support has already been loaded it simply calls the
925 * cyclecounter init routine and exits.
926 */
927void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
928{
929 /* initialize the spin lock first since we can't control when a user
930 * will call the entry functions once we have initialized the clock
931 * device
932 */
933 spin_lock_init(&adapter->tmreg_lock);
934
935 /* obtain a PTP device, or re-use an existing device */
936 if (ixgbe_ptp_create_clock(adapter))
937 return;
938
939 /* we have a clock so we can initialize work now */
940 INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
941
942 /* reset the PTP related hardware bits */
Jacob Keller1a71ab22012-08-25 03:54:19 +0000943 ixgbe_ptp_reset(adapter);
944
Jacob Keller8fecf672013-06-21 08:14:32 +0000945 /* enter the IXGBE_PTP_RUNNING state */
946 set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
Jacob Keller1a71ab22012-08-25 03:54:19 +0000947
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000948 return;
949}
950
951/**
Jacob Keller9966d1e2014-05-16 05:12:28 +0000952 * ixgbe_ptp_suspend - stop PTP work items
953 * @ adapter: pointer to adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000954 *
Jacob Keller9966d1e2014-05-16 05:12:28 +0000955 * this function suspends PTP activity, and prevents more PTP work from being
956 * generated, but does not destroy the PTP clock device.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000957 */
Jacob Keller9966d1e2014-05-16 05:12:28 +0000958void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000959{
Jacob Keller8fecf672013-06-21 08:14:32 +0000960 /* Leave the IXGBE_PTP_RUNNING state. */
961 if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
962 return;
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000963
Jacob Keller9966d1e2014-05-16 05:12:28 +0000964 /* since this might be called in suspend, we don't clear the state,
965 * but simply reset the auxiliary PPS signal control register
966 */
967 IXGBE_WRITE_REG(&adapter->hw, IXGBE_TSAUXC, 0x0);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000968
Jacob Keller9966d1e2014-05-16 05:12:28 +0000969 /* ensure that we cancel any pending PTP Tx work item in progress */
Jacob Keller891dc082012-12-05 07:24:46 +0000970 cancel_work_sync(&adapter->ptp_tx_work);
971 if (adapter->ptp_tx_skb) {
972 dev_kfree_skb_any(adapter->ptp_tx_skb);
973 adapter->ptp_tx_skb = NULL;
Jakub Kicinski151b260c2014-03-15 14:55:21 +0000974 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
Jacob Keller891dc082012-12-05 07:24:46 +0000975 }
Jacob Keller9966d1e2014-05-16 05:12:28 +0000976}
Jacob Keller891dc082012-12-05 07:24:46 +0000977
Jacob Keller9966d1e2014-05-16 05:12:28 +0000978/**
979 * ixgbe_ptp_stop - close the PTP device
980 * @adapter: pointer to adapter struct
981 *
982 * completely destroy the PTP device, should only be called when the device is
983 * being fully closed.
984 */
985void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
986{
987 /* first, suspend PTP activity */
988 ixgbe_ptp_suspend(adapter);
989
990 /* disable the PTP clock device */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000991 if (adapter->ptp_clock) {
992 ptp_clock_unregister(adapter->ptp_clock);
993 adapter->ptp_clock = NULL;
994 e_dev_info("removed PHC on %s\n",
995 adapter->netdev->name);
996 }
997}