1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
5 #include <linux/ptp_classify.h>
6 #include <linux/clocksource.h>
9 * The 82599 and the X540 do not have true 64bit nanosecond scale
10 * counter registers. Instead, SYSTIME is defined by a fixed point
11 * system which allows the user to define the scale counter increment
12 * value at every level change of the oscillator driving the SYSTIME
13 * value. For both devices the TIMINCA:IV field defines this
14 * increment. On the X540 device, 31 bits are provided. However on the
15 * 82599 only provides 24 bits. The time unit is determined by the
16 * clock frequency of the oscillator in combination with the TIMINCA
17 * register. When these devices link at 10Gb the oscillator has a
18 * period of 6.4ns. In order to convert the scale counter into
19 * nanoseconds the cyclecounter and timecounter structures are
20 * used. The SYSTIME registers need to be converted to ns values by use
21 * of only a right shift (division by power of 2). The following math
22 * determines the largest incvalue that will fit into the available
23 * bits in the TIMINCA register.
25 * PeriodWidth: Number of bits to store the clock period
26 * MaxWidth: The maximum width value of the TIMINCA register
27 * Period: The clock period for the oscillator
28 * round(): discard the fractional portion of the calculation
30 * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
32 * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
33 * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
35 * The period also changes based on the link speed:
36 * At 10Gb link or no link, the period remains the same.
37 * At 1Gb link, the period is multiplied by 10. (64ns)
38 * At 100Mb link, the period is multiplied by 100. (640ns)
40 * The calculated value allows us to right shift the SYSTIME register
41 * value in order to quickly convert it into a nanosecond clock,
42 * while allowing for the maximum possible adjustment value.
44 * These diagrams are only for the 10Gb link period
47 * +--------------+ +--------------+
48 * X540 | 32 | | 1 | 3 | 28 |
49 * *--------------+ +--------------+
50 * \________ 36 bits ______/ fract
52 * +--------------+ +--------------+
53 * 82599 | 32 | | 8 | 3 | 21 |
54 * *--------------+ +--------------+
55 * \________ 43 bits ______/ fract
57 * The 36 bit X540 SYSTIME overflows every
58 * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
60 * The 43 bit 82599 SYSTIME overflows every
61 * 2^43 * 10^-9 / 3600 = 2.4 hours
63 #define IXGBE_INCVAL_10GB 0x66666666
64 #define IXGBE_INCVAL_1GB 0x40000000
65 #define IXGBE_INCVAL_100 0x50000000
67 #define IXGBE_INCVAL_SHIFT_10GB 28
68 #define IXGBE_INCVAL_SHIFT_1GB 24
69 #define IXGBE_INCVAL_SHIFT_100 21
71 #define IXGBE_INCVAL_SHIFT_82599 7
72 #define IXGBE_INCPER_SHIFT_82599 24
74 #define IXGBE_OVERFLOW_PERIOD (HZ * 30)
75 #define IXGBE_PTP_TX_TIMEOUT (HZ * 15)
77 /* half of a one second clock period, for use with PPS signal. We have to use
78 * this instead of something pre-defined like IXGBE_PTP_PPS_HALF_SECOND, in
79 * order to force at least 64bits of precision for shifting
81 #define IXGBE_PTP_PPS_HALF_SECOND 500000000ULL
83 /* In contrast, the X550 controller has two registers, SYSTIMEH and SYSTIMEL
84 * which contain measurements of seconds and nanoseconds respectively. This
85 * matches the standard linux representation of time in the kernel. In addition,
86 * the X550 also has a SYSTIMER register which represents residue, or
87 * subnanosecond overflow adjustments. To control clock adjustment, the TIMINCA
88 * register is used, but it is unlike the X540 and 82599 devices. TIMINCA
89 * represents units of 2^-32 nanoseconds, and uses 31 bits for this, with the
90 * high bit representing whether the adjustent is positive or negative. Every
91 * clock cycle, the X550 will add 12.5 ns + TIMINCA which can result in a range
92 * of 12 to 13 nanoseconds adjustment. Unlike the 82599 and X540 devices, the
93 * X550's clock for purposes of SYSTIME generation is constant and not dependent
96 * SYSTIMEH SYSTIMEL SYSTIMER
97 * +--------------+ +--------------+ +-------------+
98 * X550 | 32 | | 32 | | 32 |
99 * *--------------+ +--------------+ +-------------+
100 * \____seconds___/ \_nanoseconds_/ \__2^-32 ns__/
102 * This results in a full 96 bits to represent the clock, with 32 bits for
103 * seconds, 32 bits for nanoseconds (largest value is 0d999999999 or just under
104 * 1 second) and an additional 32 bits to measure sub nanosecond adjustments for
105 * underflow of adjustments.
107 * The 32 bits of seconds for the X550 overflows every
108 * 2^32 / ( 365.25 * 24 * 60 * 60 ) = ~136 years.
110 * In order to adjust the clock frequency for the X550, the TIMINCA register is
111 * provided. This register represents a + or minus nearly 0.5 ns adjustment to
112 * the base frequency. It is measured in 2^-32 ns units, with the high bit being
113 * the sign bit. This register enables software to calculate frequency
114 * adjustments and apply them directly to the clock rate.
116 * The math for converting ppb into TIMINCA values is fairly straightforward.
117 * TIMINCA value = ( Base_Frequency * ppb ) / 1000000000ULL
119 * This assumes that ppb is never high enough to create a value bigger than
120 * TIMINCA's 31 bits can store. This is ensured by the stack. Calculating this
121 * value is also simple.
122 * Max ppb = ( Max Adjustment / Base Frequency ) / 1000000000ULL
124 * For the X550, the Max adjustment is +/- 0.5 ns, and the base frequency is
125 * 12.5 nanoseconds. This means that the Max ppb is 39999999
126 * Note: We subtract one in order to ensure no overflow, because the TIMINCA
127 * register can only hold slightly under 0.5 nanoseconds.
129 * Because TIMINCA is measured in 2^-32 ns units, we have to convert 12.5 ns
130 * into 2^-32 units, which is
132 * 12.5 * 2^32 = C80000000
134 * Some revisions of hardware have a faster base frequency than the registers
135 * were defined for. To fix this, we use a timecounter structure with the
136 * proper mult and shift to convert the cycles into nanoseconds of time.
138 #define IXGBE_X550_BASE_PERIOD 0xC80000000ULL
139 #define INCVALUE_MASK 0x7FFFFFFF
140 #define ISGN 0x80000000
141 #define MAX_TIMADJ 0x7FFFFFFF
144 * ixgbe_ptp_setup_sdp_x540
145 * @adapter: private adapter structure
147 * this function enables or disables the clock out feature on SDP0 for
148 * the X540 device. It will create a 1second periodic output that can
149 * be used as the PPS (via an interrupt).
151 * It calculates when the systime will be on an exact second, and then
152 * aligns the start of the PPS signal to that value. The shift is
153 * necessary because it can change based on the link speed.
155 static void ixgbe_ptp_setup_sdp_x540(struct ixgbe_adapter
*adapter
)
157 struct ixgbe_hw
*hw
= &adapter
->hw
;
158 int shift
= adapter
->hw_cc
.shift
;
159 u32 esdp
, tsauxc
, clktiml
, clktimh
, trgttiml
, trgttimh
, rem
;
160 u64 ns
= 0, clock_edge
= 0;
162 /* disable the pin first */
163 IXGBE_WRITE_REG(hw
, IXGBE_TSAUXC
, 0x0);
164 IXGBE_WRITE_FLUSH(hw
);
166 if (!(adapter
->flags2
& IXGBE_FLAG2_PTP_PPS_ENABLED
))
169 esdp
= IXGBE_READ_REG(hw
, IXGBE_ESDP
);
171 /* enable the SDP0 pin as output, and connected to the
172 * native function for Timesync (ClockOut)
174 esdp
|= IXGBE_ESDP_SDP0_DIR
|
175 IXGBE_ESDP_SDP0_NATIVE
;
177 /* enable the Clock Out feature on SDP0, and allow
178 * interrupts to occur when the pin changes
180 tsauxc
= IXGBE_TSAUXC_EN_CLK
|
181 IXGBE_TSAUXC_SYNCLK
|
182 IXGBE_TSAUXC_SDP0_INT
;
184 /* clock period (or pulse length) */
185 clktiml
= (u32
)(IXGBE_PTP_PPS_HALF_SECOND
<< shift
);
186 clktimh
= (u32
)((IXGBE_PTP_PPS_HALF_SECOND
<< shift
) >> 32);
188 /* Account for the cyclecounter wrap-around value by
189 * using the converted ns value of the current time to
190 * check for when the next aligned second would occur.
192 clock_edge
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_SYSTIML
);
193 clock_edge
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_SYSTIMH
) << 32;
194 ns
= timecounter_cyc2time(&adapter
->hw_tc
, clock_edge
);
196 div_u64_rem(ns
, IXGBE_PTP_PPS_HALF_SECOND
, &rem
);
197 clock_edge
+= ((IXGBE_PTP_PPS_HALF_SECOND
- (u64
)rem
) << shift
);
199 /* specify the initial clock start time */
200 trgttiml
= (u32
)clock_edge
;
201 trgttimh
= (u32
)(clock_edge
>> 32);
203 IXGBE_WRITE_REG(hw
, IXGBE_CLKTIML
, clktiml
);
204 IXGBE_WRITE_REG(hw
, IXGBE_CLKTIMH
, clktimh
);
205 IXGBE_WRITE_REG(hw
, IXGBE_TRGTTIML0
, trgttiml
);
206 IXGBE_WRITE_REG(hw
, IXGBE_TRGTTIMH0
, trgttimh
);
208 IXGBE_WRITE_REG(hw
, IXGBE_ESDP
, esdp
);
209 IXGBE_WRITE_REG(hw
, IXGBE_TSAUXC
, tsauxc
);
211 IXGBE_WRITE_FLUSH(hw
);
215 * ixgbe_ptp_read_X550 - read cycle counter value
216 * @hw_cc: cyclecounter structure
218 * This function reads SYSTIME registers. It is called by the cyclecounter
219 * structure to convert from internal representation into nanoseconds. We need
220 * this for X550 since some skews do not have expected clock frequency and
221 * result of SYSTIME is 32bits of "billions of cycles" and 32 bits of
222 * "cycles", rather than seconds and nanoseconds.
224 static u64
ixgbe_ptp_read_X550(const struct cyclecounter
*hw_cc
)
226 struct ixgbe_adapter
*adapter
=
227 container_of(hw_cc
, struct ixgbe_adapter
, hw_cc
);
228 struct ixgbe_hw
*hw
= &adapter
->hw
;
229 struct timespec64 ts
;
231 /* storage is 32 bits of 'billions of cycles' and 32 bits of 'cycles'.
232 * Some revisions of hardware run at a higher frequency and so the
233 * cycles are not guaranteed to be nanoseconds. The timespec64 created
234 * here is used for its math/conversions but does not necessarily
235 * represent nominal time.
237 * It should be noted that this cyclecounter will overflow at a
238 * non-bitmask field since we have to convert our billions of cycles
239 * into an actual cycles count. This results in some possible weird
240 * situations at high cycle counter stamps. However given that 32 bits
241 * of "seconds" is ~138 years this isn't a problem. Even at the
242 * increased frequency of some revisions, this is still ~103 years.
243 * Since the SYSTIME values start at 0 and we never write them, it is
244 * highly unlikely for the cyclecounter to overflow in practice.
246 IXGBE_READ_REG(hw
, IXGBE_SYSTIMR
);
247 ts
.tv_nsec
= IXGBE_READ_REG(hw
, IXGBE_SYSTIML
);
248 ts
.tv_sec
= IXGBE_READ_REG(hw
, IXGBE_SYSTIMH
);
250 return (u64
)timespec64_to_ns(&ts
);
254 * ixgbe_ptp_read_82599 - read raw cycle counter (to be used by time counter)
255 * @cc: the cyclecounter structure
257 * this function reads the cyclecounter registers and is called by the
258 * cyclecounter structure used to construct a ns counter from the
259 * arbitrary fixed point registers
261 static u64
ixgbe_ptp_read_82599(const struct cyclecounter
*cc
)
263 struct ixgbe_adapter
*adapter
=
264 container_of(cc
, struct ixgbe_adapter
, hw_cc
);
265 struct ixgbe_hw
*hw
= &adapter
->hw
;
268 stamp
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_SYSTIML
);
269 stamp
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_SYSTIMH
) << 32;
275 * ixgbe_ptp_convert_to_hwtstamp - convert register value to hw timestamp
276 * @adapter: private adapter structure
277 * @hwtstamp: stack timestamp structure
278 * @timestamp: unsigned 64bit system time value
280 * We need to convert the adapter's RX/TXSTMP registers into a hwtstamp value
281 * which can be used by the stack's ptp functions.
283 * The lock is used to protect consistency of the cyclecounter and the SYSTIME
284 * registers. However, it does not need to protect against the Rx or Tx
285 * timestamp registers, as there can't be a new timestamp until the old one is
286 * unlatched by reading.
288 * In addition to the timestamp in hardware, some controllers need a software
289 * overflow cyclecounter, and this function takes this into account as well.
291 static void ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter
*adapter
,
292 struct skb_shared_hwtstamps
*hwtstamp
,
296 struct timespec64 systime
;
299 memset(hwtstamp
, 0, sizeof(*hwtstamp
));
301 switch (adapter
->hw
.mac
.type
) {
302 /* X550 and later hardware supposedly represent time using a seconds
303 * and nanoseconds counter, instead of raw 64bits nanoseconds. We need
304 * to convert the timestamp into cycles before it can be fed to the
305 * cyclecounter. We need an actual cyclecounter because some revisions
306 * of hardware run at a higher frequency and thus the counter does
307 * not represent seconds/nanoseconds. Instead it can be thought of as
308 * cycles and billions of cycles.
311 case ixgbe_mac_X550EM_x
:
312 case ixgbe_mac_x550em_a
:
313 /* Upper 32 bits represent billions of cycles, lower 32 bits
314 * represent cycles. However, we use timespec64_to_ns for the
315 * correct math even though the units haven't been corrected
318 systime
.tv_sec
= timestamp
>> 32;
319 systime
.tv_nsec
= timestamp
& 0xFFFFFFFF;
321 timestamp
= timespec64_to_ns(&systime
);
327 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
328 ns
= timecounter_cyc2time(&adapter
->hw_tc
, timestamp
);
329 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
331 hwtstamp
->hwtstamp
= ns_to_ktime(ns
);
335 * ixgbe_ptp_adjfreq_82599
336 * @ptp: the ptp clock structure
337 * @ppb: parts per billion adjustment from base
339 * adjust the frequency of the ptp cycle counter by the
340 * indicated ppb from the base frequency.
342 static int ixgbe_ptp_adjfreq_82599(struct ptp_clock_info
*ptp
, s32 ppb
)
344 struct ixgbe_adapter
*adapter
=
345 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
346 struct ixgbe_hw
*hw
= &adapter
->hw
;
357 incval
= READ_ONCE(adapter
->base_incval
);
361 diff
= div_u64(freq
, 1000000000ULL);
363 incval
= neg_adj
? (incval
- diff
) : (incval
+ diff
);
365 switch (hw
->mac
.type
) {
367 if (incval
> 0xFFFFFFFFULL
)
368 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
369 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
, (u32
)incval
);
371 case ixgbe_mac_82599EB
:
372 if (incval
> 0x00FFFFFFULL
)
373 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
374 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
,
375 BIT(IXGBE_INCPER_SHIFT_82599
) |
376 ((u32
)incval
& 0x00FFFFFFUL
));
386 * ixgbe_ptp_adjfreq_X550
387 * @ptp: the ptp clock structure
388 * @ppb: parts per billion adjustment from base
390 * adjust the frequency of the SYSTIME registers by the indicated ppb from base
393 static int ixgbe_ptp_adjfreq_X550(struct ptp_clock_info
*ptp
, s32 ppb
)
395 struct ixgbe_adapter
*adapter
=
396 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
397 struct ixgbe_hw
*hw
= &adapter
->hw
;
399 u64 rate
= IXGBE_X550_BASE_PERIOD
;
407 rate
= div_u64(rate
, 1000000000ULL);
409 /* warn if rate is too large */
410 if (rate
>= INCVALUE_MASK
)
411 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
413 inca
= rate
& INCVALUE_MASK
;
417 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
, inca
);
424 * @ptp: the ptp clock structure
425 * @delta: offset to adjust the cycle counter by
427 * adjust the timer by resetting the timecounter structure.
429 static int ixgbe_ptp_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
431 struct ixgbe_adapter
*adapter
=
432 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
435 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
436 timecounter_adjtime(&adapter
->hw_tc
, delta
);
437 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
439 if (adapter
->ptp_setup_sdp
)
440 adapter
->ptp_setup_sdp(adapter
);
447 * @ptp: the ptp clock structure
448 * @ts: timespec structure to hold the current time value
450 * read the timecounter and return the correct value on ns,
451 * after converting it into a struct timespec.
453 static int ixgbe_ptp_gettime(struct ptp_clock_info
*ptp
, struct timespec64
*ts
)
455 struct ixgbe_adapter
*adapter
=
456 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
460 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
461 ns
= timecounter_read(&adapter
->hw_tc
);
462 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
464 *ts
= ns_to_timespec64(ns
);
471 * @ptp: the ptp clock structure
472 * @ts: the timespec containing the new time for the cycle counter
474 * reset the timecounter to use a new base value instead of the kernel
477 static int ixgbe_ptp_settime(struct ptp_clock_info
*ptp
,
478 const struct timespec64
*ts
)
480 struct ixgbe_adapter
*adapter
=
481 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
483 u64 ns
= timespec64_to_ns(ts
);
485 /* reset the timecounter */
486 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
487 timecounter_init(&adapter
->hw_tc
, &adapter
->hw_cc
, ns
);
488 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
490 if (adapter
->ptp_setup_sdp
)
491 adapter
->ptp_setup_sdp(adapter
);
496 * ixgbe_ptp_feature_enable
497 * @ptp: the ptp clock structure
498 * @rq: the requested feature to change
499 * @on: whether to enable or disable the feature
501 * enable (or disable) ancillary features of the phc subsystem.
502 * our driver only supports the PPS feature on the X540
504 static int ixgbe_ptp_feature_enable(struct ptp_clock_info
*ptp
,
505 struct ptp_clock_request
*rq
, int on
)
507 struct ixgbe_adapter
*adapter
=
508 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
511 * When PPS is enabled, unmask the interrupt for the ClockOut
512 * feature, so that the interrupt handler can send the PPS
513 * event when the clock SDP triggers. Clear mask when PPS is
516 if (rq
->type
!= PTP_CLK_REQ_PPS
|| !adapter
->ptp_setup_sdp
)
520 adapter
->flags2
|= IXGBE_FLAG2_PTP_PPS_ENABLED
;
522 adapter
->flags2
&= ~IXGBE_FLAG2_PTP_PPS_ENABLED
;
524 adapter
->ptp_setup_sdp(adapter
);
529 * ixgbe_ptp_check_pps_event
530 * @adapter: the private adapter structure
532 * This function is called by the interrupt routine when checking for
533 * interrupts. It will check and handle a pps event.
535 void ixgbe_ptp_check_pps_event(struct ixgbe_adapter
*adapter
)
537 struct ixgbe_hw
*hw
= &adapter
->hw
;
538 struct ptp_clock_event event
;
540 event
.type
= PTP_CLOCK_PPS
;
542 /* this check is necessary in case the interrupt was enabled via some
543 * alternative means (ex. debug_fs). Better to check here than
544 * everywhere that calls this function.
546 if (!adapter
->ptp_clock
)
549 switch (hw
->mac
.type
) {
551 ptp_clock_event(adapter
->ptp_clock
, &event
);
559 * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
560 * @adapter: private adapter struct
562 * this watchdog task periodically reads the timecounter
563 * in order to prevent missing when the system time registers wrap
564 * around. This needs to be run approximately twice a minute.
566 void ixgbe_ptp_overflow_check(struct ixgbe_adapter
*adapter
)
568 bool timeout
= time_is_before_jiffies(adapter
->last_overflow_check
+
569 IXGBE_OVERFLOW_PERIOD
);
570 struct timespec64 ts
;
573 ixgbe_ptp_gettime(&adapter
->ptp_caps
, &ts
);
574 adapter
->last_overflow_check
= jiffies
;
579 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
580 * @adapter: private network adapter structure
582 * this watchdog task is scheduled to detect error case where hardware has
583 * dropped an Rx packet that was timestamped when the ring is full. The
584 * particular error is rare but leaves the device in a state unable to timestamp
585 * any future packets.
587 void ixgbe_ptp_rx_hang(struct ixgbe_adapter
*adapter
)
589 struct ixgbe_hw
*hw
= &adapter
->hw
;
590 u32 tsyncrxctl
= IXGBE_READ_REG(hw
, IXGBE_TSYNCRXCTL
);
591 struct ixgbe_ring
*rx_ring
;
592 unsigned long rx_event
;
595 /* if we don't have a valid timestamp in the registers, just update the
596 * timeout counter and exit
598 if (!(tsyncrxctl
& IXGBE_TSYNCRXCTL_VALID
)) {
599 adapter
->last_rx_ptp_check
= jiffies
;
603 /* determine the most recent watchdog or rx_timestamp event */
604 rx_event
= adapter
->last_rx_ptp_check
;
605 for (n
= 0; n
< adapter
->num_rx_queues
; n
++) {
606 rx_ring
= adapter
->rx_ring
[n
];
607 if (time_after(rx_ring
->last_rx_timestamp
, rx_event
))
608 rx_event
= rx_ring
->last_rx_timestamp
;
611 /* only need to read the high RXSTMP register to clear the lock */
612 if (time_is_before_jiffies(rx_event
+ 5 * HZ
)) {
613 IXGBE_READ_REG(hw
, IXGBE_RXSTMPH
);
614 adapter
->last_rx_ptp_check
= jiffies
;
616 adapter
->rx_hwtstamp_cleared
++;
617 e_warn(drv
, "clearing RX Timestamp hang\n");
622 * ixgbe_ptp_clear_tx_timestamp - utility function to clear Tx timestamp state
623 * @adapter: the private adapter structure
625 * This function should be called whenever the state related to a Tx timestamp
626 * needs to be cleared. This helps ensure that all related bits are reset for
627 * the next Tx timestamp event.
629 static void ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter
*adapter
)
631 struct ixgbe_hw
*hw
= &adapter
->hw
;
633 IXGBE_READ_REG(hw
, IXGBE_TXSTMPH
);
634 if (adapter
->ptp_tx_skb
) {
635 dev_kfree_skb_any(adapter
->ptp_tx_skb
);
636 adapter
->ptp_tx_skb
= NULL
;
638 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS
, &adapter
->state
);
642 * ixgbe_ptp_tx_hang - detect error case where Tx timestamp never finishes
643 * @adapter: private network adapter structure
645 void ixgbe_ptp_tx_hang(struct ixgbe_adapter
*adapter
)
647 bool timeout
= time_is_before_jiffies(adapter
->ptp_tx_start
+
648 IXGBE_PTP_TX_TIMEOUT
);
650 if (!adapter
->ptp_tx_skb
)
653 if (!test_bit(__IXGBE_PTP_TX_IN_PROGRESS
, &adapter
->state
))
656 /* If we haven't received a timestamp within the timeout, it is
657 * reasonable to assume that it will never occur, so we can unlock the
658 * timestamp bit when this occurs.
661 cancel_work_sync(&adapter
->ptp_tx_work
);
662 ixgbe_ptp_clear_tx_timestamp(adapter
);
663 adapter
->tx_hwtstamp_timeouts
++;
664 e_warn(drv
, "clearing Tx timestamp hang\n");
669 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
670 * @adapter: the private adapter struct
672 * if the timestamp is valid, we convert it into the timecounter ns
673 * value, then store that result into the shhwtstamps structure which
674 * is passed up the network stack
676 static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter
*adapter
)
678 struct sk_buff
*skb
= adapter
->ptp_tx_skb
;
679 struct ixgbe_hw
*hw
= &adapter
->hw
;
680 struct skb_shared_hwtstamps shhwtstamps
;
683 regval
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_TXSTMPL
);
684 regval
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_TXSTMPH
) << 32;
685 ixgbe_ptp_convert_to_hwtstamp(adapter
, &shhwtstamps
, regval
);
687 /* Handle cleanup of the ptp_tx_skb ourselves, and unlock the state
688 * bit prior to notifying the stack via skb_tstamp_tx(). This prevents
689 * well behaved applications from attempting to timestamp again prior
690 * to the lock bit being clear.
692 adapter
->ptp_tx_skb
= NULL
;
693 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS
, &adapter
->state
);
695 /* Notify the stack and then free the skb after we've unlocked */
696 skb_tstamp_tx(skb
, &shhwtstamps
);
697 dev_kfree_skb_any(skb
);
701 * ixgbe_ptp_tx_hwtstamp_work
702 * @work: pointer to the work struct
704 * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
705 * timestamp has been taken for the current skb. It is necessary, because the
706 * descriptor's "done" bit does not correlate with the timestamp event.
708 static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct
*work
)
710 struct ixgbe_adapter
*adapter
= container_of(work
, struct ixgbe_adapter
,
712 struct ixgbe_hw
*hw
= &adapter
->hw
;
713 bool timeout
= time_is_before_jiffies(adapter
->ptp_tx_start
+
714 IXGBE_PTP_TX_TIMEOUT
);
717 /* we have to have a valid skb to poll for a timestamp */
718 if (!adapter
->ptp_tx_skb
) {
719 ixgbe_ptp_clear_tx_timestamp(adapter
);
723 /* stop polling once we have a valid timestamp */
724 tsynctxctl
= IXGBE_READ_REG(hw
, IXGBE_TSYNCTXCTL
);
725 if (tsynctxctl
& IXGBE_TSYNCTXCTL_VALID
) {
726 ixgbe_ptp_tx_hwtstamp(adapter
);
731 ixgbe_ptp_clear_tx_timestamp(adapter
);
732 adapter
->tx_hwtstamp_timeouts
++;
733 e_warn(drv
, "clearing Tx Timestamp hang\n");
735 /* reschedule to keep checking if it's not available yet */
736 schedule_work(&adapter
->ptp_tx_work
);
741 * ixgbe_ptp_rx_pktstamp - utility function to get RX time stamp from buffer
742 * @q_vector: structure containing interrupt and ring information
745 * This function will be called by the Rx routine of the timestamp for this
746 * packet is stored in the buffer. The value is stored in little endian format
747 * starting at the end of the packet data.
749 void ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector
*q_vector
,
754 /* copy the bits out of the skb, and then trim the skb length */
755 skb_copy_bits(skb
, skb
->len
- IXGBE_TS_HDR_LEN
, ®val
,
757 __pskb_trim(skb
, skb
->len
- IXGBE_TS_HDR_LEN
);
759 /* The timestamp is recorded in little endian format, and is stored at
760 * the end of the packet.
762 * DWORD: N N + 1 N + 2
763 * Field: End of Packet SYSTIMH SYSTIML
765 ixgbe_ptp_convert_to_hwtstamp(q_vector
->adapter
, skb_hwtstamps(skb
),
766 le64_to_cpu(regval
));
770 * ixgbe_ptp_rx_rgtstamp - utility function which checks for RX time stamp
771 * @q_vector: structure containing interrupt and ring information
772 * @skb: particular skb to send timestamp with
774 * if the timestamp is valid, we convert it into the timecounter ns
775 * value, then store that result into the shhwtstamps structure which
776 * is passed up the network stack
778 void ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector
*q_vector
,
781 struct ixgbe_adapter
*adapter
;
786 /* we cannot process timestamps on a ring without a q_vector */
787 if (!q_vector
|| !q_vector
->adapter
)
790 adapter
= q_vector
->adapter
;
793 /* Read the tsyncrxctl register afterwards in order to prevent taking an
794 * I/O hit on every packet.
797 tsyncrxctl
= IXGBE_READ_REG(hw
, IXGBE_TSYNCRXCTL
);
798 if (!(tsyncrxctl
& IXGBE_TSYNCRXCTL_VALID
))
801 regval
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_RXSTMPL
);
802 regval
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_RXSTMPH
) << 32;
804 ixgbe_ptp_convert_to_hwtstamp(adapter
, skb_hwtstamps(skb
), regval
);
807 int ixgbe_ptp_get_ts_config(struct ixgbe_adapter
*adapter
, struct ifreq
*ifr
)
809 struct hwtstamp_config
*config
= &adapter
->tstamp_config
;
811 return copy_to_user(ifr
->ifr_data
, config
,
812 sizeof(*config
)) ? -EFAULT
: 0;
816 * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
817 * @adapter: the private ixgbe adapter structure
818 * @config: the hwtstamp configuration requested
820 * Outgoing time stamping can be enabled and disabled. Play nice and
821 * disable it when requested, although it shouldn't cause any overhead
822 * when no packet needs it. At most one packet in the queue may be
823 * marked for time stamping, otherwise it would be impossible to tell
824 * for sure to which packet the hardware time stamp belongs.
826 * Incoming time stamping has to be configured via the hardware
827 * filters. Not all combinations are supported, in particular event
828 * type has to be specified. Matching the kind of event packet is
829 * not supported, with the exception of "all V2 events regardless of
832 * Since hardware always timestamps Path delay packets when timestamping V2
833 * packets, regardless of the type specified in the register, only use V2
834 * Event mode. This more accurately tells the user what the hardware is going
837 * Note: this may modify the hwtstamp configuration towards a more general
838 * mode, if required to support the specifically requested mode.
840 static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter
*adapter
,
841 struct hwtstamp_config
*config
)
843 struct ixgbe_hw
*hw
= &adapter
->hw
;
844 u32 tsync_tx_ctl
= IXGBE_TSYNCTXCTL_ENABLED
;
845 u32 tsync_rx_ctl
= IXGBE_TSYNCRXCTL_ENABLED
;
846 u32 tsync_rx_mtrl
= PTP_EV_PORT
<< 16;
850 /* reserved for future extensions */
854 switch (config
->tx_type
) {
855 case HWTSTAMP_TX_OFF
:
863 switch (config
->rx_filter
) {
864 case HWTSTAMP_FILTER_NONE
:
867 adapter
->flags
&= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED
|
868 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER
);
870 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
871 tsync_rx_ctl
|= IXGBE_TSYNCRXCTL_TYPE_L4_V1
;
872 tsync_rx_mtrl
|= IXGBE_RXMTRL_V1_SYNC_MSG
;
873 adapter
->flags
|= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED
|
874 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER
);
876 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
877 tsync_rx_ctl
|= IXGBE_TSYNCRXCTL_TYPE_L4_V1
;
878 tsync_rx_mtrl
|= IXGBE_RXMTRL_V1_DELAY_REQ_MSG
;
879 adapter
->flags
|= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED
|
880 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER
);
882 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
883 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
884 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
885 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
886 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
887 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
888 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
889 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
890 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
891 tsync_rx_ctl
|= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2
;
893 config
->rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
894 adapter
->flags
|= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED
|
895 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER
);
897 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
898 case HWTSTAMP_FILTER_NTP_ALL
:
899 case HWTSTAMP_FILTER_ALL
:
900 /* The X550 controller is capable of timestamping all packets,
901 * which allows it to accept any filter.
903 if (hw
->mac
.type
>= ixgbe_mac_X550
) {
904 tsync_rx_ctl
|= IXGBE_TSYNCRXCTL_TYPE_ALL
;
905 config
->rx_filter
= HWTSTAMP_FILTER_ALL
;
906 adapter
->flags
|= IXGBE_FLAG_RX_HWTSTAMP_ENABLED
;
912 * register RXMTRL must be set in order to do V1 packets,
913 * therefore it is not possible to time stamp both V1 Sync and
914 * Delay_Req messages and hardware does not support
915 * timestamping all packets => return error
917 adapter
->flags
&= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED
|
918 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER
);
919 config
->rx_filter
= HWTSTAMP_FILTER_NONE
;
923 if (hw
->mac
.type
== ixgbe_mac_82598EB
) {
924 adapter
->flags
&= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED
|
925 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER
);
926 if (tsync_rx_ctl
| tsync_tx_ctl
)
931 /* Per-packet timestamping only works if the filter is set to all
932 * packets. Since this is desired, always timestamp all packets as long
933 * as any Rx filter was configured.
935 switch (hw
->mac
.type
) {
937 case ixgbe_mac_X550EM_x
:
938 case ixgbe_mac_x550em_a
:
939 /* enable timestamping all packets only if at least some
940 * packets were requested. Otherwise, play nice and disable
943 if (config
->rx_filter
== HWTSTAMP_FILTER_NONE
)
946 tsync_rx_ctl
= IXGBE_TSYNCRXCTL_ENABLED
|
947 IXGBE_TSYNCRXCTL_TYPE_ALL
|
948 IXGBE_TSYNCRXCTL_TSIP_UT_EN
;
949 config
->rx_filter
= HWTSTAMP_FILTER_ALL
;
950 adapter
->flags
|= IXGBE_FLAG_RX_HWTSTAMP_ENABLED
;
951 adapter
->flags
&= ~IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER
;
958 /* define ethertype filter for timestamping L2 packets */
960 IXGBE_WRITE_REG(hw
, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588
),
961 (IXGBE_ETQF_FILTER_EN
| /* enable filter */
962 IXGBE_ETQF_1588
| /* enable timestamping */
963 ETH_P_1588
)); /* 1588 eth protocol type */
965 IXGBE_WRITE_REG(hw
, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588
), 0);
967 /* enable/disable TX */
968 regval
= IXGBE_READ_REG(hw
, IXGBE_TSYNCTXCTL
);
969 regval
&= ~IXGBE_TSYNCTXCTL_ENABLED
;
970 regval
|= tsync_tx_ctl
;
971 IXGBE_WRITE_REG(hw
, IXGBE_TSYNCTXCTL
, regval
);
973 /* enable/disable RX */
974 regval
= IXGBE_READ_REG(hw
, IXGBE_TSYNCRXCTL
);
975 regval
&= ~(IXGBE_TSYNCRXCTL_ENABLED
| IXGBE_TSYNCRXCTL_TYPE_MASK
);
976 regval
|= tsync_rx_ctl
;
977 IXGBE_WRITE_REG(hw
, IXGBE_TSYNCRXCTL
, regval
);
979 /* define which PTP packets are time stamped */
980 IXGBE_WRITE_REG(hw
, IXGBE_RXMTRL
, tsync_rx_mtrl
);
982 IXGBE_WRITE_FLUSH(hw
);
984 /* clear TX/RX time stamp registers, just to be sure */
985 ixgbe_ptp_clear_tx_timestamp(adapter
);
986 IXGBE_READ_REG(hw
, IXGBE_RXSTMPH
);
992 * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
993 * @adapter: pointer to adapter struct
996 * Set hardware to requested mode. If unsupported, return an error with no
997 * changes. Otherwise, store the mode for future reference.
999 int ixgbe_ptp_set_ts_config(struct ixgbe_adapter
*adapter
, struct ifreq
*ifr
)
1001 struct hwtstamp_config config
;
1004 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
1007 err
= ixgbe_ptp_set_timestamp_mode(adapter
, &config
);
1011 /* save these settings for future reference */
1012 memcpy(&adapter
->tstamp_config
, &config
,
1013 sizeof(adapter
->tstamp_config
));
1015 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)) ?
1019 static void ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter
*adapter
,
1020 u32
*shift
, u32
*incval
)
1023 * Scale the NIC cycle counter by a large factor so that
1024 * relatively small corrections to the frequency can be added
1025 * or subtracted. The drawbacks of a large factor include
1026 * (a) the clock register overflows more quickly, (b) the cycle
1027 * counter structure must be able to convert the systime value
1028 * to nanoseconds using only a multiplier and a right-shift,
1029 * and (c) the value must fit within the timinca register space
1030 * => math based on internal DMA clock rate and available bits
1032 * Note that when there is no link, internal DMA clock is same as when
1033 * link speed is 10Gb. Set the registers correctly even when link is
1034 * down to preserve the clock setting
1036 switch (adapter
->link_speed
) {
1037 case IXGBE_LINK_SPEED_100_FULL
:
1038 *shift
= IXGBE_INCVAL_SHIFT_100
;
1039 *incval
= IXGBE_INCVAL_100
;
1041 case IXGBE_LINK_SPEED_1GB_FULL
:
1042 *shift
= IXGBE_INCVAL_SHIFT_1GB
;
1043 *incval
= IXGBE_INCVAL_1GB
;
1045 case IXGBE_LINK_SPEED_10GB_FULL
:
1047 *shift
= IXGBE_INCVAL_SHIFT_10GB
;
1048 *incval
= IXGBE_INCVAL_10GB
;
1054 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
1055 * @adapter: pointer to the adapter structure
1057 * This function should be called to set the proper values for the TIMINCA
1058 * register and tell the cyclecounter structure what the tick rate of SYSTIME
1059 * is. It does not directly modify SYSTIME registers or the timecounter
1060 * structure. It should be called whenever a new TIMINCA value is necessary,
1061 * such as during initialization or when the link speed changes.
1063 void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter
*adapter
)
1065 struct ixgbe_hw
*hw
= &adapter
->hw
;
1066 struct cyclecounter cc
;
1067 unsigned long flags
;
1072 /* For some of the boards below this mask is technically incorrect.
1073 * The timestamp mask overflows at approximately 61bits. However the
1074 * particular hardware does not overflow on an even bitmask value.
1075 * Instead, it overflows due to conversion of upper 32bits billions of
1076 * cycles. Timecounters are not really intended for this purpose so
1077 * they do not properly function if the overflow point isn't 2^N-1.
1078 * However, the actual SYSTIME values in question take ~138 years to
1079 * overflow. In practice this means they won't actually overflow. A
1080 * proper fix to this problem would require modification of the
1081 * timecounter delta calculations.
1083 cc
.mask
= CLOCKSOURCE_MASK(64);
1087 switch (hw
->mac
.type
) {
1088 case ixgbe_mac_X550EM_x
:
1089 /* SYSTIME assumes X550EM_x board frequency is 300Mhz, and is
1090 * designed to represent seconds and nanoseconds when this is
1091 * the case. However, some revisions of hardware have a 400Mhz
1092 * clock and we have to compensate for this frequency
1093 * variation using corrected mult and shift values.
1095 fuse0
= IXGBE_READ_REG(hw
, IXGBE_FUSES0_GROUP(0));
1096 if (!(fuse0
& IXGBE_FUSES0_300MHZ
)) {
1101 case ixgbe_mac_x550em_a
:
1102 case ixgbe_mac_X550
:
1103 cc
.read
= ixgbe_ptp_read_X550
;
1105 /* enable SYSTIME counter */
1106 IXGBE_WRITE_REG(hw
, IXGBE_SYSTIMR
, 0);
1107 IXGBE_WRITE_REG(hw
, IXGBE_SYSTIML
, 0);
1108 IXGBE_WRITE_REG(hw
, IXGBE_SYSTIMH
, 0);
1109 tsauxc
= IXGBE_READ_REG(hw
, IXGBE_TSAUXC
);
1110 IXGBE_WRITE_REG(hw
, IXGBE_TSAUXC
,
1111 tsauxc
& ~IXGBE_TSAUXC_DISABLE_SYSTIME
);
1112 IXGBE_WRITE_REG(hw
, IXGBE_TSIM
, IXGBE_TSIM_TXTS
);
1113 IXGBE_WRITE_REG(hw
, IXGBE_EIMS
, IXGBE_EIMS_TIMESYNC
);
1115 IXGBE_WRITE_FLUSH(hw
);
1117 case ixgbe_mac_X540
:
1118 cc
.read
= ixgbe_ptp_read_82599
;
1120 ixgbe_ptp_link_speed_adjust(adapter
, &cc
.shift
, &incval
);
1121 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
, incval
);
1123 case ixgbe_mac_82599EB
:
1124 cc
.read
= ixgbe_ptp_read_82599
;
1126 ixgbe_ptp_link_speed_adjust(adapter
, &cc
.shift
, &incval
);
1127 incval
>>= IXGBE_INCVAL_SHIFT_82599
;
1128 cc
.shift
-= IXGBE_INCVAL_SHIFT_82599
;
1129 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
,
1130 BIT(IXGBE_INCPER_SHIFT_82599
) | incval
);
1133 /* other devices aren't supported */
1137 /* update the base incval used to calculate frequency adjustment */
1138 WRITE_ONCE(adapter
->base_incval
, incval
);
1141 /* need lock to prevent incorrect read while modifying cyclecounter */
1142 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
1143 memcpy(&adapter
->hw_cc
, &cc
, sizeof(adapter
->hw_cc
));
1144 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
1149 * @adapter: the ixgbe private board structure
1151 * When the MAC resets, all the hardware bits for timesync are reset. This
1152 * function is used to re-enable the device for PTP based on current settings.
1153 * We do lose the current clock time, so just reset the cyclecounter to the
1154 * system real clock time.
1156 * This function will maintain hwtstamp_config settings, and resets the SDP
1157 * output if it was enabled.
1159 void ixgbe_ptp_reset(struct ixgbe_adapter
*adapter
)
1161 struct ixgbe_hw
*hw
= &adapter
->hw
;
1162 unsigned long flags
;
1164 /* reset the hardware timestamping mode */
1165 ixgbe_ptp_set_timestamp_mode(adapter
, &adapter
->tstamp_config
);
1167 /* 82598 does not support PTP */
1168 if (hw
->mac
.type
== ixgbe_mac_82598EB
)
1171 ixgbe_ptp_start_cyclecounter(adapter
);
1173 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
1174 timecounter_init(&adapter
->hw_tc
, &adapter
->hw_cc
,
1175 ktime_to_ns(ktime_get_real()));
1176 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
1178 adapter
->last_overflow_check
= jiffies
;
1180 /* Now that the shift has been calculated and the systime
1181 * registers reset, (re-)enable the Clock out feature
1183 if (adapter
->ptp_setup_sdp
)
1184 adapter
->ptp_setup_sdp(adapter
);
1188 * ixgbe_ptp_create_clock
1189 * @adapter: the ixgbe private adapter structure
1191 * This function performs setup of the user entry point function table and
1192 * initializes the PTP clock device, which is used to access the clock-like
1193 * features of the PTP core. It will be called by ixgbe_ptp_init, and may
1194 * reuse a previously initialized clock (such as during a suspend/resume
1197 static long ixgbe_ptp_create_clock(struct ixgbe_adapter
*adapter
)
1199 struct net_device
*netdev
= adapter
->netdev
;
1202 /* do nothing if we already have a clock device */
1203 if (!IS_ERR_OR_NULL(adapter
->ptp_clock
))
1206 switch (adapter
->hw
.mac
.type
) {
1207 case ixgbe_mac_X540
:
1208 snprintf(adapter
->ptp_caps
.name
,
1209 sizeof(adapter
->ptp_caps
.name
),
1210 "%s", netdev
->name
);
1211 adapter
->ptp_caps
.owner
= THIS_MODULE
;
1212 adapter
->ptp_caps
.max_adj
= 250000000;
1213 adapter
->ptp_caps
.n_alarm
= 0;
1214 adapter
->ptp_caps
.n_ext_ts
= 0;
1215 adapter
->ptp_caps
.n_per_out
= 0;
1216 adapter
->ptp_caps
.pps
= 1;
1217 adapter
->ptp_caps
.adjfreq
= ixgbe_ptp_adjfreq_82599
;
1218 adapter
->ptp_caps
.adjtime
= ixgbe_ptp_adjtime
;
1219 adapter
->ptp_caps
.gettime64
= ixgbe_ptp_gettime
;
1220 adapter
->ptp_caps
.settime64
= ixgbe_ptp_settime
;
1221 adapter
->ptp_caps
.enable
= ixgbe_ptp_feature_enable
;
1222 adapter
->ptp_setup_sdp
= ixgbe_ptp_setup_sdp_x540
;
1224 case ixgbe_mac_82599EB
:
1225 snprintf(adapter
->ptp_caps
.name
,
1226 sizeof(adapter
->ptp_caps
.name
),
1227 "%s", netdev
->name
);
1228 adapter
->ptp_caps
.owner
= THIS_MODULE
;
1229 adapter
->ptp_caps
.max_adj
= 250000000;
1230 adapter
->ptp_caps
.n_alarm
= 0;
1231 adapter
->ptp_caps
.n_ext_ts
= 0;
1232 adapter
->ptp_caps
.n_per_out
= 0;
1233 adapter
->ptp_caps
.pps
= 0;
1234 adapter
->ptp_caps
.adjfreq
= ixgbe_ptp_adjfreq_82599
;
1235 adapter
->ptp_caps
.adjtime
= ixgbe_ptp_adjtime
;
1236 adapter
->ptp_caps
.gettime64
= ixgbe_ptp_gettime
;
1237 adapter
->ptp_caps
.settime64
= ixgbe_ptp_settime
;
1238 adapter
->ptp_caps
.enable
= ixgbe_ptp_feature_enable
;
1240 case ixgbe_mac_X550
:
1241 case ixgbe_mac_X550EM_x
:
1242 case ixgbe_mac_x550em_a
:
1243 snprintf(adapter
->ptp_caps
.name
, 16, "%s", netdev
->name
);
1244 adapter
->ptp_caps
.owner
= THIS_MODULE
;
1245 adapter
->ptp_caps
.max_adj
= 30000000;
1246 adapter
->ptp_caps
.n_alarm
= 0;
1247 adapter
->ptp_caps
.n_ext_ts
= 0;
1248 adapter
->ptp_caps
.n_per_out
= 0;
1249 adapter
->ptp_caps
.pps
= 0;
1250 adapter
->ptp_caps
.adjfreq
= ixgbe_ptp_adjfreq_X550
;
1251 adapter
->ptp_caps
.adjtime
= ixgbe_ptp_adjtime
;
1252 adapter
->ptp_caps
.gettime64
= ixgbe_ptp_gettime
;
1253 adapter
->ptp_caps
.settime64
= ixgbe_ptp_settime
;
1254 adapter
->ptp_caps
.enable
= ixgbe_ptp_feature_enable
;
1255 adapter
->ptp_setup_sdp
= NULL
;
1258 adapter
->ptp_clock
= NULL
;
1259 adapter
->ptp_setup_sdp
= NULL
;
1263 adapter
->ptp_clock
= ptp_clock_register(&adapter
->ptp_caps
,
1264 &adapter
->pdev
->dev
);
1265 if (IS_ERR(adapter
->ptp_clock
)) {
1266 err
= PTR_ERR(adapter
->ptp_clock
);
1267 adapter
->ptp_clock
= NULL
;
1268 e_dev_err("ptp_clock_register failed\n");
1270 } else if (adapter
->ptp_clock
)
1271 e_dev_info("registered PHC device on %s\n", netdev
->name
);
1273 /* set default timestamp mode to disabled here. We do this in
1274 * create_clock instead of init, because we don't want to override the
1275 * previous settings during a resume cycle.
1277 adapter
->tstamp_config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
1278 adapter
->tstamp_config
.tx_type
= HWTSTAMP_TX_OFF
;
1285 * @adapter: the ixgbe private adapter structure
1287 * This function performs the required steps for enabling PTP
1288 * support. If PTP support has already been loaded it simply calls the
1289 * cyclecounter init routine and exits.
1291 void ixgbe_ptp_init(struct ixgbe_adapter
*adapter
)
1293 /* initialize the spin lock first since we can't control when a user
1294 * will call the entry functions once we have initialized the clock
1297 spin_lock_init(&adapter
->tmreg_lock
);
1299 /* obtain a PTP device, or re-use an existing device */
1300 if (ixgbe_ptp_create_clock(adapter
))
1303 /* we have a clock so we can initialize work now */
1304 INIT_WORK(&adapter
->ptp_tx_work
, ixgbe_ptp_tx_hwtstamp_work
);
1306 /* reset the PTP related hardware bits */
1307 ixgbe_ptp_reset(adapter
);
1309 /* enter the IXGBE_PTP_RUNNING state */
1310 set_bit(__IXGBE_PTP_RUNNING
, &adapter
->state
);
1316 * ixgbe_ptp_suspend - stop PTP work items
1317 * @adapter: pointer to adapter struct
1319 * this function suspends PTP activity, and prevents more PTP work from being
1320 * generated, but does not destroy the PTP clock device.
1322 void ixgbe_ptp_suspend(struct ixgbe_adapter
*adapter
)
1324 /* Leave the IXGBE_PTP_RUNNING state. */
1325 if (!test_and_clear_bit(__IXGBE_PTP_RUNNING
, &adapter
->state
))
1328 adapter
->flags2
&= ~IXGBE_FLAG2_PTP_PPS_ENABLED
;
1329 if (adapter
->ptp_setup_sdp
)
1330 adapter
->ptp_setup_sdp(adapter
);
1332 /* ensure that we cancel any pending PTP Tx work item in progress */
1333 cancel_work_sync(&adapter
->ptp_tx_work
);
1334 ixgbe_ptp_clear_tx_timestamp(adapter
);
1338 * ixgbe_ptp_stop - close the PTP device
1339 * @adapter: pointer to adapter struct
1341 * completely destroy the PTP device, should only be called when the device is
1342 * being fully closed.
1344 void ixgbe_ptp_stop(struct ixgbe_adapter
*adapter
)
1346 /* first, suspend PTP activity */
1347 ixgbe_ptp_suspend(adapter
);
1349 /* disable the PTP clock device */
1350 if (adapter
->ptp_clock
) {
1351 ptp_clock_unregister(adapter
->ptp_clock
);
1352 adapter
->ptp_clock
= NULL
;
1353 e_dev_info("removed PHC on %s\n",
1354 adapter
->netdev
->name
);