Linux 3.11-rc3
[cris-mirror.git] / drivers / net / ethernet / intel / igb / igb_ptp.c
blob7e8c477b0ab9fb6e2758a763411dbde42ae57769
1 /* PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
3 * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/pci.h>
22 #include <linux/ptp_classify.h>
24 #include "igb.h"
26 #define INCVALUE_MASK 0x7fffffff
27 #define ISGN 0x80000000
29 /* The 82580 timesync updates the system timer every 8ns by 8ns,
30 * and this update value cannot be reprogrammed.
32 * Neither the 82576 nor the 82580 offer registers wide enough to hold
33 * nanoseconds time values for very long. For the 82580, SYSTIM always
34 * counts nanoseconds, but the upper 24 bits are not availible. The
35 * frequency is adjusted by changing the 32 bit fractional nanoseconds
36 * register, TIMINCA.
38 * For the 82576, the SYSTIM register time unit is affect by the
39 * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
40 * field are needed to provide the nominal 16 nanosecond period,
41 * leaving 19 bits for fractional nanoseconds.
43 * We scale the NIC clock cycle by a large factor so that relatively
44 * small clock corrections can be added or subtracted at each clock
45 * tick. The drawbacks of a large factor are a) that the clock
46 * register overflows more quickly (not such a big deal) and b) that
47 * the increment per tick has to fit into 24 bits. As a result we
48 * need to use a shift of 19 so we can fit a value of 16 into the
49 * TIMINCA register.
52 * SYSTIMH SYSTIML
53 * +--------------+ +---+---+------+
54 * 82576 | 32 | | 8 | 5 | 19 |
55 * +--------------+ +---+---+------+
56 * \________ 45 bits _______/ fract
58 * +----------+---+ +--------------+
59 * 82580 | 24 | 8 | | 32 |
60 * +----------+---+ +--------------+
61 * reserved \______ 40 bits _____/
64 * The 45 bit 82576 SYSTIM overflows every
65 * 2^45 * 10^-9 / 3600 = 9.77 hours.
67 * The 40 bit 82580 SYSTIM overflows every
68 * 2^40 * 10^-9 / 60 = 18.3 minutes.
71 #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9)
72 #define IGB_PTP_TX_TIMEOUT (HZ * 15)
73 #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT)
74 #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
75 #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT)
76 #define IGB_NBITS_82580 40
78 /* SYSTIM read access for the 82576 */
79 static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc)
81 struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
82 struct e1000_hw *hw = &igb->hw;
83 u64 val;
84 u32 lo, hi;
86 lo = rd32(E1000_SYSTIML);
87 hi = rd32(E1000_SYSTIMH);
89 val = ((u64) hi) << 32;
90 val |= lo;
92 return val;
95 /* SYSTIM read access for the 82580 */
96 static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc)
98 struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
99 struct e1000_hw *hw = &igb->hw;
100 u64 val;
101 u32 lo, hi, jk;
103 /* The timestamp latches on lowest register read. For the 82580
104 * the lowest register is SYSTIMR instead of SYSTIML. However we only
105 * need to provide nanosecond resolution, so we just ignore it.
107 jk = rd32(E1000_SYSTIMR);
108 lo = rd32(E1000_SYSTIML);
109 hi = rd32(E1000_SYSTIMH);
111 val = ((u64) hi) << 32;
112 val |= lo;
114 return val;
117 /* SYSTIM read access for I210/I211 */
118 static void igb_ptp_read_i210(struct igb_adapter *adapter, struct timespec *ts)
120 struct e1000_hw *hw = &adapter->hw;
121 u32 sec, nsec, jk;
123 /* The timestamp latches on lowest register read. For I210/I211, the
124 * lowest register is SYSTIMR. Since we only need to provide nanosecond
125 * resolution, we can ignore it.
127 jk = rd32(E1000_SYSTIMR);
128 nsec = rd32(E1000_SYSTIML);
129 sec = rd32(E1000_SYSTIMH);
131 ts->tv_sec = sec;
132 ts->tv_nsec = nsec;
135 static void igb_ptp_write_i210(struct igb_adapter *adapter,
136 const struct timespec *ts)
138 struct e1000_hw *hw = &adapter->hw;
140 /* Writing the SYSTIMR register is not necessary as it only provides
141 * sub-nanosecond resolution.
143 wr32(E1000_SYSTIML, ts->tv_nsec);
144 wr32(E1000_SYSTIMH, ts->tv_sec);
148 * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
149 * @adapter: board private structure
150 * @hwtstamps: timestamp structure to update
151 * @systim: unsigned 64bit system time value.
153 * We need to convert the system time value stored in the RX/TXSTMP registers
154 * into a hwtstamp which can be used by the upper level timestamping functions.
156 * The 'tmreg_lock' spinlock is used to protect the consistency of the
157 * system time value. This is needed because reading the 64 bit time
158 * value involves reading two (or three) 32 bit registers. The first
159 * read latches the value. Ditto for writing.
161 * In addition, here have extended the system time with an overflow
162 * counter in software.
164 static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
165 struct skb_shared_hwtstamps *hwtstamps,
166 u64 systim)
168 unsigned long flags;
169 u64 ns;
171 switch (adapter->hw.mac.type) {
172 case e1000_82576:
173 case e1000_82580:
174 case e1000_i354:
175 case e1000_i350:
176 spin_lock_irqsave(&adapter->tmreg_lock, flags);
178 ns = timecounter_cyc2time(&adapter->tc, systim);
180 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
182 memset(hwtstamps, 0, sizeof(*hwtstamps));
183 hwtstamps->hwtstamp = ns_to_ktime(ns);
184 break;
185 case e1000_i210:
186 case e1000_i211:
187 memset(hwtstamps, 0, sizeof(*hwtstamps));
188 /* Upper 32 bits contain s, lower 32 bits contain ns. */
189 hwtstamps->hwtstamp = ktime_set(systim >> 32,
190 systim & 0xFFFFFFFF);
191 break;
192 default:
193 break;
197 /* PTP clock operations */
198 static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
200 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
201 ptp_caps);
202 struct e1000_hw *hw = &igb->hw;
203 int neg_adj = 0;
204 u64 rate;
205 u32 incvalue;
207 if (ppb < 0) {
208 neg_adj = 1;
209 ppb = -ppb;
211 rate = ppb;
212 rate <<= 14;
213 rate = div_u64(rate, 1953125);
215 incvalue = 16 << IGB_82576_TSYNC_SHIFT;
217 if (neg_adj)
218 incvalue -= rate;
219 else
220 incvalue += rate;
222 wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
224 return 0;
227 static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
229 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
230 ptp_caps);
231 struct e1000_hw *hw = &igb->hw;
232 int neg_adj = 0;
233 u64 rate;
234 u32 inca;
236 if (ppb < 0) {
237 neg_adj = 1;
238 ppb = -ppb;
240 rate = ppb;
241 rate <<= 26;
242 rate = div_u64(rate, 1953125);
244 inca = rate & INCVALUE_MASK;
245 if (neg_adj)
246 inca |= ISGN;
248 wr32(E1000_TIMINCA, inca);
250 return 0;
253 static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
255 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
256 ptp_caps);
257 unsigned long flags;
258 s64 now;
260 spin_lock_irqsave(&igb->tmreg_lock, flags);
262 now = timecounter_read(&igb->tc);
263 now += delta;
264 timecounter_init(&igb->tc, &igb->cc, now);
266 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
268 return 0;
271 static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
273 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
274 ptp_caps);
275 unsigned long flags;
276 struct timespec now, then = ns_to_timespec(delta);
278 spin_lock_irqsave(&igb->tmreg_lock, flags);
280 igb_ptp_read_i210(igb, &now);
281 now = timespec_add(now, then);
282 igb_ptp_write_i210(igb, (const struct timespec *)&now);
284 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
286 return 0;
289 static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
290 struct timespec *ts)
292 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
293 ptp_caps);
294 unsigned long flags;
295 u64 ns;
296 u32 remainder;
298 spin_lock_irqsave(&igb->tmreg_lock, flags);
300 ns = timecounter_read(&igb->tc);
302 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
304 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
305 ts->tv_nsec = remainder;
307 return 0;
310 static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
311 struct timespec *ts)
313 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
314 ptp_caps);
315 unsigned long flags;
317 spin_lock_irqsave(&igb->tmreg_lock, flags);
319 igb_ptp_read_i210(igb, ts);
321 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
323 return 0;
326 static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
327 const struct timespec *ts)
329 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
330 ptp_caps);
331 unsigned long flags;
332 u64 ns;
334 ns = ts->tv_sec * 1000000000ULL;
335 ns += ts->tv_nsec;
337 spin_lock_irqsave(&igb->tmreg_lock, flags);
339 timecounter_init(&igb->tc, &igb->cc, ns);
341 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
343 return 0;
346 static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
347 const struct timespec *ts)
349 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
350 ptp_caps);
351 unsigned long flags;
353 spin_lock_irqsave(&igb->tmreg_lock, flags);
355 igb_ptp_write_i210(igb, ts);
357 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
359 return 0;
362 static int igb_ptp_enable(struct ptp_clock_info *ptp,
363 struct ptp_clock_request *rq, int on)
365 return -EOPNOTSUPP;
369 * igb_ptp_tx_work
370 * @work: pointer to work struct
372 * This work function polls the TSYNCTXCTL valid bit to determine when a
373 * timestamp has been taken for the current stored skb.
375 void igb_ptp_tx_work(struct work_struct *work)
377 struct igb_adapter *adapter = container_of(work, struct igb_adapter,
378 ptp_tx_work);
379 struct e1000_hw *hw = &adapter->hw;
380 u32 tsynctxctl;
382 if (!adapter->ptp_tx_skb)
383 return;
385 if (time_is_before_jiffies(adapter->ptp_tx_start +
386 IGB_PTP_TX_TIMEOUT)) {
387 dev_kfree_skb_any(adapter->ptp_tx_skb);
388 adapter->ptp_tx_skb = NULL;
389 adapter->tx_hwtstamp_timeouts++;
390 dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang");
391 return;
394 tsynctxctl = rd32(E1000_TSYNCTXCTL);
395 if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
396 igb_ptp_tx_hwtstamp(adapter);
397 else
398 /* reschedule to check later */
399 schedule_work(&adapter->ptp_tx_work);
402 static void igb_ptp_overflow_check(struct work_struct *work)
404 struct igb_adapter *igb =
405 container_of(work, struct igb_adapter, ptp_overflow_work.work);
406 struct timespec ts;
408 igb->ptp_caps.gettime(&igb->ptp_caps, &ts);
410 pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
412 schedule_delayed_work(&igb->ptp_overflow_work,
413 IGB_SYSTIM_OVERFLOW_PERIOD);
417 * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
418 * @adapter: private network adapter structure
420 * This watchdog task is scheduled to detect error case where hardware has
421 * dropped an Rx packet that was timestamped when the ring is full. The
422 * particular error is rare but leaves the device in a state unable to timestamp
423 * any future packets.
425 void igb_ptp_rx_hang(struct igb_adapter *adapter)
427 struct e1000_hw *hw = &adapter->hw;
428 struct igb_ring *rx_ring;
429 u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
430 unsigned long rx_event;
431 int n;
433 if (hw->mac.type != e1000_82576)
434 return;
436 /* If we don't have a valid timestamp in the registers, just update the
437 * timeout counter and exit
439 if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
440 adapter->last_rx_ptp_check = jiffies;
441 return;
444 /* Determine the most recent watchdog or rx_timestamp event */
445 rx_event = adapter->last_rx_ptp_check;
446 for (n = 0; n < adapter->num_rx_queues; n++) {
447 rx_ring = adapter->rx_ring[n];
448 if (time_after(rx_ring->last_rx_timestamp, rx_event))
449 rx_event = rx_ring->last_rx_timestamp;
452 /* Only need to read the high RXSTMP register to clear the lock */
453 if (time_is_before_jiffies(rx_event + 5 * HZ)) {
454 rd32(E1000_RXSTMPH);
455 adapter->last_rx_ptp_check = jiffies;
456 adapter->rx_hwtstamp_cleared++;
457 dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang");
462 * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
463 * @adapter: Board private structure.
465 * If we were asked to do hardware stamping and such a time stamp is
466 * available, then it must have been for this skb here because we only
467 * allow only one such packet into the queue.
469 void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
471 struct e1000_hw *hw = &adapter->hw;
472 struct skb_shared_hwtstamps shhwtstamps;
473 u64 regval;
475 regval = rd32(E1000_TXSTMPL);
476 regval |= (u64)rd32(E1000_TXSTMPH) << 32;
478 igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
479 skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
480 dev_kfree_skb_any(adapter->ptp_tx_skb);
481 adapter->ptp_tx_skb = NULL;
485 * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
486 * @q_vector: Pointer to interrupt specific structure
487 * @va: Pointer to address containing Rx buffer
488 * @skb: Buffer containing timestamp and packet
490 * This function is meant to retrieve a timestamp from the first buffer of an
491 * incoming frame. The value is stored in little endian format starting on
492 * byte 8.
494 void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
495 unsigned char *va,
496 struct sk_buff *skb)
498 __le64 *regval = (__le64 *)va;
500 /* The timestamp is recorded in little endian format.
501 * DWORD: 0 1 2 3
502 * Field: Reserved Reserved SYSTIML SYSTIMH
504 igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
505 le64_to_cpu(regval[1]));
509 * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
510 * @q_vector: Pointer to interrupt specific structure
511 * @skb: Buffer containing timestamp and packet
513 * This function is meant to retrieve a timestamp from the internal registers
514 * of the adapter and store it in the skb.
516 void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
517 struct sk_buff *skb)
519 struct igb_adapter *adapter = q_vector->adapter;
520 struct e1000_hw *hw = &adapter->hw;
521 u64 regval;
523 /* If this bit is set, then the RX registers contain the time stamp. No
524 * other packet will be time stamped until we read these registers, so
525 * read the registers to make them available again. Because only one
526 * packet can be time stamped at a time, we know that the register
527 * values must belong to this one here and therefore we don't need to
528 * compare any of the additional attributes stored for it.
530 * If nothing went wrong, then it should have a shared tx_flags that we
531 * can turn into a skb_shared_hwtstamps.
533 if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
534 return;
536 regval = rd32(E1000_RXSTMPL);
537 regval |= (u64)rd32(E1000_RXSTMPH) << 32;
539 igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
543 * igb_ptp_hwtstamp_ioctl - control hardware time stamping
544 * @netdev:
545 * @ifreq:
546 * @cmd:
548 * Outgoing time stamping can be enabled and disabled. Play nice and
549 * disable it when requested, although it shouldn't case any overhead
550 * when no packet needs it. At most one packet in the queue may be
551 * marked for time stamping, otherwise it would be impossible to tell
552 * for sure to which packet the hardware time stamp belongs.
554 * Incoming time stamping has to be configured via the hardware
555 * filters. Not all combinations are supported, in particular event
556 * type has to be specified. Matching the kind of event packet is
557 * not supported, with the exception of "all V2 events regardless of
558 * level 2 or 4".
560 int igb_ptp_hwtstamp_ioctl(struct net_device *netdev,
561 struct ifreq *ifr, int cmd)
563 struct igb_adapter *adapter = netdev_priv(netdev);
564 struct e1000_hw *hw = &adapter->hw;
565 struct hwtstamp_config config;
566 u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
567 u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
568 u32 tsync_rx_cfg = 0;
569 bool is_l4 = false;
570 bool is_l2 = false;
571 u32 regval;
573 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
574 return -EFAULT;
576 /* reserved for future extensions */
577 if (config.flags)
578 return -EINVAL;
580 switch (config.tx_type) {
581 case HWTSTAMP_TX_OFF:
582 tsync_tx_ctl = 0;
583 case HWTSTAMP_TX_ON:
584 break;
585 default:
586 return -ERANGE;
589 switch (config.rx_filter) {
590 case HWTSTAMP_FILTER_NONE:
591 tsync_rx_ctl = 0;
592 break;
593 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
594 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
595 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
596 is_l4 = true;
597 break;
598 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
599 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
600 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
601 is_l4 = true;
602 break;
603 case HWTSTAMP_FILTER_PTP_V2_EVENT:
604 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
605 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
606 case HWTSTAMP_FILTER_PTP_V2_SYNC:
607 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
608 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
609 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
610 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
611 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
612 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
613 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
614 is_l2 = true;
615 is_l4 = true;
616 break;
617 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
618 case HWTSTAMP_FILTER_ALL:
619 /* 82576 cannot timestamp all packets, which it needs to do to
620 * support both V1 Sync and Delay_Req messages
622 if (hw->mac.type != e1000_82576) {
623 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
624 config.rx_filter = HWTSTAMP_FILTER_ALL;
625 break;
627 /* fall through */
628 default:
629 config.rx_filter = HWTSTAMP_FILTER_NONE;
630 return -ERANGE;
633 if (hw->mac.type == e1000_82575) {
634 if (tsync_rx_ctl | tsync_tx_ctl)
635 return -EINVAL;
636 return 0;
639 /* Per-packet timestamping only works if all packets are
640 * timestamped, so enable timestamping in all packets as
641 * long as one Rx filter was configured.
643 if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
644 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
645 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
646 config.rx_filter = HWTSTAMP_FILTER_ALL;
647 is_l2 = true;
648 is_l4 = true;
650 if ((hw->mac.type == e1000_i210) ||
651 (hw->mac.type == e1000_i211)) {
652 regval = rd32(E1000_RXPBS);
653 regval |= E1000_RXPBS_CFG_TS_EN;
654 wr32(E1000_RXPBS, regval);
658 /* enable/disable TX */
659 regval = rd32(E1000_TSYNCTXCTL);
660 regval &= ~E1000_TSYNCTXCTL_ENABLED;
661 regval |= tsync_tx_ctl;
662 wr32(E1000_TSYNCTXCTL, regval);
664 /* enable/disable RX */
665 regval = rd32(E1000_TSYNCRXCTL);
666 regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
667 regval |= tsync_rx_ctl;
668 wr32(E1000_TSYNCRXCTL, regval);
670 /* define which PTP packets are time stamped */
671 wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
673 /* define ethertype filter for timestamped packets */
674 if (is_l2)
675 wr32(E1000_ETQF(3),
676 (E1000_ETQF_FILTER_ENABLE | /* enable filter */
677 E1000_ETQF_1588 | /* enable timestamping */
678 ETH_P_1588)); /* 1588 eth protocol type */
679 else
680 wr32(E1000_ETQF(3), 0);
682 /* L4 Queue Filter[3]: filter by destination port and protocol */
683 if (is_l4) {
684 u32 ftqf = (IPPROTO_UDP /* UDP */
685 | E1000_FTQF_VF_BP /* VF not compared */
686 | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
687 | E1000_FTQF_MASK); /* mask all inputs */
688 ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
690 wr32(E1000_IMIR(3), htons(PTP_EV_PORT));
691 wr32(E1000_IMIREXT(3),
692 (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
693 if (hw->mac.type == e1000_82576) {
694 /* enable source port check */
695 wr32(E1000_SPQF(3), htons(PTP_EV_PORT));
696 ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
698 wr32(E1000_FTQF(3), ftqf);
699 } else {
700 wr32(E1000_FTQF(3), E1000_FTQF_MASK);
702 wrfl();
704 /* clear TX/RX time stamp registers, just to be sure */
705 regval = rd32(E1000_TXSTMPL);
706 regval = rd32(E1000_TXSTMPH);
707 regval = rd32(E1000_RXSTMPL);
708 regval = rd32(E1000_RXSTMPH);
710 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
711 -EFAULT : 0;
714 void igb_ptp_init(struct igb_adapter *adapter)
716 struct e1000_hw *hw = &adapter->hw;
717 struct net_device *netdev = adapter->netdev;
719 switch (hw->mac.type) {
720 case e1000_82576:
721 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
722 adapter->ptp_caps.owner = THIS_MODULE;
723 adapter->ptp_caps.max_adj = 999999881;
724 adapter->ptp_caps.n_ext_ts = 0;
725 adapter->ptp_caps.pps = 0;
726 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
727 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
728 adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
729 adapter->ptp_caps.settime = igb_ptp_settime_82576;
730 adapter->ptp_caps.enable = igb_ptp_enable;
731 adapter->cc.read = igb_ptp_read_82576;
732 adapter->cc.mask = CLOCKSOURCE_MASK(64);
733 adapter->cc.mult = 1;
734 adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
735 /* Dial the nominal frequency. */
736 wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
737 break;
738 case e1000_82580:
739 case e1000_i354:
740 case e1000_i350:
741 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
742 adapter->ptp_caps.owner = THIS_MODULE;
743 adapter->ptp_caps.max_adj = 62499999;
744 adapter->ptp_caps.n_ext_ts = 0;
745 adapter->ptp_caps.pps = 0;
746 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
747 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
748 adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
749 adapter->ptp_caps.settime = igb_ptp_settime_82576;
750 adapter->ptp_caps.enable = igb_ptp_enable;
751 adapter->cc.read = igb_ptp_read_82580;
752 adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580);
753 adapter->cc.mult = 1;
754 adapter->cc.shift = 0;
755 /* Enable the timer functions by clearing bit 31. */
756 wr32(E1000_TSAUXC, 0x0);
757 break;
758 case e1000_i210:
759 case e1000_i211:
760 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
761 adapter->ptp_caps.owner = THIS_MODULE;
762 adapter->ptp_caps.max_adj = 62499999;
763 adapter->ptp_caps.n_ext_ts = 0;
764 adapter->ptp_caps.pps = 0;
765 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
766 adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
767 adapter->ptp_caps.gettime = igb_ptp_gettime_i210;
768 adapter->ptp_caps.settime = igb_ptp_settime_i210;
769 adapter->ptp_caps.enable = igb_ptp_enable;
770 /* Enable the timer functions by clearing bit 31. */
771 wr32(E1000_TSAUXC, 0x0);
772 break;
773 default:
774 adapter->ptp_clock = NULL;
775 return;
778 wrfl();
780 spin_lock_init(&adapter->tmreg_lock);
781 INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
783 /* Initialize the clock and overflow work for devices that need it. */
784 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
785 struct timespec ts = ktime_to_timespec(ktime_get_real());
787 igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
788 } else {
789 timecounter_init(&adapter->tc, &adapter->cc,
790 ktime_to_ns(ktime_get_real()));
792 INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
793 igb_ptp_overflow_check);
795 schedule_delayed_work(&adapter->ptp_overflow_work,
796 IGB_SYSTIM_OVERFLOW_PERIOD);
799 /* Initialize the time sync interrupts for devices that support it. */
800 if (hw->mac.type >= e1000_82580) {
801 wr32(E1000_TSIM, E1000_TSIM_TXTS);
802 wr32(E1000_IMS, E1000_IMS_TS);
805 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
806 &adapter->pdev->dev);
807 if (IS_ERR(adapter->ptp_clock)) {
808 adapter->ptp_clock = NULL;
809 dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
810 } else {
811 dev_info(&adapter->pdev->dev, "added PHC on %s\n",
812 adapter->netdev->name);
813 adapter->flags |= IGB_FLAG_PTP;
818 * igb_ptp_stop - Disable PTP device and stop the overflow check.
819 * @adapter: Board private structure.
821 * This function stops the PTP support and cancels the delayed work.
823 void igb_ptp_stop(struct igb_adapter *adapter)
825 switch (adapter->hw.mac.type) {
826 case e1000_82576:
827 case e1000_82580:
828 case e1000_i354:
829 case e1000_i350:
830 cancel_delayed_work_sync(&adapter->ptp_overflow_work);
831 break;
832 case e1000_i210:
833 case e1000_i211:
834 /* No delayed work to cancel. */
835 break;
836 default:
837 return;
840 cancel_work_sync(&adapter->ptp_tx_work);
841 if (adapter->ptp_tx_skb) {
842 dev_kfree_skb_any(adapter->ptp_tx_skb);
843 adapter->ptp_tx_skb = NULL;
846 if (adapter->ptp_clock) {
847 ptp_clock_unregister(adapter->ptp_clock);
848 dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
849 adapter->netdev->name);
850 adapter->flags &= ~IGB_FLAG_PTP;
855 * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
856 * @adapter: Board private structure.
858 * This function handles the reset work required to re-enable the PTP device.
860 void igb_ptp_reset(struct igb_adapter *adapter)
862 struct e1000_hw *hw = &adapter->hw;
864 if (!(adapter->flags & IGB_FLAG_PTP))
865 return;
867 switch (adapter->hw.mac.type) {
868 case e1000_82576:
869 /* Dial the nominal frequency. */
870 wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
871 break;
872 case e1000_82580:
873 case e1000_i354:
874 case e1000_i350:
875 case e1000_i210:
876 case e1000_i211:
877 /* Enable the timer functions and interrupts. */
878 wr32(E1000_TSAUXC, 0x0);
879 wr32(E1000_TSIM, E1000_TSIM_TXTS);
880 wr32(E1000_IMS, E1000_IMS_TS);
881 break;
882 default:
883 /* No work to do. */
884 return;
887 /* Re-initialize the timer. */
888 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
889 struct timespec ts = ktime_to_timespec(ktime_get_real());
891 igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
892 } else {
893 timecounter_init(&adapter->tc, &adapter->cc,
894 ktime_to_ns(ktime_get_real()));