igb_ptp.c 25 KB

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