blob: dcc67e2f8db437ce4a7d8ea9c13d3380534953f2 [file] [log] [blame]
Jacob Keller3a6a4ed2012-05-01 05:24:58 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2012 Intel Corporation.
5
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:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27#include "ixgbe.h"
28#include <linux/export.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)
99
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000100#ifndef NSECS_PER_SEC
101#define NSECS_PER_SEC 1000000000ULL
102#endif
103
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000104/**
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000105 * ixgbe_ptp_setup_sdp
Jacob Keller82083672012-08-01 07:12:25 +0000106 * @hw: the hardware private structure
Jacob Keller82083672012-08-01 07:12:25 +0000107 *
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000108 * this function enables or disables the clock out feature on SDP0 for
109 * the X540 device. It will create a 1second periodic output that can
110 * be used as the PPS (via an interrupt).
Jacob Keller82083672012-08-01 07:12:25 +0000111 *
112 * It calculates when the systime will be on an exact second, and then
113 * aligns the start of the PPS signal to that value. The shift is
114 * necessary because it can change based on the link speed.
115 */
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000116static void ixgbe_ptp_setup_sdp(struct ixgbe_adapter *adapter)
Jacob Keller82083672012-08-01 07:12:25 +0000117{
118 struct ixgbe_hw *hw = &adapter->hw;
119 int shift = adapter->cc.shift;
120 u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
121 u64 ns = 0, clock_edge = 0;
122
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000123 if ((adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED) &&
124 (hw->mac.type == ixgbe_mac_X540)) {
125
126 /* disable the pin first */
127 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
128 IXGBE_WRITE_FLUSH(hw);
129
Jacob Keller82083672012-08-01 07:12:25 +0000130 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
131
132 /*
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000133 * enable the SDP0 pin as output, and connected to the
134 * native function for Timesync (ClockOut)
Jacob Keller82083672012-08-01 07:12:25 +0000135 */
136 esdp |= (IXGBE_ESDP_SDP0_DIR |
137 IXGBE_ESDP_SDP0_NATIVE);
138
139 /*
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000140 * enable the Clock Out feature on SDP0, and allow
141 * interrupts to occur when the pin changes
Jacob Keller82083672012-08-01 07:12:25 +0000142 */
143 tsauxc = (IXGBE_TSAUXC_EN_CLK |
144 IXGBE_TSAUXC_SYNCLK |
145 IXGBE_TSAUXC_SDP0_INT);
146
147 /* clock period (or pulse length) */
148 clktiml = (u32)(NSECS_PER_SEC << shift);
149 clktimh = (u32)((NSECS_PER_SEC << shift) >> 32);
150
151 /*
152 * Account for the cyclecounter wrap-around value by
153 * using the converted ns value of the current time to
154 * check for when the next aligned second would occur.
155 */
156 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
157 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
158 ns = timecounter_cyc2time(&adapter->tc, clock_edge);
159
160 div_u64_rem(ns, NSECS_PER_SEC, &rem);
161 clock_edge += ((NSECS_PER_SEC - (u64)rem) << shift);
162
163 /* specify the initial clock start time */
164 trgttiml = (u32)clock_edge;
165 trgttimh = (u32)(clock_edge >> 32);
166
167 IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
168 IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
169 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
170 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
171
172 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
173 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000174 } else {
175 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
Jacob Keller82083672012-08-01 07:12:25 +0000176 }
Jacob Keller82083672012-08-01 07:12:25 +0000177
Jacob Keller82083672012-08-01 07:12:25 +0000178 IXGBE_WRITE_FLUSH(hw);
179}
180
181/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000182 * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000183 * @cc: the cyclecounter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000184 *
185 * this function reads the cyclecounter registers and is called by the
186 * cyclecounter structure used to construct a ns counter from the
187 * arbitrary fixed point registers
188 */
189static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
190{
191 struct ixgbe_adapter *adapter =
192 container_of(cc, struct ixgbe_adapter, cc);
193 struct ixgbe_hw *hw = &adapter->hw;
194 u64 stamp = 0;
195
196 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
197 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
198
199 return stamp;
200}
201
202/**
203 * ixgbe_ptp_adjfreq
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000204 * @ptp: the ptp clock structure
205 * @ppb: parts per billion adjustment from base
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000206 *
207 * adjust the frequency of the ptp cycle counter by the
208 * indicated ppb from the base frequency.
209 */
210static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
211{
212 struct ixgbe_adapter *adapter =
213 container_of(ptp, struct ixgbe_adapter, ptp_caps);
214 struct ixgbe_hw *hw = &adapter->hw;
215 u64 freq;
216 u32 diff, incval;
217 int neg_adj = 0;
218
219 if (ppb < 0) {
220 neg_adj = 1;
221 ppb = -ppb;
222 }
223
224 smp_mb();
225 incval = ACCESS_ONCE(adapter->base_incval);
226
227 freq = incval;
228 freq *= ppb;
229 diff = div_u64(freq, 1000000000ULL);
230
231 incval = neg_adj ? (incval - diff) : (incval + diff);
232
233 switch (hw->mac.type) {
234 case ixgbe_mac_X540:
235 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
236 break;
237 case ixgbe_mac_82599EB:
238 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
239 (1 << IXGBE_INCPER_SHIFT_82599) |
240 incval);
241 break;
242 default:
243 break;
244 }
245
246 return 0;
247}
248
249/**
250 * ixgbe_ptp_adjtime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000251 * @ptp: the ptp clock structure
252 * @delta: offset to adjust the cycle counter by
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000253 *
254 * adjust the timer by resetting the timecounter structure.
255 */
256static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
257{
258 struct ixgbe_adapter *adapter =
259 container_of(ptp, struct ixgbe_adapter, ptp_caps);
260 unsigned long flags;
261 u64 now;
262
263 spin_lock_irqsave(&adapter->tmreg_lock, flags);
264
265 now = timecounter_read(&adapter->tc);
266 now += delta;
267
268 /* reset the timecounter */
269 timecounter_init(&adapter->tc,
270 &adapter->cc,
271 now);
272
273 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000274
275 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller82083672012-08-01 07:12:25 +0000276
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000277 return 0;
278}
279
280/**
281 * ixgbe_ptp_gettime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000282 * @ptp: the ptp clock structure
283 * @ts: timespec structure to hold the current time value
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000284 *
285 * read the timecounter and return the correct value on ns,
286 * after converting it into a struct timespec.
287 */
288static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
289{
290 struct ixgbe_adapter *adapter =
291 container_of(ptp, struct ixgbe_adapter, ptp_caps);
292 u64 ns;
293 u32 remainder;
294 unsigned long flags;
295
296 spin_lock_irqsave(&adapter->tmreg_lock, flags);
297 ns = timecounter_read(&adapter->tc);
298 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
299
300 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
301 ts->tv_nsec = remainder;
302
303 return 0;
304}
305
306/**
307 * ixgbe_ptp_settime
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000308 * @ptp: the ptp clock structure
309 * @ts: the timespec containing the new time for the cycle counter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000310 *
311 * reset the timecounter to use a new base value instead of the kernel
312 * wall timer value.
313 */
314static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
315 const struct timespec *ts)
316{
317 struct ixgbe_adapter *adapter =
318 container_of(ptp, struct ixgbe_adapter, ptp_caps);
319 u64 ns;
320 unsigned long flags;
321
322 ns = ts->tv_sec * 1000000000ULL;
323 ns += ts->tv_nsec;
324
325 /* reset the timecounter */
326 spin_lock_irqsave(&adapter->tmreg_lock, flags);
327 timecounter_init(&adapter->tc, &adapter->cc, ns);
328 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
329
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000330 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000331 return 0;
332}
333
334/**
335 * ixgbe_ptp_enable
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000336 * @ptp: the ptp clock structure
337 * @rq: the requested feature to change
338 * @on: whether to enable or disable the feature
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000339 *
340 * enable (or disable) ancillary features of the phc subsystem.
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000341 * our driver only supports the PPS feature on the X540
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000342 */
343static int ixgbe_ptp_enable(struct ptp_clock_info *ptp,
344 struct ptp_clock_request *rq, int on)
345{
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000346 struct ixgbe_adapter *adapter =
347 container_of(ptp, struct ixgbe_adapter, ptp_caps);
348
349 /**
350 * When PPS is enabled, unmask the interrupt for the ClockOut
351 * feature, so that the interrupt handler can send the PPS
352 * event when the clock SDP triggers. Clear mask when PPS is
353 * disabled
354 */
355 if (rq->type == PTP_CLK_REQ_PPS) {
356 switch (adapter->hw.mac.type) {
357 case ixgbe_mac_X540:
358 if (on)
359 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
360 else
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000361 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
362
363 ixgbe_ptp_setup_sdp(adapter);
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000364 return 0;
365 default:
366 break;
367 }
368 }
369
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000370 return -ENOTSUPP;
371}
372
373/**
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000374 * ixgbe_ptp_check_pps_event
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000375 * @adapter: the private adapter structure
376 * @eicr: the interrupt cause register value
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000377 *
378 * This function is called by the interrupt routine when checking for
379 * interrupts. It will check and handle a pps event.
380 */
381void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
382{
383 struct ixgbe_hw *hw = &adapter->hw;
384 struct ptp_clock_event event;
385
Jacob Keller3645adb2012-10-13 05:00:06 +0000386 event.type = PTP_CLOCK_PPS;
387
388 /* this check is necessary in case the interrupt was enabled via some
389 * alternative means (ex. debug_fs). Better to check here than
390 * everywhere that calls this function.
391 */
392 if (!adapter->ptp_clock)
393 return;
394
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000395 switch (hw->mac.type) {
396 case ixgbe_mac_X540:
397 ptp_clock_event(adapter->ptp_clock, &event);
398 break;
399 default:
400 break;
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000401 }
402}
403
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000404
405/**
Jacob Kellerf2f333872012-12-05 07:24:35 +0000406 * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
407 * @adapter: private adapter struct
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000408 *
Jacob Kellerf2f333872012-12-05 07:24:35 +0000409 * this watchdog task periodically reads the timecounter
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000410 * in order to prevent missing when the system time registers wrap
Jacob Kellerf2f333872012-12-05 07:24:35 +0000411 * around. This needs to be run approximately twice a minute.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000412 */
413void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
414{
Jacob Kellerf2f333872012-12-05 07:24:35 +0000415 bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
416 IXGBE_OVERFLOW_PERIOD);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000417 struct timespec ts;
418
Jacob Keller1a71ab22012-08-25 03:54:19 +0000419 if ((adapter->flags2 & IXGBE_FLAG2_PTP_ENABLED) &&
Jacob Kellerf2f333872012-12-05 07:24:35 +0000420 (timeout)) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000421 ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
422 adapter->last_overflow_check = jiffies;
423 }
424}
425
426/**
Jacob Keller6cb562d2012-12-05 07:24:41 +0000427 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
428 * @adapter: private network adapter structure
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000429 *
Jacob Keller6cb562d2012-12-05 07:24:41 +0000430 * this watchdog task is scheduled to detect error case where hardware has
431 * dropped an Rx packet that was timestamped when the ring is full. The
432 * particular error is rare but leaves the device in a state unable to timestamp
433 * any future packets.
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000434 */
Jacob Keller6cb562d2012-12-05 07:24:41 +0000435void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000436{
Jacob Keller6cb562d2012-12-05 07:24:41 +0000437 struct ixgbe_hw *hw = &adapter->hw;
438 struct ixgbe_ring *rx_ring;
439 u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
440 unsigned long rx_event;
441 int n;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000442
Jacob Keller6cb562d2012-12-05 07:24:41 +0000443 /* if we don't have a valid timestamp in the registers, just update the
444 * timeout counter and exit
445 */
446 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
447 adapter->last_rx_ptp_check = jiffies;
448 return;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000449 }
450
Jacob Keller6cb562d2012-12-05 07:24:41 +0000451 /* determine the most recent watchdog or rx_timestamp event */
452 rx_event = adapter->last_rx_ptp_check;
453 for (n = 0; n < adapter->num_rx_queues; n++) {
454 rx_ring = adapter->rx_ring[n];
455 if (time_after(rx_ring->last_rx_timestamp, rx_event))
456 rx_event = rx_ring->last_rx_timestamp;
457 }
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000458
Jacob Keller6cb562d2012-12-05 07:24:41 +0000459 /* only need to read the high RXSTMP register to clear the lock */
460 if (time_is_before_jiffies(rx_event + 5*HZ)) {
461 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
462 adapter->last_rx_ptp_check = jiffies;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000463
Jacob Keller6cb562d2012-12-05 07:24:41 +0000464 e_warn(drv, "clearing RX Timestamp hang");
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000465 }
466}
467
468/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000469 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
470 * @q_vector: structure containing interrupt and ring information
471 * @skb: particular skb to send timestamp with
472 *
473 * if the timestamp is valid, we convert it into the timecounter ns
474 * value, then store that result into the shhwtstamps structure which
475 * is passed up the network stack
476 */
477void ixgbe_ptp_tx_hwtstamp(struct ixgbe_q_vector *q_vector,
478 struct sk_buff *skb)
479{
480 struct ixgbe_adapter *adapter;
481 struct ixgbe_hw *hw;
482 struct skb_shared_hwtstamps shhwtstamps;
483 u64 regval = 0, ns;
484 u32 tsynctxctl;
485 unsigned long flags;
486
487 /* we cannot process timestamps on a ring without a q_vector */
488 if (!q_vector || !q_vector->adapter)
489 return;
490
491 adapter = q_vector->adapter;
492 hw = &adapter->hw;
493
494 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
495 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
496 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
497
498 /*
499 * if TX timestamp is not valid, exit after clearing the
500 * timestamp registers
501 */
502 if (!(tsynctxctl & IXGBE_TSYNCTXCTL_VALID))
503 return;
504
505 spin_lock_irqsave(&adapter->tmreg_lock, flags);
506 ns = timecounter_cyc2time(&adapter->tc, regval);
507 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
508
509 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
510 shhwtstamps.hwtstamp = ns_to_ktime(ns);
511 skb_tstamp_tx(skb, &shhwtstamps);
512}
513
514/**
515 * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
516 * @q_vector: structure containing interrupt and ring information
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000517 * @rx_desc: the rx descriptor
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000518 * @skb: particular skb to send timestamp with
519 *
520 * if the timestamp is valid, we convert it into the timecounter ns
521 * value, then store that result into the shhwtstamps structure which
522 * is passed up the network stack
523 */
Jacob Keller6cb562d2012-12-05 07:24:41 +0000524void ixgbe_ptp_rx_hwtstamp(struct ixgbe_ring *rx_ring,
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000525 union ixgbe_adv_rx_desc *rx_desc,
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000526 struct sk_buff *skb)
527{
528 struct ixgbe_adapter *adapter;
529 struct ixgbe_hw *hw;
530 struct skb_shared_hwtstamps *shhwtstamps;
531 u64 regval = 0, ns;
532 u32 tsyncrxctl;
533 unsigned long flags;
534
535 /* we cannot process timestamps on a ring without a q_vector */
Jacob Keller6cb562d2012-12-05 07:24:41 +0000536 if (!rx_ring->q_vector || !rx_ring->q_vector->adapter)
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000537 return;
538
Jacob Keller6cb562d2012-12-05 07:24:41 +0000539 adapter = rx_ring->q_vector->adapter;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000540 hw = &adapter->hw;
541
Jacob Keller6cb562d2012-12-05 07:24:41 +0000542 if (unlikely(!ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_STAT_TS)))
Jiri Bencf42df162012-10-25 18:12:05 +0000543 return;
544
Jacob Keller6cb562d2012-12-05 07:24:41 +0000545 /*
546 * Read the tsyncrxctl register afterwards in order to prevent taking an
547 * I/O hit on every packet.
548 */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000549 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
Jiri Bencf42df162012-10-25 18:12:05 +0000550 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000551 return;
552
553 /*
Jacob Keller6cb562d2012-12-05 07:24:41 +0000554 * Update the last_rx_timestamp timer in order to enable watchdog check
555 * for error case of latched timestamp on a dropped packet.
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000556 */
Jacob Keller6cb562d2012-12-05 07:24:41 +0000557 rx_ring->last_rx_timestamp = jiffies;
558
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000559 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
560 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
561
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000562
563 spin_lock_irqsave(&adapter->tmreg_lock, flags);
564 ns = timecounter_cyc2time(&adapter->tc, regval);
565 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
566
567 shhwtstamps = skb_hwtstamps(skb);
568 shhwtstamps->hwtstamp = ns_to_ktime(ns);
569}
570
571/**
572 * ixgbe_ptp_hwtstamp_ioctl - control hardware time stamping
573 * @adapter: pointer to adapter struct
574 * @ifreq: ioctl data
575 * @cmd: particular ioctl requested
576 *
577 * Outgoing time stamping can be enabled and disabled. Play nice and
578 * disable it when requested, although it shouldn't case any overhead
579 * when no packet needs it. At most one packet in the queue may be
580 * marked for time stamping, otherwise it would be impossible to tell
581 * for sure to which packet the hardware time stamp belongs.
582 *
583 * Incoming time stamping has to be configured via the hardware
584 * filters. Not all combinations are supported, in particular event
585 * type has to be specified. Matching the kind of event packet is
586 * not supported, with the exception of "all V2 events regardless of
587 * level 2 or 4".
Jacob Kellerc19197a2012-05-22 06:08:37 +0000588 *
589 * Since hardware always timestamps Path delay packets when timestamping V2
590 * packets, regardless of the type specified in the register, only use V2
591 * Event mode. This more accurately tells the user what the hardware is going
592 * to do anyways.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000593 */
594int ixgbe_ptp_hwtstamp_ioctl(struct ixgbe_adapter *adapter,
595 struct ifreq *ifr, int cmd)
596{
597 struct ixgbe_hw *hw = &adapter->hw;
598 struct hwtstamp_config config;
599 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
600 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000601 u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000602 bool is_l2 = false;
603 u32 regval;
604
605 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
606 return -EFAULT;
607
608 /* reserved for future extensions */
609 if (config.flags)
610 return -EINVAL;
611
612 switch (config.tx_type) {
613 case HWTSTAMP_TX_OFF:
614 tsync_tx_ctl = 0;
615 case HWTSTAMP_TX_ON:
616 break;
617 default:
618 return -ERANGE;
619 }
620
621 switch (config.rx_filter) {
622 case HWTSTAMP_FILTER_NONE:
623 tsync_rx_ctl = 0;
Jacob Kellerf3444d82012-10-24 02:31:47 +0000624 tsync_rx_mtrl = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000625 break;
626 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
627 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
628 tsync_rx_mtrl = IXGBE_RXMTRL_V1_SYNC_MSG;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000629 break;
630 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
631 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
632 tsync_rx_mtrl = IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000633 break;
Jacob Kellerc19197a2012-05-22 06:08:37 +0000634 case HWTSTAMP_FILTER_PTP_V2_EVENT:
635 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
636 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000637 case HWTSTAMP_FILTER_PTP_V2_SYNC:
638 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
639 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000640 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
641 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
642 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000643 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000644 is_l2 = true;
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000645 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000646 break;
647 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
648 case HWTSTAMP_FILTER_ALL:
649 default:
650 /*
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000651 * register RXMTRL must be set in order to do V1 packets,
652 * therefore it is not possible to time stamp both V1 Sync and
653 * Delay_Req messages and hardware does not support
654 * timestamping all packets => return error
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000655 */
Jacob Keller1d1a79b2012-05-22 06:18:08 +0000656 config.rx_filter = HWTSTAMP_FILTER_NONE;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000657 return -ERANGE;
658 }
659
660 if (hw->mac.type == ixgbe_mac_82598EB) {
661 if (tsync_rx_ctl | tsync_tx_ctl)
662 return -ERANGE;
663 return 0;
664 }
665
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000666 /* define ethertype filter for timestamping L2 packets */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000667 if (is_l2)
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000668 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000669 (IXGBE_ETQF_FILTER_EN | /* enable filter */
670 IXGBE_ETQF_1588 | /* enable timestamping */
671 ETH_P_1588)); /* 1588 eth protocol type */
672 else
Jacob Keller6ccf7a52012-10-23 08:09:21 +0000673 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000674
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000675
676 /* enable/disable TX */
677 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
678 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
679 regval |= tsync_tx_ctl;
680 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
681
682 /* enable/disable RX */
683 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
684 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
685 regval |= tsync_rx_ctl;
686 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
687
688 /* define which PTP packets are time stamped */
689 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
690
691 IXGBE_WRITE_FLUSH(hw);
692
693 /* clear TX/RX time stamp registers, just to be sure */
694 regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
695 regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
696
697 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
698 -EFAULT : 0;
699}
700
701/**
702 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000703 * @adapter: pointer to the adapter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000704 *
Jacob Keller1a71ab22012-08-25 03:54:19 +0000705 * This function should be called to set the proper values for the TIMINCA
706 * register and tell the cyclecounter structure what the tick rate of SYSTIME
707 * is. It does not directly modify SYSTIME registers or the timecounter
708 * structure. It should be called whenever a new TIMINCA value is necessary,
709 * such as during initialization or when the link speed changes.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000710 */
711void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
712{
713 struct ixgbe_hw *hw = &adapter->hw;
714 u32 incval = 0;
715 u32 shift = 0;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000716 unsigned long flags;
717
718 /**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000719 * Scale the NIC cycle counter by a large factor so that
720 * relatively small corrections to the frequency can be added
721 * or subtracted. The drawbacks of a large factor include
722 * (a) the clock register overflows more quickly, (b) the cycle
723 * counter structure must be able to convert the systime value
724 * to nanoseconds using only a multiplier and a right-shift,
725 * and (c) the value must fit within the timinca register space
726 * => math based on internal DMA clock rate and available bits
Jacob Keller1a71ab22012-08-25 03:54:19 +0000727 *
728 * Note that when there is no link, internal DMA clock is same as when
729 * link speed is 10Gb. Set the registers correctly even when link is
730 * down to preserve the clock setting
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000731 */
Jacob Keller1a71ab22012-08-25 03:54:19 +0000732 switch (adapter->link_speed) {
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000733 case IXGBE_LINK_SPEED_100_FULL:
734 incval = IXGBE_INCVAL_100;
735 shift = IXGBE_INCVAL_SHIFT_100;
736 break;
737 case IXGBE_LINK_SPEED_1GB_FULL:
738 incval = IXGBE_INCVAL_1GB;
739 shift = IXGBE_INCVAL_SHIFT_1GB;
740 break;
741 case IXGBE_LINK_SPEED_10GB_FULL:
Jacob Keller1a71ab22012-08-25 03:54:19 +0000742 default:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000743 incval = IXGBE_INCVAL_10GB;
744 shift = IXGBE_INCVAL_SHIFT_10GB;
745 break;
746 }
747
748 /**
749 * Modify the calculated values to fit within the correct
750 * number of bits specified by the hardware. The 82599 doesn't
751 * have the same space as the X540, so bitshift the calculated
752 * values to fit.
753 */
754 switch (hw->mac.type) {
755 case ixgbe_mac_X540:
756 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
757 break;
758 case ixgbe_mac_82599EB:
759 incval >>= IXGBE_INCVAL_SHIFT_82599;
760 shift -= IXGBE_INCVAL_SHIFT_82599;
761 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
762 (1 << IXGBE_INCPER_SHIFT_82599) |
763 incval);
764 break;
765 default:
766 /* other devices aren't supported */
767 return;
768 }
769
Jacob Keller1a71ab22012-08-25 03:54:19 +0000770 /* update the base incval used to calculate frequency adjustment */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000771 ACCESS_ONCE(adapter->base_incval) = incval;
772 smp_mb();
773
Jacob Keller1a71ab22012-08-25 03:54:19 +0000774 /* need lock to prevent incorrect read while modifying cyclecounter */
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000775 spin_lock_irqsave(&adapter->tmreg_lock, flags);
776
777 memset(&adapter->cc, 0, sizeof(adapter->cc));
778 adapter->cc.read = ixgbe_ptp_read;
779 adapter->cc.mask = CLOCKSOURCE_MASK(64);
780 adapter->cc.shift = shift;
781 adapter->cc.mult = 1;
782
Jacob Keller1a71ab22012-08-25 03:54:19 +0000783 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
784}
785
786/**
787 * ixgbe_ptp_reset
788 * @adapter: the ixgbe private board structure
789 *
790 * When the MAC resets, all timesync features are reset. This function should be
791 * called to re-enable the PTP clock structure. It will re-init the timecounter
792 * structure based on the kernel time as well as setup the cycle counter data.
793 */
794void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
795{
796 struct ixgbe_hw *hw = &adapter->hw;
797 unsigned long flags;
798
799 /* set SYSTIME registers to 0 just in case */
800 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
801 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
802 IXGBE_WRITE_FLUSH(hw);
803
804 ixgbe_ptp_start_cyclecounter(adapter);
805
806 spin_lock_irqsave(&adapter->tmreg_lock, flags);
807
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000808 /* reset the ns time counter */
809 timecounter_init(&adapter->tc, &adapter->cc,
810 ktime_to_ns(ktime_get_real()));
811
812 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
Jacob Keller82083672012-08-01 07:12:25 +0000813
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000814 /*
815 * Now that the shift has been calculated and the systime
Jacob Keller82083672012-08-01 07:12:25 +0000816 * registers reset, (re-)enable the Clock out feature
817 */
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000818 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000819}
820
821/**
822 * ixgbe_ptp_init
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000823 * @adapter: the ixgbe private adapter structure
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000824 *
825 * This function performs the required steps for enabling ptp
826 * support. If ptp support has already been loaded it simply calls the
827 * cyclecounter init routine and exits.
828 */
829void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
830{
831 struct net_device *netdev = adapter->netdev;
832
833 switch (adapter->hw.mac.type) {
834 case ixgbe_mac_X540:
Jacob Keller1a71ab22012-08-25 03:54:19 +0000835 snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000836 adapter->ptp_caps.owner = THIS_MODULE;
837 adapter->ptp_caps.max_adj = 250000000;
838 adapter->ptp_caps.n_alarm = 0;
839 adapter->ptp_caps.n_ext_ts = 0;
840 adapter->ptp_caps.n_per_out = 0;
841 adapter->ptp_caps.pps = 1;
842 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
843 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
844 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
845 adapter->ptp_caps.settime = ixgbe_ptp_settime;
846 adapter->ptp_caps.enable = ixgbe_ptp_enable;
847 break;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000848 case ixgbe_mac_82599EB:
Jacob Keller1a71ab22012-08-25 03:54:19 +0000849 snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000850 adapter->ptp_caps.owner = THIS_MODULE;
851 adapter->ptp_caps.max_adj = 250000000;
852 adapter->ptp_caps.n_alarm = 0;
853 adapter->ptp_caps.n_ext_ts = 0;
854 adapter->ptp_caps.n_per_out = 0;
855 adapter->ptp_caps.pps = 0;
856 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
857 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
858 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
859 adapter->ptp_caps.settime = ixgbe_ptp_settime;
860 adapter->ptp_caps.enable = ixgbe_ptp_enable;
861 break;
862 default:
863 adapter->ptp_clock = NULL;
864 return;
865 }
866
867 spin_lock_init(&adapter->tmreg_lock);
868
Richard Cochran1ef76152012-09-22 07:02:03 +0000869 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
870 &adapter->pdev->dev);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000871 if (IS_ERR(adapter->ptp_clock)) {
872 adapter->ptp_clock = NULL;
873 e_dev_err("ptp_clock_register failed\n");
874 } else
875 e_dev_info("registered PHC device on %s\n", netdev->name);
876
Jacob Keller1a71ab22012-08-25 03:54:19 +0000877 ixgbe_ptp_reset(adapter);
878
879 /* set the flag that PTP has been enabled */
880 adapter->flags2 |= IXGBE_FLAG2_PTP_ENABLED;
881
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000882 return;
883}
884
885/**
886 * ixgbe_ptp_stop - disable ptp device and stop the overflow check
887 * @adapter: pointer to adapter struct
888 *
889 * this function stops the ptp support, and cancels the delayed work.
890 */
891void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
892{
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000893 /* stop the overflow check task */
Jacob Keller1a71ab22012-08-25 03:54:19 +0000894 adapter->flags2 &= ~(IXGBE_FLAG2_PTP_ENABLED |
Jacob Kellerdb0677f2012-08-24 07:46:54 +0000895 IXGBE_FLAG2_PTP_PPS_ENABLED);
896
897 ixgbe_ptp_setup_sdp(adapter);
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000898
899 if (adapter->ptp_clock) {
900 ptp_clock_unregister(adapter->ptp_clock);
901 adapter->ptp_clock = NULL;
902 e_dev_info("removed PHC on %s\n",
903 adapter->netdev->name);
904 }
905}