blob: 5ed8cffee178003dd7bb9b0f248a9574d5f0d706 [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>
29
30/*
31 * The 82599 and the X540 do not have true 64bit nanosecond scale
32 * counter registers. Instead, SYSTIME is defined by a fixed point
33 * system which allows the user to define the scale counter increment
34 * value at every level change of the oscillator driving the SYSTIME
35 * value. For both devices the TIMINCA:IV field defines this
36 * increment. On the X540 device, 31 bits are provided. However on the
37 * 82599 only provides 24 bits. The time unit is determined by the
38 * clock frequency of the oscillator in combination with the TIMINCA
39 * register. When these devices link at 10Gb the oscillator has a
40 * period of 6.4ns. In order to convert the scale counter into
41 * nanoseconds the cyclecounter and timecounter structures are
42 * used. The SYSTIME registers need to be converted to ns values by use
43 * of only a right shift (division by power of 2). The following math
44 * determines the largest incvalue that will fit into the available
45 * bits in the TIMINCA register.
46 *
47 * PeriodWidth: Number of bits to store the clock period
48 * MaxWidth: The maximum width value of the TIMINCA register
49 * Period: The clock period for the oscillator
50 * round(): discard the fractional portion of the calculation
51 *
52 * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
53 *
54 * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
55 * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
56 *
57 * The period also changes based on the link speed:
58 * At 10Gb link or no link, the period remains the same.
59 * At 1Gb link, the period is multiplied by 10. (64ns)
60 * At 100Mb link, the period is multiplied by 100. (640ns)
61 *
62 * The calculated value allows us to right shift the SYSTIME register
63 * value in order to quickly convert it into a nanosecond clock,
64 * while allowing for the maximum possible adjustment value.
65 *
66 * These diagrams are only for the 10Gb link period
67 *
68 * SYSTIMEH SYSTIMEL
69 * +--------------+ +--------------+
70 * X540 | 32 | | 1 | 3 | 28 |
71 * *--------------+ +--------------+
72 * \________ 36 bits ______/ fract
73 *
74 * +--------------+ +--------------+
75 * 82599 | 32 | | 8 | 3 | 21 |
76 * *--------------+ +--------------+
77 * \________ 43 bits ______/ fract
78 *
79 * The 36 bit X540 SYSTIME overflows every
80 * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
81 *
82 * The 43 bit 82599 SYSTIME overflows every
83 * 2^43 * 10^-9 / 3600 = 2.4 hours
84 */
85#define IXGBE_INCVAL_10GB 0x66666666
86#define IXGBE_INCVAL_1GB 0x40000000
87#define IXGBE_INCVAL_100 0x50000000
88
89#define IXGBE_INCVAL_SHIFT_10GB 28
90#define IXGBE_INCVAL_SHIFT_1GB 24
91#define IXGBE_INCVAL_SHIFT_100 21
92
93#define IXGBE_INCVAL_SHIFT_82599 7
94#define IXGBE_INCPER_SHIFT_82599 24
95#define IXGBE_MAX_TIMEADJ_VALUE 0x7FFFFFFFFFFFFFFFULL
96
97#define IXGBE_OVERFLOW_PERIOD (HZ * 30)
98
Jacob E Keller681ae1a2012-05-01 05:24:41 +000099#ifndef NSECS_PER_SEC
100#define NSECS_PER_SEC 1000000000ULL
101#endif
102
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000103/**
104 * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
105 * @cc - the cyclecounter structure
106 *
107 * this function reads the cyclecounter registers and is called by the
108 * cyclecounter structure used to construct a ns counter from the
109 * arbitrary fixed point registers
110 */
111static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
112{
113 struct ixgbe_adapter *adapter =
114 container_of(cc, struct ixgbe_adapter, cc);
115 struct ixgbe_hw *hw = &adapter->hw;
116 u64 stamp = 0;
117
118 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
119 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
120
121 return stamp;
122}
123
124/**
125 * ixgbe_ptp_adjfreq
126 * @ptp - the ptp clock structure
127 * @ppb - parts per billion adjustment from base
128 *
129 * adjust the frequency of the ptp cycle counter by the
130 * indicated ppb from the base frequency.
131 */
132static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
133{
134 struct ixgbe_adapter *adapter =
135 container_of(ptp, struct ixgbe_adapter, ptp_caps);
136 struct ixgbe_hw *hw = &adapter->hw;
137 u64 freq;
138 u32 diff, incval;
139 int neg_adj = 0;
140
141 if (ppb < 0) {
142 neg_adj = 1;
143 ppb = -ppb;
144 }
145
146 smp_mb();
147 incval = ACCESS_ONCE(adapter->base_incval);
148
149 freq = incval;
150 freq *= ppb;
151 diff = div_u64(freq, 1000000000ULL);
152
153 incval = neg_adj ? (incval - diff) : (incval + diff);
154
155 switch (hw->mac.type) {
156 case ixgbe_mac_X540:
157 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
158 break;
159 case ixgbe_mac_82599EB:
160 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
161 (1 << IXGBE_INCPER_SHIFT_82599) |
162 incval);
163 break;
164 default:
165 break;
166 }
167
168 return 0;
169}
170
171/**
172 * ixgbe_ptp_adjtime
173 * @ptp - the ptp clock structure
174 * @delta - offset to adjust the cycle counter by
175 *
176 * adjust the timer by resetting the timecounter structure.
177 */
178static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
179{
180 struct ixgbe_adapter *adapter =
181 container_of(ptp, struct ixgbe_adapter, ptp_caps);
182 unsigned long flags;
183 u64 now;
184
185 spin_lock_irqsave(&adapter->tmreg_lock, flags);
186
187 now = timecounter_read(&adapter->tc);
188 now += delta;
189
190 /* reset the timecounter */
191 timecounter_init(&adapter->tc,
192 &adapter->cc,
193 now);
194
195 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
196 return 0;
197}
198
199/**
200 * ixgbe_ptp_gettime
201 * @ptp - the ptp clock structure
202 * @ts - timespec structure to hold the current time value
203 *
204 * read the timecounter and return the correct value on ns,
205 * after converting it into a struct timespec.
206 */
207static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
208{
209 struct ixgbe_adapter *adapter =
210 container_of(ptp, struct ixgbe_adapter, ptp_caps);
211 u64 ns;
212 u32 remainder;
213 unsigned long flags;
214
215 spin_lock_irqsave(&adapter->tmreg_lock, flags);
216 ns = timecounter_read(&adapter->tc);
217 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
218
219 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
220 ts->tv_nsec = remainder;
221
222 return 0;
223}
224
225/**
226 * ixgbe_ptp_settime
227 * @ptp - the ptp clock structure
228 * @ts - the timespec containing the new time for the cycle counter
229 *
230 * reset the timecounter to use a new base value instead of the kernel
231 * wall timer value.
232 */
233static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
234 const struct timespec *ts)
235{
236 struct ixgbe_adapter *adapter =
237 container_of(ptp, struct ixgbe_adapter, ptp_caps);
238 u64 ns;
239 unsigned long flags;
240
241 ns = ts->tv_sec * 1000000000ULL;
242 ns += ts->tv_nsec;
243
244 /* reset the timecounter */
245 spin_lock_irqsave(&adapter->tmreg_lock, flags);
246 timecounter_init(&adapter->tc, &adapter->cc, ns);
247 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
248
249 return 0;
250}
251
252/**
253 * ixgbe_ptp_enable
254 * @ptp - the ptp clock structure
255 * @rq - the requested feature to change
256 * @on - whether to enable or disable the feature
257 *
258 * enable (or disable) ancillary features of the phc subsystem.
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000259 * our driver only supports the PPS feature on the X540
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000260 */
261static int ixgbe_ptp_enable(struct ptp_clock_info *ptp,
262 struct ptp_clock_request *rq, int on)
263{
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000264 struct ixgbe_adapter *adapter =
265 container_of(ptp, struct ixgbe_adapter, ptp_caps);
266
267 /**
268 * When PPS is enabled, unmask the interrupt for the ClockOut
269 * feature, so that the interrupt handler can send the PPS
270 * event when the clock SDP triggers. Clear mask when PPS is
271 * disabled
272 */
273 if (rq->type == PTP_CLK_REQ_PPS) {
274 switch (adapter->hw.mac.type) {
275 case ixgbe_mac_X540:
276 if (on)
277 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
278 else
279 adapter->flags2 &=
280 ~IXGBE_FLAG2_PTP_PPS_ENABLED;
281 return 0;
282 default:
283 break;
284 }
285 }
286
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000287 return -ENOTSUPP;
288}
289
290/**
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000291 * ixgbe_ptp_check_pps_event
292 * @adapter - the private adapter structure
293 * @eicr - the interrupt cause register value
294 *
295 * This function is called by the interrupt routine when checking for
296 * interrupts. It will check and handle a pps event.
297 */
298void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
299{
300 struct ixgbe_hw *hw = &adapter->hw;
301 struct ptp_clock_event event;
302
303 event.type = PTP_CLOCK_PPS;
304
305 /* Make sure ptp clock is valid, and PPS event enabled */
306 if (!adapter->ptp_clock ||
307 !(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
308 return;
309
Jacob Keller0ede4a62012-05-22 06:08:32 +0000310 if (unlikely(eicr & IXGBE_EICR_TIMESYNC)) {
311 switch (hw->mac.type) {
312 case ixgbe_mac_X540:
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000313 ptp_clock_event(adapter->ptp_clock, &event);
Jacob Keller0ede4a62012-05-22 06:08:32 +0000314 break;
315 default:
316 break;
317 }
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000318 }
319}
320
321/**
322 * ixgbe_ptp_enable_sdp
323 * @hw - the hardware private structure
324 * @shift - the clock shift for calculating nanoseconds
325 *
326 * this function enables the clock out feature on the sdp0 for the
327 * X540 device. It will create a 1second periodic output that can be
328 * used as the PPS (via an interrupt).
329 *
330 * It calculates when the systime will be on an exact second, and then
331 * aligns the start of the PPS signal to that value. The shift is
332 * necessary because it can change based on the link speed.
333 */
334static void ixgbe_ptp_enable_sdp(struct ixgbe_hw *hw, int shift)
335{
336 u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh;
337 u64 clock_edge = 0;
338 u32 rem;
339
340 switch (hw->mac.type) {
341 case ixgbe_mac_X540:
342 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
343
344 /*
345 * enable the SDP0 pin as output, and connected to the native
346 * function for Timesync (ClockOut)
347 */
348 esdp |= (IXGBE_ESDP_SDP0_DIR |
349 IXGBE_ESDP_SDP0_NATIVE);
350
351 /*
352 * enable the Clock Out feature on SDP0, and allow interrupts
353 * to occur when the pin changes
354 */
355 tsauxc = (IXGBE_TSAUXC_EN_CLK |
356 IXGBE_TSAUXC_SYNCLK |
357 IXGBE_TSAUXC_SDP0_INT);
358
359 /* clock period (or pulse length) */
360 clktiml = (u32)(NSECS_PER_SEC << shift);
361 clktimh = (u32)((NSECS_PER_SEC << shift) >> 32);
362
363 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
364 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
365
366 /*
367 * account for the fact that we can't do u64 division
368 * with remainder, by converting the clock values into
369 * nanoseconds first
370 */
371 clock_edge >>= shift;
372 div_u64_rem(clock_edge, NSECS_PER_SEC, &rem);
373 clock_edge += (NSECS_PER_SEC - rem);
374 clock_edge <<= shift;
375
376 /* specify the initial clock start time */
377 trgttiml = (u32)clock_edge;
378 trgttimh = (u32)(clock_edge >> 32);
379
380 IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
381 IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
382 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
383 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
384
385 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
386 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
387
388 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_TIMESYNC);
389 break;
390 default:
391 break;
392 }
393}
394
395/**
396 * ixgbe_ptp_disable_sdp
397 * @hw - the private hardware structure
398 *
399 * this function disables the auxiliary SDP clock out feature
400 */
401static void ixgbe_ptp_disable_sdp(struct ixgbe_hw *hw)
402{
403 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_TIMESYNC);
404 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0);
405}
406
407/**
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000408 * ixgbe_ptp_overflow_check - delayed work to detect SYSTIME overflow
409 * @work: structure containing information about this work task
410 *
411 * this work function is scheduled to continue reading the timecounter
412 * in order to prevent missing when the system time registers wrap
413 * around. This needs to be run approximately twice a minute when no
414 * PTP activity is occurring.
415 */
416void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
417{
418 unsigned long elapsed_jiffies = adapter->last_overflow_check - jiffies;
419 struct timespec ts;
420
421 if ((adapter->flags2 & IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED) &&
422 (elapsed_jiffies >= IXGBE_OVERFLOW_PERIOD)) {
423 ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
424 adapter->last_overflow_check = jiffies;
425 }
426}
427
428/**
429 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
430 * @q_vector: structure containing interrupt and ring information
431 * @skb: particular skb to send timestamp with
432 *
433 * if the timestamp is valid, we convert it into the timecounter ns
434 * value, then store that result into the shhwtstamps structure which
435 * is passed up the network stack
436 */
437void ixgbe_ptp_tx_hwtstamp(struct ixgbe_q_vector *q_vector,
438 struct sk_buff *skb)
439{
440 struct ixgbe_adapter *adapter;
441 struct ixgbe_hw *hw;
442 struct skb_shared_hwtstamps shhwtstamps;
443 u64 regval = 0, ns;
444 u32 tsynctxctl;
445 unsigned long flags;
446
447 /* we cannot process timestamps on a ring without a q_vector */
448 if (!q_vector || !q_vector->adapter)
449 return;
450
451 adapter = q_vector->adapter;
452 hw = &adapter->hw;
453
454 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
455 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
456 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
457
458 /*
459 * if TX timestamp is not valid, exit after clearing the
460 * timestamp registers
461 */
462 if (!(tsynctxctl & IXGBE_TSYNCTXCTL_VALID))
463 return;
464
465 spin_lock_irqsave(&adapter->tmreg_lock, flags);
466 ns = timecounter_cyc2time(&adapter->tc, regval);
467 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
468
469 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
470 shhwtstamps.hwtstamp = ns_to_ktime(ns);
471 skb_tstamp_tx(skb, &shhwtstamps);
472}
473
474/**
475 * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
476 * @q_vector: structure containing interrupt and ring information
477 * @skb: particular skb to send timestamp with
478 *
479 * if the timestamp is valid, we convert it into the timecounter ns
480 * value, then store that result into the shhwtstamps structure which
481 * is passed up the network stack
482 */
483void ixgbe_ptp_rx_hwtstamp(struct ixgbe_q_vector *q_vector,
484 struct sk_buff *skb)
485{
486 struct ixgbe_adapter *adapter;
487 struct ixgbe_hw *hw;
488 struct skb_shared_hwtstamps *shhwtstamps;
489 u64 regval = 0, ns;
490 u32 tsyncrxctl;
491 unsigned long flags;
492
493 /* we cannot process timestamps on a ring without a q_vector */
494 if (!q_vector || !q_vector->adapter)
495 return;
496
497 adapter = q_vector->adapter;
498 hw = &adapter->hw;
499
500 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
501 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
502 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
503
504 /*
505 * If this bit is set, then the RX registers contain the time stamp. No
506 * other packet will be time stamped until we read these registers, so
507 * read the registers to make them available again. Because only one
508 * packet can be time stamped at a time, we know that the register
509 * values must belong to this one here and therefore we don't need to
510 * compare any of the additional attributes stored for it.
511 *
512 * If nothing went wrong, then it should have a skb_shared_tx that we
513 * can turn into a skb_shared_hwtstamps.
514 */
515 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
516 return;
517
518 spin_lock_irqsave(&adapter->tmreg_lock, flags);
519 ns = timecounter_cyc2time(&adapter->tc, regval);
520 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
521
522 shhwtstamps = skb_hwtstamps(skb);
523 shhwtstamps->hwtstamp = ns_to_ktime(ns);
524}
525
526/**
527 * ixgbe_ptp_hwtstamp_ioctl - control hardware time stamping
528 * @adapter: pointer to adapter struct
529 * @ifreq: ioctl data
530 * @cmd: particular ioctl requested
531 *
532 * Outgoing time stamping can be enabled and disabled. Play nice and
533 * disable it when requested, although it shouldn't case any overhead
534 * when no packet needs it. At most one packet in the queue may be
535 * marked for time stamping, otherwise it would be impossible to tell
536 * for sure to which packet the hardware time stamp belongs.
537 *
538 * Incoming time stamping has to be configured via the hardware
539 * filters. Not all combinations are supported, in particular event
540 * type has to be specified. Matching the kind of event packet is
541 * not supported, with the exception of "all V2 events regardless of
542 * level 2 or 4".
Jacob Kellerc19197a2012-05-22 06:08:37 +0000543 *
544 * Since hardware always timestamps Path delay packets when timestamping V2
545 * packets, regardless of the type specified in the register, only use V2
546 * Event mode. This more accurately tells the user what the hardware is going
547 * to do anyways.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000548 */
549int ixgbe_ptp_hwtstamp_ioctl(struct ixgbe_adapter *adapter,
550 struct ifreq *ifr, int cmd)
551{
552 struct ixgbe_hw *hw = &adapter->hw;
553 struct hwtstamp_config config;
554 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
555 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
556 u32 tsync_rx_mtrl = 0;
557 bool is_l4 = false;
558 bool is_l2 = false;
559 u32 regval;
560
561 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
562 return -EFAULT;
563
564 /* reserved for future extensions */
565 if (config.flags)
566 return -EINVAL;
567
568 switch (config.tx_type) {
569 case HWTSTAMP_TX_OFF:
570 tsync_tx_ctl = 0;
571 case HWTSTAMP_TX_ON:
572 break;
573 default:
574 return -ERANGE;
575 }
576
577 switch (config.rx_filter) {
578 case HWTSTAMP_FILTER_NONE:
579 tsync_rx_ctl = 0;
580 break;
581 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
582 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
583 tsync_rx_mtrl = IXGBE_RXMTRL_V1_SYNC_MSG;
584 is_l4 = true;
585 break;
586 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
587 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
588 tsync_rx_mtrl = IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
589 is_l4 = true;
590 break;
Jacob Kellerc19197a2012-05-22 06:08:37 +0000591 case HWTSTAMP_FILTER_PTP_V2_EVENT:
592 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
593 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000594 case HWTSTAMP_FILTER_PTP_V2_SYNC:
595 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
596 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000597 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
598 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
599 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000600 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
601 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
602 is_l2 = true;
603 is_l4 = true;
604 break;
605 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
606 case HWTSTAMP_FILTER_ALL:
607 default:
608 /*
609 * register RXMTRL must be set, therefore it is not
610 * possible to time stamp both V1 Sync and Delay_Req messages
611 * and hardware does not support timestamping all packets
612 * => return error
613 */
614 return -ERANGE;
615 }
616
617 if (hw->mac.type == ixgbe_mac_82598EB) {
618 if (tsync_rx_ctl | tsync_tx_ctl)
619 return -ERANGE;
620 return 0;
621 }
622
623 /* define ethertype filter for timestamped packets */
624 if (is_l2)
625 IXGBE_WRITE_REG(hw, IXGBE_ETQF(3),
626 (IXGBE_ETQF_FILTER_EN | /* enable filter */
627 IXGBE_ETQF_1588 | /* enable timestamping */
628 ETH_P_1588)); /* 1588 eth protocol type */
629 else
630 IXGBE_WRITE_REG(hw, IXGBE_ETQF(3), 0);
631
632#define PTP_PORT 319
633 /* L4 Queue Filter[3]: filter by destination port and protocol */
634 if (is_l4) {
635 u32 ftqf = (IXGBE_FTQF_PROTOCOL_UDP /* UDP */
636 | IXGBE_FTQF_POOL_MASK_EN /* Pool not compared */
637 | IXGBE_FTQF_QUEUE_ENABLE);
638
639 ftqf |= ((IXGBE_FTQF_PROTOCOL_COMP_MASK /* protocol check */
640 & IXGBE_FTQF_DEST_PORT_MASK /* dest check */
641 & IXGBE_FTQF_SOURCE_PORT_MASK) /* source check */
642 << IXGBE_FTQF_5TUPLE_MASK_SHIFT);
643
644 IXGBE_WRITE_REG(hw, IXGBE_L34T_IMIR(3),
645 (3 << IXGBE_IMIR_RX_QUEUE_SHIFT_82599 |
646 IXGBE_IMIR_SIZE_BP_82599));
647
648 /* enable port check */
649 IXGBE_WRITE_REG(hw, IXGBE_SDPQF(3),
650 (htons(PTP_PORT) |
651 htons(PTP_PORT) << 16));
652
653 IXGBE_WRITE_REG(hw, IXGBE_FTQF(3), ftqf);
654
655 tsync_rx_mtrl |= PTP_PORT << 16;
656 } else {
657 IXGBE_WRITE_REG(hw, IXGBE_FTQF(3), 0);
658 }
659
660 /* enable/disable TX */
661 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
662 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
663 regval |= tsync_tx_ctl;
664 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
665
666 /* enable/disable RX */
667 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
668 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
669 regval |= tsync_rx_ctl;
670 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
671
672 /* define which PTP packets are time stamped */
673 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
674
675 IXGBE_WRITE_FLUSH(hw);
676
677 /* clear TX/RX time stamp registers, just to be sure */
678 regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
679 regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
680
681 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
682 -EFAULT : 0;
683}
684
685/**
686 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
687 * @adapter - pointer to the adapter structure
688 *
689 * this function initializes the timecounter and cyclecounter
690 * structures for use in generated a ns counter from the arbitrary
691 * fixed point cycles registers in the hardware.
692 *
693 * A change in link speed impacts the frequency of the DMA clock on
694 * the device, which is used to generate the cycle counter
695 * registers. Therefor this function is called whenever the link speed
696 * changes.
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000697 *
698 * This function also turns on the SDP pin for clock out feature (X540
699 * only), because this is where the shift is first calculated.
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000700 */
701void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
702{
703 struct ixgbe_hw *hw = &adapter->hw;
704 u32 incval = 0;
705 u32 shift = 0;
706 u32 cycle_speed;
707 unsigned long flags;
708
709 /**
710 * Determine what speed we need to set the cyclecounter
711 * for. It should be different for 100Mb, 1Gb, and 10Gb. Treat
712 * unknown speeds as 10Gb. (Hence why we can't just copy the
713 * link_speed.
714 */
715 switch (adapter->link_speed) {
716 case IXGBE_LINK_SPEED_100_FULL:
717 case IXGBE_LINK_SPEED_1GB_FULL:
718 case IXGBE_LINK_SPEED_10GB_FULL:
719 cycle_speed = adapter->link_speed;
720 break;
721 default:
722 /* cycle speed should be 10Gb when there is no link */
723 cycle_speed = IXGBE_LINK_SPEED_10GB_FULL;
724 break;
725 }
726
727 /* Bail if the cycle speed didn't change */
728 if (adapter->cycle_speed == cycle_speed)
729 return;
730
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000731 /* disable the SDP clock out */
732 ixgbe_ptp_disable_sdp(hw);
733
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000734 /**
735 * Scale the NIC cycle counter by a large factor so that
736 * relatively small corrections to the frequency can be added
737 * or subtracted. The drawbacks of a large factor include
738 * (a) the clock register overflows more quickly, (b) the cycle
739 * counter structure must be able to convert the systime value
740 * to nanoseconds using only a multiplier and a right-shift,
741 * and (c) the value must fit within the timinca register space
742 * => math based on internal DMA clock rate and available bits
743 */
744 switch (cycle_speed) {
745 case IXGBE_LINK_SPEED_100_FULL:
746 incval = IXGBE_INCVAL_100;
747 shift = IXGBE_INCVAL_SHIFT_100;
748 break;
749 case IXGBE_LINK_SPEED_1GB_FULL:
750 incval = IXGBE_INCVAL_1GB;
751 shift = IXGBE_INCVAL_SHIFT_1GB;
752 break;
753 case IXGBE_LINK_SPEED_10GB_FULL:
754 incval = IXGBE_INCVAL_10GB;
755 shift = IXGBE_INCVAL_SHIFT_10GB;
756 break;
757 }
758
759 /**
760 * Modify the calculated values to fit within the correct
761 * number of bits specified by the hardware. The 82599 doesn't
762 * have the same space as the X540, so bitshift the calculated
763 * values to fit.
764 */
765 switch (hw->mac.type) {
766 case ixgbe_mac_X540:
767 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
768 break;
769 case ixgbe_mac_82599EB:
770 incval >>= IXGBE_INCVAL_SHIFT_82599;
771 shift -= IXGBE_INCVAL_SHIFT_82599;
772 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
773 (1 << IXGBE_INCPER_SHIFT_82599) |
774 incval);
775 break;
776 default:
777 /* other devices aren't supported */
778 return;
779 }
780
781 /* reset the system time registers */
782 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
783 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
784 IXGBE_WRITE_FLUSH(hw);
785
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000786 /* now that the shift has been calculated and the systime
787 * registers reset, (re-)enable the Clock out feature*/
788 ixgbe_ptp_enable_sdp(hw, shift);
789
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000790 /* store the new cycle speed */
791 adapter->cycle_speed = cycle_speed;
792
793 ACCESS_ONCE(adapter->base_incval) = incval;
794 smp_mb();
795
796 /* grab the ptp lock */
797 spin_lock_irqsave(&adapter->tmreg_lock, flags);
798
799 memset(&adapter->cc, 0, sizeof(adapter->cc));
800 adapter->cc.read = ixgbe_ptp_read;
801 adapter->cc.mask = CLOCKSOURCE_MASK(64);
802 adapter->cc.shift = shift;
803 adapter->cc.mult = 1;
804
805 /* reset the ns time counter */
806 timecounter_init(&adapter->tc, &adapter->cc,
807 ktime_to_ns(ktime_get_real()));
808
809 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
810}
811
812/**
813 * ixgbe_ptp_init
814 * @adapter - the ixgbe private adapter structure
815 *
816 * This function performs the required steps for enabling ptp
817 * support. If ptp support has already been loaded it simply calls the
818 * cyclecounter init routine and exits.
819 */
820void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
821{
822 struct net_device *netdev = adapter->netdev;
823
824 switch (adapter->hw.mac.type) {
825 case ixgbe_mac_X540:
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000826 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
827 adapter->ptp_caps.owner = THIS_MODULE;
828 adapter->ptp_caps.max_adj = 250000000;
829 adapter->ptp_caps.n_alarm = 0;
830 adapter->ptp_caps.n_ext_ts = 0;
831 adapter->ptp_caps.n_per_out = 0;
832 adapter->ptp_caps.pps = 1;
833 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
834 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
835 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
836 adapter->ptp_caps.settime = ixgbe_ptp_settime;
837 adapter->ptp_caps.enable = ixgbe_ptp_enable;
838 break;
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000839 case ixgbe_mac_82599EB:
840 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
841 adapter->ptp_caps.owner = THIS_MODULE;
842 adapter->ptp_caps.max_adj = 250000000;
843 adapter->ptp_caps.n_alarm = 0;
844 adapter->ptp_caps.n_ext_ts = 0;
845 adapter->ptp_caps.n_per_out = 0;
846 adapter->ptp_caps.pps = 0;
847 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
848 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
849 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
850 adapter->ptp_caps.settime = ixgbe_ptp_settime;
851 adapter->ptp_caps.enable = ixgbe_ptp_enable;
852 break;
853 default:
854 adapter->ptp_clock = NULL;
855 return;
856 }
857
858 spin_lock_init(&adapter->tmreg_lock);
859
860 ixgbe_ptp_start_cyclecounter(adapter);
861
862 /* (Re)start the overflow check */
863 adapter->flags2 |= IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED;
864
865 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps);
866 if (IS_ERR(adapter->ptp_clock)) {
867 adapter->ptp_clock = NULL;
868 e_dev_err("ptp_clock_register failed\n");
869 } else
870 e_dev_info("registered PHC device on %s\n", netdev->name);
871
872 return;
873}
874
875/**
876 * ixgbe_ptp_stop - disable ptp device and stop the overflow check
877 * @adapter: pointer to adapter struct
878 *
879 * this function stops the ptp support, and cancels the delayed work.
880 */
881void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
882{
Jacob E Keller681ae1a2012-05-01 05:24:41 +0000883 ixgbe_ptp_disable_sdp(&adapter->hw);
884
Jacob Keller3a6a4ed2012-05-01 05:24:58 +0000885 /* stop the overflow check task */
886 adapter->flags2 &= ~IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED;
887
888 if (adapter->ptp_clock) {
889 ptp_clock_unregister(adapter->ptp_clock);
890 adapter->ptp_clock = NULL;
891 e_dev_info("removed PHC on %s\n",
892 adapter->netdev->name);
893 }
894}