i40e_ptp.c 23 KB

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