blob: bd46f5d1c943b0aa674bd619b5df67dc9aa58d33 [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 */
282static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
283{
284 struct ixgbe_adapter *adapter =
285 container_of(ptp, struct ixgbe_adapter, ptp_caps);
286 u64 ns;
287 u32 remainder;
288 unsigned long flags;
289
290 spin_lock_irqsave(&adapter->tmreg_lock, flags);
291 ns = timecounter_read(&adapter->tc);
292 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
293
294 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
295 ts->tv_nsec = remainder;
296
297 return 0;
298}
299
300/**
301 * ixgbe_ptp_settime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000302 * @ptp: the ptp clock structure
303 * @ts: the timespec containing the new time for the cycle counter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000304 *
305 * reset the timecounter to use a new base value instead of the kernel
306 * wall timer value.
307 */
308static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
309 const struct timespec *ts)
310{
311 struct ixgbe_adapter *adapter =
312 container_of(ptp, struct ixgbe_adapter, ptp_caps);
313 u64 ns;
314 unsigned long flags;
315
316 ns = ts->tv_sec * 1000000000ULL;
317 ns += ts->tv_nsec;
318
319 /* reset the timecounter */
320 spin_lock_irqsave(&adapter->tmreg_lock, flags);
321 timecounter_init(&adapter->tc, &adapter->cc, ns);
322 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
323
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000324 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000325 return 0;
326}
327
328/**
Jacob Keller04c8de82014-05-16 05:12:24 +0000329 * ixgbe_ptp_feature_enable
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000330 * @ptp: the ptp clock structure
331 * @rq: the requested feature to change
332 * @on: whether to enable or disable the feature
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000333 *
334 * enable (or disable) ancillary features of the phc subsystem.
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000335 * our driver only supports the PPS feature on the X540
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000336 */
Jacob Keller04c8de82014-05-16 05:12:24 +0000337static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
338 struct ptp_clock_request *rq, int on)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000339{
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000340 struct ixgbe_adapter *adapter =
341 container_of(ptp, struct ixgbe_adapter, ptp_caps);
342
343 /**
344 * When PPS is enabled, unmask the interrupt for the ClockOut
345 * feature, so that the interrupt handler can send the PPS
346 * event when the clock SDP triggers. Clear mask when PPS is
347 * disabled
348 */
349 if (rq->type == PTP_CLK_REQ_PPS) {
350 switch (adapter->hw.mac.type) {
351 case ixgbe_mac_X540:
352 if (on)
353 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
354 else
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000355 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
356
357 ixgbe_ptp_setup_sdp(adapter);
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000358 return 0;
359 default:
360 break;
361 }
362 }
363
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000364 return -ENOTSUPP;
365}
366
367/**
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000368 * ixgbe_ptp_check_pps_event
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000369 * @adapter: the private adapter structure
370 * @eicr: the interrupt cause register value
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000371 *
372 * This function is called by the interrupt routine when checking for
373 * interrupts. It will check and handle a pps event.
374 */
375void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
376{
377 struct ixgbe_hw *hw = &adapter->hw;
378 struct ptp_clock_event event;
379
Jacob Keller3645adb2012-10-13 05:00:06 +0000380 event.type = PTP_CLOCK_PPS;
381
382 /* this check is necessary in case the interrupt was enabled via some
383 * alternative means (ex. debug_fs). Better to check here than
384 * everywhere that calls this function.
385 */
386 if (!adapter->ptp_clock)
387 return;
388
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000389 switch (hw->mac.type) {
390 case ixgbe_mac_X540:
391 ptp_clock_event(adapter->ptp_clock, &event);
392 break;
393 default:
394 break;
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000395 }
396}
397
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000398/**
Jacob Kellerf2f333872012-12-05 07:24:35 +0000399 * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
400 * @adapter: private adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000401 *
Jacob Kellerf2f333872012-12-05 07:24:35 +0000402 * this watchdog task periodically reads the timecounter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000403 * in order to prevent missing when the system time registers wrap
Jacob Kellerf2f333872012-12-05 07:24:35 +0000404 * around. This needs to be run approximately twice a minute.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000405 */
406void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
407{
Jacob Kellerf2f333872012-12-05 07:24:35 +0000408 bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
409 IXGBE_OVERFLOW_PERIOD);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000410 struct timespec ts;
411
Jacob Keller891dc082012-12-05 07:24:46 +0000412 if (timeout) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000413 ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
414 adapter->last_overflow_check = jiffies;
415 }
416}
417
418/**
Jacob Keller6cb562d2012-12-05 07:24:41 +0000419 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
420 * @adapter: private network adapter structure
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000421 *
Jacob Keller6cb562d2012-12-05 07:24:41 +0000422 * this watchdog task is scheduled to detect error case where hardware has
423 * dropped an Rx packet that was timestamped when the ring is full. The
424 * particular error is rare but leaves the device in a state unable to timestamp
425 * any future packets.
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000426 */
Jacob Keller6cb562d2012-12-05 07:24:41 +0000427void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000428{
Jacob Keller6cb562d2012-12-05 07:24:41 +0000429 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller6cb562d2012-12-05 07:24:41 +0000430 u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
431 unsigned long rx_event;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000432
Jacob Keller6cb562d2012-12-05 07:24:41 +0000433 /* if we don't have a valid timestamp in the registers, just update the
434 * timeout counter and exit
435 */
436 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
437 adapter->last_rx_ptp_check = jiffies;
438 return;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000439 }
440
Jacob Keller6cb562d2012-12-05 07:24:41 +0000441 /* determine the most recent watchdog or rx_timestamp event */
442 rx_event = adapter->last_rx_ptp_check;
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000443 if (time_after(adapter->last_rx_timestamp, rx_event))
444 rx_event = adapter->last_rx_timestamp;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000445
Jacob Keller6cb562d2012-12-05 07:24:41 +0000446 /* only need to read the high RXSTMP register to clear the lock */
447 if (time_is_before_jiffies(rx_event + 5*HZ)) {
448 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
449 adapter->last_rx_ptp_check = jiffies;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000450
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000451 e_warn(drv, "clearing RX Timestamp hang\n");
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000452 }
453}
454
455/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000456 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
Jacob Keller891dc082012-12-05 07:24:46 +0000457 * @adapter: the private adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000458 *
459 * if the timestamp is valid, we convert it into the timecounter ns
460 * value, then store that result into the shhwtstamps structure which
461 * is passed up the network stack
462 */
Jacob Keller891dc082012-12-05 07:24:46 +0000463static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000464{
Jacob Keller891dc082012-12-05 07:24:46 +0000465 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000466 struct skb_shared_hwtstamps shhwtstamps;
467 u64 regval = 0, ns;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000468 unsigned long flags;
469
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000470 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
471 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
472
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000473 spin_lock_irqsave(&adapter->tmreg_lock, flags);
474 ns = timecounter_cyc2time(&adapter->tc, regval);
475 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
476
477 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
478 shhwtstamps.hwtstamp = ns_to_ktime(ns);
Jacob Keller891dc082012-12-05 07:24:46 +0000479 skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
480
481 dev_kfree_skb_any(adapter->ptp_tx_skb);
482 adapter->ptp_tx_skb = NULL;
Jakub Kicinski151b260c2014-03-15 14:55:21 +0000483 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
Jacob Keller891dc082012-12-05 07:24:46 +0000484}
485
486/**
487 * ixgbe_ptp_tx_hwtstamp_work
488 * @work: pointer to the work struct
489 *
490 * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
Joe Perchesdbedd442015-03-06 20:49:12 -0800491 * timestamp has been taken for the current skb. It is necessary, because the
Jacob Keller891dc082012-12-05 07:24:46 +0000492 * descriptor's "done" bit does not correlate with the timestamp event.
493 */
494static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
495{
496 struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
497 ptp_tx_work);
498 struct ixgbe_hw *hw = &adapter->hw;
499 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
500 IXGBE_PTP_TX_TIMEOUT);
501 u32 tsynctxctl;
502
Jacob Keller891dc082012-12-05 07:24:46 +0000503 if (timeout) {
504 dev_kfree_skb_any(adapter->ptp_tx_skb);
505 adapter->ptp_tx_skb = NULL;
Jakub Kicinski151b260c2014-03-15 14:55:21 +0000506 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +0000507 e_warn(drv, "clearing Tx Timestamp hang\n");
Jacob Keller891dc082012-12-05 07:24:46 +0000508 return;
509 }
510
511 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
512 if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID)
513 ixgbe_ptp_tx_hwtstamp(adapter);
514 else
515 /* reschedule to keep checking if it's not available yet */
516 schedule_work(&adapter->ptp_tx_work);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000517}
518
519/**
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000520 * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
521 * @adapter: pointer to adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000522 * @skb: particular skb to send timestamp with
523 *
524 * if the timestamp is valid, we convert it into the timecounter ns
525 * value, then store that result into the shhwtstamps structure which
526 * is passed up the network stack
527 */
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000528void ixgbe_ptp_rx_hwtstamp(struct ixgbe_adapter *adapter, struct sk_buff *skb)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000529{
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000530 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000531 struct skb_shared_hwtstamps *shhwtstamps;
532 u64 regval = 0, ns;
533 u32 tsyncrxctl;
534 unsigned long flags;
535
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000536 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
Jiri Bencf42df162012-10-25 18:12:05 +0000537 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000538 return;
539
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000540 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
541 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
542
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000543 spin_lock_irqsave(&adapter->tmreg_lock, flags);
544 ns = timecounter_cyc2time(&adapter->tc, regval);
545 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
546
547 shhwtstamps = skb_hwtstamps(skb);
548 shhwtstamps->hwtstamp = ns_to_ktime(ns);
Jakub Kicinskieda183c2014-04-02 10:33:28 +0000549
550 /* Update the last_rx_timestamp timer in order to enable watchdog check
551 * for error case of latched timestamp on a dropped packet.
552 */
553 adapter->last_rx_timestamp = jiffies;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000554}
555
Jacob Keller93501d42014-02-28 15:48:58 -0800556int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
557{
558 struct hwtstamp_config *config = &adapter->tstamp_config;
559
560 return copy_to_user(ifr->ifr_data, config,
561 sizeof(*config)) ? -EFAULT : 0;
562}
563
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000564/**
Jacob Kellera7ef4282014-05-16 05:12:25 +0000565 * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
566 * @adapter: the private ixgbe adapter structure
567 * @config: the hwtstamp configuration requested
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000568 *
569 * Outgoing time stamping can be enabled and disabled. Play nice and
Jacob Keller93501d42014-02-28 15:48:58 -0800570 * disable it when requested, although it shouldn't cause any overhead
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000571 * when no packet needs it. At most one packet in the queue may be
572 * marked for time stamping, otherwise it would be impossible to tell
573 * for sure to which packet the hardware time stamp belongs.
574 *
575 * Incoming time stamping has to be configured via the hardware
576 * filters. Not all combinations are supported, in particular event
577 * type has to be specified. Matching the kind of event packet is
578 * not supported, with the exception of "all V2 events regardless of
579 * level 2 or 4".
Jacob Kellerc19197a2012-05-22 06:08:37 +0000580 *
581 * Since hardware always timestamps Path delay packets when timestamping V2
582 * packets, regardless of the type specified in the register, only use V2
583 * Event mode. This more accurately tells the user what the hardware is going
584 * to do anyways.
Jacob Kellera7ef4282014-05-16 05:12:25 +0000585 *
586 * Note: this may modify the hwtstamp configuration towards a more general
587 * mode, if required to support the specifically requested mode.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000588 */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000589static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
590 struct hwtstamp_config *config)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000591{
592 struct ixgbe_hw *hw = &adapter->hw;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000593 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
594 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000595 u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000596 bool is_l2 = false;
597 u32 regval;
598
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000599 /* reserved for future extensions */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000600 if (config->flags)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000601 return -EINVAL;
602
Jacob Kellera7ef4282014-05-16 05:12:25 +0000603 switch (config->tx_type) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000604 case HWTSTAMP_TX_OFF:
605 tsync_tx_ctl = 0;
606 case HWTSTAMP_TX_ON:
607 break;
608 default:
609 return -ERANGE;
610 }
611
Jacob Kellera7ef4282014-05-16 05:12:25 +0000612 switch (config->rx_filter) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000613 case HWTSTAMP_FILTER_NONE:
614 tsync_rx_ctl = 0;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000615 tsync_rx_mtrl = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000616 break;
617 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
618 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
Jacob Kellerb1e50f72012-12-05 07:53:38 +0000619 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000620 break;
621 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
622 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
Jacob Kellerb1e50f72012-12-05 07:53:38 +0000623 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000624 break;
Jacob Kellerc19197a2012-05-22 06:08:37 +0000625 case HWTSTAMP_FILTER_PTP_V2_EVENT:
626 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
627 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000628 case HWTSTAMP_FILTER_PTP_V2_SYNC:
629 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
630 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000631 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
632 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
633 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000634 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000635 is_l2 = true;
Jacob Kellera7ef4282014-05-16 05:12:25 +0000636 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000637 break;
638 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
639 case HWTSTAMP_FILTER_ALL:
640 default:
641 /*
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000642 * register RXMTRL must be set in order to do V1 packets,
643 * therefore it is not possible to time stamp both V1 Sync and
644 * Delay_Req messages and hardware does not support
645 * timestamping all packets => return error
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000646 */
Jacob Kellera7ef4282014-05-16 05:12:25 +0000647 config->rx_filter = HWTSTAMP_FILTER_NONE;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000648 return -ERANGE;
649 }
650
651 if (hw->mac.type == ixgbe_mac_82598EB) {
652 if (tsync_rx_ctl | tsync_tx_ctl)
653 return -ERANGE;
654 return 0;
655 }
656
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000657 /* define ethertype filter for timestamping L2 packets */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000658 if (is_l2)
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000659 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000660 (IXGBE_ETQF_FILTER_EN | /* enable filter */
661 IXGBE_ETQF_1588 | /* enable timestamping */
662 ETH_P_1588)); /* 1588 eth protocol type */
663 else
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000664 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000665
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000666 /* enable/disable TX */
667 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
668 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
669 regval |= tsync_tx_ctl;
670 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
671
672 /* enable/disable RX */
673 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
674 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
675 regval |= tsync_rx_ctl;
676 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
677
678 /* define which PTP packets are time stamped */
679 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
680
681 IXGBE_WRITE_FLUSH(hw);
682
683 /* clear TX/RX time stamp registers, just to be sure */
684 regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
685 regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
686
Jacob Kellera7ef4282014-05-16 05:12:25 +0000687 return 0;
688}
689
690/**
691 * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
692 * @adapter: pointer to adapter struct
693 * @ifreq: ioctl data
694 *
695 * Set hardware to requested mode. If unsupported, return an error with no
696 * changes. Otherwise, store the mode for future reference.
697 */
698int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
699{
700 struct hwtstamp_config config;
701 int err;
702
703 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
704 return -EFAULT;
705
706 err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
707 if (err)
708 return err;
709
Jacob Keller93501d42014-02-28 15:48:58 -0800710 /* save these settings for future reference */
711 memcpy(&adapter->tstamp_config, &config,
712 sizeof(adapter->tstamp_config));
713
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000714 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
715 -EFAULT : 0;
716}
717
718/**
719 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000720 * @adapter: pointer to the adapter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000721 *
Jacob Keller1a71ab22012-08-25 03:54:19 +0000722 * This function should be called to set the proper values for the TIMINCA
723 * register and tell the cyclecounter structure what the tick rate of SYSTIME
724 * is. It does not directly modify SYSTIME registers or the timecounter
725 * structure. It should be called whenever a new TIMINCA value is necessary,
726 * such as during initialization or when the link speed changes.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000727 */
728void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
729{
730 struct ixgbe_hw *hw = &adapter->hw;
731 u32 incval = 0;
732 u32 shift = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000733 unsigned long flags;
734
735 /**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000736 * Scale the NIC cycle counter by a large factor so that
737 * relatively small corrections to the frequency can be added
738 * or subtracted. The drawbacks of a large factor include
739 * (a) the clock register overflows more quickly, (b) the cycle
740 * counter structure must be able to convert the systime value
741 * to nanoseconds using only a multiplier and a right-shift,
742 * and (c) the value must fit within the timinca register space
743 * => math based on internal DMA clock rate and available bits
Jacob Keller1a71ab22012-08-25 03:54:19 +0000744 *
745 * Note that when there is no link, internal DMA clock is same as when
746 * link speed is 10Gb. Set the registers correctly even when link is
747 * down to preserve the clock setting
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000748 */
Jacob Keller1a71ab22012-08-25 03:54:19 +0000749 switch (adapter->link_speed) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000750 case IXGBE_LINK_SPEED_100_FULL:
751 incval = IXGBE_INCVAL_100;
752 shift = IXGBE_INCVAL_SHIFT_100;
753 break;
754 case IXGBE_LINK_SPEED_1GB_FULL:
755 incval = IXGBE_INCVAL_1GB;
756 shift = IXGBE_INCVAL_SHIFT_1GB;
757 break;
758 case IXGBE_LINK_SPEED_10GB_FULL:
Jacob Keller1a71ab22012-08-25 03:54:19 +0000759 default:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000760 incval = IXGBE_INCVAL_10GB;
761 shift = IXGBE_INCVAL_SHIFT_10GB;
762 break;
763 }
764
765 /**
766 * Modify the calculated values to fit within the correct
767 * number of bits specified by the hardware. The 82599 doesn't
768 * have the same space as the X540, so bitshift the calculated
769 * values to fit.
770 */
771 switch (hw->mac.type) {
772 case ixgbe_mac_X540:
773 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
774 break;
775 case ixgbe_mac_82599EB:
776 incval >>= IXGBE_INCVAL_SHIFT_82599;
777 shift -= IXGBE_INCVAL_SHIFT_82599;
778 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
779 (1 << IXGBE_INCPER_SHIFT_82599) |
780 incval);
781 break;
782 default:
783 /* other devices aren't supported */
784 return;
785 }
786
Jacob Keller1a71ab22012-08-25 03:54:19 +0000787 /* update the base incval used to calculate frequency adjustment */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000788 ACCESS_ONCE(adapter->base_incval) = incval;
789 smp_mb();
790
Jacob Keller1a71ab22012-08-25 03:54:19 +0000791 /* need lock to prevent incorrect read while modifying cyclecounter */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000792 spin_lock_irqsave(&adapter->tmreg_lock, flags);
793
794 memset(&adapter->cc, 0, sizeof(adapter->cc));
795 adapter->cc.read = ixgbe_ptp_read;
Richard Cochrand312da22015-01-02 20:22:07 +0100796 adapter->cc.mask = CYCLECOUNTER_MASK(64);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000797 adapter->cc.shift = shift;
798 adapter->cc.mult = 1;
799
Jacob Keller1a71ab22012-08-25 03:54:19 +0000800 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
801}
802
803/**
804 * ixgbe_ptp_reset
805 * @adapter: the ixgbe private board structure
806 *
Jacob Kellerd6321402014-05-16 05:12:26 +0000807 * When the MAC resets, all the hardware bits for timesync are reset. This
808 * function is used to re-enable the device for PTP based on current settings.
809 * We do lose the current clock time, so just reset the cyclecounter to the
810 * system real clock time.
811 *
812 * This function will maintain hwtstamp_config settings, and resets the SDP
813 * output if it was enabled.
Jacob Keller1a71ab22012-08-25 03:54:19 +0000814 */
815void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
816{
817 struct ixgbe_hw *hw = &adapter->hw;
818 unsigned long flags;
819
820 /* set SYSTIME registers to 0 just in case */
821 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
822 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
823 IXGBE_WRITE_FLUSH(hw);
824
Jacob Kellerd6321402014-05-16 05:12:26 +0000825 /* reset the hardware timestamping mode */
826 ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
Jacob Keller93501d42014-02-28 15:48:58 -0800827
Jacob Keller1a71ab22012-08-25 03:54:19 +0000828 ixgbe_ptp_start_cyclecounter(adapter);
829
830 spin_lock_irqsave(&adapter->tmreg_lock, flags);
831
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000832 /* reset the ns time counter */
833 timecounter_init(&adapter->tc, &adapter->cc,
834 ktime_to_ns(ktime_get_real()));
835
836 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
Jacob Keller82083672012-08-01 07:12:25 +0000837
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000838 /*
839 * Now that the shift has been calculated and the systime
Jacob Keller82083672012-08-01 07:12:25 +0000840 * registers reset, (re-)enable the Clock out feature
841 */
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000842 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000843}
844
845/**
Jacob Keller63328ad2014-05-16 05:12:27 +0000846 * ixgbe_ptp_create_clock
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000847 * @adapter: the ixgbe private adapter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000848 *
Jacob Keller63328ad2014-05-16 05:12:27 +0000849 * This function performs setup of the user entry point function table and
850 * initializes the PTP clock device, which is used to access the clock-like
851 * features of the PTP core. It will be called by ixgbe_ptp_init, only if
852 * there isn't already a clock device (such as after a suspend/resume cycle,
853 * where the clock device wasn't destroyed).
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000854 */
Jacob Keller63328ad2014-05-16 05:12:27 +0000855static int ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000856{
857 struct net_device *netdev = adapter->netdev;
Jacob Keller63328ad2014-05-16 05:12:27 +0000858 long err;
859
860 /* do nothing if we already have a clock device */
861 if (!IS_ERR_OR_NULL(adapter->ptp_clock))
862 return 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000863
864 switch (adapter->hw.mac.type) {
865 case ixgbe_mac_X540:
Jacob Kellerca324092014-02-25 17:58:54 -0800866 snprintf(adapter->ptp_caps.name,
867 sizeof(adapter->ptp_caps.name),
868 "%s", netdev->name);
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000869 adapter->ptp_caps.owner = THIS_MODULE;
870 adapter->ptp_caps.max_adj = 250000000;
871 adapter->ptp_caps.n_alarm = 0;
872 adapter->ptp_caps.n_ext_ts = 0;
873 adapter->ptp_caps.n_per_out = 0;
874 adapter->ptp_caps.pps = 1;
875 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
876 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
877 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
878 adapter->ptp_caps.settime = ixgbe_ptp_settime;
Jacob Keller04c8de82014-05-16 05:12:24 +0000879 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000880 break;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000881 case ixgbe_mac_82599EB:
Jacob Kellerca324092014-02-25 17:58:54 -0800882 snprintf(adapter->ptp_caps.name,
883 sizeof(adapter->ptp_caps.name),
884 "%s", netdev->name);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000885 adapter->ptp_caps.owner = THIS_MODULE;
886 adapter->ptp_caps.max_adj = 250000000;
887 adapter->ptp_caps.n_alarm = 0;
888 adapter->ptp_caps.n_ext_ts = 0;
889 adapter->ptp_caps.n_per_out = 0;
890 adapter->ptp_caps.pps = 0;
891 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
892 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
893 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
894 adapter->ptp_caps.settime = ixgbe_ptp_settime;
Jacob Keller04c8de82014-05-16 05:12:24 +0000895 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000896 break;
897 default:
898 adapter->ptp_clock = NULL;
Jacob Keller63328ad2014-05-16 05:12:27 +0000899 return -EOPNOTSUPP;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000900 }
901
Richard Cochran1ef76152012-09-22 07:02:03 +0000902 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
903 &adapter->pdev->dev);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000904 if (IS_ERR(adapter->ptp_clock)) {
Jacob Keller63328ad2014-05-16 05:12:27 +0000905 err = PTR_ERR(adapter->ptp_clock);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000906 adapter->ptp_clock = NULL;
907 e_dev_err("ptp_clock_register failed\n");
Jacob Keller63328ad2014-05-16 05:12:27 +0000908 return err;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000909 } else
910 e_dev_info("registered PHC device on %s\n", netdev->name);
911
Jacob Keller63328ad2014-05-16 05:12:27 +0000912 /* set default timestamp mode to disabled here. We do this in
913 * create_clock instead of init, because we don't want to override the
914 * previous settings during a resume cycle.
915 */
Jacob Kellerd6321402014-05-16 05:12:26 +0000916 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
917 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
Jacob Keller63328ad2014-05-16 05:12:27 +0000918
919 return 0;
920}
921
922/**
923 * ixgbe_ptp_init
924 * @adapter: the ixgbe private adapter structure
925 *
926 * This function performs the required steps for enabling PTP
927 * support. If PTP support has already been loaded it simply calls the
928 * cyclecounter init routine and exits.
929 */
930void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
931{
932 /* initialize the spin lock first since we can't control when a user
933 * will call the entry functions once we have initialized the clock
934 * device
935 */
936 spin_lock_init(&adapter->tmreg_lock);
937
938 /* obtain a PTP device, or re-use an existing device */
939 if (ixgbe_ptp_create_clock(adapter))
940 return;
941
942 /* we have a clock so we can initialize work now */
943 INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
944
945 /* reset the PTP related hardware bits */
Jacob Keller1a71ab22012-08-25 03:54:19 +0000946 ixgbe_ptp_reset(adapter);
947
Jacob Keller8fecf672013-06-21 08:14:32 +0000948 /* enter the IXGBE_PTP_RUNNING state */
949 set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
Jacob Keller1a71ab22012-08-25 03:54:19 +0000950
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000951 return;
952}
953
954/**
Jacob Keller9966d1e2014-05-16 05:12:28 +0000955 * ixgbe_ptp_suspend - stop PTP work items
956 * @ adapter: pointer to adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000957 *
Jacob Keller9966d1e2014-05-16 05:12:28 +0000958 * this function suspends PTP activity, and prevents more PTP work from being
959 * generated, but does not destroy the PTP clock device.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000960 */
Jacob Keller9966d1e2014-05-16 05:12:28 +0000961void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000962{
Jacob Keller8fecf672013-06-21 08:14:32 +0000963 /* Leave the IXGBE_PTP_RUNNING state. */
964 if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
965 return;
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000966
Jacob Keller9966d1e2014-05-16 05:12:28 +0000967 /* since this might be called in suspend, we don't clear the state,
968 * but simply reset the auxiliary PPS signal control register
969 */
970 IXGBE_WRITE_REG(&adapter->hw, IXGBE_TSAUXC, 0x0);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000971
Jacob Keller9966d1e2014-05-16 05:12:28 +0000972 /* ensure that we cancel any pending PTP Tx work item in progress */
Jacob Keller891dc082012-12-05 07:24:46 +0000973 cancel_work_sync(&adapter->ptp_tx_work);
974 if (adapter->ptp_tx_skb) {
975 dev_kfree_skb_any(adapter->ptp_tx_skb);
976 adapter->ptp_tx_skb = NULL;
Jakub Kicinski151b260c2014-03-15 14:55:21 +0000977 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
Jacob Keller891dc082012-12-05 07:24:46 +0000978 }
Jacob Keller9966d1e2014-05-16 05:12:28 +0000979}
Jacob Keller891dc082012-12-05 07:24:46 +0000980
Jacob Keller9966d1e2014-05-16 05:12:28 +0000981/**
982 * ixgbe_ptp_stop - close the PTP device
983 * @adapter: pointer to adapter struct
984 *
985 * completely destroy the PTP device, should only be called when the device is
986 * being fully closed.
987 */
988void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
989{
990 /* first, suspend PTP activity */
991 ixgbe_ptp_suspend(adapter);
992
993 /* disable the PTP clock device */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000994 if (adapter->ptp_clock) {
995 ptp_clock_unregister(adapter->ptp_clock);
996 adapter->ptp_clock = NULL;
997 e_dev_info("removed PHC on %s\n",
998 adapter->netdev->name);
999 }
1000}