1 /*******************************************************************************
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2012 Intel Corporation.
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.
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
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.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
28 #include <linux/export.h>
29 #include <linux/ptp_classify.h>
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.
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
53 * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
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
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)
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.
67 * These diagrams are only for the 10Gb link period
70 * +--------------+ +--------------+
71 * X540 | 32 | | 1 | 3 | 28 |
72 * *--------------+ +--------------+
73 * \________ 36 bits ______/ fract
75 * +--------------+ +--------------+
76 * 82599 | 32 | | 8 | 3 | 21 |
77 * *--------------+ +--------------+
78 * \________ 43 bits ______/ fract
80 * The 36 bit X540 SYSTIME overflows every
81 * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
83 * The 43 bit 82599 SYSTIME overflows every
84 * 2^43 * 10^-9 / 3600 = 2.4 hours
86 #define IXGBE_INCVAL_10GB 0x66666666
87 #define IXGBE_INCVAL_1GB 0x40000000
88 #define IXGBE_INCVAL_100 0x50000000
90 #define IXGBE_INCVAL_SHIFT_10GB 28
91 #define IXGBE_INCVAL_SHIFT_1GB 24
92 #define IXGBE_INCVAL_SHIFT_100 21
94 #define IXGBE_INCVAL_SHIFT_82599 7
95 #define IXGBE_INCPER_SHIFT_82599 24
96 #define IXGBE_MAX_TIMEADJ_VALUE 0x7FFFFFFFFFFFFFFFULL
98 #define IXGBE_OVERFLOW_PERIOD (HZ * 30)
100 #ifndef NSECS_PER_SEC
101 #define NSECS_PER_SEC 1000000000ULL
105 * ixgbe_ptp_setup_sdp
106 * @hw: the hardware private structure
108 * 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).
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.
116 static void ixgbe_ptp_setup_sdp(struct ixgbe_adapter
*adapter
)
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;
123 if ((adapter
->flags2
& IXGBE_FLAG2_PTP_PPS_ENABLED
) &&
124 (hw
->mac
.type
== ixgbe_mac_X540
)) {
126 /* disable the pin first */
127 IXGBE_WRITE_REG(hw
, IXGBE_TSAUXC
, 0x0);
128 IXGBE_WRITE_FLUSH(hw
);
130 esdp
= IXGBE_READ_REG(hw
, IXGBE_ESDP
);
133 * enable the SDP0 pin as output, and connected to the
134 * native function for Timesync (ClockOut)
136 esdp
|= (IXGBE_ESDP_SDP0_DIR
|
137 IXGBE_ESDP_SDP0_NATIVE
);
140 * enable the Clock Out feature on SDP0, and allow
141 * interrupts to occur when the pin changes
143 tsauxc
= (IXGBE_TSAUXC_EN_CLK
|
144 IXGBE_TSAUXC_SYNCLK
|
145 IXGBE_TSAUXC_SDP0_INT
);
147 /* clock period (or pulse length) */
148 clktiml
= (u32
)(NSECS_PER_SEC
<< shift
);
149 clktimh
= (u32
)((NSECS_PER_SEC
<< shift
) >> 32);
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.
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
);
160 div_u64_rem(ns
, NSECS_PER_SEC
, &rem
);
161 clock_edge
+= ((NSECS_PER_SEC
- (u64
)rem
) << shift
);
163 /* specify the initial clock start time */
164 trgttiml
= (u32
)clock_edge
;
165 trgttimh
= (u32
)(clock_edge
>> 32);
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
);
172 IXGBE_WRITE_REG(hw
, IXGBE_ESDP
, esdp
);
173 IXGBE_WRITE_REG(hw
, IXGBE_TSAUXC
, tsauxc
);
175 IXGBE_WRITE_REG(hw
, IXGBE_TSAUXC
, 0x0);
178 IXGBE_WRITE_FLUSH(hw
);
182 * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
183 * @cc: the cyclecounter structure
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
189 static cycle_t
ixgbe_ptp_read(const struct cyclecounter
*cc
)
191 struct ixgbe_adapter
*adapter
=
192 container_of(cc
, struct ixgbe_adapter
, cc
);
193 struct ixgbe_hw
*hw
= &adapter
->hw
;
196 stamp
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_SYSTIML
);
197 stamp
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_SYSTIMH
) << 32;
204 * @ptp: the ptp clock structure
205 * @ppb: parts per billion adjustment from base
207 * adjust the frequency of the ptp cycle counter by the
208 * indicated ppb from the base frequency.
210 static int ixgbe_ptp_adjfreq(struct ptp_clock_info
*ptp
, s32 ppb
)
212 struct ixgbe_adapter
*adapter
=
213 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
214 struct ixgbe_hw
*hw
= &adapter
->hw
;
225 incval
= ACCESS_ONCE(adapter
->base_incval
);
229 diff
= div_u64(freq
, 1000000000ULL);
231 incval
= neg_adj
? (incval
- diff
) : (incval
+ diff
);
233 switch (hw
->mac
.type
) {
235 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
, incval
);
237 case ixgbe_mac_82599EB
:
238 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
,
239 (1 << IXGBE_INCPER_SHIFT_82599
) |
251 * @ptp: the ptp clock structure
252 * @delta: offset to adjust the cycle counter by
254 * adjust the timer by resetting the timecounter structure.
256 static int ixgbe_ptp_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
258 struct ixgbe_adapter
*adapter
=
259 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
263 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
265 now
= timecounter_read(&adapter
->tc
);
268 /* reset the timecounter */
269 timecounter_init(&adapter
->tc
,
273 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
275 ixgbe_ptp_setup_sdp(adapter
);
282 * @ptp: the ptp clock structure
283 * @ts: timespec structure to hold the current time value
285 * read the timecounter and return the correct value on ns,
286 * after converting it into a struct timespec.
288 static int ixgbe_ptp_gettime(struct ptp_clock_info
*ptp
, struct timespec
*ts
)
290 struct ixgbe_adapter
*adapter
=
291 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
296 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
297 ns
= timecounter_read(&adapter
->tc
);
298 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
300 ts
->tv_sec
= div_u64_rem(ns
, 1000000000ULL, &remainder
);
301 ts
->tv_nsec
= remainder
;
308 * @ptp: the ptp clock structure
309 * @ts: the timespec containing the new time for the cycle counter
311 * reset the timecounter to use a new base value instead of the kernel
314 static int ixgbe_ptp_settime(struct ptp_clock_info
*ptp
,
315 const struct timespec
*ts
)
317 struct ixgbe_adapter
*adapter
=
318 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
322 ns
= ts
->tv_sec
* 1000000000ULL;
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
);
330 ixgbe_ptp_setup_sdp(adapter
);
336 * @ptp: the ptp clock structure
337 * @rq: the requested feature to change
338 * @on: whether to enable or disable the feature
340 * enable (or disable) ancillary features of the phc subsystem.
341 * our driver only supports the PPS feature on the X540
343 static int ixgbe_ptp_enable(struct ptp_clock_info
*ptp
,
344 struct ptp_clock_request
*rq
, int on
)
346 struct ixgbe_adapter
*adapter
=
347 container_of(ptp
, struct ixgbe_adapter
, ptp_caps
);
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
355 if (rq
->type
== PTP_CLK_REQ_PPS
) {
356 switch (adapter
->hw
.mac
.type
) {
359 adapter
->flags2
|= IXGBE_FLAG2_PTP_PPS_ENABLED
;
361 adapter
->flags2
&= ~IXGBE_FLAG2_PTP_PPS_ENABLED
;
363 ixgbe_ptp_setup_sdp(adapter
);
374 * ixgbe_ptp_check_pps_event
375 * @adapter: the private adapter structure
376 * @eicr: the interrupt cause register value
378 * This function is called by the interrupt routine when checking for
379 * interrupts. It will check and handle a pps event.
381 void ixgbe_ptp_check_pps_event(struct ixgbe_adapter
*adapter
, u32 eicr
)
383 struct ixgbe_hw
*hw
= &adapter
->hw
;
384 struct ptp_clock_event event
;
386 event
.type
= PTP_CLOCK_PPS
;
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.
392 if (!adapter
->ptp_clock
)
395 switch (hw
->mac
.type
) {
397 ptp_clock_event(adapter
->ptp_clock
, &event
);
406 * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
407 * @adapter: private adapter struct
409 * this watchdog task periodically reads the timecounter
410 * in order to prevent missing when the system time registers wrap
411 * around. This needs to be run approximately twice a minute.
413 void ixgbe_ptp_overflow_check(struct ixgbe_adapter
*adapter
)
415 bool timeout
= time_is_before_jiffies(adapter
->last_overflow_check
+
416 IXGBE_OVERFLOW_PERIOD
);
419 if ((adapter
->flags2
& IXGBE_FLAG2_PTP_ENABLED
) &&
421 ixgbe_ptp_gettime(&adapter
->ptp_caps
, &ts
);
422 adapter
->last_overflow_check
= jiffies
;
427 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
428 * @adapter: private network adapter structure
430 * 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.
435 void ixgbe_ptp_rx_hang(struct ixgbe_adapter
*adapter
)
437 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
;
443 /* if we don't have a valid timestamp in the registers, just update the
444 * timeout counter and exit
446 if (!(tsyncrxctl
& IXGBE_TSYNCRXCTL_VALID
)) {
447 adapter
->last_rx_ptp_check
= jiffies
;
451 /* 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
;
459 /* 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
;
464 e_warn(drv
, "clearing RX Timestamp hang");
469 * 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
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
477 void ixgbe_ptp_tx_hwtstamp(struct ixgbe_q_vector
*q_vector
,
480 struct ixgbe_adapter
*adapter
;
482 struct skb_shared_hwtstamps shhwtstamps
;
487 /* we cannot process timestamps on a ring without a q_vector */
488 if (!q_vector
|| !q_vector
->adapter
)
491 adapter
= q_vector
->adapter
;
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;
499 * if TX timestamp is not valid, exit after clearing the
500 * timestamp registers
502 if (!(tsynctxctl
& IXGBE_TSYNCTXCTL_VALID
))
505 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
506 ns
= timecounter_cyc2time(&adapter
->tc
, regval
);
507 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
509 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
510 shhwtstamps
.hwtstamp
= ns_to_ktime(ns
);
511 skb_tstamp_tx(skb
, &shhwtstamps
);
515 * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
516 * @q_vector: structure containing interrupt and ring information
517 * @rx_desc: the rx descriptor
518 * @skb: particular skb to send timestamp with
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
524 void ixgbe_ptp_rx_hwtstamp(struct ixgbe_ring
*rx_ring
,
525 union ixgbe_adv_rx_desc
*rx_desc
,
528 struct ixgbe_adapter
*adapter
;
530 struct skb_shared_hwtstamps
*shhwtstamps
;
535 /* we cannot process timestamps on a ring without a q_vector */
536 if (!rx_ring
->q_vector
|| !rx_ring
->q_vector
->adapter
)
539 adapter
= rx_ring
->q_vector
->adapter
;
542 if (unlikely(!ixgbe_test_staterr(rx_desc
, IXGBE_RXDADV_STAT_TS
)))
546 * Read the tsyncrxctl register afterwards in order to prevent taking an
547 * I/O hit on every packet.
549 tsyncrxctl
= IXGBE_READ_REG(hw
, IXGBE_TSYNCRXCTL
);
550 if (!(tsyncrxctl
& IXGBE_TSYNCRXCTL_VALID
))
554 * Update the last_rx_timestamp timer in order to enable watchdog check
555 * for error case of latched timestamp on a dropped packet.
557 rx_ring
->last_rx_timestamp
= jiffies
;
559 regval
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_RXSTMPL
);
560 regval
|= (u64
)IXGBE_READ_REG(hw
, IXGBE_RXSTMPH
) << 32;
563 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
564 ns
= timecounter_cyc2time(&adapter
->tc
, regval
);
565 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
567 shhwtstamps
= skb_hwtstamps(skb
);
568 shhwtstamps
->hwtstamp
= ns_to_ktime(ns
);
572 * ixgbe_ptp_hwtstamp_ioctl - control hardware time stamping
573 * @adapter: pointer to adapter struct
575 * @cmd: particular ioctl requested
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.
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
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
594 int ixgbe_ptp_hwtstamp_ioctl(struct ixgbe_adapter
*adapter
,
595 struct ifreq
*ifr
, int cmd
)
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
;
601 u32 tsync_rx_mtrl
= PTP_EV_PORT
<< 16;
605 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
608 /* reserved for future extensions */
612 switch (config
.tx_type
) {
613 case HWTSTAMP_TX_OFF
:
621 switch (config
.rx_filter
) {
622 case HWTSTAMP_FILTER_NONE
:
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
;
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
;
634 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
635 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
636 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
637 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
638 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
639 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
640 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
:
643 tsync_rx_ctl
|= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2
;
645 config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
647 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
648 case HWTSTAMP_FILTER_ALL
:
651 * 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
656 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
660 if (hw
->mac
.type
== ixgbe_mac_82598EB
) {
661 if (tsync_rx_ctl
| tsync_tx_ctl
)
666 /* define ethertype filter for timestamping L2 packets */
668 IXGBE_WRITE_REG(hw
, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588
),
669 (IXGBE_ETQF_FILTER_EN
| /* enable filter */
670 IXGBE_ETQF_1588
| /* enable timestamping */
671 ETH_P_1588
)); /* 1588 eth protocol type */
673 IXGBE_WRITE_REG(hw
, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588
), 0);
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
);
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
);
688 /* define which PTP packets are time stamped */
689 IXGBE_WRITE_REG(hw
, IXGBE_RXMTRL
, tsync_rx_mtrl
);
691 IXGBE_WRITE_FLUSH(hw
);
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
);
697 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)) ?
702 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
703 * @adapter: pointer to the adapter structure
705 * 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.
711 void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter
*adapter
)
713 struct ixgbe_hw
*hw
= &adapter
->hw
;
719 * 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
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
732 switch (adapter
->link_speed
) {
733 case IXGBE_LINK_SPEED_100_FULL
:
734 incval
= IXGBE_INCVAL_100
;
735 shift
= IXGBE_INCVAL_SHIFT_100
;
737 case IXGBE_LINK_SPEED_1GB_FULL
:
738 incval
= IXGBE_INCVAL_1GB
;
739 shift
= IXGBE_INCVAL_SHIFT_1GB
;
741 case IXGBE_LINK_SPEED_10GB_FULL
:
743 incval
= IXGBE_INCVAL_10GB
;
744 shift
= IXGBE_INCVAL_SHIFT_10GB
;
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
754 switch (hw
->mac
.type
) {
756 IXGBE_WRITE_REG(hw
, IXGBE_TIMINCA
, incval
);
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
) |
766 /* other devices aren't supported */
770 /* update the base incval used to calculate frequency adjustment */
771 ACCESS_ONCE(adapter
->base_incval
) = incval
;
774 /* need lock to prevent incorrect read while modifying cyclecounter */
775 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
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;
783 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
788 * @adapter: the ixgbe private board structure
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.
794 void ixgbe_ptp_reset(struct ixgbe_adapter
*adapter
)
796 struct ixgbe_hw
*hw
= &adapter
->hw
;
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
);
804 ixgbe_ptp_start_cyclecounter(adapter
);
806 spin_lock_irqsave(&adapter
->tmreg_lock
, flags
);
808 /* reset the ns time counter */
809 timecounter_init(&adapter
->tc
, &adapter
->cc
,
810 ktime_to_ns(ktime_get_real()));
812 spin_unlock_irqrestore(&adapter
->tmreg_lock
, flags
);
815 * Now that the shift has been calculated and the systime
816 * registers reset, (re-)enable the Clock out feature
818 ixgbe_ptp_setup_sdp(adapter
);
823 * @adapter: the ixgbe private adapter structure
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.
829 void ixgbe_ptp_init(struct ixgbe_adapter
*adapter
)
831 struct net_device
*netdev
= adapter
->netdev
;
833 switch (adapter
->hw
.mac
.type
) {
835 snprintf(adapter
->ptp_caps
.name
, 16, "%s", netdev
->name
);
836 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
;
848 case ixgbe_mac_82599EB
:
849 snprintf(adapter
->ptp_caps
.name
, 16, "%s", netdev
->name
);
850 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
;
863 adapter
->ptp_clock
= NULL
;
867 spin_lock_init(&adapter
->tmreg_lock
);
869 adapter
->ptp_clock
= ptp_clock_register(&adapter
->ptp_caps
,
870 &adapter
->pdev
->dev
);
871 if (IS_ERR(adapter
->ptp_clock
)) {
872 adapter
->ptp_clock
= NULL
;
873 e_dev_err("ptp_clock_register failed\n");
875 e_dev_info("registered PHC device on %s\n", netdev
->name
);
877 ixgbe_ptp_reset(adapter
);
879 /* set the flag that PTP has been enabled */
880 adapter
->flags2
|= IXGBE_FLAG2_PTP_ENABLED
;
886 * ixgbe_ptp_stop - disable ptp device and stop the overflow check
887 * @adapter: pointer to adapter struct
889 * this function stops the ptp support, and cancels the delayed work.
891 void ixgbe_ptp_stop(struct ixgbe_adapter
*adapter
)
893 /* stop the overflow check task */
894 adapter
->flags2
&= ~(IXGBE_FLAG2_PTP_ENABLED
|
895 IXGBE_FLAG2_PTP_PPS_ENABLED
);
897 ixgbe_ptp_setup_sdp(adapter
);
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
);