ixgbe: Fix overwriting of rx_mtrl in ixgbe_ptp_hwtstamp_ioctl
[linux-2.6/cjktty.git] / drivers / net / ethernet / intel / ixgbe / ixgbe_ptp.c
blob63046167fe6a9e7f098f37e134a3740b282b6cb8
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
13 more details.
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".
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
26 *******************************************************************************/
27 #include "ixgbe.h"
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
69 * SYSTIMEH SYSTIMEL
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)
99 #define IXGBE_PTP_TX_TIMEOUT (HZ * 15)
101 #ifndef NSECS_PER_SEC
102 #define NSECS_PER_SEC 1000000000ULL
103 #endif
106 * ixgbe_ptp_setup_sdp
107 * @hw: the hardware private structure
109 * this function enables or disables the clock out feature on SDP0 for
110 * the X540 device. It will create a 1second periodic output that can
111 * be used as the PPS (via an interrupt).
113 * It calculates when the systime will be on an exact second, and then
114 * aligns the start of the PPS signal to that value. The shift is
115 * necessary because it can change based on the link speed.
117 static void ixgbe_ptp_setup_sdp(struct ixgbe_adapter *adapter)
119 struct ixgbe_hw *hw = &adapter->hw;
120 int shift = adapter->cc.shift;
121 u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
122 u64 ns = 0, clock_edge = 0;
124 if ((adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED) &&
125 (hw->mac.type == ixgbe_mac_X540)) {
127 /* disable the pin first */
128 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
129 IXGBE_WRITE_FLUSH(hw);
131 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
134 * enable the SDP0 pin as output, and connected to the
135 * native function for Timesync (ClockOut)
137 esdp |= (IXGBE_ESDP_SDP0_DIR |
138 IXGBE_ESDP_SDP0_NATIVE);
141 * enable the Clock Out feature on SDP0, and allow
142 * interrupts to occur when the pin changes
144 tsauxc = (IXGBE_TSAUXC_EN_CLK |
145 IXGBE_TSAUXC_SYNCLK |
146 IXGBE_TSAUXC_SDP0_INT);
148 /* clock period (or pulse length) */
149 clktiml = (u32)(NSECS_PER_SEC << shift);
150 clktimh = (u32)((NSECS_PER_SEC << shift) >> 32);
153 * Account for the cyclecounter wrap-around value by
154 * using the converted ns value of the current time to
155 * check for when the next aligned second would occur.
157 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
158 clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
159 ns = timecounter_cyc2time(&adapter->tc, clock_edge);
161 div_u64_rem(ns, NSECS_PER_SEC, &rem);
162 clock_edge += ((NSECS_PER_SEC - (u64)rem) << shift);
164 /* specify the initial clock start time */
165 trgttiml = (u32)clock_edge;
166 trgttimh = (u32)(clock_edge >> 32);
168 IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
169 IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
170 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
171 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
173 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
174 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
175 } else {
176 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
179 IXGBE_WRITE_FLUSH(hw);
183 * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
184 * @cc: the cyclecounter structure
186 * this function reads the cyclecounter registers and is called by the
187 * cyclecounter structure used to construct a ns counter from the
188 * arbitrary fixed point registers
190 static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
192 struct ixgbe_adapter *adapter =
193 container_of(cc, struct ixgbe_adapter, cc);
194 struct ixgbe_hw *hw = &adapter->hw;
195 u64 stamp = 0;
197 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
198 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
200 return stamp;
204 * ixgbe_ptp_adjfreq
205 * @ptp: the ptp clock structure
206 * @ppb: parts per billion adjustment from base
208 * adjust the frequency of the ptp cycle counter by the
209 * indicated ppb from the base frequency.
211 static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
213 struct ixgbe_adapter *adapter =
214 container_of(ptp, struct ixgbe_adapter, ptp_caps);
215 struct ixgbe_hw *hw = &adapter->hw;
216 u64 freq;
217 u32 diff, incval;
218 int neg_adj = 0;
220 if (ppb < 0) {
221 neg_adj = 1;
222 ppb = -ppb;
225 smp_mb();
226 incval = ACCESS_ONCE(adapter->base_incval);
228 freq = incval;
229 freq *= ppb;
230 diff = div_u64(freq, 1000000000ULL);
232 incval = neg_adj ? (incval - diff) : (incval + diff);
234 switch (hw->mac.type) {
235 case ixgbe_mac_X540:
236 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
237 break;
238 case ixgbe_mac_82599EB:
239 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
240 (1 << IXGBE_INCPER_SHIFT_82599) |
241 incval);
242 break;
243 default:
244 break;
247 return 0;
251 * ixgbe_ptp_adjtime
252 * @ptp: the ptp clock structure
253 * @delta: offset to adjust the cycle counter by
255 * adjust the timer by resetting the timecounter structure.
257 static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
259 struct ixgbe_adapter *adapter =
260 container_of(ptp, struct ixgbe_adapter, ptp_caps);
261 unsigned long flags;
262 u64 now;
264 spin_lock_irqsave(&adapter->tmreg_lock, flags);
266 now = timecounter_read(&adapter->tc);
267 now += delta;
269 /* reset the timecounter */
270 timecounter_init(&adapter->tc,
271 &adapter->cc,
272 now);
274 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
276 ixgbe_ptp_setup_sdp(adapter);
278 return 0;
282 * ixgbe_ptp_gettime
283 * @ptp: the ptp clock structure
284 * @ts: timespec structure to hold the current time value
286 * read the timecounter and return the correct value on ns,
287 * after converting it into a struct timespec.
289 static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
291 struct ixgbe_adapter *adapter =
292 container_of(ptp, struct ixgbe_adapter, ptp_caps);
293 u64 ns;
294 u32 remainder;
295 unsigned long flags;
297 spin_lock_irqsave(&adapter->tmreg_lock, flags);
298 ns = timecounter_read(&adapter->tc);
299 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
301 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
302 ts->tv_nsec = remainder;
304 return 0;
308 * ixgbe_ptp_settime
309 * @ptp: the ptp clock structure
310 * @ts: the timespec containing the new time for the cycle counter
312 * reset the timecounter to use a new base value instead of the kernel
313 * wall timer value.
315 static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
316 const struct timespec *ts)
318 struct ixgbe_adapter *adapter =
319 container_of(ptp, struct ixgbe_adapter, ptp_caps);
320 u64 ns;
321 unsigned long flags;
323 ns = ts->tv_sec * 1000000000ULL;
324 ns += ts->tv_nsec;
326 /* reset the timecounter */
327 spin_lock_irqsave(&adapter->tmreg_lock, flags);
328 timecounter_init(&adapter->tc, &adapter->cc, ns);
329 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
331 ixgbe_ptp_setup_sdp(adapter);
332 return 0;
336 * ixgbe_ptp_enable
337 * @ptp: the ptp clock structure
338 * @rq: the requested feature to change
339 * @on: whether to enable or disable the feature
341 * enable (or disable) ancillary features of the phc subsystem.
342 * our driver only supports the PPS feature on the X540
344 static int ixgbe_ptp_enable(struct ptp_clock_info *ptp,
345 struct ptp_clock_request *rq, int on)
347 struct ixgbe_adapter *adapter =
348 container_of(ptp, struct ixgbe_adapter, ptp_caps);
351 * When PPS is enabled, unmask the interrupt for the ClockOut
352 * feature, so that the interrupt handler can send the PPS
353 * event when the clock SDP triggers. Clear mask when PPS is
354 * disabled
356 if (rq->type == PTP_CLK_REQ_PPS) {
357 switch (adapter->hw.mac.type) {
358 case ixgbe_mac_X540:
359 if (on)
360 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
361 else
362 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
364 ixgbe_ptp_setup_sdp(adapter);
365 return 0;
366 default:
367 break;
371 return -ENOTSUPP;
375 * ixgbe_ptp_check_pps_event
376 * @adapter: the private adapter structure
377 * @eicr: the interrupt cause register value
379 * This function is called by the interrupt routine when checking for
380 * interrupts. It will check and handle a pps event.
382 void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
384 struct ixgbe_hw *hw = &adapter->hw;
385 struct ptp_clock_event event;
387 event.type = PTP_CLOCK_PPS;
389 /* this check is necessary in case the interrupt was enabled via some
390 * alternative means (ex. debug_fs). Better to check here than
391 * everywhere that calls this function.
393 if (!adapter->ptp_clock)
394 return;
396 switch (hw->mac.type) {
397 case ixgbe_mac_X540:
398 ptp_clock_event(adapter->ptp_clock, &event);
399 break;
400 default:
401 break;
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);
417 struct timespec ts;
419 if (timeout) {
420 ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
421 adapter->last_overflow_check = jiffies;
426 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
427 * @adapter: private network adapter structure
429 * this watchdog task is scheduled to detect error case where hardware has
430 * dropped an Rx packet that was timestamped when the ring is full. The
431 * particular error is rare but leaves the device in a state unable to timestamp
432 * any future packets.
434 void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
436 struct ixgbe_hw *hw = &adapter->hw;
437 struct ixgbe_ring *rx_ring;
438 u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
439 unsigned long rx_event;
440 int n;
442 /* if we don't have a valid timestamp in the registers, just update the
443 * timeout counter and exit
445 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
446 adapter->last_rx_ptp_check = jiffies;
447 return;
450 /* determine the most recent watchdog or rx_timestamp event */
451 rx_event = adapter->last_rx_ptp_check;
452 for (n = 0; n < adapter->num_rx_queues; n++) {
453 rx_ring = adapter->rx_ring[n];
454 if (time_after(rx_ring->last_rx_timestamp, rx_event))
455 rx_event = rx_ring->last_rx_timestamp;
458 /* only need to read the high RXSTMP register to clear the lock */
459 if (time_is_before_jiffies(rx_event + 5*HZ)) {
460 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
461 adapter->last_rx_ptp_check = jiffies;
463 e_warn(drv, "clearing RX Timestamp hang");
468 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
469 * @adapter: the private adapter struct
471 * if the timestamp is valid, we convert it into the timecounter ns
472 * value, then store that result into the shhwtstamps structure which
473 * is passed up the network stack
475 static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
477 struct ixgbe_hw *hw = &adapter->hw;
478 struct skb_shared_hwtstamps shhwtstamps;
479 u64 regval = 0, ns;
480 unsigned long flags;
482 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
483 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
485 spin_lock_irqsave(&adapter->tmreg_lock, flags);
486 ns = timecounter_cyc2time(&adapter->tc, regval);
487 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
489 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
490 shhwtstamps.hwtstamp = ns_to_ktime(ns);
491 skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
493 dev_kfree_skb_any(adapter->ptp_tx_skb);
494 adapter->ptp_tx_skb = NULL;
498 * ixgbe_ptp_tx_hwtstamp_work
499 * @work: pointer to the work struct
501 * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
502 * timestamp has been taken for the current skb. It is necesary, because the
503 * descriptor's "done" bit does not correlate with the timestamp event.
505 static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
507 struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
508 ptp_tx_work);
509 struct ixgbe_hw *hw = &adapter->hw;
510 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
511 IXGBE_PTP_TX_TIMEOUT);
512 u32 tsynctxctl;
514 /* we have to have a valid skb */
515 if (!adapter->ptp_tx_skb)
516 return;
518 if (timeout) {
519 dev_kfree_skb_any(adapter->ptp_tx_skb);
520 adapter->ptp_tx_skb = NULL;
521 e_warn(drv, "clearing Tx Timestamp hang");
522 return;
525 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
526 if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID)
527 ixgbe_ptp_tx_hwtstamp(adapter);
528 else
529 /* reschedule to keep checking if it's not available yet */
530 schedule_work(&adapter->ptp_tx_work);
534 * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
535 * @q_vector: structure containing interrupt and ring information
536 * @rx_desc: the rx descriptor
537 * @skb: particular skb to send timestamp with
539 * if the timestamp is valid, we convert it into the timecounter ns
540 * value, then store that result into the shhwtstamps structure which
541 * is passed up the network stack
543 void ixgbe_ptp_rx_hwtstamp(struct ixgbe_ring *rx_ring,
544 union ixgbe_adv_rx_desc *rx_desc,
545 struct sk_buff *skb)
547 struct ixgbe_adapter *adapter;
548 struct ixgbe_hw *hw;
549 struct skb_shared_hwtstamps *shhwtstamps;
550 u64 regval = 0, ns;
551 u32 tsyncrxctl;
552 unsigned long flags;
554 /* we cannot process timestamps on a ring without a q_vector */
555 if (!rx_ring->q_vector || !rx_ring->q_vector->adapter)
556 return;
558 adapter = rx_ring->q_vector->adapter;
559 hw = &adapter->hw;
561 if (unlikely(!ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_STAT_TS)))
562 return;
565 * Read the tsyncrxctl register afterwards in order to prevent taking an
566 * I/O hit on every packet.
568 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
569 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
570 return;
573 * Update the last_rx_timestamp timer in order to enable watchdog check
574 * for error case of latched timestamp on a dropped packet.
576 rx_ring->last_rx_timestamp = jiffies;
578 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
579 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
582 spin_lock_irqsave(&adapter->tmreg_lock, flags);
583 ns = timecounter_cyc2time(&adapter->tc, regval);
584 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
586 shhwtstamps = skb_hwtstamps(skb);
587 shhwtstamps->hwtstamp = ns_to_ktime(ns);
591 * ixgbe_ptp_hwtstamp_ioctl - control hardware time stamping
592 * @adapter: pointer to adapter struct
593 * @ifreq: ioctl data
594 * @cmd: particular ioctl requested
596 * Outgoing time stamping can be enabled and disabled. Play nice and
597 * disable it when requested, although it shouldn't case any overhead
598 * when no packet needs it. At most one packet in the queue may be
599 * marked for time stamping, otherwise it would be impossible to tell
600 * for sure to which packet the hardware time stamp belongs.
602 * Incoming time stamping has to be configured via the hardware
603 * filters. Not all combinations are supported, in particular event
604 * type has to be specified. Matching the kind of event packet is
605 * not supported, with the exception of "all V2 events regardless of
606 * level 2 or 4".
608 * Since hardware always timestamps Path delay packets when timestamping V2
609 * packets, regardless of the type specified in the register, only use V2
610 * Event mode. This more accurately tells the user what the hardware is going
611 * to do anyways.
613 int ixgbe_ptp_hwtstamp_ioctl(struct ixgbe_adapter *adapter,
614 struct ifreq *ifr, int cmd)
616 struct ixgbe_hw *hw = &adapter->hw;
617 struct hwtstamp_config config;
618 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
619 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
620 u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
621 bool is_l2 = false;
622 u32 regval;
624 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
625 return -EFAULT;
627 /* reserved for future extensions */
628 if (config.flags)
629 return -EINVAL;
631 switch (config.tx_type) {
632 case HWTSTAMP_TX_OFF:
633 tsync_tx_ctl = 0;
634 case HWTSTAMP_TX_ON:
635 break;
636 default:
637 return -ERANGE;
640 switch (config.rx_filter) {
641 case HWTSTAMP_FILTER_NONE:
642 tsync_rx_ctl = 0;
643 tsync_rx_mtrl = 0;
644 break;
645 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
646 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
647 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
648 break;
649 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
650 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
651 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
652 break;
653 case HWTSTAMP_FILTER_PTP_V2_EVENT:
654 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
655 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
656 case HWTSTAMP_FILTER_PTP_V2_SYNC:
657 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
658 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
659 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
660 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
661 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
662 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
663 is_l2 = true;
664 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
665 break;
666 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
667 case HWTSTAMP_FILTER_ALL:
668 default:
670 * register RXMTRL must be set in order to do V1 packets,
671 * therefore it is not possible to time stamp both V1 Sync and
672 * Delay_Req messages and hardware does not support
673 * timestamping all packets => return error
675 config.rx_filter = HWTSTAMP_FILTER_NONE;
676 return -ERANGE;
679 if (hw->mac.type == ixgbe_mac_82598EB) {
680 if (tsync_rx_ctl | tsync_tx_ctl)
681 return -ERANGE;
682 return 0;
685 /* define ethertype filter for timestamping L2 packets */
686 if (is_l2)
687 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
688 (IXGBE_ETQF_FILTER_EN | /* enable filter */
689 IXGBE_ETQF_1588 | /* enable timestamping */
690 ETH_P_1588)); /* 1588 eth protocol type */
691 else
692 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
695 /* enable/disable TX */
696 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
697 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
698 regval |= tsync_tx_ctl;
699 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
701 /* enable/disable RX */
702 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
703 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
704 regval |= tsync_rx_ctl;
705 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
707 /* define which PTP packets are time stamped */
708 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
710 IXGBE_WRITE_FLUSH(hw);
712 /* clear TX/RX time stamp registers, just to be sure */
713 regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
714 regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
716 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
717 -EFAULT : 0;
721 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
722 * @adapter: pointer to the adapter structure
724 * This function should be called to set the proper values for the TIMINCA
725 * register and tell the cyclecounter structure what the tick rate of SYSTIME
726 * is. It does not directly modify SYSTIME registers or the timecounter
727 * structure. It should be called whenever a new TIMINCA value is necessary,
728 * such as during initialization or when the link speed changes.
730 void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
732 struct ixgbe_hw *hw = &adapter->hw;
733 u32 incval = 0;
734 u32 shift = 0;
735 unsigned long flags;
738 * Scale the NIC cycle counter by a large factor so that
739 * relatively small corrections to the frequency can be added
740 * or subtracted. The drawbacks of a large factor include
741 * (a) the clock register overflows more quickly, (b) the cycle
742 * counter structure must be able to convert the systime value
743 * to nanoseconds using only a multiplier and a right-shift,
744 * and (c) the value must fit within the timinca register space
745 * => math based on internal DMA clock rate and available bits
747 * Note that when there is no link, internal DMA clock is same as when
748 * link speed is 10Gb. Set the registers correctly even when link is
749 * down to preserve the clock setting
751 switch (adapter->link_speed) {
752 case IXGBE_LINK_SPEED_100_FULL:
753 incval = IXGBE_INCVAL_100;
754 shift = IXGBE_INCVAL_SHIFT_100;
755 break;
756 case IXGBE_LINK_SPEED_1GB_FULL:
757 incval = IXGBE_INCVAL_1GB;
758 shift = IXGBE_INCVAL_SHIFT_1GB;
759 break;
760 case IXGBE_LINK_SPEED_10GB_FULL:
761 default:
762 incval = IXGBE_INCVAL_10GB;
763 shift = IXGBE_INCVAL_SHIFT_10GB;
764 break;
768 * Modify the calculated values to fit within the correct
769 * number of bits specified by the hardware. The 82599 doesn't
770 * have the same space as the X540, so bitshift the calculated
771 * values to fit.
773 switch (hw->mac.type) {
774 case ixgbe_mac_X540:
775 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
776 break;
777 case ixgbe_mac_82599EB:
778 incval >>= IXGBE_INCVAL_SHIFT_82599;
779 shift -= IXGBE_INCVAL_SHIFT_82599;
780 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
781 (1 << IXGBE_INCPER_SHIFT_82599) |
782 incval);
783 break;
784 default:
785 /* other devices aren't supported */
786 return;
789 /* update the base incval used to calculate frequency adjustment */
790 ACCESS_ONCE(adapter->base_incval) = incval;
791 smp_mb();
793 /* need lock to prevent incorrect read while modifying cyclecounter */
794 spin_lock_irqsave(&adapter->tmreg_lock, flags);
796 memset(&adapter->cc, 0, sizeof(adapter->cc));
797 adapter->cc.read = ixgbe_ptp_read;
798 adapter->cc.mask = CLOCKSOURCE_MASK(64);
799 adapter->cc.shift = shift;
800 adapter->cc.mult = 1;
802 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
806 * ixgbe_ptp_reset
807 * @adapter: the ixgbe private board structure
809 * When the MAC resets, all timesync features are reset. This function should be
810 * called to re-enable the PTP clock structure. It will re-init the timecounter
811 * structure based on the kernel time as well as setup the cycle counter data.
813 void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
815 struct ixgbe_hw *hw = &adapter->hw;
816 unsigned long flags;
818 /* set SYSTIME registers to 0 just in case */
819 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
820 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
821 IXGBE_WRITE_FLUSH(hw);
823 ixgbe_ptp_start_cyclecounter(adapter);
825 spin_lock_irqsave(&adapter->tmreg_lock, flags);
827 /* reset the ns time counter */
828 timecounter_init(&adapter->tc, &adapter->cc,
829 ktime_to_ns(ktime_get_real()));
831 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
834 * Now that the shift has been calculated and the systime
835 * registers reset, (re-)enable the Clock out feature
837 ixgbe_ptp_setup_sdp(adapter);
841 * ixgbe_ptp_init
842 * @adapter: the ixgbe private adapter structure
844 * This function performs the required steps for enabling ptp
845 * support. If ptp support has already been loaded it simply calls the
846 * cyclecounter init routine and exits.
848 void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
850 struct net_device *netdev = adapter->netdev;
852 switch (adapter->hw.mac.type) {
853 case ixgbe_mac_X540:
854 snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
855 adapter->ptp_caps.owner = THIS_MODULE;
856 adapter->ptp_caps.max_adj = 250000000;
857 adapter->ptp_caps.n_alarm = 0;
858 adapter->ptp_caps.n_ext_ts = 0;
859 adapter->ptp_caps.n_per_out = 0;
860 adapter->ptp_caps.pps = 1;
861 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
862 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
863 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
864 adapter->ptp_caps.settime = ixgbe_ptp_settime;
865 adapter->ptp_caps.enable = ixgbe_ptp_enable;
866 break;
867 case ixgbe_mac_82599EB:
868 snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
869 adapter->ptp_caps.owner = THIS_MODULE;
870 adapter->ptp_caps.max_adj = 250000000;
871 adapter->ptp_caps.n_alarm = 0;
872 adapter->ptp_caps.n_ext_ts = 0;
873 adapter->ptp_caps.n_per_out = 0;
874 adapter->ptp_caps.pps = 0;
875 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
876 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
877 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
878 adapter->ptp_caps.settime = ixgbe_ptp_settime;
879 adapter->ptp_caps.enable = ixgbe_ptp_enable;
880 break;
881 default:
882 adapter->ptp_clock = NULL;
883 return;
886 spin_lock_init(&adapter->tmreg_lock);
887 INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
889 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
890 &adapter->pdev->dev);
891 if (IS_ERR(adapter->ptp_clock)) {
892 adapter->ptp_clock = NULL;
893 e_dev_err("ptp_clock_register failed\n");
894 } else
895 e_dev_info("registered PHC device on %s\n", netdev->name);
897 ixgbe_ptp_reset(adapter);
899 /* set the flag that PTP has been enabled */
900 adapter->flags2 |= IXGBE_FLAG2_PTP_ENABLED;
902 return;
906 * ixgbe_ptp_stop - disable ptp device and stop the overflow check
907 * @adapter: pointer to adapter struct
909 * this function stops the ptp support, and cancels the delayed work.
911 void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
913 /* stop the overflow check task */
914 adapter->flags2 &= ~(IXGBE_FLAG2_PTP_ENABLED |
915 IXGBE_FLAG2_PTP_PPS_ENABLED);
917 ixgbe_ptp_setup_sdp(adapter);
919 cancel_work_sync(&adapter->ptp_tx_work);
920 if (adapter->ptp_tx_skb) {
921 dev_kfree_skb_any(adapter->ptp_tx_skb);
922 adapter->ptp_tx_skb = NULL;
925 if (adapter->ptp_clock) {
926 ptp_clock_unregister(adapter->ptp_clock);
927 adapter->ptp_clock = NULL;
928 e_dev_info("removed PHC on %s\n",
929 adapter->netdev->name);