igb_ptp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /* PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
  2. *
  3. * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/pci.h>
  22. #include <linux/ptp_classify.h>
  23. #include "igb.h"
  24. #define INCVALUE_MASK 0x7fffffff
  25. #define ISGN 0x80000000
  26. /* The 82580 timesync updates the system timer every 8ns by 8ns,
  27. * and this update value cannot be reprogrammed.
  28. *
  29. * Neither the 82576 nor the 82580 offer registers wide enough to hold
  30. * nanoseconds time values for very long. For the 82580, SYSTIM always
  31. * counts nanoseconds, but the upper 24 bits are not availible. The
  32. * frequency is adjusted by changing the 32 bit fractional nanoseconds
  33. * register, TIMINCA.
  34. *
  35. * For the 82576, the SYSTIM register time unit is affect by the
  36. * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
  37. * field are needed to provide the nominal 16 nanosecond period,
  38. * leaving 19 bits for fractional nanoseconds.
  39. *
  40. * We scale the NIC clock cycle by a large factor so that relatively
  41. * small clock corrections can be added or subtracted at each clock
  42. * tick. The drawbacks of a large factor are a) that the clock
  43. * register overflows more quickly (not such a big deal) and b) that
  44. * the increment per tick has to fit into 24 bits. As a result we
  45. * need to use a shift of 19 so we can fit a value of 16 into the
  46. * TIMINCA register.
  47. *
  48. *
  49. * SYSTIMH SYSTIML
  50. * +--------------+ +---+---+------+
  51. * 82576 | 32 | | 8 | 5 | 19 |
  52. * +--------------+ +---+---+------+
  53. * \________ 45 bits _______/ fract
  54. *
  55. * +----------+---+ +--------------+
  56. * 82580 | 24 | 8 | | 32 |
  57. * +----------+---+ +--------------+
  58. * reserved \______ 40 bits _____/
  59. *
  60. *
  61. * The 45 bit 82576 SYSTIM overflows every
  62. * 2^45 * 10^-9 / 3600 = 9.77 hours.
  63. *
  64. * The 40 bit 82580 SYSTIM overflows every
  65. * 2^40 * 10^-9 / 60 = 18.3 minutes.
  66. */
  67. #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9)
  68. #define IGB_PTP_TX_TIMEOUT (HZ * 15)
  69. #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT)
  70. #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
  71. #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT)
  72. #define IGB_NBITS_82580 40
  73. /* SYSTIM read access for the 82576 */
  74. static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc)
  75. {
  76. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  77. struct e1000_hw *hw = &igb->hw;
  78. u64 val;
  79. u32 lo, hi;
  80. lo = rd32(E1000_SYSTIML);
  81. hi = rd32(E1000_SYSTIMH);
  82. val = ((u64) hi) << 32;
  83. val |= lo;
  84. return val;
  85. }
  86. /* SYSTIM read access for the 82580 */
  87. static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc)
  88. {
  89. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  90. struct e1000_hw *hw = &igb->hw;
  91. u64 val;
  92. u32 lo, hi, jk;
  93. /* The timestamp latches on lowest register read. For the 82580
  94. * the lowest register is SYSTIMR instead of SYSTIML. However we only
  95. * need to provide nanosecond resolution, so we just ignore it.
  96. */
  97. jk = rd32(E1000_SYSTIMR);
  98. lo = rd32(E1000_SYSTIML);
  99. hi = rd32(E1000_SYSTIMH);
  100. val = ((u64) hi) << 32;
  101. val |= lo;
  102. return val;
  103. }
  104. /* SYSTIM read access for I210/I211 */
  105. static void igb_ptp_read_i210(struct igb_adapter *adapter, struct timespec *ts)
  106. {
  107. struct e1000_hw *hw = &adapter->hw;
  108. u32 sec, nsec, jk;
  109. /* The timestamp latches on lowest register read. For I210/I211, the
  110. * lowest register is SYSTIMR. Since we only need to provide nanosecond
  111. * resolution, we can ignore it.
  112. */
  113. jk = rd32(E1000_SYSTIMR);
  114. nsec = rd32(E1000_SYSTIML);
  115. sec = rd32(E1000_SYSTIMH);
  116. ts->tv_sec = sec;
  117. ts->tv_nsec = nsec;
  118. }
  119. static void igb_ptp_write_i210(struct igb_adapter *adapter,
  120. const struct timespec *ts)
  121. {
  122. struct e1000_hw *hw = &adapter->hw;
  123. /* Writing the SYSTIMR register is not necessary as it only provides
  124. * sub-nanosecond resolution.
  125. */
  126. wr32(E1000_SYSTIML, ts->tv_nsec);
  127. wr32(E1000_SYSTIMH, ts->tv_sec);
  128. }
  129. /**
  130. * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
  131. * @adapter: board private structure
  132. * @hwtstamps: timestamp structure to update
  133. * @systim: unsigned 64bit system time value.
  134. *
  135. * We need to convert the system time value stored in the RX/TXSTMP registers
  136. * into a hwtstamp which can be used by the upper level timestamping functions.
  137. *
  138. * The 'tmreg_lock' spinlock is used to protect the consistency of the
  139. * system time value. This is needed because reading the 64 bit time
  140. * value involves reading two (or three) 32 bit registers. The first
  141. * read latches the value. Ditto for writing.
  142. *
  143. * In addition, here have extended the system time with an overflow
  144. * counter in software.
  145. **/
  146. static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
  147. struct skb_shared_hwtstamps *hwtstamps,
  148. u64 systim)
  149. {
  150. unsigned long flags;
  151. u64 ns;
  152. switch (adapter->hw.mac.type) {
  153. case e1000_82576:
  154. case e1000_82580:
  155. case e1000_i354:
  156. case e1000_i350:
  157. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  158. ns = timecounter_cyc2time(&adapter->tc, systim);
  159. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  160. memset(hwtstamps, 0, sizeof(*hwtstamps));
  161. hwtstamps->hwtstamp = ns_to_ktime(ns);
  162. break;
  163. case e1000_i210:
  164. case e1000_i211:
  165. memset(hwtstamps, 0, sizeof(*hwtstamps));
  166. /* Upper 32 bits contain s, lower 32 bits contain ns. */
  167. hwtstamps->hwtstamp = ktime_set(systim >> 32,
  168. systim & 0xFFFFFFFF);
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. /* PTP clock operations */
  175. static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
  176. {
  177. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  178. ptp_caps);
  179. struct e1000_hw *hw = &igb->hw;
  180. int neg_adj = 0;
  181. u64 rate;
  182. u32 incvalue;
  183. if (ppb < 0) {
  184. neg_adj = 1;
  185. ppb = -ppb;
  186. }
  187. rate = ppb;
  188. rate <<= 14;
  189. rate = div_u64(rate, 1953125);
  190. incvalue = 16 << IGB_82576_TSYNC_SHIFT;
  191. if (neg_adj)
  192. incvalue -= rate;
  193. else
  194. incvalue += rate;
  195. wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
  196. return 0;
  197. }
  198. static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
  199. {
  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 inca;
  206. if (ppb < 0) {
  207. neg_adj = 1;
  208. ppb = -ppb;
  209. }
  210. rate = ppb;
  211. rate <<= 26;
  212. rate = div_u64(rate, 1953125);
  213. inca = rate & INCVALUE_MASK;
  214. if (neg_adj)
  215. inca |= ISGN;
  216. wr32(E1000_TIMINCA, inca);
  217. return 0;
  218. }
  219. static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
  220. {
  221. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  222. ptp_caps);
  223. unsigned long flags;
  224. s64 now;
  225. spin_lock_irqsave(&igb->tmreg_lock, flags);
  226. now = timecounter_read(&igb->tc);
  227. now += delta;
  228. timecounter_init(&igb->tc, &igb->cc, now);
  229. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  230. return 0;
  231. }
  232. static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
  233. {
  234. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  235. ptp_caps);
  236. unsigned long flags;
  237. struct timespec now, then = ns_to_timespec(delta);
  238. spin_lock_irqsave(&igb->tmreg_lock, flags);
  239. igb_ptp_read_i210(igb, &now);
  240. now = timespec_add(now, then);
  241. igb_ptp_write_i210(igb, (const struct timespec *)&now);
  242. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  243. return 0;
  244. }
  245. static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
  246. struct timespec *ts)
  247. {
  248. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  249. ptp_caps);
  250. unsigned long flags;
  251. u64 ns;
  252. u32 remainder;
  253. spin_lock_irqsave(&igb->tmreg_lock, flags);
  254. ns = timecounter_read(&igb->tc);
  255. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  256. ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
  257. ts->tv_nsec = remainder;
  258. return 0;
  259. }
  260. static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
  261. struct timespec *ts)
  262. {
  263. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  264. ptp_caps);
  265. unsigned long flags;
  266. spin_lock_irqsave(&igb->tmreg_lock, flags);
  267. igb_ptp_read_i210(igb, ts);
  268. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  269. return 0;
  270. }
  271. static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
  272. const struct timespec *ts)
  273. {
  274. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  275. ptp_caps);
  276. unsigned long flags;
  277. u64 ns;
  278. ns = ts->tv_sec * 1000000000ULL;
  279. ns += ts->tv_nsec;
  280. spin_lock_irqsave(&igb->tmreg_lock, flags);
  281. timecounter_init(&igb->tc, &igb->cc, ns);
  282. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  283. return 0;
  284. }
  285. static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
  286. const struct timespec *ts)
  287. {
  288. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  289. ptp_caps);
  290. unsigned long flags;
  291. spin_lock_irqsave(&igb->tmreg_lock, flags);
  292. igb_ptp_write_i210(igb, ts);
  293. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  294. return 0;
  295. }
  296. static int igb_ptp_enable(struct ptp_clock_info *ptp,
  297. struct ptp_clock_request *rq, int on)
  298. {
  299. return -EOPNOTSUPP;
  300. }
  301. /**
  302. * igb_ptp_tx_work
  303. * @work: pointer to work struct
  304. *
  305. * This work function polls the TSYNCTXCTL valid bit to determine when a
  306. * timestamp has been taken for the current stored skb.
  307. **/
  308. void igb_ptp_tx_work(struct work_struct *work)
  309. {
  310. struct igb_adapter *adapter = container_of(work, struct igb_adapter,
  311. ptp_tx_work);
  312. struct e1000_hw *hw = &adapter->hw;
  313. u32 tsynctxctl;
  314. if (!adapter->ptp_tx_skb)
  315. return;
  316. if (time_is_before_jiffies(adapter->ptp_tx_start +
  317. IGB_PTP_TX_TIMEOUT)) {
  318. dev_kfree_skb_any(adapter->ptp_tx_skb);
  319. adapter->ptp_tx_skb = NULL;
  320. adapter->tx_hwtstamp_timeouts++;
  321. dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang");
  322. return;
  323. }
  324. tsynctxctl = rd32(E1000_TSYNCTXCTL);
  325. if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
  326. igb_ptp_tx_hwtstamp(adapter);
  327. else
  328. /* reschedule to check later */
  329. schedule_work(&adapter->ptp_tx_work);
  330. }
  331. static void igb_ptp_overflow_check(struct work_struct *work)
  332. {
  333. struct igb_adapter *igb =
  334. container_of(work, struct igb_adapter, ptp_overflow_work.work);
  335. struct timespec ts;
  336. igb->ptp_caps.gettime(&igb->ptp_caps, &ts);
  337. pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  338. schedule_delayed_work(&igb->ptp_overflow_work,
  339. IGB_SYSTIM_OVERFLOW_PERIOD);
  340. }
  341. /**
  342. * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
  343. * @adapter: private network adapter structure
  344. *
  345. * This watchdog task is scheduled to detect error case where hardware has
  346. * dropped an Rx packet that was timestamped when the ring is full. The
  347. * particular error is rare but leaves the device in a state unable to timestamp
  348. * any future packets.
  349. **/
  350. void igb_ptp_rx_hang(struct igb_adapter *adapter)
  351. {
  352. struct e1000_hw *hw = &adapter->hw;
  353. struct igb_ring *rx_ring;
  354. u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
  355. unsigned long rx_event;
  356. int n;
  357. if (hw->mac.type != e1000_82576)
  358. return;
  359. /* If we don't have a valid timestamp in the registers, just update the
  360. * timeout counter and exit
  361. */
  362. if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
  363. adapter->last_rx_ptp_check = jiffies;
  364. return;
  365. }
  366. /* Determine the most recent watchdog or rx_timestamp event */
  367. rx_event = adapter->last_rx_ptp_check;
  368. for (n = 0; n < adapter->num_rx_queues; n++) {
  369. rx_ring = adapter->rx_ring[n];
  370. if (time_after(rx_ring->last_rx_timestamp, rx_event))
  371. rx_event = rx_ring->last_rx_timestamp;
  372. }
  373. /* Only need to read the high RXSTMP register to clear the lock */
  374. if (time_is_before_jiffies(rx_event + 5 * HZ)) {
  375. rd32(E1000_RXSTMPH);
  376. adapter->last_rx_ptp_check = jiffies;
  377. adapter->rx_hwtstamp_cleared++;
  378. dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang");
  379. }
  380. }
  381. /**
  382. * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  383. * @adapter: Board private structure.
  384. *
  385. * If we were asked to do hardware stamping and such a time stamp is
  386. * available, then it must have been for this skb here because we only
  387. * allow only one such packet into the queue.
  388. **/
  389. void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
  390. {
  391. struct e1000_hw *hw = &adapter->hw;
  392. struct skb_shared_hwtstamps shhwtstamps;
  393. u64 regval;
  394. regval = rd32(E1000_TXSTMPL);
  395. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  396. igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  397. skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
  398. dev_kfree_skb_any(adapter->ptp_tx_skb);
  399. adapter->ptp_tx_skb = NULL;
  400. }
  401. /**
  402. * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
  403. * @q_vector: Pointer to interrupt specific structure
  404. * @va: Pointer to address containing Rx buffer
  405. * @skb: Buffer containing timestamp and packet
  406. *
  407. * This function is meant to retrieve a timestamp from the first buffer of an
  408. * incoming frame. The value is stored in little endian format starting on
  409. * byte 8.
  410. **/
  411. void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
  412. unsigned char *va,
  413. struct sk_buff *skb)
  414. {
  415. __le64 *regval = (__le64 *)va;
  416. /* The timestamp is recorded in little endian format.
  417. * DWORD: 0 1 2 3
  418. * Field: Reserved Reserved SYSTIML SYSTIMH
  419. */
  420. igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
  421. le64_to_cpu(regval[1]));
  422. }
  423. /**
  424. * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
  425. * @q_vector: Pointer to interrupt specific structure
  426. * @skb: Buffer containing timestamp and packet
  427. *
  428. * This function is meant to retrieve a timestamp from the internal registers
  429. * of the adapter and store it in the skb.
  430. **/
  431. void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
  432. struct sk_buff *skb)
  433. {
  434. struct igb_adapter *adapter = q_vector->adapter;
  435. struct e1000_hw *hw = &adapter->hw;
  436. u64 regval;
  437. /* If this bit is set, then the RX registers contain the time stamp. No
  438. * other packet will be time stamped until we read these registers, so
  439. * read the registers to make them available again. Because only one
  440. * packet can be time stamped at a time, we know that the register
  441. * values must belong to this one here and therefore we don't need to
  442. * compare any of the additional attributes stored for it.
  443. *
  444. * If nothing went wrong, then it should have a shared tx_flags that we
  445. * can turn into a skb_shared_hwtstamps.
  446. */
  447. if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  448. return;
  449. regval = rd32(E1000_RXSTMPL);
  450. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  451. igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  452. }
  453. /**
  454. * igb_ptp_hwtstamp_ioctl - control hardware time stamping
  455. * @netdev:
  456. * @ifreq:
  457. * @cmd:
  458. *
  459. * Outgoing time stamping can be enabled and disabled. Play nice and
  460. * disable it when requested, although it shouldn't case any overhead
  461. * when no packet needs it. At most one packet in the queue may be
  462. * marked for time stamping, otherwise it would be impossible to tell
  463. * for sure to which packet the hardware time stamp belongs.
  464. *
  465. * Incoming time stamping has to be configured via the hardware
  466. * filters. Not all combinations are supported, in particular event
  467. * type has to be specified. Matching the kind of event packet is
  468. * not supported, with the exception of "all V2 events regardless of
  469. * level 2 or 4".
  470. **/
  471. int igb_ptp_hwtstamp_ioctl(struct net_device *netdev,
  472. struct ifreq *ifr, int cmd)
  473. {
  474. struct igb_adapter *adapter = netdev_priv(netdev);
  475. struct e1000_hw *hw = &adapter->hw;
  476. struct hwtstamp_config config;
  477. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  478. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  479. u32 tsync_rx_cfg = 0;
  480. bool is_l4 = false;
  481. bool is_l2 = false;
  482. u32 regval;
  483. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  484. return -EFAULT;
  485. /* reserved for future extensions */
  486. if (config.flags)
  487. return -EINVAL;
  488. switch (config.tx_type) {
  489. case HWTSTAMP_TX_OFF:
  490. tsync_tx_ctl = 0;
  491. case HWTSTAMP_TX_ON:
  492. break;
  493. default:
  494. return -ERANGE;
  495. }
  496. switch (config.rx_filter) {
  497. case HWTSTAMP_FILTER_NONE:
  498. tsync_rx_ctl = 0;
  499. break;
  500. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  501. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  502. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  503. is_l4 = true;
  504. break;
  505. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  506. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  507. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  508. is_l4 = true;
  509. break;
  510. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  511. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  512. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  513. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  514. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  515. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  516. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  517. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  518. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  519. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  520. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  521. is_l2 = true;
  522. is_l4 = true;
  523. break;
  524. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  525. case HWTSTAMP_FILTER_ALL:
  526. /* 82576 cannot timestamp all packets, which it needs to do to
  527. * support both V1 Sync and Delay_Req messages
  528. */
  529. if (hw->mac.type != e1000_82576) {
  530. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  531. config.rx_filter = HWTSTAMP_FILTER_ALL;
  532. break;
  533. }
  534. /* fall through */
  535. default:
  536. config.rx_filter = HWTSTAMP_FILTER_NONE;
  537. return -ERANGE;
  538. }
  539. if (hw->mac.type == e1000_82575) {
  540. if (tsync_rx_ctl | tsync_tx_ctl)
  541. return -EINVAL;
  542. return 0;
  543. }
  544. /* Per-packet timestamping only works if all packets are
  545. * timestamped, so enable timestamping in all packets as
  546. * long as one Rx filter was configured.
  547. */
  548. if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
  549. tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  550. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  551. config.rx_filter = HWTSTAMP_FILTER_ALL;
  552. is_l2 = true;
  553. is_l4 = true;
  554. if ((hw->mac.type == e1000_i210) ||
  555. (hw->mac.type == e1000_i211)) {
  556. regval = rd32(E1000_RXPBS);
  557. regval |= E1000_RXPBS_CFG_TS_EN;
  558. wr32(E1000_RXPBS, regval);
  559. }
  560. }
  561. /* enable/disable TX */
  562. regval = rd32(E1000_TSYNCTXCTL);
  563. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  564. regval |= tsync_tx_ctl;
  565. wr32(E1000_TSYNCTXCTL, regval);
  566. /* enable/disable RX */
  567. regval = rd32(E1000_TSYNCRXCTL);
  568. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  569. regval |= tsync_rx_ctl;
  570. wr32(E1000_TSYNCRXCTL, regval);
  571. /* define which PTP packets are time stamped */
  572. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  573. /* define ethertype filter for timestamped packets */
  574. if (is_l2)
  575. wr32(E1000_ETQF(3),
  576. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  577. E1000_ETQF_1588 | /* enable timestamping */
  578. ETH_P_1588)); /* 1588 eth protocol type */
  579. else
  580. wr32(E1000_ETQF(3), 0);
  581. /* L4 Queue Filter[3]: filter by destination port and protocol */
  582. if (is_l4) {
  583. u32 ftqf = (IPPROTO_UDP /* UDP */
  584. | E1000_FTQF_VF_BP /* VF not compared */
  585. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  586. | E1000_FTQF_MASK); /* mask all inputs */
  587. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  588. wr32(E1000_IMIR(3), htons(PTP_EV_PORT));
  589. wr32(E1000_IMIREXT(3),
  590. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  591. if (hw->mac.type == e1000_82576) {
  592. /* enable source port check */
  593. wr32(E1000_SPQF(3), htons(PTP_EV_PORT));
  594. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  595. }
  596. wr32(E1000_FTQF(3), ftqf);
  597. } else {
  598. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  599. }
  600. wrfl();
  601. /* clear TX/RX time stamp registers, just to be sure */
  602. regval = rd32(E1000_TXSTMPL);
  603. regval = rd32(E1000_TXSTMPH);
  604. regval = rd32(E1000_RXSTMPL);
  605. regval = rd32(E1000_RXSTMPH);
  606. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  607. -EFAULT : 0;
  608. }
  609. void igb_ptp_init(struct igb_adapter *adapter)
  610. {
  611. struct e1000_hw *hw = &adapter->hw;
  612. struct net_device *netdev = adapter->netdev;
  613. switch (hw->mac.type) {
  614. case e1000_82576:
  615. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  616. adapter->ptp_caps.owner = THIS_MODULE;
  617. adapter->ptp_caps.max_adj = 999999881;
  618. adapter->ptp_caps.n_ext_ts = 0;
  619. adapter->ptp_caps.pps = 0;
  620. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
  621. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  622. adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
  623. adapter->ptp_caps.settime = igb_ptp_settime_82576;
  624. adapter->ptp_caps.enable = igb_ptp_enable;
  625. adapter->cc.read = igb_ptp_read_82576;
  626. adapter->cc.mask = CLOCKSOURCE_MASK(64);
  627. adapter->cc.mult = 1;
  628. adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
  629. /* Dial the nominal frequency. */
  630. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  631. break;
  632. case e1000_82580:
  633. case e1000_i354:
  634. case e1000_i350:
  635. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  636. adapter->ptp_caps.owner = THIS_MODULE;
  637. adapter->ptp_caps.max_adj = 62499999;
  638. adapter->ptp_caps.n_ext_ts = 0;
  639. adapter->ptp_caps.pps = 0;
  640. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  641. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  642. adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
  643. adapter->ptp_caps.settime = igb_ptp_settime_82576;
  644. adapter->ptp_caps.enable = igb_ptp_enable;
  645. adapter->cc.read = igb_ptp_read_82580;
  646. adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580);
  647. adapter->cc.mult = 1;
  648. adapter->cc.shift = 0;
  649. /* Enable the timer functions by clearing bit 31. */
  650. wr32(E1000_TSAUXC, 0x0);
  651. break;
  652. case e1000_i210:
  653. case e1000_i211:
  654. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  655. adapter->ptp_caps.owner = THIS_MODULE;
  656. adapter->ptp_caps.max_adj = 62499999;
  657. adapter->ptp_caps.n_ext_ts = 0;
  658. adapter->ptp_caps.pps = 0;
  659. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  660. adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
  661. adapter->ptp_caps.gettime = igb_ptp_gettime_i210;
  662. adapter->ptp_caps.settime = igb_ptp_settime_i210;
  663. adapter->ptp_caps.enable = igb_ptp_enable;
  664. /* Enable the timer functions by clearing bit 31. */
  665. wr32(E1000_TSAUXC, 0x0);
  666. break;
  667. default:
  668. adapter->ptp_clock = NULL;
  669. return;
  670. }
  671. wrfl();
  672. spin_lock_init(&adapter->tmreg_lock);
  673. INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
  674. /* Initialize the clock and overflow work for devices that need it. */
  675. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  676. struct timespec ts = ktime_to_timespec(ktime_get_real());
  677. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  678. } else {
  679. timecounter_init(&adapter->tc, &adapter->cc,
  680. ktime_to_ns(ktime_get_real()));
  681. INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
  682. igb_ptp_overflow_check);
  683. schedule_delayed_work(&adapter->ptp_overflow_work,
  684. IGB_SYSTIM_OVERFLOW_PERIOD);
  685. }
  686. /* Initialize the time sync interrupts for devices that support it. */
  687. if (hw->mac.type >= e1000_82580) {
  688. wr32(E1000_TSIM, E1000_TSIM_TXTS);
  689. wr32(E1000_IMS, E1000_IMS_TS);
  690. }
  691. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  692. &adapter->pdev->dev);
  693. if (IS_ERR(adapter->ptp_clock)) {
  694. adapter->ptp_clock = NULL;
  695. dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
  696. } else {
  697. dev_info(&adapter->pdev->dev, "added PHC on %s\n",
  698. adapter->netdev->name);
  699. adapter->flags |= IGB_FLAG_PTP;
  700. }
  701. }
  702. /**
  703. * igb_ptp_stop - Disable PTP device and stop the overflow check.
  704. * @adapter: Board private structure.
  705. *
  706. * This function stops the PTP support and cancels the delayed work.
  707. **/
  708. void igb_ptp_stop(struct igb_adapter *adapter)
  709. {
  710. switch (adapter->hw.mac.type) {
  711. case e1000_82576:
  712. case e1000_82580:
  713. case e1000_i354:
  714. case e1000_i350:
  715. cancel_delayed_work_sync(&adapter->ptp_overflow_work);
  716. break;
  717. case e1000_i210:
  718. case e1000_i211:
  719. /* No delayed work to cancel. */
  720. break;
  721. default:
  722. return;
  723. }
  724. cancel_work_sync(&adapter->ptp_tx_work);
  725. if (adapter->ptp_tx_skb) {
  726. dev_kfree_skb_any(adapter->ptp_tx_skb);
  727. adapter->ptp_tx_skb = NULL;
  728. }
  729. if (adapter->ptp_clock) {
  730. ptp_clock_unregister(adapter->ptp_clock);
  731. dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
  732. adapter->netdev->name);
  733. adapter->flags &= ~IGB_FLAG_PTP;
  734. }
  735. }
  736. /**
  737. * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
  738. * @adapter: Board private structure.
  739. *
  740. * This function handles the reset work required to re-enable the PTP device.
  741. **/
  742. void igb_ptp_reset(struct igb_adapter *adapter)
  743. {
  744. struct e1000_hw *hw = &adapter->hw;
  745. if (!(adapter->flags & IGB_FLAG_PTP))
  746. return;
  747. switch (adapter->hw.mac.type) {
  748. case e1000_82576:
  749. /* Dial the nominal frequency. */
  750. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  751. break;
  752. case e1000_82580:
  753. case e1000_i354:
  754. case e1000_i350:
  755. case e1000_i210:
  756. case e1000_i211:
  757. /* Enable the timer functions and interrupts. */
  758. wr32(E1000_TSAUXC, 0x0);
  759. wr32(E1000_TSIM, E1000_TSIM_TXTS);
  760. wr32(E1000_IMS, E1000_IMS_TS);
  761. break;
  762. default:
  763. /* No work to do. */
  764. return;
  765. }
  766. /* Re-initialize the timer. */
  767. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  768. struct timespec ts = ktime_to_timespec(ktime_get_real());
  769. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  770. } else {
  771. timecounter_init(&adapter->tc, &adapter->cc,
  772. ktime_to_ns(ktime_get_real()));
  773. }
  774. }