1b980ac18SJeff Kirsher /* PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580 2d339b133SRichard Cochran * 3d339b133SRichard Cochran * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com> 4d339b133SRichard Cochran * 5d339b133SRichard Cochran * This program is free software; you can redistribute it and/or modify 6d339b133SRichard Cochran * it under the terms of the GNU General Public License as published by 7d339b133SRichard Cochran * the Free Software Foundation; either version 2 of the License, or 8d339b133SRichard Cochran * (at your option) any later version. 9d339b133SRichard Cochran * 10d339b133SRichard Cochran * This program is distributed in the hope that it will be useful, 11d339b133SRichard Cochran * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d339b133SRichard Cochran * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d339b133SRichard Cochran * GNU General Public License for more details. 14d339b133SRichard Cochran * 1574cfb2e1SCarolyn Wyborny * You should have received a copy of the GNU General Public License along with 1674cfb2e1SCarolyn Wyborny * this program; if not, see <http://www.gnu.org/licenses/>. 17d339b133SRichard Cochran */ 18d339b133SRichard Cochran #include <linux/module.h> 19d339b133SRichard Cochran #include <linux/device.h> 20d339b133SRichard Cochran #include <linux/pci.h> 21ba59814bSMatthew Vick #include <linux/ptp_classify.h> 22d339b133SRichard Cochran 23d339b133SRichard Cochran #include "igb.h" 24d339b133SRichard Cochran 25d339b133SRichard Cochran #define INCVALUE_MASK 0x7fffffff 26d339b133SRichard Cochran #define ISGN 0x80000000 27d339b133SRichard Cochran 28b980ac18SJeff Kirsher /* The 82580 timesync updates the system timer every 8ns by 8ns, 297ebae817SRichard Cochran * and this update value cannot be reprogrammed. 307ebae817SRichard Cochran * 31d339b133SRichard Cochran * Neither the 82576 nor the 82580 offer registers wide enough to hold 32d339b133SRichard Cochran * nanoseconds time values for very long. For the 82580, SYSTIM always 33d339b133SRichard Cochran * counts nanoseconds, but the upper 24 bits are not availible. The 34d339b133SRichard Cochran * frequency is adjusted by changing the 32 bit fractional nanoseconds 35d339b133SRichard Cochran * register, TIMINCA. 36d339b133SRichard Cochran * 37d339b133SRichard Cochran * For the 82576, the SYSTIM register time unit is affect by the 38d339b133SRichard Cochran * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this 39d339b133SRichard Cochran * field are needed to provide the nominal 16 nanosecond period, 40d339b133SRichard Cochran * leaving 19 bits for fractional nanoseconds. 41d339b133SRichard Cochran * 427ebae817SRichard Cochran * We scale the NIC clock cycle by a large factor so that relatively 437ebae817SRichard Cochran * small clock corrections can be added or subtracted at each clock 447ebae817SRichard Cochran * tick. The drawbacks of a large factor are a) that the clock 457ebae817SRichard Cochran * register overflows more quickly (not such a big deal) and b) that 467ebae817SRichard Cochran * the increment per tick has to fit into 24 bits. As a result we 477ebae817SRichard Cochran * need to use a shift of 19 so we can fit a value of 16 into the 487ebae817SRichard Cochran * TIMINCA register. 497ebae817SRichard Cochran * 50d339b133SRichard Cochran * 51d339b133SRichard Cochran * SYSTIMH SYSTIML 52d339b133SRichard Cochran * +--------------+ +---+---+------+ 53d339b133SRichard Cochran * 82576 | 32 | | 8 | 5 | 19 | 54d339b133SRichard Cochran * +--------------+ +---+---+------+ 55d339b133SRichard Cochran * \________ 45 bits _______/ fract 56d339b133SRichard Cochran * 57d339b133SRichard Cochran * +----------+---+ +--------------+ 58d339b133SRichard Cochran * 82580 | 24 | 8 | | 32 | 59d339b133SRichard Cochran * +----------+---+ +--------------+ 60d339b133SRichard Cochran * reserved \______ 40 bits _____/ 61d339b133SRichard Cochran * 62d339b133SRichard Cochran * 63d339b133SRichard Cochran * The 45 bit 82576 SYSTIM overflows every 64d339b133SRichard Cochran * 2^45 * 10^-9 / 3600 = 9.77 hours. 65d339b133SRichard Cochran * 66d339b133SRichard Cochran * The 40 bit 82580 SYSTIM overflows every 67d339b133SRichard Cochran * 2^40 * 10^-9 / 60 = 18.3 minutes. 68d339b133SRichard Cochran */ 69d339b133SRichard Cochran 70a79f4f88SMatthew Vick #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9) 71428f1f71SMatthew Vick #define IGB_PTP_TX_TIMEOUT (HZ * 15) 72d339b133SRichard Cochran #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT) 73d339b133SRichard Cochran #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1) 74d339b133SRichard Cochran #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT) 75d339b133SRichard Cochran #define IGB_NBITS_82580 40 76d339b133SRichard Cochran 77167f3f71SJeff Kirsher static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter); 78167f3f71SJeff Kirsher 79b980ac18SJeff Kirsher /* SYSTIM read access for the 82576 */ 80a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc) 81d339b133SRichard Cochran { 82d339b133SRichard Cochran struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc); 83d339b133SRichard Cochran struct e1000_hw *hw = &igb->hw; 84a79f4f88SMatthew Vick u64 val; 85a79f4f88SMatthew Vick u32 lo, hi; 86d339b133SRichard Cochran 87d339b133SRichard Cochran lo = rd32(E1000_SYSTIML); 88d339b133SRichard Cochran hi = rd32(E1000_SYSTIMH); 89d339b133SRichard Cochran 90d339b133SRichard Cochran val = ((u64) hi) << 32; 91d339b133SRichard Cochran val |= lo; 92d339b133SRichard Cochran 93d339b133SRichard Cochran return val; 94d339b133SRichard Cochran } 95d339b133SRichard Cochran 96b980ac18SJeff Kirsher /* SYSTIM read access for the 82580 */ 97a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc) 98d339b133SRichard Cochran { 99d339b133SRichard Cochran struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc); 100d339b133SRichard Cochran struct e1000_hw *hw = &igb->hw; 101e5c3370fSAkeem G Abodunrin u32 lo, hi; 102a79f4f88SMatthew Vick u64 val; 103d339b133SRichard Cochran 104b980ac18SJeff Kirsher /* The timestamp latches on lowest register read. For the 82580 1057ebae817SRichard Cochran * the lowest register is SYSTIMR instead of SYSTIML. However we only 1067ebae817SRichard Cochran * need to provide nanosecond resolution, so we just ignore it. 1077ebae817SRichard Cochran */ 108e5c3370fSAkeem G Abodunrin rd32(E1000_SYSTIMR); 109d339b133SRichard Cochran lo = rd32(E1000_SYSTIML); 110d339b133SRichard Cochran hi = rd32(E1000_SYSTIMH); 111d339b133SRichard Cochran 112d339b133SRichard Cochran val = ((u64) hi) << 32; 113d339b133SRichard Cochran val |= lo; 114d339b133SRichard Cochran 115d339b133SRichard Cochran return val; 116d339b133SRichard Cochran } 117d339b133SRichard Cochran 118b980ac18SJeff Kirsher /* SYSTIM read access for I210/I211 */ 119e57b8bdbSMatthew Vick static void igb_ptp_read_i210(struct igb_adapter *adapter, struct timespec *ts) 120e57b8bdbSMatthew Vick { 121e57b8bdbSMatthew Vick struct e1000_hw *hw = &adapter->hw; 122e5c3370fSAkeem G Abodunrin u32 sec, nsec; 123e57b8bdbSMatthew Vick 124b980ac18SJeff Kirsher /* The timestamp latches on lowest register read. For I210/I211, the 125e57b8bdbSMatthew Vick * lowest register is SYSTIMR. Since we only need to provide nanosecond 126e57b8bdbSMatthew Vick * resolution, we can ignore it. 127e57b8bdbSMatthew Vick */ 128e5c3370fSAkeem G Abodunrin rd32(E1000_SYSTIMR); 129e57b8bdbSMatthew Vick nsec = rd32(E1000_SYSTIML); 130e57b8bdbSMatthew Vick sec = rd32(E1000_SYSTIMH); 131e57b8bdbSMatthew Vick 132e57b8bdbSMatthew Vick ts->tv_sec = sec; 133e57b8bdbSMatthew Vick ts->tv_nsec = nsec; 134e57b8bdbSMatthew Vick } 135e57b8bdbSMatthew Vick 136e57b8bdbSMatthew Vick static void igb_ptp_write_i210(struct igb_adapter *adapter, 137e57b8bdbSMatthew Vick const struct timespec *ts) 138e57b8bdbSMatthew Vick { 139e57b8bdbSMatthew Vick struct e1000_hw *hw = &adapter->hw; 140e57b8bdbSMatthew Vick 141b980ac18SJeff Kirsher /* Writing the SYSTIMR register is not necessary as it only provides 142e57b8bdbSMatthew Vick * sub-nanosecond resolution. 143e57b8bdbSMatthew Vick */ 144e57b8bdbSMatthew Vick wr32(E1000_SYSTIML, ts->tv_nsec); 145e57b8bdbSMatthew Vick wr32(E1000_SYSTIMH, ts->tv_sec); 146e57b8bdbSMatthew Vick } 147e57b8bdbSMatthew Vick 148a79f4f88SMatthew Vick /** 149a79f4f88SMatthew Vick * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp 150a79f4f88SMatthew Vick * @adapter: board private structure 151a79f4f88SMatthew Vick * @hwtstamps: timestamp structure to update 152a79f4f88SMatthew Vick * @systim: unsigned 64bit system time value. 153a79f4f88SMatthew Vick * 154a79f4f88SMatthew Vick * We need to convert the system time value stored in the RX/TXSTMP registers 155a79f4f88SMatthew Vick * into a hwtstamp which can be used by the upper level timestamping functions. 156a79f4f88SMatthew Vick * 157a79f4f88SMatthew Vick * The 'tmreg_lock' spinlock is used to protect the consistency of the 158a79f4f88SMatthew Vick * system time value. This is needed because reading the 64 bit time 159a79f4f88SMatthew Vick * value involves reading two (or three) 32 bit registers. The first 160a79f4f88SMatthew Vick * read latches the value. Ditto for writing. 161a79f4f88SMatthew Vick * 162a79f4f88SMatthew Vick * In addition, here have extended the system time with an overflow 163a79f4f88SMatthew Vick * counter in software. 164a79f4f88SMatthew Vick **/ 165a79f4f88SMatthew Vick static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter, 166a79f4f88SMatthew Vick struct skb_shared_hwtstamps *hwtstamps, 167a79f4f88SMatthew Vick u64 systim) 168a79f4f88SMatthew Vick { 169a79f4f88SMatthew Vick unsigned long flags; 170a79f4f88SMatthew Vick u64 ns; 171a79f4f88SMatthew Vick 172a79f4f88SMatthew Vick switch (adapter->hw.mac.type) { 173a79f4f88SMatthew Vick case e1000_82576: 174e57b8bdbSMatthew Vick case e1000_82580: 175ceb5f13bSCarolyn Wyborny case e1000_i354: 176e57b8bdbSMatthew Vick case e1000_i350: 177a79f4f88SMatthew Vick spin_lock_irqsave(&adapter->tmreg_lock, flags); 178a79f4f88SMatthew Vick 179a79f4f88SMatthew Vick ns = timecounter_cyc2time(&adapter->tc, systim); 180a79f4f88SMatthew Vick 181a79f4f88SMatthew Vick spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 182a79f4f88SMatthew Vick 183a79f4f88SMatthew Vick memset(hwtstamps, 0, sizeof(*hwtstamps)); 184a79f4f88SMatthew Vick hwtstamps->hwtstamp = ns_to_ktime(ns); 185e57b8bdbSMatthew Vick break; 186e57b8bdbSMatthew Vick case e1000_i210: 187e57b8bdbSMatthew Vick case e1000_i211: 188e57b8bdbSMatthew Vick memset(hwtstamps, 0, sizeof(*hwtstamps)); 189e57b8bdbSMatthew Vick /* Upper 32 bits contain s, lower 32 bits contain ns. */ 190e57b8bdbSMatthew Vick hwtstamps->hwtstamp = ktime_set(systim >> 32, 191e57b8bdbSMatthew Vick systim & 0xFFFFFFFF); 192e57b8bdbSMatthew Vick break; 193e57b8bdbSMatthew Vick default: 194e57b8bdbSMatthew Vick break; 195e57b8bdbSMatthew Vick } 196a79f4f88SMatthew Vick } 197a79f4f88SMatthew Vick 198b980ac18SJeff Kirsher /* PTP clock operations */ 199a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb) 200d339b133SRichard Cochran { 201a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 202a79f4f88SMatthew Vick ptp_caps); 203a79f4f88SMatthew Vick struct e1000_hw *hw = &igb->hw; 204a79f4f88SMatthew Vick int neg_adj = 0; 205d339b133SRichard Cochran u64 rate; 206d339b133SRichard Cochran u32 incvalue; 207d339b133SRichard Cochran 208d339b133SRichard Cochran if (ppb < 0) { 209d339b133SRichard Cochran neg_adj = 1; 210d339b133SRichard Cochran ppb = -ppb; 211d339b133SRichard Cochran } 212d339b133SRichard Cochran rate = ppb; 213d339b133SRichard Cochran rate <<= 14; 214d339b133SRichard Cochran rate = div_u64(rate, 1953125); 215d339b133SRichard Cochran 216d339b133SRichard Cochran incvalue = 16 << IGB_82576_TSYNC_SHIFT; 217d339b133SRichard Cochran 218d339b133SRichard Cochran if (neg_adj) 219d339b133SRichard Cochran incvalue -= rate; 220d339b133SRichard Cochran else 221d339b133SRichard Cochran incvalue += rate; 222d339b133SRichard Cochran 223d339b133SRichard Cochran wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK)); 224d339b133SRichard Cochran 225d339b133SRichard Cochran return 0; 226d339b133SRichard Cochran } 227d339b133SRichard Cochran 228a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb) 229d339b133SRichard Cochran { 230a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 231a79f4f88SMatthew Vick ptp_caps); 232a79f4f88SMatthew Vick struct e1000_hw *hw = &igb->hw; 233a79f4f88SMatthew Vick int neg_adj = 0; 234d339b133SRichard Cochran u64 rate; 235d339b133SRichard Cochran u32 inca; 236d339b133SRichard Cochran 237d339b133SRichard Cochran if (ppb < 0) { 238d339b133SRichard Cochran neg_adj = 1; 239d339b133SRichard Cochran ppb = -ppb; 240d339b133SRichard Cochran } 241d339b133SRichard Cochran rate = ppb; 242d339b133SRichard Cochran rate <<= 26; 243d339b133SRichard Cochran rate = div_u64(rate, 1953125); 244d339b133SRichard Cochran 245d339b133SRichard Cochran inca = rate & INCVALUE_MASK; 246d339b133SRichard Cochran if (neg_adj) 247d339b133SRichard Cochran inca |= ISGN; 248d339b133SRichard Cochran 249d339b133SRichard Cochran wr32(E1000_TIMINCA, inca); 250d339b133SRichard Cochran 251d339b133SRichard Cochran return 0; 252d339b133SRichard Cochran } 253d339b133SRichard Cochran 254e57b8bdbSMatthew Vick static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta) 255d339b133SRichard Cochran { 256a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 257a79f4f88SMatthew Vick ptp_caps); 258d339b133SRichard Cochran unsigned long flags; 259a79f4f88SMatthew Vick s64 now; 260d339b133SRichard Cochran 261d339b133SRichard Cochran spin_lock_irqsave(&igb->tmreg_lock, flags); 262d339b133SRichard Cochran 263d339b133SRichard Cochran now = timecounter_read(&igb->tc); 264d339b133SRichard Cochran now += delta; 265d339b133SRichard Cochran timecounter_init(&igb->tc, &igb->cc, now); 266d339b133SRichard Cochran 267d339b133SRichard Cochran spin_unlock_irqrestore(&igb->tmreg_lock, flags); 268d339b133SRichard Cochran 269d339b133SRichard Cochran return 0; 270d339b133SRichard Cochran } 271d339b133SRichard Cochran 272e57b8bdbSMatthew Vick static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta) 273e57b8bdbSMatthew Vick { 274e57b8bdbSMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 275e57b8bdbSMatthew Vick ptp_caps); 276e57b8bdbSMatthew Vick unsigned long flags; 277e57b8bdbSMatthew Vick struct timespec now, then = ns_to_timespec(delta); 278e57b8bdbSMatthew Vick 279e57b8bdbSMatthew Vick spin_lock_irqsave(&igb->tmreg_lock, flags); 280e57b8bdbSMatthew Vick 281e57b8bdbSMatthew Vick igb_ptp_read_i210(igb, &now); 282e57b8bdbSMatthew Vick now = timespec_add(now, then); 283e57b8bdbSMatthew Vick igb_ptp_write_i210(igb, (const struct timespec *)&now); 284e57b8bdbSMatthew Vick 285e57b8bdbSMatthew Vick spin_unlock_irqrestore(&igb->tmreg_lock, flags); 286e57b8bdbSMatthew Vick 287e57b8bdbSMatthew Vick return 0; 288e57b8bdbSMatthew Vick } 289e57b8bdbSMatthew Vick 290e57b8bdbSMatthew Vick static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp, 291e57b8bdbSMatthew Vick struct timespec *ts) 292d339b133SRichard Cochran { 293a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 294a79f4f88SMatthew Vick ptp_caps); 295a79f4f88SMatthew Vick unsigned long flags; 296d339b133SRichard Cochran u64 ns; 297d339b133SRichard Cochran u32 remainder; 298d339b133SRichard Cochran 299d339b133SRichard Cochran spin_lock_irqsave(&igb->tmreg_lock, flags); 300d339b133SRichard Cochran 301d339b133SRichard Cochran ns = timecounter_read(&igb->tc); 302d339b133SRichard Cochran 303d339b133SRichard Cochran spin_unlock_irqrestore(&igb->tmreg_lock, flags); 304d339b133SRichard Cochran 305d339b133SRichard Cochran ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder); 306d339b133SRichard Cochran ts->tv_nsec = remainder; 307d339b133SRichard Cochran 308d339b133SRichard Cochran return 0; 309d339b133SRichard Cochran } 310d339b133SRichard Cochran 311e57b8bdbSMatthew Vick static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp, 312e57b8bdbSMatthew Vick struct timespec *ts) 313e57b8bdbSMatthew Vick { 314e57b8bdbSMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 315e57b8bdbSMatthew Vick ptp_caps); 316e57b8bdbSMatthew Vick unsigned long flags; 317e57b8bdbSMatthew Vick 318e57b8bdbSMatthew Vick spin_lock_irqsave(&igb->tmreg_lock, flags); 319e57b8bdbSMatthew Vick 320e57b8bdbSMatthew Vick igb_ptp_read_i210(igb, ts); 321e57b8bdbSMatthew Vick 322e57b8bdbSMatthew Vick spin_unlock_irqrestore(&igb->tmreg_lock, flags); 323e57b8bdbSMatthew Vick 324e57b8bdbSMatthew Vick return 0; 325e57b8bdbSMatthew Vick } 326e57b8bdbSMatthew Vick 327e57b8bdbSMatthew Vick static int igb_ptp_settime_82576(struct ptp_clock_info *ptp, 328a79f4f88SMatthew Vick const struct timespec *ts) 329d339b133SRichard Cochran { 330a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 331a79f4f88SMatthew Vick ptp_caps); 332d339b133SRichard Cochran unsigned long flags; 333a79f4f88SMatthew Vick u64 ns; 334d339b133SRichard Cochran 335d339b133SRichard Cochran ns = ts->tv_sec * 1000000000ULL; 336d339b133SRichard Cochran ns += ts->tv_nsec; 337d339b133SRichard Cochran 338d339b133SRichard Cochran spin_lock_irqsave(&igb->tmreg_lock, flags); 339d339b133SRichard Cochran 340d339b133SRichard Cochran timecounter_init(&igb->tc, &igb->cc, ns); 341d339b133SRichard Cochran 342d339b133SRichard Cochran spin_unlock_irqrestore(&igb->tmreg_lock, flags); 343d339b133SRichard Cochran 344d339b133SRichard Cochran return 0; 345d339b133SRichard Cochran } 346d339b133SRichard Cochran 347e57b8bdbSMatthew Vick static int igb_ptp_settime_i210(struct ptp_clock_info *ptp, 348e57b8bdbSMatthew Vick const struct timespec *ts) 349e57b8bdbSMatthew Vick { 350e57b8bdbSMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 351e57b8bdbSMatthew Vick ptp_caps); 352e57b8bdbSMatthew Vick unsigned long flags; 353e57b8bdbSMatthew Vick 354e57b8bdbSMatthew Vick spin_lock_irqsave(&igb->tmreg_lock, flags); 355e57b8bdbSMatthew Vick 356e57b8bdbSMatthew Vick igb_ptp_write_i210(igb, ts); 357e57b8bdbSMatthew Vick 358e57b8bdbSMatthew Vick spin_unlock_irqrestore(&igb->tmreg_lock, flags); 359e57b8bdbSMatthew Vick 360e57b8bdbSMatthew Vick return 0; 361e57b8bdbSMatthew Vick } 362e57b8bdbSMatthew Vick 363a79f4f88SMatthew Vick static int igb_ptp_enable(struct ptp_clock_info *ptp, 364d339b133SRichard Cochran struct ptp_clock_request *rq, int on) 365d339b133SRichard Cochran { 366d339b133SRichard Cochran return -EOPNOTSUPP; 367d339b133SRichard Cochran } 368d339b133SRichard Cochran 3691f6e8178SMatthew Vick /** 3701f6e8178SMatthew Vick * igb_ptp_tx_work 3711f6e8178SMatthew Vick * @work: pointer to work struct 3721f6e8178SMatthew Vick * 3731f6e8178SMatthew Vick * This work function polls the TSYNCTXCTL valid bit to determine when a 3741f6e8178SMatthew Vick * timestamp has been taken for the current stored skb. 375b980ac18SJeff Kirsher **/ 376167f3f71SJeff Kirsher static void igb_ptp_tx_work(struct work_struct *work) 3771f6e8178SMatthew Vick { 3781f6e8178SMatthew Vick struct igb_adapter *adapter = container_of(work, struct igb_adapter, 3791f6e8178SMatthew Vick ptp_tx_work); 3801f6e8178SMatthew Vick struct e1000_hw *hw = &adapter->hw; 3811f6e8178SMatthew Vick u32 tsynctxctl; 3821f6e8178SMatthew Vick 3831f6e8178SMatthew Vick if (!adapter->ptp_tx_skb) 3841f6e8178SMatthew Vick return; 3851f6e8178SMatthew Vick 386428f1f71SMatthew Vick if (time_is_before_jiffies(adapter->ptp_tx_start + 387428f1f71SMatthew Vick IGB_PTP_TX_TIMEOUT)) { 388428f1f71SMatthew Vick dev_kfree_skb_any(adapter->ptp_tx_skb); 389428f1f71SMatthew Vick adapter->ptp_tx_skb = NULL; 390428f1f71SMatthew Vick adapter->tx_hwtstamp_timeouts++; 391428f1f71SMatthew Vick dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang"); 392428f1f71SMatthew Vick return; 393428f1f71SMatthew Vick } 394428f1f71SMatthew Vick 3951f6e8178SMatthew Vick tsynctxctl = rd32(E1000_TSYNCTXCTL); 3961f6e8178SMatthew Vick if (tsynctxctl & E1000_TSYNCTXCTL_VALID) 3971f6e8178SMatthew Vick igb_ptp_tx_hwtstamp(adapter); 3981f6e8178SMatthew Vick else 3991f6e8178SMatthew Vick /* reschedule to check later */ 4001f6e8178SMatthew Vick schedule_work(&adapter->ptp_tx_work); 4011f6e8178SMatthew Vick } 4021f6e8178SMatthew Vick 403a79f4f88SMatthew Vick static void igb_ptp_overflow_check(struct work_struct *work) 404d339b133SRichard Cochran { 405d339b133SRichard Cochran struct igb_adapter *igb = 406a79f4f88SMatthew Vick container_of(work, struct igb_adapter, ptp_overflow_work.work); 407a79f4f88SMatthew Vick struct timespec ts; 408d339b133SRichard Cochran 409e57b8bdbSMatthew Vick igb->ptp_caps.gettime(&igb->ptp_caps, &ts); 410d339b133SRichard Cochran 411d339b133SRichard Cochran pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec); 412d339b133SRichard Cochran 413a79f4f88SMatthew Vick schedule_delayed_work(&igb->ptp_overflow_work, 414a79f4f88SMatthew Vick IGB_SYSTIM_OVERFLOW_PERIOD); 415a79f4f88SMatthew Vick } 416a79f4f88SMatthew Vick 417a79f4f88SMatthew Vick /** 418fc580751SMatthew Vick * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched 419fc580751SMatthew Vick * @adapter: private network adapter structure 420fc580751SMatthew Vick * 421fc580751SMatthew Vick * This watchdog task is scheduled to detect error case where hardware has 422fc580751SMatthew Vick * dropped an Rx packet that was timestamped when the ring is full. The 423fc580751SMatthew Vick * particular error is rare but leaves the device in a state unable to timestamp 424fc580751SMatthew Vick * any future packets. 425b980ac18SJeff Kirsher **/ 426fc580751SMatthew Vick void igb_ptp_rx_hang(struct igb_adapter *adapter) 427fc580751SMatthew Vick { 428fc580751SMatthew Vick struct e1000_hw *hw = &adapter->hw; 429fc580751SMatthew Vick struct igb_ring *rx_ring; 430fc580751SMatthew Vick u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL); 431fc580751SMatthew Vick unsigned long rx_event; 432fc580751SMatthew Vick int n; 433fc580751SMatthew Vick 434fc580751SMatthew Vick if (hw->mac.type != e1000_82576) 435fc580751SMatthew Vick return; 436fc580751SMatthew Vick 437fc580751SMatthew Vick /* If we don't have a valid timestamp in the registers, just update the 438fc580751SMatthew Vick * timeout counter and exit 439fc580751SMatthew Vick */ 440fc580751SMatthew Vick if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) { 441fc580751SMatthew Vick adapter->last_rx_ptp_check = jiffies; 442fc580751SMatthew Vick return; 443fc580751SMatthew Vick } 444fc580751SMatthew Vick 445fc580751SMatthew Vick /* Determine the most recent watchdog or rx_timestamp event */ 446fc580751SMatthew Vick rx_event = adapter->last_rx_ptp_check; 447fc580751SMatthew Vick for (n = 0; n < adapter->num_rx_queues; n++) { 448fc580751SMatthew Vick rx_ring = adapter->rx_ring[n]; 449fc580751SMatthew Vick if (time_after(rx_ring->last_rx_timestamp, rx_event)) 450fc580751SMatthew Vick rx_event = rx_ring->last_rx_timestamp; 451fc580751SMatthew Vick } 452fc580751SMatthew Vick 453fc580751SMatthew Vick /* Only need to read the high RXSTMP register to clear the lock */ 454fc580751SMatthew Vick if (time_is_before_jiffies(rx_event + 5 * HZ)) { 455fc580751SMatthew Vick rd32(E1000_RXSTMPH); 456fc580751SMatthew Vick adapter->last_rx_ptp_check = jiffies; 457fc580751SMatthew Vick adapter->rx_hwtstamp_cleared++; 458fc580751SMatthew Vick dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang"); 459fc580751SMatthew Vick } 460fc580751SMatthew Vick } 461fc580751SMatthew Vick 462fc580751SMatthew Vick /** 463a79f4f88SMatthew Vick * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp 4641f6e8178SMatthew Vick * @adapter: Board private structure. 465a79f4f88SMatthew Vick * 466a79f4f88SMatthew Vick * If we were asked to do hardware stamping and such a time stamp is 467a79f4f88SMatthew Vick * available, then it must have been for this skb here because we only 468a79f4f88SMatthew Vick * allow only one such packet into the queue. 469b980ac18SJeff Kirsher **/ 470167f3f71SJeff Kirsher static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter) 471a79f4f88SMatthew Vick { 472a79f4f88SMatthew Vick struct e1000_hw *hw = &adapter->hw; 473a79f4f88SMatthew Vick struct skb_shared_hwtstamps shhwtstamps; 474a79f4f88SMatthew Vick u64 regval; 475a79f4f88SMatthew Vick 476a79f4f88SMatthew Vick regval = rd32(E1000_TXSTMPL); 477a79f4f88SMatthew Vick regval |= (u64)rd32(E1000_TXSTMPH) << 32; 478a79f4f88SMatthew Vick 479a79f4f88SMatthew Vick igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval); 4801f6e8178SMatthew Vick skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps); 4811f6e8178SMatthew Vick dev_kfree_skb_any(adapter->ptp_tx_skb); 4821f6e8178SMatthew Vick adapter->ptp_tx_skb = NULL; 483a79f4f88SMatthew Vick } 484a79f4f88SMatthew Vick 485b534550aSAlexander Duyck /** 486b534550aSAlexander Duyck * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp 487b534550aSAlexander Duyck * @q_vector: Pointer to interrupt specific structure 488b534550aSAlexander Duyck * @va: Pointer to address containing Rx buffer 489b534550aSAlexander Duyck * @skb: Buffer containing timestamp and packet 490b534550aSAlexander Duyck * 491b534550aSAlexander Duyck * This function is meant to retrieve a timestamp from the first buffer of an 492b534550aSAlexander Duyck * incoming frame. The value is stored in little endian format starting on 493b534550aSAlexander Duyck * byte 8. 494b980ac18SJeff Kirsher **/ 495b534550aSAlexander Duyck void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector, 496b534550aSAlexander Duyck unsigned char *va, 497b534550aSAlexander Duyck struct sk_buff *skb) 498b534550aSAlexander Duyck { 499ac61d515SAlexander Duyck __le64 *regval = (__le64 *)va; 500b534550aSAlexander Duyck 501b980ac18SJeff Kirsher /* The timestamp is recorded in little endian format. 502b534550aSAlexander Duyck * DWORD: 0 1 2 3 503b534550aSAlexander Duyck * Field: Reserved Reserved SYSTIML SYSTIMH 504b534550aSAlexander Duyck */ 505b534550aSAlexander Duyck igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb), 506b534550aSAlexander Duyck le64_to_cpu(regval[1])); 507b534550aSAlexander Duyck } 508b534550aSAlexander Duyck 509b534550aSAlexander Duyck /** 510b534550aSAlexander Duyck * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register 511b534550aSAlexander Duyck * @q_vector: Pointer to interrupt specific structure 512b534550aSAlexander Duyck * @skb: Buffer containing timestamp and packet 513b534550aSAlexander Duyck * 514b534550aSAlexander Duyck * This function is meant to retrieve a timestamp from the internal registers 515b534550aSAlexander Duyck * of the adapter and store it in the skb. 516b980ac18SJeff Kirsher **/ 517b534550aSAlexander Duyck void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector, 518a79f4f88SMatthew Vick struct sk_buff *skb) 519a79f4f88SMatthew Vick { 520a79f4f88SMatthew Vick struct igb_adapter *adapter = q_vector->adapter; 521a79f4f88SMatthew Vick struct e1000_hw *hw = &adapter->hw; 522a79f4f88SMatthew Vick u64 regval; 523a79f4f88SMatthew Vick 524b980ac18SJeff Kirsher /* If this bit is set, then the RX registers contain the time stamp. No 525a79f4f88SMatthew Vick * other packet will be time stamped until we read these registers, so 526a79f4f88SMatthew Vick * read the registers to make them available again. Because only one 527a79f4f88SMatthew Vick * packet can be time stamped at a time, we know that the register 528a79f4f88SMatthew Vick * values must belong to this one here and therefore we don't need to 529a79f4f88SMatthew Vick * compare any of the additional attributes stored for it. 530a79f4f88SMatthew Vick * 531a79f4f88SMatthew Vick * If nothing went wrong, then it should have a shared tx_flags that we 532a79f4f88SMatthew Vick * can turn into a skb_shared_hwtstamps. 533a79f4f88SMatthew Vick */ 534a79f4f88SMatthew Vick if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID)) 535a79f4f88SMatthew Vick return; 536a79f4f88SMatthew Vick 537a79f4f88SMatthew Vick regval = rd32(E1000_RXSTMPL); 538a79f4f88SMatthew Vick regval |= (u64)rd32(E1000_RXSTMPH) << 32; 539a79f4f88SMatthew Vick 540a79f4f88SMatthew Vick igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval); 541a79f4f88SMatthew Vick } 542a79f4f88SMatthew Vick 543a79f4f88SMatthew Vick /** 544a79f4f88SMatthew Vick * igb_ptp_hwtstamp_ioctl - control hardware time stamping 545a79f4f88SMatthew Vick * @netdev: 546a79f4f88SMatthew Vick * @ifreq: 547a79f4f88SMatthew Vick * @cmd: 548a79f4f88SMatthew Vick * 549a79f4f88SMatthew Vick * Outgoing time stamping can be enabled and disabled. Play nice and 550a79f4f88SMatthew Vick * disable it when requested, although it shouldn't case any overhead 551a79f4f88SMatthew Vick * when no packet needs it. At most one packet in the queue may be 552a79f4f88SMatthew Vick * marked for time stamping, otherwise it would be impossible to tell 553a79f4f88SMatthew Vick * for sure to which packet the hardware time stamp belongs. 554a79f4f88SMatthew Vick * 555a79f4f88SMatthew Vick * Incoming time stamping has to be configured via the hardware 556a79f4f88SMatthew Vick * filters. Not all combinations are supported, in particular event 557a79f4f88SMatthew Vick * type has to be specified. Matching the kind of event packet is 558a79f4f88SMatthew Vick * not supported, with the exception of "all V2 events regardless of 559a79f4f88SMatthew Vick * level 2 or 4". 560a79f4f88SMatthew Vick **/ 561a79f4f88SMatthew Vick int igb_ptp_hwtstamp_ioctl(struct net_device *netdev, 562a79f4f88SMatthew Vick struct ifreq *ifr, int cmd) 563a79f4f88SMatthew Vick { 564a79f4f88SMatthew Vick struct igb_adapter *adapter = netdev_priv(netdev); 565a79f4f88SMatthew Vick struct e1000_hw *hw = &adapter->hw; 566a79f4f88SMatthew Vick struct hwtstamp_config config; 567a79f4f88SMatthew Vick u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED; 568a79f4f88SMatthew Vick u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED; 569a79f4f88SMatthew Vick u32 tsync_rx_cfg = 0; 570a79f4f88SMatthew Vick bool is_l4 = false; 571a79f4f88SMatthew Vick bool is_l2 = false; 572a79f4f88SMatthew Vick u32 regval; 573a79f4f88SMatthew Vick 574a79f4f88SMatthew Vick if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 575a79f4f88SMatthew Vick return -EFAULT; 576a79f4f88SMatthew Vick 577a79f4f88SMatthew Vick /* reserved for future extensions */ 578a79f4f88SMatthew Vick if (config.flags) 579a79f4f88SMatthew Vick return -EINVAL; 580a79f4f88SMatthew Vick 581a79f4f88SMatthew Vick switch (config.tx_type) { 582a79f4f88SMatthew Vick case HWTSTAMP_TX_OFF: 583a79f4f88SMatthew Vick tsync_tx_ctl = 0; 584a79f4f88SMatthew Vick case HWTSTAMP_TX_ON: 585a79f4f88SMatthew Vick break; 586a79f4f88SMatthew Vick default: 587a79f4f88SMatthew Vick return -ERANGE; 588a79f4f88SMatthew Vick } 589a79f4f88SMatthew Vick 590a79f4f88SMatthew Vick switch (config.rx_filter) { 591a79f4f88SMatthew Vick case HWTSTAMP_FILTER_NONE: 592a79f4f88SMatthew Vick tsync_rx_ctl = 0; 593a79f4f88SMatthew Vick break; 594a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 595a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1; 596a79f4f88SMatthew Vick tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE; 597a79f4f88SMatthew Vick is_l4 = true; 598a79f4f88SMatthew Vick break; 599a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 600a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1; 601a79f4f88SMatthew Vick tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE; 602a79f4f88SMatthew Vick is_l4 = true; 603a79f4f88SMatthew Vick break; 6043e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_EVENT: 6053e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 6063e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 6073e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_SYNC: 608a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 609a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 6103e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 611a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 612a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 613a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2; 614a79f4f88SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 615a79f4f88SMatthew Vick is_l2 = true; 616a79f4f88SMatthew Vick is_l4 = true; 617a79f4f88SMatthew Vick break; 6183e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 6193e961a06SMatthew Vick case HWTSTAMP_FILTER_ALL: 6203e961a06SMatthew Vick /* 82576 cannot timestamp all packets, which it needs to do to 6213e961a06SMatthew Vick * support both V1 Sync and Delay_Req messages 6223e961a06SMatthew Vick */ 6233e961a06SMatthew Vick if (hw->mac.type != e1000_82576) { 6243e961a06SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL; 6253e961a06SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_ALL; 6263e961a06SMatthew Vick break; 6273e961a06SMatthew Vick } 6283e961a06SMatthew Vick /* fall through */ 629a79f4f88SMatthew Vick default: 6303e961a06SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_NONE; 631a79f4f88SMatthew Vick return -ERANGE; 632a79f4f88SMatthew Vick } 633a79f4f88SMatthew Vick 634a79f4f88SMatthew Vick if (hw->mac.type == e1000_82575) { 635a79f4f88SMatthew Vick if (tsync_rx_ctl | tsync_tx_ctl) 636a79f4f88SMatthew Vick return -EINVAL; 637a79f4f88SMatthew Vick return 0; 638a79f4f88SMatthew Vick } 639a79f4f88SMatthew Vick 640b980ac18SJeff Kirsher /* Per-packet timestamping only works if all packets are 641a79f4f88SMatthew Vick * timestamped, so enable timestamping in all packets as 642b980ac18SJeff Kirsher * long as one Rx filter was configured. 643a79f4f88SMatthew Vick */ 644a79f4f88SMatthew Vick if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) { 645a79f4f88SMatthew Vick tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED; 646a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL; 6473e961a06SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_ALL; 6483e961a06SMatthew Vick is_l2 = true; 6493e961a06SMatthew Vick is_l4 = true; 650e57b8bdbSMatthew Vick 651e57b8bdbSMatthew Vick if ((hw->mac.type == e1000_i210) || 652e57b8bdbSMatthew Vick (hw->mac.type == e1000_i211)) { 653e57b8bdbSMatthew Vick regval = rd32(E1000_RXPBS); 654e57b8bdbSMatthew Vick regval |= E1000_RXPBS_CFG_TS_EN; 655e57b8bdbSMatthew Vick wr32(E1000_RXPBS, regval); 656e57b8bdbSMatthew Vick } 657a79f4f88SMatthew Vick } 658a79f4f88SMatthew Vick 659a79f4f88SMatthew Vick /* enable/disable TX */ 660a79f4f88SMatthew Vick regval = rd32(E1000_TSYNCTXCTL); 661a79f4f88SMatthew Vick regval &= ~E1000_TSYNCTXCTL_ENABLED; 662a79f4f88SMatthew Vick regval |= tsync_tx_ctl; 663a79f4f88SMatthew Vick wr32(E1000_TSYNCTXCTL, regval); 664a79f4f88SMatthew Vick 665a79f4f88SMatthew Vick /* enable/disable RX */ 666a79f4f88SMatthew Vick regval = rd32(E1000_TSYNCRXCTL); 667a79f4f88SMatthew Vick regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK); 668a79f4f88SMatthew Vick regval |= tsync_rx_ctl; 669a79f4f88SMatthew Vick wr32(E1000_TSYNCRXCTL, regval); 670a79f4f88SMatthew Vick 671a79f4f88SMatthew Vick /* define which PTP packets are time stamped */ 672a79f4f88SMatthew Vick wr32(E1000_TSYNCRXCFG, tsync_rx_cfg); 673a79f4f88SMatthew Vick 674a79f4f88SMatthew Vick /* define ethertype filter for timestamped packets */ 675a79f4f88SMatthew Vick if (is_l2) 676a79f4f88SMatthew Vick wr32(E1000_ETQF(3), 677a79f4f88SMatthew Vick (E1000_ETQF_FILTER_ENABLE | /* enable filter */ 678a79f4f88SMatthew Vick E1000_ETQF_1588 | /* enable timestamping */ 679a79f4f88SMatthew Vick ETH_P_1588)); /* 1588 eth protocol type */ 680a79f4f88SMatthew Vick else 681a79f4f88SMatthew Vick wr32(E1000_ETQF(3), 0); 682a79f4f88SMatthew Vick 683a79f4f88SMatthew Vick /* L4 Queue Filter[3]: filter by destination port and protocol */ 684a79f4f88SMatthew Vick if (is_l4) { 685a79f4f88SMatthew Vick u32 ftqf = (IPPROTO_UDP /* UDP */ 686a79f4f88SMatthew Vick | E1000_FTQF_VF_BP /* VF not compared */ 687a79f4f88SMatthew Vick | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */ 688a79f4f88SMatthew Vick | E1000_FTQF_MASK); /* mask all inputs */ 689a79f4f88SMatthew Vick ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */ 690a79f4f88SMatthew Vick 691ba59814bSMatthew Vick wr32(E1000_IMIR(3), htons(PTP_EV_PORT)); 692a79f4f88SMatthew Vick wr32(E1000_IMIREXT(3), 693a79f4f88SMatthew Vick (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP)); 694a79f4f88SMatthew Vick if (hw->mac.type == e1000_82576) { 695a79f4f88SMatthew Vick /* enable source port check */ 696ba59814bSMatthew Vick wr32(E1000_SPQF(3), htons(PTP_EV_PORT)); 697a79f4f88SMatthew Vick ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP; 698a79f4f88SMatthew Vick } 699a79f4f88SMatthew Vick wr32(E1000_FTQF(3), ftqf); 700a79f4f88SMatthew Vick } else { 701a79f4f88SMatthew Vick wr32(E1000_FTQF(3), E1000_FTQF_MASK); 702a79f4f88SMatthew Vick } 703a79f4f88SMatthew Vick wrfl(); 704a79f4f88SMatthew Vick 705a79f4f88SMatthew Vick /* clear TX/RX time stamp registers, just to be sure */ 706e57b8bdbSMatthew Vick regval = rd32(E1000_TXSTMPL); 707a79f4f88SMatthew Vick regval = rd32(E1000_TXSTMPH); 708e57b8bdbSMatthew Vick regval = rd32(E1000_RXSTMPL); 709a79f4f88SMatthew Vick regval = rd32(E1000_RXSTMPH); 710a79f4f88SMatthew Vick 711a79f4f88SMatthew Vick return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 712a79f4f88SMatthew Vick -EFAULT : 0; 713d339b133SRichard Cochran } 714d339b133SRichard Cochran 715d339b133SRichard Cochran void igb_ptp_init(struct igb_adapter *adapter) 716d339b133SRichard Cochran { 717d339b133SRichard Cochran struct e1000_hw *hw = &adapter->hw; 718201987e3SMatthew Vick struct net_device *netdev = adapter->netdev; 719d339b133SRichard Cochran 720d339b133SRichard Cochran switch (hw->mac.type) { 721d339b133SRichard Cochran case e1000_82576: 722201987e3SMatthew Vick snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); 723a79f4f88SMatthew Vick adapter->ptp_caps.owner = THIS_MODULE; 72475517d92SJiri Benc adapter->ptp_caps.max_adj = 999999881; 725a79f4f88SMatthew Vick adapter->ptp_caps.n_ext_ts = 0; 726a79f4f88SMatthew Vick adapter->ptp_caps.pps = 0; 727a79f4f88SMatthew Vick adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576; 728e57b8bdbSMatthew Vick adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576; 729e57b8bdbSMatthew Vick adapter->ptp_caps.gettime = igb_ptp_gettime_82576; 730e57b8bdbSMatthew Vick adapter->ptp_caps.settime = igb_ptp_settime_82576; 731a79f4f88SMatthew Vick adapter->ptp_caps.enable = igb_ptp_enable; 732a79f4f88SMatthew Vick adapter->cc.read = igb_ptp_read_82576; 733d339b133SRichard Cochran adapter->cc.mask = CLOCKSOURCE_MASK(64); 734d339b133SRichard Cochran adapter->cc.mult = 1; 735d339b133SRichard Cochran adapter->cc.shift = IGB_82576_TSYNC_SHIFT; 736d339b133SRichard Cochran /* Dial the nominal frequency. */ 737d339b133SRichard Cochran wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576); 738d339b133SRichard Cochran break; 739e57b8bdbSMatthew Vick case e1000_82580: 740ceb5f13bSCarolyn Wyborny case e1000_i354: 741e57b8bdbSMatthew Vick case e1000_i350: 742e57b8bdbSMatthew Vick snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); 743e57b8bdbSMatthew Vick adapter->ptp_caps.owner = THIS_MODULE; 744e57b8bdbSMatthew Vick adapter->ptp_caps.max_adj = 62499999; 745e57b8bdbSMatthew Vick adapter->ptp_caps.n_ext_ts = 0; 746e57b8bdbSMatthew Vick adapter->ptp_caps.pps = 0; 747e57b8bdbSMatthew Vick adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580; 748e57b8bdbSMatthew Vick adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576; 749e57b8bdbSMatthew Vick adapter->ptp_caps.gettime = igb_ptp_gettime_82576; 750e57b8bdbSMatthew Vick adapter->ptp_caps.settime = igb_ptp_settime_82576; 751e57b8bdbSMatthew Vick adapter->ptp_caps.enable = igb_ptp_enable; 752e57b8bdbSMatthew Vick adapter->cc.read = igb_ptp_read_82580; 753e57b8bdbSMatthew Vick adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580); 754e57b8bdbSMatthew Vick adapter->cc.mult = 1; 755e57b8bdbSMatthew Vick adapter->cc.shift = 0; 756e57b8bdbSMatthew Vick /* Enable the timer functions by clearing bit 31. */ 757e57b8bdbSMatthew Vick wr32(E1000_TSAUXC, 0x0); 758e57b8bdbSMatthew Vick break; 759e57b8bdbSMatthew Vick case e1000_i210: 760e57b8bdbSMatthew Vick case e1000_i211: 761e57b8bdbSMatthew Vick snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); 762e57b8bdbSMatthew Vick adapter->ptp_caps.owner = THIS_MODULE; 763e57b8bdbSMatthew Vick adapter->ptp_caps.max_adj = 62499999; 764e57b8bdbSMatthew Vick adapter->ptp_caps.n_ext_ts = 0; 765e57b8bdbSMatthew Vick adapter->ptp_caps.pps = 0; 766e57b8bdbSMatthew Vick adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580; 767e57b8bdbSMatthew Vick adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210; 768e57b8bdbSMatthew Vick adapter->ptp_caps.gettime = igb_ptp_gettime_i210; 769e57b8bdbSMatthew Vick adapter->ptp_caps.settime = igb_ptp_settime_i210; 770e57b8bdbSMatthew Vick adapter->ptp_caps.enable = igb_ptp_enable; 771e57b8bdbSMatthew Vick /* Enable the timer functions by clearing bit 31. */ 772e57b8bdbSMatthew Vick wr32(E1000_TSAUXC, 0x0); 773e57b8bdbSMatthew Vick break; 774d339b133SRichard Cochran default: 775d339b133SRichard Cochran adapter->ptp_clock = NULL; 776d339b133SRichard Cochran return; 777d339b133SRichard Cochran } 778d339b133SRichard Cochran 779d339b133SRichard Cochran wrfl(); 780d339b133SRichard Cochran 781e57b8bdbSMatthew Vick spin_lock_init(&adapter->tmreg_lock); 782e57b8bdbSMatthew Vick INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work); 783e57b8bdbSMatthew Vick 784e57b8bdbSMatthew Vick /* Initialize the clock and overflow work for devices that need it. */ 785e57b8bdbSMatthew Vick if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) { 786e57b8bdbSMatthew Vick struct timespec ts = ktime_to_timespec(ktime_get_real()); 787e57b8bdbSMatthew Vick 788e57b8bdbSMatthew Vick igb_ptp_settime_i210(&adapter->ptp_caps, &ts); 789e57b8bdbSMatthew Vick } else { 790d339b133SRichard Cochran timecounter_init(&adapter->tc, &adapter->cc, 791d339b133SRichard Cochran ktime_to_ns(ktime_get_real())); 792d339b133SRichard Cochran 793e57b8bdbSMatthew Vick INIT_DELAYED_WORK(&adapter->ptp_overflow_work, 794e57b8bdbSMatthew Vick igb_ptp_overflow_check); 7951f6e8178SMatthew Vick 796a79f4f88SMatthew Vick schedule_delayed_work(&adapter->ptp_overflow_work, 797a79f4f88SMatthew Vick IGB_SYSTIM_OVERFLOW_PERIOD); 798e57b8bdbSMatthew Vick } 799d339b133SRichard Cochran 8001f6e8178SMatthew Vick /* Initialize the time sync interrupts for devices that support it. */ 8011f6e8178SMatthew Vick if (hw->mac.type >= e1000_82580) { 8021f6e8178SMatthew Vick wr32(E1000_TSIM, E1000_TSIM_TXTS); 8031f6e8178SMatthew Vick wr32(E1000_IMS, E1000_IMS_TS); 8041f6e8178SMatthew Vick } 8051f6e8178SMatthew Vick 8061ef76158SRichard Cochran adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps, 8071ef76158SRichard Cochran &adapter->pdev->dev); 808d339b133SRichard Cochran if (IS_ERR(adapter->ptp_clock)) { 809d339b133SRichard Cochran adapter->ptp_clock = NULL; 810d339b133SRichard Cochran dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n"); 8111f6e8178SMatthew Vick } else { 812d339b133SRichard Cochran dev_info(&adapter->pdev->dev, "added PHC on %s\n", 813d339b133SRichard Cochran adapter->netdev->name); 8141f6e8178SMatthew Vick adapter->flags |= IGB_FLAG_PTP; 8151f6e8178SMatthew Vick } 816d339b133SRichard Cochran } 817d339b133SRichard Cochran 818a79f4f88SMatthew Vick /** 819a79f4f88SMatthew Vick * igb_ptp_stop - Disable PTP device and stop the overflow check. 820a79f4f88SMatthew Vick * @adapter: Board private structure. 821a79f4f88SMatthew Vick * 822a79f4f88SMatthew Vick * This function stops the PTP support and cancels the delayed work. 823a79f4f88SMatthew Vick **/ 824a79f4f88SMatthew Vick void igb_ptp_stop(struct igb_adapter *adapter) 825d339b133SRichard Cochran { 826d3eef8c8SCarolyn Wyborny switch (adapter->hw.mac.type) { 827d3eef8c8SCarolyn Wyborny case e1000_82576: 8281f6e8178SMatthew Vick case e1000_82580: 829ceb5f13bSCarolyn Wyborny case e1000_i354: 8301f6e8178SMatthew Vick case e1000_i350: 831a79f4f88SMatthew Vick cancel_delayed_work_sync(&adapter->ptp_overflow_work); 832d3eef8c8SCarolyn Wyborny break; 8331f6e8178SMatthew Vick case e1000_i210: 8341f6e8178SMatthew Vick case e1000_i211: 8351f6e8178SMatthew Vick /* No delayed work to cancel. */ 8361f6e8178SMatthew Vick break; 837d3eef8c8SCarolyn Wyborny default: 838d3eef8c8SCarolyn Wyborny return; 839d3eef8c8SCarolyn Wyborny } 840d339b133SRichard Cochran 8411f6e8178SMatthew Vick cancel_work_sync(&adapter->ptp_tx_work); 842badc26ddSMatthew Vick if (adapter->ptp_tx_skb) { 843badc26ddSMatthew Vick dev_kfree_skb_any(adapter->ptp_tx_skb); 844badc26ddSMatthew Vick adapter->ptp_tx_skb = NULL; 845badc26ddSMatthew Vick } 8461f6e8178SMatthew Vick 847d339b133SRichard Cochran if (adapter->ptp_clock) { 848d339b133SRichard Cochran ptp_clock_unregister(adapter->ptp_clock); 849d339b133SRichard Cochran dev_info(&adapter->pdev->dev, "removed PHC on %s\n", 850d339b133SRichard Cochran adapter->netdev->name); 8511f6e8178SMatthew Vick adapter->flags &= ~IGB_FLAG_PTP; 852d339b133SRichard Cochran } 853d339b133SRichard Cochran } 8541f6e8178SMatthew Vick 8551f6e8178SMatthew Vick /** 8561f6e8178SMatthew Vick * igb_ptp_reset - Re-enable the adapter for PTP following a reset. 8571f6e8178SMatthew Vick * @adapter: Board private structure. 8581f6e8178SMatthew Vick * 8591f6e8178SMatthew Vick * This function handles the reset work required to re-enable the PTP device. 8601f6e8178SMatthew Vick **/ 8611f6e8178SMatthew Vick void igb_ptp_reset(struct igb_adapter *adapter) 8621f6e8178SMatthew Vick { 8631f6e8178SMatthew Vick struct e1000_hw *hw = &adapter->hw; 8641f6e8178SMatthew Vick 8651f6e8178SMatthew Vick if (!(adapter->flags & IGB_FLAG_PTP)) 8661f6e8178SMatthew Vick return; 8671f6e8178SMatthew Vick 8681f6e8178SMatthew Vick switch (adapter->hw.mac.type) { 8691f6e8178SMatthew Vick case e1000_82576: 8701f6e8178SMatthew Vick /* Dial the nominal frequency. */ 8711f6e8178SMatthew Vick wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576); 8721f6e8178SMatthew Vick break; 8731f6e8178SMatthew Vick case e1000_82580: 874ceb5f13bSCarolyn Wyborny case e1000_i354: 8751f6e8178SMatthew Vick case e1000_i350: 8761f6e8178SMatthew Vick case e1000_i210: 8771f6e8178SMatthew Vick case e1000_i211: 8781f6e8178SMatthew Vick /* Enable the timer functions and interrupts. */ 8791f6e8178SMatthew Vick wr32(E1000_TSAUXC, 0x0); 8801f6e8178SMatthew Vick wr32(E1000_TSIM, E1000_TSIM_TXTS); 8811f6e8178SMatthew Vick wr32(E1000_IMS, E1000_IMS_TS); 8821f6e8178SMatthew Vick break; 8831f6e8178SMatthew Vick default: 8841f6e8178SMatthew Vick /* No work to do. */ 8851f6e8178SMatthew Vick return; 8861f6e8178SMatthew Vick } 8871f6e8178SMatthew Vick 888e57b8bdbSMatthew Vick /* Re-initialize the timer. */ 889e57b8bdbSMatthew Vick if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) { 890e57b8bdbSMatthew Vick struct timespec ts = ktime_to_timespec(ktime_get_real()); 891e57b8bdbSMatthew Vick 892e57b8bdbSMatthew Vick igb_ptp_settime_i210(&adapter->ptp_caps, &ts); 893e57b8bdbSMatthew Vick } else { 8941f6e8178SMatthew Vick timecounter_init(&adapter->tc, &adapter->cc, 8951f6e8178SMatthew Vick ktime_to_ns(ktime_get_real())); 8961f6e8178SMatthew Vick } 897e57b8bdbSMatthew Vick } 898