igb_ptp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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 with
  16. * this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/pci.h>
  21. #include <linux/ptp_classify.h>
  22. #include "igb.h"
  23. #define INCVALUE_MASK 0x7fffffff
  24. #define ISGN 0x80000000
  25. /* The 82580 timesync updates the system timer every 8ns by 8ns,
  26. * and this update value cannot be reprogrammed.
  27. *
  28. * Neither the 82576 nor the 82580 offer registers wide enough to hold
  29. * nanoseconds time values for very long. For the 82580, SYSTIM always
  30. * counts nanoseconds, but the upper 24 bits are not availible. The
  31. * frequency is adjusted by changing the 32 bit fractional nanoseconds
  32. * register, TIMINCA.
  33. *
  34. * For the 82576, the SYSTIM register time unit is affect by the
  35. * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
  36. * field are needed to provide the nominal 16 nanosecond period,
  37. * leaving 19 bits for fractional nanoseconds.
  38. *
  39. * We scale the NIC clock cycle by a large factor so that relatively
  40. * small clock corrections can be added or subtracted at each clock
  41. * tick. The drawbacks of a large factor are a) that the clock
  42. * register overflows more quickly (not such a big deal) and b) that
  43. * the increment per tick has to fit into 24 bits. As a result we
  44. * need to use a shift of 19 so we can fit a value of 16 into the
  45. * TIMINCA register.
  46. *
  47. *
  48. * SYSTIMH SYSTIML
  49. * +--------------+ +---+---+------+
  50. * 82576 | 32 | | 8 | 5 | 19 |
  51. * +--------------+ +---+---+------+
  52. * \________ 45 bits _______/ fract
  53. *
  54. * +----------+---+ +--------------+
  55. * 82580 | 24 | 8 | | 32 |
  56. * +----------+---+ +--------------+
  57. * reserved \______ 40 bits _____/
  58. *
  59. *
  60. * The 45 bit 82576 SYSTIM overflows every
  61. * 2^45 * 10^-9 / 3600 = 9.77 hours.
  62. *
  63. * The 40 bit 82580 SYSTIM overflows every
  64. * 2^40 * 10^-9 / 60 = 18.3 minutes.
  65. */
  66. #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9)
  67. #define IGB_PTP_TX_TIMEOUT (HZ * 15)
  68. #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT)
  69. #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
  70. #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT)
  71. #define IGB_NBITS_82580 40
  72. static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter);
  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. u32 lo, hi;
  92. u64 val;
  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. 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;
  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. 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. spin_lock_irqsave(&igb->tmreg_lock, flags);
  225. timecounter_adjtime(&igb->tc, delta);
  226. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  227. return 0;
  228. }
  229. static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
  230. {
  231. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  232. ptp_caps);
  233. unsigned long flags;
  234. struct timespec now, then = ns_to_timespec(delta);
  235. spin_lock_irqsave(&igb->tmreg_lock, flags);
  236. igb_ptp_read_i210(igb, &now);
  237. now = timespec_add(now, then);
  238. igb_ptp_write_i210(igb, (const struct timespec *)&now);
  239. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  240. return 0;
  241. }
  242. static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
  243. struct timespec *ts)
  244. {
  245. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  246. ptp_caps);
  247. unsigned long flags;
  248. u64 ns;
  249. u32 remainder;
  250. spin_lock_irqsave(&igb->tmreg_lock, flags);
  251. ns = timecounter_read(&igb->tc);
  252. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  253. ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
  254. ts->tv_nsec = remainder;
  255. return 0;
  256. }
  257. static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
  258. struct timespec *ts)
  259. {
  260. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  261. ptp_caps);
  262. unsigned long flags;
  263. spin_lock_irqsave(&igb->tmreg_lock, flags);
  264. igb_ptp_read_i210(igb, ts);
  265. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  266. return 0;
  267. }
  268. static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
  269. const struct timespec *ts)
  270. {
  271. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  272. ptp_caps);
  273. unsigned long flags;
  274. u64 ns;
  275. ns = ts->tv_sec * 1000000000ULL;
  276. ns += ts->tv_nsec;
  277. spin_lock_irqsave(&igb->tmreg_lock, flags);
  278. timecounter_init(&igb->tc, &igb->cc, ns);
  279. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  280. return 0;
  281. }
  282. static int igb_ptp_settime_i210(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. spin_lock_irqsave(&igb->tmreg_lock, flags);
  289. igb_ptp_write_i210(igb, ts);
  290. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  291. return 0;
  292. }
  293. static int igb_ptp_feature_enable(struct ptp_clock_info *ptp,
  294. struct ptp_clock_request *rq, int on)
  295. {
  296. return -EOPNOTSUPP;
  297. }
  298. /**
  299. * igb_ptp_tx_work
  300. * @work: pointer to work struct
  301. *
  302. * This work function polls the TSYNCTXCTL valid bit to determine when a
  303. * timestamp has been taken for the current stored skb.
  304. **/
  305. static void igb_ptp_tx_work(struct work_struct *work)
  306. {
  307. struct igb_adapter *adapter = container_of(work, struct igb_adapter,
  308. ptp_tx_work);
  309. struct e1000_hw *hw = &adapter->hw;
  310. u32 tsynctxctl;
  311. if (!adapter->ptp_tx_skb)
  312. return;
  313. if (time_is_before_jiffies(adapter->ptp_tx_start +
  314. IGB_PTP_TX_TIMEOUT)) {
  315. dev_kfree_skb_any(adapter->ptp_tx_skb);
  316. adapter->ptp_tx_skb = NULL;
  317. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  318. adapter->tx_hwtstamp_timeouts++;
  319. dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
  320. return;
  321. }
  322. tsynctxctl = rd32(E1000_TSYNCTXCTL);
  323. if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
  324. igb_ptp_tx_hwtstamp(adapter);
  325. else
  326. /* reschedule to check later */
  327. schedule_work(&adapter->ptp_tx_work);
  328. }
  329. static void igb_ptp_overflow_check(struct work_struct *work)
  330. {
  331. struct igb_adapter *igb =
  332. container_of(work, struct igb_adapter, ptp_overflow_work.work);
  333. struct timespec ts;
  334. igb->ptp_caps.gettime(&igb->ptp_caps, &ts);
  335. pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  336. schedule_delayed_work(&igb->ptp_overflow_work,
  337. IGB_SYSTIM_OVERFLOW_PERIOD);
  338. }
  339. /**
  340. * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
  341. * @adapter: private network adapter structure
  342. *
  343. * This watchdog task is scheduled to detect error case where hardware has
  344. * dropped an Rx packet that was timestamped when the ring is full. The
  345. * particular error is rare but leaves the device in a state unable to timestamp
  346. * any future packets.
  347. **/
  348. void igb_ptp_rx_hang(struct igb_adapter *adapter)
  349. {
  350. struct e1000_hw *hw = &adapter->hw;
  351. u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
  352. unsigned long rx_event;
  353. if (hw->mac.type != e1000_82576)
  354. return;
  355. /* If we don't have a valid timestamp in the registers, just update the
  356. * timeout counter and exit
  357. */
  358. if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
  359. adapter->last_rx_ptp_check = jiffies;
  360. return;
  361. }
  362. /* Determine the most recent watchdog or rx_timestamp event */
  363. rx_event = adapter->last_rx_ptp_check;
  364. if (time_after(adapter->last_rx_timestamp, rx_event))
  365. rx_event = adapter->last_rx_timestamp;
  366. /* Only need to read the high RXSTMP register to clear the lock */
  367. if (time_is_before_jiffies(rx_event + 5 * HZ)) {
  368. rd32(E1000_RXSTMPH);
  369. adapter->last_rx_ptp_check = jiffies;
  370. adapter->rx_hwtstamp_cleared++;
  371. dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang\n");
  372. }
  373. }
  374. /**
  375. * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  376. * @adapter: Board private structure.
  377. *
  378. * If we were asked to do hardware stamping and such a time stamp is
  379. * available, then it must have been for this skb here because we only
  380. * allow only one such packet into the queue.
  381. **/
  382. static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
  383. {
  384. struct e1000_hw *hw = &adapter->hw;
  385. struct skb_shared_hwtstamps shhwtstamps;
  386. u64 regval;
  387. regval = rd32(E1000_TXSTMPL);
  388. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  389. igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  390. skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
  391. dev_kfree_skb_any(adapter->ptp_tx_skb);
  392. adapter->ptp_tx_skb = NULL;
  393. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  394. }
  395. /**
  396. * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
  397. * @q_vector: Pointer to interrupt specific structure
  398. * @va: Pointer to address containing Rx buffer
  399. * @skb: Buffer containing timestamp and packet
  400. *
  401. * This function is meant to retrieve a timestamp from the first buffer of an
  402. * incoming frame. The value is stored in little endian format starting on
  403. * byte 8.
  404. **/
  405. void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
  406. unsigned char *va,
  407. struct sk_buff *skb)
  408. {
  409. __le64 *regval = (__le64 *)va;
  410. /* The timestamp is recorded in little endian format.
  411. * DWORD: 0 1 2 3
  412. * Field: Reserved Reserved SYSTIML SYSTIMH
  413. */
  414. igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
  415. le64_to_cpu(regval[1]));
  416. }
  417. /**
  418. * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
  419. * @q_vector: Pointer to interrupt specific structure
  420. * @skb: Buffer containing timestamp and packet
  421. *
  422. * This function is meant to retrieve a timestamp from the internal registers
  423. * of the adapter and store it in the skb.
  424. **/
  425. void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
  426. struct sk_buff *skb)
  427. {
  428. struct igb_adapter *adapter = q_vector->adapter;
  429. struct e1000_hw *hw = &adapter->hw;
  430. u64 regval;
  431. /* If this bit is set, then the RX registers contain the time stamp. No
  432. * other packet will be time stamped until we read these registers, so
  433. * read the registers to make them available again. Because only one
  434. * packet can be time stamped at a time, we know that the register
  435. * values must belong to this one here and therefore we don't need to
  436. * compare any of the additional attributes stored for it.
  437. *
  438. * If nothing went wrong, then it should have a shared tx_flags that we
  439. * can turn into a skb_shared_hwtstamps.
  440. */
  441. if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  442. return;
  443. regval = rd32(E1000_RXSTMPL);
  444. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  445. igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  446. /* Update the last_rx_timestamp timer in order to enable watchdog check
  447. * for error case of latched timestamp on a dropped packet.
  448. */
  449. adapter->last_rx_timestamp = jiffies;
  450. }
  451. /**
  452. * igb_ptp_get_ts_config - get hardware time stamping config
  453. * @netdev:
  454. * @ifreq:
  455. *
  456. * Get the hwtstamp_config settings to return to the user. Rather than attempt
  457. * to deconstruct the settings from the registers, just return a shadow copy
  458. * of the last known settings.
  459. **/
  460. int igb_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
  461. {
  462. struct igb_adapter *adapter = netdev_priv(netdev);
  463. struct hwtstamp_config *config = &adapter->tstamp_config;
  464. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  465. -EFAULT : 0;
  466. }
  467. /**
  468. * igb_ptp_set_timestamp_mode - setup hardware for timestamping
  469. * @adapter: networking device structure
  470. * @config: hwtstamp configuration
  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. static int igb_ptp_set_timestamp_mode(struct igb_adapter *adapter,
  485. struct hwtstamp_config *config)
  486. {
  487. struct e1000_hw *hw = &adapter->hw;
  488. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  489. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  490. u32 tsync_rx_cfg = 0;
  491. bool is_l4 = false;
  492. bool is_l2 = false;
  493. u32 regval;
  494. /* reserved for future extensions */
  495. if (config->flags)
  496. return -EINVAL;
  497. switch (config->tx_type) {
  498. case HWTSTAMP_TX_OFF:
  499. tsync_tx_ctl = 0;
  500. case HWTSTAMP_TX_ON:
  501. break;
  502. default:
  503. return -ERANGE;
  504. }
  505. switch (config->rx_filter) {
  506. case HWTSTAMP_FILTER_NONE:
  507. tsync_rx_ctl = 0;
  508. break;
  509. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  510. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  511. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  512. is_l4 = true;
  513. break;
  514. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  515. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  516. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  517. is_l4 = true;
  518. break;
  519. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  520. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  521. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  522. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  523. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  524. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  525. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  526. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  527. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  528. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  529. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  530. is_l2 = true;
  531. is_l4 = true;
  532. break;
  533. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  534. case HWTSTAMP_FILTER_ALL:
  535. /* 82576 cannot timestamp all packets, which it needs to do to
  536. * support both V1 Sync and Delay_Req messages
  537. */
  538. if (hw->mac.type != e1000_82576) {
  539. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  540. config->rx_filter = HWTSTAMP_FILTER_ALL;
  541. break;
  542. }
  543. /* fall through */
  544. default:
  545. config->rx_filter = HWTSTAMP_FILTER_NONE;
  546. return -ERANGE;
  547. }
  548. if (hw->mac.type == e1000_82575) {
  549. if (tsync_rx_ctl | tsync_tx_ctl)
  550. return -EINVAL;
  551. return 0;
  552. }
  553. /* Per-packet timestamping only works if all packets are
  554. * timestamped, so enable timestamping in all packets as
  555. * long as one Rx filter was configured.
  556. */
  557. if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
  558. tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  559. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  560. config->rx_filter = HWTSTAMP_FILTER_ALL;
  561. is_l2 = true;
  562. is_l4 = true;
  563. if ((hw->mac.type == e1000_i210) ||
  564. (hw->mac.type == e1000_i211)) {
  565. regval = rd32(E1000_RXPBS);
  566. regval |= E1000_RXPBS_CFG_TS_EN;
  567. wr32(E1000_RXPBS, regval);
  568. }
  569. }
  570. /* enable/disable TX */
  571. regval = rd32(E1000_TSYNCTXCTL);
  572. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  573. regval |= tsync_tx_ctl;
  574. wr32(E1000_TSYNCTXCTL, regval);
  575. /* enable/disable RX */
  576. regval = rd32(E1000_TSYNCRXCTL);
  577. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  578. regval |= tsync_rx_ctl;
  579. wr32(E1000_TSYNCRXCTL, regval);
  580. /* define which PTP packets are time stamped */
  581. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  582. /* define ethertype filter for timestamped packets */
  583. if (is_l2)
  584. wr32(E1000_ETQF(3),
  585. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  586. E1000_ETQF_1588 | /* enable timestamping */
  587. ETH_P_1588)); /* 1588 eth protocol type */
  588. else
  589. wr32(E1000_ETQF(3), 0);
  590. /* L4 Queue Filter[3]: filter by destination port and protocol */
  591. if (is_l4) {
  592. u32 ftqf = (IPPROTO_UDP /* UDP */
  593. | E1000_FTQF_VF_BP /* VF not compared */
  594. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  595. | E1000_FTQF_MASK); /* mask all inputs */
  596. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  597. wr32(E1000_IMIR(3), htons(PTP_EV_PORT));
  598. wr32(E1000_IMIREXT(3),
  599. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  600. if (hw->mac.type == e1000_82576) {
  601. /* enable source port check */
  602. wr32(E1000_SPQF(3), htons(PTP_EV_PORT));
  603. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  604. }
  605. wr32(E1000_FTQF(3), ftqf);
  606. } else {
  607. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  608. }
  609. wrfl();
  610. /* clear TX/RX time stamp registers, just to be sure */
  611. regval = rd32(E1000_TXSTMPL);
  612. regval = rd32(E1000_TXSTMPH);
  613. regval = rd32(E1000_RXSTMPL);
  614. regval = rd32(E1000_RXSTMPH);
  615. return 0;
  616. }
  617. /**
  618. * igb_ptp_set_ts_config - set hardware time stamping config
  619. * @netdev:
  620. * @ifreq:
  621. *
  622. **/
  623. int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
  624. {
  625. struct igb_adapter *adapter = netdev_priv(netdev);
  626. struct hwtstamp_config config;
  627. int err;
  628. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  629. return -EFAULT;
  630. err = igb_ptp_set_timestamp_mode(adapter, &config);
  631. if (err)
  632. return err;
  633. /* save these settings for future reference */
  634. memcpy(&adapter->tstamp_config, &config,
  635. sizeof(adapter->tstamp_config));
  636. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  637. -EFAULT : 0;
  638. }
  639. void igb_ptp_init(struct igb_adapter *adapter)
  640. {
  641. struct e1000_hw *hw = &adapter->hw;
  642. struct net_device *netdev = adapter->netdev;
  643. switch (hw->mac.type) {
  644. case e1000_82576:
  645. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  646. adapter->ptp_caps.owner = THIS_MODULE;
  647. adapter->ptp_caps.max_adj = 999999881;
  648. adapter->ptp_caps.n_ext_ts = 0;
  649. adapter->ptp_caps.pps = 0;
  650. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
  651. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  652. adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
  653. adapter->ptp_caps.settime = igb_ptp_settime_82576;
  654. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  655. adapter->cc.read = igb_ptp_read_82576;
  656. adapter->cc.mask = CYCLECOUNTER_MASK(64);
  657. adapter->cc.mult = 1;
  658. adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
  659. /* Dial the nominal frequency. */
  660. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  661. break;
  662. case e1000_82580:
  663. case e1000_i354:
  664. case e1000_i350:
  665. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  666. adapter->ptp_caps.owner = THIS_MODULE;
  667. adapter->ptp_caps.max_adj = 62499999;
  668. adapter->ptp_caps.n_ext_ts = 0;
  669. adapter->ptp_caps.pps = 0;
  670. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  671. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  672. adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
  673. adapter->ptp_caps.settime = igb_ptp_settime_82576;
  674. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  675. adapter->cc.read = igb_ptp_read_82580;
  676. adapter->cc.mask = CYCLECOUNTER_MASK(IGB_NBITS_82580);
  677. adapter->cc.mult = 1;
  678. adapter->cc.shift = 0;
  679. /* Enable the timer functions by clearing bit 31. */
  680. wr32(E1000_TSAUXC, 0x0);
  681. break;
  682. case e1000_i210:
  683. case e1000_i211:
  684. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  685. adapter->ptp_caps.owner = THIS_MODULE;
  686. adapter->ptp_caps.max_adj = 62499999;
  687. adapter->ptp_caps.n_ext_ts = 0;
  688. adapter->ptp_caps.pps = 0;
  689. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  690. adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
  691. adapter->ptp_caps.gettime = igb_ptp_gettime_i210;
  692. adapter->ptp_caps.settime = igb_ptp_settime_i210;
  693. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  694. /* Enable the timer functions by clearing bit 31. */
  695. wr32(E1000_TSAUXC, 0x0);
  696. break;
  697. default:
  698. adapter->ptp_clock = NULL;
  699. return;
  700. }
  701. wrfl();
  702. spin_lock_init(&adapter->tmreg_lock);
  703. INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
  704. /* Initialize the clock and overflow work for devices that need it. */
  705. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  706. struct timespec ts = ktime_to_timespec(ktime_get_real());
  707. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  708. } else {
  709. timecounter_init(&adapter->tc, &adapter->cc,
  710. ktime_to_ns(ktime_get_real()));
  711. INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
  712. igb_ptp_overflow_check);
  713. schedule_delayed_work(&adapter->ptp_overflow_work,
  714. IGB_SYSTIM_OVERFLOW_PERIOD);
  715. }
  716. /* Initialize the time sync interrupts for devices that support it. */
  717. if (hw->mac.type >= e1000_82580) {
  718. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  719. wr32(E1000_IMS, E1000_IMS_TS);
  720. }
  721. adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  722. adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  723. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  724. &adapter->pdev->dev);
  725. if (IS_ERR(adapter->ptp_clock)) {
  726. adapter->ptp_clock = NULL;
  727. dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
  728. } else {
  729. dev_info(&adapter->pdev->dev, "added PHC on %s\n",
  730. adapter->netdev->name);
  731. adapter->flags |= IGB_FLAG_PTP;
  732. }
  733. }
  734. /**
  735. * igb_ptp_stop - Disable PTP device and stop the overflow check.
  736. * @adapter: Board private structure.
  737. *
  738. * This function stops the PTP support and cancels the delayed work.
  739. **/
  740. void igb_ptp_stop(struct igb_adapter *adapter)
  741. {
  742. switch (adapter->hw.mac.type) {
  743. case e1000_82576:
  744. case e1000_82580:
  745. case e1000_i354:
  746. case e1000_i350:
  747. cancel_delayed_work_sync(&adapter->ptp_overflow_work);
  748. break;
  749. case e1000_i210:
  750. case e1000_i211:
  751. /* No delayed work to cancel. */
  752. break;
  753. default:
  754. return;
  755. }
  756. cancel_work_sync(&adapter->ptp_tx_work);
  757. if (adapter->ptp_tx_skb) {
  758. dev_kfree_skb_any(adapter->ptp_tx_skb);
  759. adapter->ptp_tx_skb = NULL;
  760. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  761. }
  762. if (adapter->ptp_clock) {
  763. ptp_clock_unregister(adapter->ptp_clock);
  764. dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
  765. adapter->netdev->name);
  766. adapter->flags &= ~IGB_FLAG_PTP;
  767. }
  768. }
  769. /**
  770. * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
  771. * @adapter: Board private structure.
  772. *
  773. * This function handles the reset work required to re-enable the PTP device.
  774. **/
  775. void igb_ptp_reset(struct igb_adapter *adapter)
  776. {
  777. struct e1000_hw *hw = &adapter->hw;
  778. if (!(adapter->flags & IGB_FLAG_PTP))
  779. return;
  780. /* reset the tstamp_config */
  781. igb_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
  782. switch (adapter->hw.mac.type) {
  783. case e1000_82576:
  784. /* Dial the nominal frequency. */
  785. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  786. break;
  787. case e1000_82580:
  788. case e1000_i354:
  789. case e1000_i350:
  790. case e1000_i210:
  791. case e1000_i211:
  792. /* Enable the timer functions and interrupts. */
  793. wr32(E1000_TSAUXC, 0x0);
  794. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  795. wr32(E1000_IMS, E1000_IMS_TS);
  796. break;
  797. default:
  798. /* No work to do. */
  799. return;
  800. }
  801. /* Re-initialize the timer. */
  802. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  803. struct timespec ts = ktime_to_timespec(ktime_get_real());
  804. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  805. } else {
  806. timecounter_init(&adapter->tc, &adapter->cc,
  807. ktime_to_ns(ktime_get_real()));
  808. }
  809. }