i40e_ptp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "i40e.h"
  4. #include <linux/ptp_classify.h>
  5. /* The XL710 timesync is very much like Intel's 82599 design when it comes to
  6. * the fundamental clock design. However, the clock operations are much simpler
  7. * in the XL710 because the device supports a full 64 bits of nanoseconds.
  8. * Because the field is so wide, we can forgo the cycle counter and just
  9. * operate with the nanosecond field directly without fear of overflow.
  10. *
  11. * Much like the 82599, the update period is dependent upon the link speed:
  12. * At 40Gb link or no link, the period is 1.6ns.
  13. * At 10Gb link, the period is multiplied by 2. (3.2ns)
  14. * At 1Gb link, the period is multiplied by 20. (32ns)
  15. * 1588 functionality is not supported at 100Mbps.
  16. */
  17. #define I40E_PTP_40GB_INCVAL 0x0199999999ULL
  18. #define I40E_PTP_10GB_INCVAL_MULT 2
  19. #define I40E_PTP_1GB_INCVAL_MULT 20
  20. #define I40E_PRTTSYN_CTL1_TSYNTYPE_V1 BIT(I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
  21. #define I40E_PRTTSYN_CTL1_TSYNTYPE_V2 (2 << \
  22. I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
  23. /**
  24. * i40e_ptp_read - Read the PHC time from the device
  25. * @pf: Board private structure
  26. * @ts: timespec structure to hold the current time value
  27. *
  28. * This function reads the PRTTSYN_TIME registers and stores them in a
  29. * timespec. However, since the registers are 64 bits of nanoseconds, we must
  30. * convert the result to a timespec before we can return.
  31. **/
  32. static void i40e_ptp_read(struct i40e_pf *pf, struct timespec64 *ts)
  33. {
  34. struct i40e_hw *hw = &pf->hw;
  35. u32 hi, lo;
  36. u64 ns;
  37. /* The timer latches on the lowest register read. */
  38. lo = rd32(hw, I40E_PRTTSYN_TIME_L);
  39. hi = rd32(hw, I40E_PRTTSYN_TIME_H);
  40. ns = (((u64)hi) << 32) | lo;
  41. *ts = ns_to_timespec64(ns);
  42. }
  43. /**
  44. * i40e_ptp_write - Write the PHC time to the device
  45. * @pf: Board private structure
  46. * @ts: timespec structure that holds the new time value
  47. *
  48. * This function writes the PRTTSYN_TIME registers with the user value. Since
  49. * we receive a timespec from the stack, we must convert that timespec into
  50. * nanoseconds before programming the registers.
  51. **/
  52. static void i40e_ptp_write(struct i40e_pf *pf, const struct timespec64 *ts)
  53. {
  54. struct i40e_hw *hw = &pf->hw;
  55. u64 ns = timespec64_to_ns(ts);
  56. /* The timer will not update until the high register is written, so
  57. * write the low register first.
  58. */
  59. wr32(hw, I40E_PRTTSYN_TIME_L, ns & 0xFFFFFFFF);
  60. wr32(hw, I40E_PRTTSYN_TIME_H, ns >> 32);
  61. }
  62. /**
  63. * i40e_ptp_convert_to_hwtstamp - Convert device clock to system time
  64. * @hwtstamps: Timestamp structure to update
  65. * @timestamp: Timestamp from the hardware
  66. *
  67. * We need to convert the NIC clock value into a hwtstamp which can be used by
  68. * the upper level timestamping functions. Since the timestamp is simply a 64-
  69. * bit nanosecond value, we can call ns_to_ktime directly to handle this.
  70. **/
  71. static void i40e_ptp_convert_to_hwtstamp(struct skb_shared_hwtstamps *hwtstamps,
  72. u64 timestamp)
  73. {
  74. memset(hwtstamps, 0, sizeof(*hwtstamps));
  75. hwtstamps->hwtstamp = ns_to_ktime(timestamp);
  76. }
  77. /**
  78. * i40e_ptp_adjfreq - Adjust the PHC frequency
  79. * @ptp: The PTP clock structure
  80. * @ppb: Parts per billion adjustment from the base
  81. *
  82. * Adjust the frequency of the PHC by the indicated parts per billion from the
  83. * base frequency.
  84. **/
  85. static int i40e_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  86. {
  87. struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
  88. struct i40e_hw *hw = &pf->hw;
  89. u64 adj, freq, diff;
  90. int neg_adj = 0;
  91. if (ppb < 0) {
  92. neg_adj = 1;
  93. ppb = -ppb;
  94. }
  95. freq = I40E_PTP_40GB_INCVAL;
  96. freq *= ppb;
  97. diff = div_u64(freq, 1000000000ULL);
  98. if (neg_adj)
  99. adj = I40E_PTP_40GB_INCVAL - diff;
  100. else
  101. adj = I40E_PTP_40GB_INCVAL + diff;
  102. /* At some link speeds, the base incval is so large that directly
  103. * multiplying by ppb would result in arithmetic overflow even when
  104. * using a u64. Avoid this by instead calculating the new incval
  105. * always in terms of the 40GbE clock rate and then multiplying by the
  106. * link speed factor afterwards. This does result in slightly lower
  107. * precision at lower link speeds, but it is fairly minor.
  108. */
  109. smp_mb(); /* Force any pending update before accessing. */
  110. adj *= READ_ONCE(pf->ptp_adj_mult);
  111. wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
  112. wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
  113. return 0;
  114. }
  115. /**
  116. * i40e_ptp_adjtime - Adjust the PHC time
  117. * @ptp: The PTP clock structure
  118. * @delta: Offset in nanoseconds to adjust the PHC time by
  119. *
  120. * Adjust the frequency of the PHC by the indicated parts per billion from the
  121. * base frequency.
  122. **/
  123. static int i40e_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  124. {
  125. struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
  126. struct timespec64 now;
  127. mutex_lock(&pf->tmreg_lock);
  128. i40e_ptp_read(pf, &now);
  129. timespec64_add_ns(&now, delta);
  130. i40e_ptp_write(pf, (const struct timespec64 *)&now);
  131. mutex_unlock(&pf->tmreg_lock);
  132. return 0;
  133. }
  134. /**
  135. * i40e_ptp_gettime - Get the time of the PHC
  136. * @ptp: The PTP clock structure
  137. * @ts: timespec structure to hold the current time value
  138. *
  139. * Read the device clock and return the correct value on ns, after converting it
  140. * into a timespec struct.
  141. **/
  142. static int i40e_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  143. {
  144. struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
  145. mutex_lock(&pf->tmreg_lock);
  146. i40e_ptp_read(pf, ts);
  147. mutex_unlock(&pf->tmreg_lock);
  148. return 0;
  149. }
  150. /**
  151. * i40e_ptp_settime - Set the time of the PHC
  152. * @ptp: The PTP clock structure
  153. * @ts: timespec structure that holds the new time value
  154. *
  155. * Set the device clock to the user input value. The conversion from timespec
  156. * to ns happens in the write function.
  157. **/
  158. static int i40e_ptp_settime(struct ptp_clock_info *ptp,
  159. const struct timespec64 *ts)
  160. {
  161. struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
  162. mutex_lock(&pf->tmreg_lock);
  163. i40e_ptp_write(pf, ts);
  164. mutex_unlock(&pf->tmreg_lock);
  165. return 0;
  166. }
  167. /**
  168. * i40e_ptp_feature_enable - Enable/disable ancillary features of the PHC subsystem
  169. * @ptp: The PTP clock structure
  170. * @rq: The requested feature to change
  171. * @on: Enable/disable flag
  172. *
  173. * The XL710 does not support any of the ancillary features of the PHC
  174. * subsystem, so this function may just return.
  175. **/
  176. static int i40e_ptp_feature_enable(struct ptp_clock_info *ptp,
  177. struct ptp_clock_request *rq, int on)
  178. {
  179. return -EOPNOTSUPP;
  180. }
  181. /**
  182. * i40e_ptp_update_latch_events - Read I40E_PRTTSYN_STAT_1 and latch events
  183. * @pf: the PF data structure
  184. *
  185. * This function reads I40E_PRTTSYN_STAT_1 and updates the corresponding timers
  186. * for noticed latch events. This allows the driver to keep track of the first
  187. * time a latch event was noticed which will be used to help clear out Rx
  188. * timestamps for packets that got dropped or lost.
  189. *
  190. * This function will return the current value of I40E_PRTTSYN_STAT_1 and is
  191. * expected to be called only while under the ptp_rx_lock.
  192. **/
  193. static u32 i40e_ptp_get_rx_events(struct i40e_pf *pf)
  194. {
  195. struct i40e_hw *hw = &pf->hw;
  196. u32 prttsyn_stat, new_latch_events;
  197. int i;
  198. prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_1);
  199. new_latch_events = prttsyn_stat & ~pf->latch_event_flags;
  200. /* Update the jiffies time for any newly latched timestamp. This
  201. * ensures that we store the time that we first discovered a timestamp
  202. * was latched by the hardware. The service task will later determine
  203. * if we should free the latch and drop that timestamp should too much
  204. * time pass. This flow ensures that we only update jiffies for new
  205. * events latched since the last time we checked, and not all events
  206. * currently latched, so that the service task accounting remains
  207. * accurate.
  208. */
  209. for (i = 0; i < 4; i++) {
  210. if (new_latch_events & BIT(i))
  211. pf->latch_events[i] = jiffies;
  212. }
  213. /* Finally, we store the current status of the Rx timestamp latches */
  214. pf->latch_event_flags = prttsyn_stat;
  215. return prttsyn_stat;
  216. }
  217. /**
  218. * i40e_ptp_rx_hang - Detect error case when Rx timestamp registers are hung
  219. * @pf: The PF private data structure
  220. * @vsi: The VSI with the rings relevant to 1588
  221. *
  222. * This watchdog task is scheduled to detect error case where hardware has
  223. * dropped an Rx packet that was timestamped when the ring is full. The
  224. * particular error is rare but leaves the device in a state unable to timestamp
  225. * any future packets.
  226. **/
  227. void i40e_ptp_rx_hang(struct i40e_pf *pf)
  228. {
  229. struct i40e_hw *hw = &pf->hw;
  230. unsigned int i, cleared = 0;
  231. /* Since we cannot turn off the Rx timestamp logic if the device is
  232. * configured for Tx timestamping, we check if Rx timestamping is
  233. * configured. We don't want to spuriously warn about Rx timestamp
  234. * hangs if we don't care about the timestamps.
  235. */
  236. if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
  237. return;
  238. spin_lock_bh(&pf->ptp_rx_lock);
  239. /* Update current latch times for Rx events */
  240. i40e_ptp_get_rx_events(pf);
  241. /* Check all the currently latched Rx events and see whether they have
  242. * been latched for over a second. It is assumed that any timestamp
  243. * should have been cleared within this time, or else it was captured
  244. * for a dropped frame that the driver never received. Thus, we will
  245. * clear any timestamp that has been latched for over 1 second.
  246. */
  247. for (i = 0; i < 4; i++) {
  248. if ((pf->latch_event_flags & BIT(i)) &&
  249. time_is_before_jiffies(pf->latch_events[i] + HZ)) {
  250. rd32(hw, I40E_PRTTSYN_RXTIME_H(i));
  251. pf->latch_event_flags &= ~BIT(i);
  252. cleared++;
  253. }
  254. }
  255. spin_unlock_bh(&pf->ptp_rx_lock);
  256. /* Log a warning if more than 2 timestamps got dropped in the same
  257. * check. We don't want to warn about all drops because it can occur
  258. * in normal scenarios such as PTP frames on multicast addresses we
  259. * aren't listening to. However, administrator should know if this is
  260. * the reason packets aren't receiving timestamps.
  261. */
  262. if (cleared > 2)
  263. dev_dbg(&pf->pdev->dev,
  264. "Dropped %d missed RXTIME timestamp events\n",
  265. cleared);
  266. /* Finally, update the rx_hwtstamp_cleared counter */
  267. pf->rx_hwtstamp_cleared += cleared;
  268. }
  269. /**
  270. * i40e_ptp_tx_hang - Detect error case when Tx timestamp register is hung
  271. * @pf: The PF private data structure
  272. *
  273. * This watchdog task is run periodically to make sure that we clear the Tx
  274. * timestamp logic if we don't obtain a timestamp in a reasonable amount of
  275. * time. It is unexpected in the normal case but if it occurs it results in
  276. * permanently preventing timestamps of future packets.
  277. **/
  278. void i40e_ptp_tx_hang(struct i40e_pf *pf)
  279. {
  280. struct sk_buff *skb;
  281. if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
  282. return;
  283. /* Nothing to do if we're not already waiting for a timestamp */
  284. if (!test_bit(__I40E_PTP_TX_IN_PROGRESS, pf->state))
  285. return;
  286. /* We already have a handler routine which is run when we are notified
  287. * of a Tx timestamp in the hardware. If we don't get an interrupt
  288. * within a second it is reasonable to assume that we never will.
  289. */
  290. if (time_is_before_jiffies(pf->ptp_tx_start + HZ)) {
  291. skb = pf->ptp_tx_skb;
  292. pf->ptp_tx_skb = NULL;
  293. clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
  294. /* Free the skb after we clear the bitlock */
  295. dev_kfree_skb_any(skb);
  296. pf->tx_hwtstamp_timeouts++;
  297. }
  298. }
  299. /**
  300. * i40e_ptp_tx_hwtstamp - Utility function which returns the Tx timestamp
  301. * @pf: Board private structure
  302. *
  303. * Read the value of the Tx timestamp from the registers, convert it into a
  304. * value consumable by the stack, and store that result into the shhwtstamps
  305. * struct before returning it up the stack.
  306. **/
  307. void i40e_ptp_tx_hwtstamp(struct i40e_pf *pf)
  308. {
  309. struct skb_shared_hwtstamps shhwtstamps;
  310. struct sk_buff *skb = pf->ptp_tx_skb;
  311. struct i40e_hw *hw = &pf->hw;
  312. u32 hi, lo;
  313. u64 ns;
  314. if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
  315. return;
  316. /* don't attempt to timestamp if we don't have an skb */
  317. if (!pf->ptp_tx_skb)
  318. return;
  319. lo = rd32(hw, I40E_PRTTSYN_TXTIME_L);
  320. hi = rd32(hw, I40E_PRTTSYN_TXTIME_H);
  321. ns = (((u64)hi) << 32) | lo;
  322. i40e_ptp_convert_to_hwtstamp(&shhwtstamps, ns);
  323. /* Clear the bit lock as soon as possible after reading the register,
  324. * and prior to notifying the stack via skb_tstamp_tx(). Otherwise
  325. * applications might wake up and attempt to request another transmit
  326. * timestamp prior to the bit lock being cleared.
  327. */
  328. pf->ptp_tx_skb = NULL;
  329. clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
  330. /* Notify the stack and free the skb after we've unlocked */
  331. skb_tstamp_tx(skb, &shhwtstamps);
  332. dev_kfree_skb_any(skb);
  333. }
  334. /**
  335. * i40e_ptp_rx_hwtstamp - Utility function which checks for an Rx timestamp
  336. * @pf: Board private structure
  337. * @skb: Particular skb to send timestamp with
  338. * @index: Index into the receive timestamp registers for the timestamp
  339. *
  340. * The XL710 receives a notification in the receive descriptor with an offset
  341. * into the set of RXTIME registers where the timestamp is for that skb. This
  342. * function goes and fetches the receive timestamp from that offset, if a valid
  343. * one exists. The RXTIME registers are in ns, so we must convert the result
  344. * first.
  345. **/
  346. void i40e_ptp_rx_hwtstamp(struct i40e_pf *pf, struct sk_buff *skb, u8 index)
  347. {
  348. u32 prttsyn_stat, hi, lo;
  349. struct i40e_hw *hw;
  350. u64 ns;
  351. /* Since we cannot turn off the Rx timestamp logic if the device is
  352. * doing Tx timestamping, check if Rx timestamping is configured.
  353. */
  354. if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
  355. return;
  356. hw = &pf->hw;
  357. spin_lock_bh(&pf->ptp_rx_lock);
  358. /* Get current Rx events and update latch times */
  359. prttsyn_stat = i40e_ptp_get_rx_events(pf);
  360. /* TODO: Should we warn about missing Rx timestamp event? */
  361. if (!(prttsyn_stat & BIT(index))) {
  362. spin_unlock_bh(&pf->ptp_rx_lock);
  363. return;
  364. }
  365. /* Clear the latched event since we're about to read its register */
  366. pf->latch_event_flags &= ~BIT(index);
  367. lo = rd32(hw, I40E_PRTTSYN_RXTIME_L(index));
  368. hi = rd32(hw, I40E_PRTTSYN_RXTIME_H(index));
  369. spin_unlock_bh(&pf->ptp_rx_lock);
  370. ns = (((u64)hi) << 32) | lo;
  371. i40e_ptp_convert_to_hwtstamp(skb_hwtstamps(skb), ns);
  372. }
  373. /**
  374. * i40e_ptp_set_increment - Utility function to update clock increment rate
  375. * @pf: Board private structure
  376. *
  377. * During a link change, the DMA frequency that drives the 1588 logic will
  378. * change. In order to keep the PRTTSYN_TIME registers in units of nanoseconds,
  379. * we must update the increment value per clock tick.
  380. **/
  381. void i40e_ptp_set_increment(struct i40e_pf *pf)
  382. {
  383. struct i40e_link_status *hw_link_info;
  384. struct i40e_hw *hw = &pf->hw;
  385. u64 incval;
  386. u32 mult;
  387. hw_link_info = &hw->phy.link_info;
  388. i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
  389. switch (hw_link_info->link_speed) {
  390. case I40E_LINK_SPEED_10GB:
  391. mult = I40E_PTP_10GB_INCVAL_MULT;
  392. break;
  393. case I40E_LINK_SPEED_1GB:
  394. mult = I40E_PTP_1GB_INCVAL_MULT;
  395. break;
  396. case I40E_LINK_SPEED_100MB:
  397. {
  398. static int warn_once;
  399. if (!warn_once) {
  400. dev_warn(&pf->pdev->dev,
  401. "1588 functionality is not supported at 100 Mbps. Stopping the PHC.\n");
  402. warn_once++;
  403. }
  404. mult = 0;
  405. break;
  406. }
  407. case I40E_LINK_SPEED_40GB:
  408. default:
  409. mult = 1;
  410. break;
  411. }
  412. /* The increment value is calculated by taking the base 40GbE incvalue
  413. * and multiplying it by a factor based on the link speed.
  414. */
  415. incval = I40E_PTP_40GB_INCVAL * mult;
  416. /* Write the new increment value into the increment register. The
  417. * hardware will not update the clock until both registers have been
  418. * written.
  419. */
  420. wr32(hw, I40E_PRTTSYN_INC_L, incval & 0xFFFFFFFF);
  421. wr32(hw, I40E_PRTTSYN_INC_H, incval >> 32);
  422. /* Update the base adjustement value. */
  423. WRITE_ONCE(pf->ptp_adj_mult, mult);
  424. smp_mb(); /* Force the above update. */
  425. }
  426. /**
  427. * i40e_ptp_get_ts_config - ioctl interface to read the HW timestamping
  428. * @pf: Board private structure
  429. * @ifr: ioctl data
  430. *
  431. * Obtain the current hardware timestamping settigs as requested. To do this,
  432. * keep a shadow copy of the timestamp settings rather than attempting to
  433. * deconstruct it from the registers.
  434. **/
  435. int i40e_ptp_get_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
  436. {
  437. struct hwtstamp_config *config = &pf->tstamp_config;
  438. if (!(pf->flags & I40E_FLAG_PTP))
  439. return -EOPNOTSUPP;
  440. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  441. -EFAULT : 0;
  442. }
  443. /**
  444. * i40e_ptp_set_timestamp_mode - setup hardware for requested timestamp mode
  445. * @pf: Board private structure
  446. * @config: hwtstamp settings requested or saved
  447. *
  448. * Control hardware registers to enter the specific mode requested by the
  449. * user. Also used during reset path to ensure that timestamp settings are
  450. * maintained.
  451. *
  452. * Note: modifies config in place, and may update the requested mode to be
  453. * more broad if the specific filter is not directly supported.
  454. **/
  455. static int i40e_ptp_set_timestamp_mode(struct i40e_pf *pf,
  456. struct hwtstamp_config *config)
  457. {
  458. struct i40e_hw *hw = &pf->hw;
  459. u32 tsyntype, regval;
  460. /* Reserved for future extensions. */
  461. if (config->flags)
  462. return -EINVAL;
  463. switch (config->tx_type) {
  464. case HWTSTAMP_TX_OFF:
  465. pf->ptp_tx = false;
  466. break;
  467. case HWTSTAMP_TX_ON:
  468. pf->ptp_tx = true;
  469. break;
  470. default:
  471. return -ERANGE;
  472. }
  473. switch (config->rx_filter) {
  474. case HWTSTAMP_FILTER_NONE:
  475. pf->ptp_rx = false;
  476. /* We set the type to V1, but do not enable UDP packet
  477. * recognition. In this way, we should be as close to
  478. * disabling PTP Rx timestamps as possible since V1 packets
  479. * are always UDP, since L2 packets are a V2 feature.
  480. */
  481. tsyntype = I40E_PRTTSYN_CTL1_TSYNTYPE_V1;
  482. break;
  483. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  484. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  485. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  486. if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
  487. return -ERANGE;
  488. pf->ptp_rx = true;
  489. tsyntype = I40E_PRTTSYN_CTL1_V1MESSTYPE0_MASK |
  490. I40E_PRTTSYN_CTL1_TSYNTYPE_V1 |
  491. I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
  492. config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  493. break;
  494. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  495. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  496. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  497. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  498. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  499. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  500. if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
  501. return -ERANGE;
  502. /* fall through */
  503. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  504. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  505. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  506. pf->ptp_rx = true;
  507. tsyntype = I40E_PRTTSYN_CTL1_V2MESSTYPE0_MASK |
  508. I40E_PRTTSYN_CTL1_TSYNTYPE_V2;
  509. if (pf->hw_features & I40E_HW_PTP_L4_CAPABLE) {
  510. tsyntype |= I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
  511. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  512. } else {
  513. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  514. }
  515. break;
  516. case HWTSTAMP_FILTER_NTP_ALL:
  517. case HWTSTAMP_FILTER_ALL:
  518. default:
  519. return -ERANGE;
  520. }
  521. /* Clear out all 1588-related registers to clear and unlatch them. */
  522. spin_lock_bh(&pf->ptp_rx_lock);
  523. rd32(hw, I40E_PRTTSYN_STAT_0);
  524. rd32(hw, I40E_PRTTSYN_TXTIME_H);
  525. rd32(hw, I40E_PRTTSYN_RXTIME_H(0));
  526. rd32(hw, I40E_PRTTSYN_RXTIME_H(1));
  527. rd32(hw, I40E_PRTTSYN_RXTIME_H(2));
  528. rd32(hw, I40E_PRTTSYN_RXTIME_H(3));
  529. pf->latch_event_flags = 0;
  530. spin_unlock_bh(&pf->ptp_rx_lock);
  531. /* Enable/disable the Tx timestamp interrupt based on user input. */
  532. regval = rd32(hw, I40E_PRTTSYN_CTL0);
  533. if (pf->ptp_tx)
  534. regval |= I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
  535. else
  536. regval &= ~I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
  537. wr32(hw, I40E_PRTTSYN_CTL0, regval);
  538. regval = rd32(hw, I40E_PFINT_ICR0_ENA);
  539. if (pf->ptp_tx)
  540. regval |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  541. else
  542. regval &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  543. wr32(hw, I40E_PFINT_ICR0_ENA, regval);
  544. /* Although there is no simple on/off switch for Rx, we "disable" Rx
  545. * timestamps by setting to V1 only mode and clear the UDP
  546. * recognition. This ought to disable all PTP Rx timestamps as V1
  547. * packets are always over UDP. Note that software is configured to
  548. * ignore Rx timestamps via the pf->ptp_rx flag.
  549. */
  550. regval = rd32(hw, I40E_PRTTSYN_CTL1);
  551. /* clear everything but the enable bit */
  552. regval &= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
  553. /* now enable bits for desired Rx timestamps */
  554. regval |= tsyntype;
  555. wr32(hw, I40E_PRTTSYN_CTL1, regval);
  556. return 0;
  557. }
  558. /**
  559. * i40e_ptp_set_ts_config - ioctl interface to control the HW timestamping
  560. * @pf: Board private structure
  561. * @ifr: ioctl data
  562. *
  563. * Respond to the user filter requests and make the appropriate hardware
  564. * changes here. The XL710 cannot support splitting of the Tx/Rx timestamping
  565. * logic, so keep track in software of whether to indicate these timestamps
  566. * or not.
  567. *
  568. * It is permissible to "upgrade" the user request to a broader filter, as long
  569. * as the user receives the timestamps they care about and the user is notified
  570. * the filter has been broadened.
  571. **/
  572. int i40e_ptp_set_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
  573. {
  574. struct hwtstamp_config config;
  575. int err;
  576. if (!(pf->flags & I40E_FLAG_PTP))
  577. return -EOPNOTSUPP;
  578. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  579. return -EFAULT;
  580. err = i40e_ptp_set_timestamp_mode(pf, &config);
  581. if (err)
  582. return err;
  583. /* save these settings for future reference */
  584. pf->tstamp_config = config;
  585. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  586. -EFAULT : 0;
  587. }
  588. /**
  589. * i40e_ptp_create_clock - Create PTP clock device for userspace
  590. * @pf: Board private structure
  591. *
  592. * This function creates a new PTP clock device. It only creates one if we
  593. * don't already have one, so it is safe to call. Will return error if it
  594. * can't create one, but success if we already have a device. Should be used
  595. * by i40e_ptp_init to create clock initially, and prevent global resets from
  596. * creating new clock devices.
  597. **/
  598. static long i40e_ptp_create_clock(struct i40e_pf *pf)
  599. {
  600. /* no need to create a clock device if we already have one */
  601. if (!IS_ERR_OR_NULL(pf->ptp_clock))
  602. return 0;
  603. strncpy(pf->ptp_caps.name, i40e_driver_name, sizeof(pf->ptp_caps.name));
  604. pf->ptp_caps.owner = THIS_MODULE;
  605. pf->ptp_caps.max_adj = 999999999;
  606. pf->ptp_caps.n_ext_ts = 0;
  607. pf->ptp_caps.pps = 0;
  608. pf->ptp_caps.adjfreq = i40e_ptp_adjfreq;
  609. pf->ptp_caps.adjtime = i40e_ptp_adjtime;
  610. pf->ptp_caps.gettime64 = i40e_ptp_gettime;
  611. pf->ptp_caps.settime64 = i40e_ptp_settime;
  612. pf->ptp_caps.enable = i40e_ptp_feature_enable;
  613. /* Attempt to register the clock before enabling the hardware. */
  614. pf->ptp_clock = ptp_clock_register(&pf->ptp_caps, &pf->pdev->dev);
  615. if (IS_ERR(pf->ptp_clock))
  616. return PTR_ERR(pf->ptp_clock);
  617. /* clear the hwtstamp settings here during clock create, instead of
  618. * during regular init, so that we can maintain settings across a
  619. * reset or suspend.
  620. */
  621. pf->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  622. pf->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  623. return 0;
  624. }
  625. /**
  626. * i40e_ptp_init - Initialize the 1588 support after device probe or reset
  627. * @pf: Board private structure
  628. *
  629. * This function sets device up for 1588 support. The first time it is run, it
  630. * will create a PHC clock device. It does not create a clock device if one
  631. * already exists. It also reconfigures the device after a reset.
  632. **/
  633. void i40e_ptp_init(struct i40e_pf *pf)
  634. {
  635. struct net_device *netdev = pf->vsi[pf->lan_vsi]->netdev;
  636. struct i40e_hw *hw = &pf->hw;
  637. u32 pf_id;
  638. long err;
  639. /* Only one PF is assigned to control 1588 logic per port. Do not
  640. * enable any support for PFs not assigned via PRTTSYN_CTL0.PF_ID
  641. */
  642. pf_id = (rd32(hw, I40E_PRTTSYN_CTL0) & I40E_PRTTSYN_CTL0_PF_ID_MASK) >>
  643. I40E_PRTTSYN_CTL0_PF_ID_SHIFT;
  644. if (hw->pf_id != pf_id) {
  645. pf->flags &= ~I40E_FLAG_PTP;
  646. dev_info(&pf->pdev->dev, "%s: PTP not supported on %s\n",
  647. __func__,
  648. netdev->name);
  649. return;
  650. }
  651. mutex_init(&pf->tmreg_lock);
  652. spin_lock_init(&pf->ptp_rx_lock);
  653. /* ensure we have a clock device */
  654. err = i40e_ptp_create_clock(pf);
  655. if (err) {
  656. pf->ptp_clock = NULL;
  657. dev_err(&pf->pdev->dev, "%s: ptp_clock_register failed\n",
  658. __func__);
  659. } else if (pf->ptp_clock) {
  660. struct timespec64 ts;
  661. u32 regval;
  662. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  663. dev_info(&pf->pdev->dev, "PHC enabled\n");
  664. pf->flags |= I40E_FLAG_PTP;
  665. /* Ensure the clocks are running. */
  666. regval = rd32(hw, I40E_PRTTSYN_CTL0);
  667. regval |= I40E_PRTTSYN_CTL0_TSYNENA_MASK;
  668. wr32(hw, I40E_PRTTSYN_CTL0, regval);
  669. regval = rd32(hw, I40E_PRTTSYN_CTL1);
  670. regval |= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
  671. wr32(hw, I40E_PRTTSYN_CTL1, regval);
  672. /* Set the increment value per clock tick. */
  673. i40e_ptp_set_increment(pf);
  674. /* reset timestamping mode */
  675. i40e_ptp_set_timestamp_mode(pf, &pf->tstamp_config);
  676. /* Set the clock value. */
  677. ts = ktime_to_timespec64(ktime_get_real());
  678. i40e_ptp_settime(&pf->ptp_caps, &ts);
  679. }
  680. }
  681. /**
  682. * i40e_ptp_stop - Disable the driver/hardware support and unregister the PHC
  683. * @pf: Board private structure
  684. *
  685. * This function handles the cleanup work required from the initialization by
  686. * clearing out the important information and unregistering the PHC.
  687. **/
  688. void i40e_ptp_stop(struct i40e_pf *pf)
  689. {
  690. pf->flags &= ~I40E_FLAG_PTP;
  691. pf->ptp_tx = false;
  692. pf->ptp_rx = false;
  693. if (pf->ptp_tx_skb) {
  694. struct sk_buff *skb = pf->ptp_tx_skb;
  695. pf->ptp_tx_skb = NULL;
  696. clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
  697. dev_kfree_skb_any(skb);
  698. }
  699. if (pf->ptp_clock) {
  700. ptp_clock_unregister(pf->ptp_clock);
  701. pf->ptp_clock = NULL;
  702. dev_info(&pf->pdev->dev, "%s: removed PHC on %s\n", __func__,
  703. pf->vsi[pf->lan_vsi]->netdev->name);
  704. }
  705. }