ixgbe_ptp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2013 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include "ixgbe.h"
  22. #include <linux/ptp_classify.h>
  23. /*
  24. * The 82599 and the X540 do not have true 64bit nanosecond scale
  25. * counter registers. Instead, SYSTIME is defined by a fixed point
  26. * system which allows the user to define the scale counter increment
  27. * value at every level change of the oscillator driving the SYSTIME
  28. * value. For both devices the TIMINCA:IV field defines this
  29. * increment. On the X540 device, 31 bits are provided. However on the
  30. * 82599 only provides 24 bits. The time unit is determined by the
  31. * clock frequency of the oscillator in combination with the TIMINCA
  32. * register. When these devices link at 10Gb the oscillator has a
  33. * period of 6.4ns. In order to convert the scale counter into
  34. * nanoseconds the cyclecounter and timecounter structures are
  35. * used. The SYSTIME registers need to be converted to ns values by use
  36. * of only a right shift (division by power of 2). The following math
  37. * determines the largest incvalue that will fit into the available
  38. * bits in the TIMINCA register.
  39. *
  40. * PeriodWidth: Number of bits to store the clock period
  41. * MaxWidth: The maximum width value of the TIMINCA register
  42. * Period: The clock period for the oscillator
  43. * round(): discard the fractional portion of the calculation
  44. *
  45. * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
  46. *
  47. * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
  48. * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
  49. *
  50. * The period also changes based on the link speed:
  51. * At 10Gb link or no link, the period remains the same.
  52. * At 1Gb link, the period is multiplied by 10. (64ns)
  53. * At 100Mb link, the period is multiplied by 100. (640ns)
  54. *
  55. * The calculated value allows us to right shift the SYSTIME register
  56. * value in order to quickly convert it into a nanosecond clock,
  57. * while allowing for the maximum possible adjustment value.
  58. *
  59. * These diagrams are only for the 10Gb link period
  60. *
  61. * SYSTIMEH SYSTIMEL
  62. * +--------------+ +--------------+
  63. * X540 | 32 | | 1 | 3 | 28 |
  64. * *--------------+ +--------------+
  65. * \________ 36 bits ______/ fract
  66. *
  67. * +--------------+ +--------------+
  68. * 82599 | 32 | | 8 | 3 | 21 |
  69. * *--------------+ +--------------+
  70. * \________ 43 bits ______/ fract
  71. *
  72. * The 36 bit X540 SYSTIME overflows every
  73. * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
  74. *
  75. * The 43 bit 82599 SYSTIME overflows every
  76. * 2^43 * 10^-9 / 3600 = 2.4 hours
  77. */
  78. #define IXGBE_INCVAL_10GB 0x66666666
  79. #define IXGBE_INCVAL_1GB 0x40000000
  80. #define IXGBE_INCVAL_100 0x50000000
  81. #define IXGBE_INCVAL_SHIFT_10GB 28
  82. #define IXGBE_INCVAL_SHIFT_1GB 24
  83. #define IXGBE_INCVAL_SHIFT_100 21
  84. #define IXGBE_INCVAL_SHIFT_82599 7
  85. #define IXGBE_INCPER_SHIFT_82599 24
  86. #define IXGBE_MAX_TIMEADJ_VALUE 0x7FFFFFFFFFFFFFFFULL
  87. #define IXGBE_OVERFLOW_PERIOD (HZ * 30)
  88. #define IXGBE_PTP_TX_TIMEOUT (HZ * 15)
  89. /* half of a one second clock period, for use with PPS signal. We have to use
  90. * this instead of something pre-defined like IXGBE_PTP_PPS_HALF_SECOND, in
  91. * order to force at least 64bits of precision for shifting
  92. */
  93. #define IXGBE_PTP_PPS_HALF_SECOND 500000000ULL
  94. /**
  95. * ixgbe_ptp_setup_sdp
  96. * @hw: the hardware private structure
  97. *
  98. * this function enables or disables the clock out feature on SDP0 for
  99. * the X540 device. It will create a 1second periodic output that can
  100. * be used as the PPS (via an interrupt).
  101. *
  102. * It calculates when the systime will be on an exact second, and then
  103. * aligns the start of the PPS signal to that value. The shift is
  104. * necessary because it can change based on the link speed.
  105. */
  106. static void ixgbe_ptp_setup_sdp(struct ixgbe_adapter *adapter)
  107. {
  108. struct ixgbe_hw *hw = &adapter->hw;
  109. int shift = adapter->cc.shift;
  110. u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
  111. u64 ns = 0, clock_edge = 0;
  112. if ((adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED) &&
  113. (hw->mac.type == ixgbe_mac_X540)) {
  114. /* disable the pin first */
  115. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
  116. IXGBE_WRITE_FLUSH(hw);
  117. esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
  118. /*
  119. * enable the SDP0 pin as output, and connected to the
  120. * native function for Timesync (ClockOut)
  121. */
  122. esdp |= (IXGBE_ESDP_SDP0_DIR |
  123. IXGBE_ESDP_SDP0_NATIVE);
  124. /*
  125. * enable the Clock Out feature on SDP0, and allow
  126. * interrupts to occur when the pin changes
  127. */
  128. tsauxc = (IXGBE_TSAUXC_EN_CLK |
  129. IXGBE_TSAUXC_SYNCLK |
  130. IXGBE_TSAUXC_SDP0_INT);
  131. /* clock period (or pulse length) */
  132. clktiml = (u32)(IXGBE_PTP_PPS_HALF_SECOND << shift);
  133. clktimh = (u32)((IXGBE_PTP_PPS_HALF_SECOND << shift) >> 32);
  134. /*
  135. * Account for the cyclecounter wrap-around value by
  136. * using the converted ns value of the current time to
  137. * check for when the next aligned second would occur.
  138. */
  139. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  140. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  141. ns = timecounter_cyc2time(&adapter->tc, clock_edge);
  142. div_u64_rem(ns, IXGBE_PTP_PPS_HALF_SECOND, &rem);
  143. clock_edge += ((IXGBE_PTP_PPS_HALF_SECOND - (u64)rem) << shift);
  144. /* specify the initial clock start time */
  145. trgttiml = (u32)clock_edge;
  146. trgttimh = (u32)(clock_edge >> 32);
  147. IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
  148. IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
  149. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
  150. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
  151. IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
  152. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
  153. } else {
  154. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
  155. }
  156. IXGBE_WRITE_FLUSH(hw);
  157. }
  158. /**
  159. * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
  160. * @cc: the cyclecounter structure
  161. *
  162. * this function reads the cyclecounter registers and is called by the
  163. * cyclecounter structure used to construct a ns counter from the
  164. * arbitrary fixed point registers
  165. */
  166. static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
  167. {
  168. struct ixgbe_adapter *adapter =
  169. container_of(cc, struct ixgbe_adapter, cc);
  170. struct ixgbe_hw *hw = &adapter->hw;
  171. u64 stamp = 0;
  172. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  173. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  174. return stamp;
  175. }
  176. /**
  177. * ixgbe_ptp_adjfreq
  178. * @ptp: the ptp clock structure
  179. * @ppb: parts per billion adjustment from base
  180. *
  181. * adjust the frequency of the ptp cycle counter by the
  182. * indicated ppb from the base frequency.
  183. */
  184. static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  185. {
  186. struct ixgbe_adapter *adapter =
  187. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  188. struct ixgbe_hw *hw = &adapter->hw;
  189. u64 freq;
  190. u32 diff, incval;
  191. int neg_adj = 0;
  192. if (ppb < 0) {
  193. neg_adj = 1;
  194. ppb = -ppb;
  195. }
  196. smp_mb();
  197. incval = ACCESS_ONCE(adapter->base_incval);
  198. freq = incval;
  199. freq *= ppb;
  200. diff = div_u64(freq, 1000000000ULL);
  201. incval = neg_adj ? (incval - diff) : (incval + diff);
  202. switch (hw->mac.type) {
  203. case ixgbe_mac_X540:
  204. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
  205. break;
  206. case ixgbe_mac_82599EB:
  207. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  208. (1 << IXGBE_INCPER_SHIFT_82599) |
  209. incval);
  210. break;
  211. default:
  212. break;
  213. }
  214. return 0;
  215. }
  216. /**
  217. * ixgbe_ptp_adjtime
  218. * @ptp: the ptp clock structure
  219. * @delta: offset to adjust the cycle counter by
  220. *
  221. * adjust the timer by resetting the timecounter structure.
  222. */
  223. static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  224. {
  225. struct ixgbe_adapter *adapter =
  226. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  227. unsigned long flags;
  228. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  229. timecounter_adjtime(&adapter->tc, delta);
  230. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  231. ixgbe_ptp_setup_sdp(adapter);
  232. return 0;
  233. }
  234. /**
  235. * ixgbe_ptp_gettime
  236. * @ptp: the ptp clock structure
  237. * @ts: timespec structure to hold the current time value
  238. *
  239. * read the timecounter and return the correct value on ns,
  240. * after converting it into a struct timespec.
  241. */
  242. static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  243. {
  244. struct ixgbe_adapter *adapter =
  245. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  246. u64 ns;
  247. unsigned long flags;
  248. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  249. ns = timecounter_read(&adapter->tc);
  250. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  251. *ts = ns_to_timespec64(ns);
  252. return 0;
  253. }
  254. /**
  255. * ixgbe_ptp_settime
  256. * @ptp: the ptp clock structure
  257. * @ts: the timespec containing the new time for the cycle counter
  258. *
  259. * reset the timecounter to use a new base value instead of the kernel
  260. * wall timer value.
  261. */
  262. static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
  263. const struct timespec64 *ts)
  264. {
  265. struct ixgbe_adapter *adapter =
  266. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  267. u64 ns;
  268. unsigned long flags;
  269. ns = timespec64_to_ns(ts);
  270. /* reset the timecounter */
  271. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  272. timecounter_init(&adapter->tc, &adapter->cc, ns);
  273. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  274. ixgbe_ptp_setup_sdp(adapter);
  275. return 0;
  276. }
  277. /**
  278. * ixgbe_ptp_feature_enable
  279. * @ptp: the ptp clock structure
  280. * @rq: the requested feature to change
  281. * @on: whether to enable or disable the feature
  282. *
  283. * enable (or disable) ancillary features of the phc subsystem.
  284. * our driver only supports the PPS feature on the X540
  285. */
  286. static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
  287. struct ptp_clock_request *rq, int on)
  288. {
  289. struct ixgbe_adapter *adapter =
  290. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  291. /**
  292. * When PPS is enabled, unmask the interrupt for the ClockOut
  293. * feature, so that the interrupt handler can send the PPS
  294. * event when the clock SDP triggers. Clear mask when PPS is
  295. * disabled
  296. */
  297. if (rq->type == PTP_CLK_REQ_PPS) {
  298. switch (adapter->hw.mac.type) {
  299. case ixgbe_mac_X540:
  300. if (on)
  301. adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
  302. else
  303. adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
  304. ixgbe_ptp_setup_sdp(adapter);
  305. return 0;
  306. default:
  307. break;
  308. }
  309. }
  310. return -ENOTSUPP;
  311. }
  312. /**
  313. * ixgbe_ptp_check_pps_event
  314. * @adapter: the private adapter structure
  315. * @eicr: the interrupt cause register value
  316. *
  317. * This function is called by the interrupt routine when checking for
  318. * interrupts. It will check and handle a pps event.
  319. */
  320. void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
  321. {
  322. struct ixgbe_hw *hw = &adapter->hw;
  323. struct ptp_clock_event event;
  324. event.type = PTP_CLOCK_PPS;
  325. /* this check is necessary in case the interrupt was enabled via some
  326. * alternative means (ex. debug_fs). Better to check here than
  327. * everywhere that calls this function.
  328. */
  329. if (!adapter->ptp_clock)
  330. return;
  331. switch (hw->mac.type) {
  332. case ixgbe_mac_X540:
  333. ptp_clock_event(adapter->ptp_clock, &event);
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. /**
  340. * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
  341. * @adapter: private adapter struct
  342. *
  343. * this watchdog task periodically reads the timecounter
  344. * in order to prevent missing when the system time registers wrap
  345. * around. This needs to be run approximately twice a minute.
  346. */
  347. void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
  348. {
  349. bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
  350. IXGBE_OVERFLOW_PERIOD);
  351. struct timespec64 ts;
  352. if (timeout) {
  353. ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
  354. adapter->last_overflow_check = jiffies;
  355. }
  356. }
  357. /**
  358. * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
  359. * @adapter: private network adapter structure
  360. *
  361. * this watchdog task is scheduled to detect error case where hardware has
  362. * dropped an Rx packet that was timestamped when the ring is full. The
  363. * particular error is rare but leaves the device in a state unable to timestamp
  364. * any future packets.
  365. */
  366. void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
  367. {
  368. struct ixgbe_hw *hw = &adapter->hw;
  369. u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  370. unsigned long rx_event;
  371. /* if we don't have a valid timestamp in the registers, just update the
  372. * timeout counter and exit
  373. */
  374. if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
  375. adapter->last_rx_ptp_check = jiffies;
  376. return;
  377. }
  378. /* determine the most recent watchdog or rx_timestamp event */
  379. rx_event = adapter->last_rx_ptp_check;
  380. if (time_after(adapter->last_rx_timestamp, rx_event))
  381. rx_event = adapter->last_rx_timestamp;
  382. /* only need to read the high RXSTMP register to clear the lock */
  383. if (time_is_before_jiffies(rx_event + 5*HZ)) {
  384. IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
  385. adapter->last_rx_ptp_check = jiffies;
  386. e_warn(drv, "clearing RX Timestamp hang\n");
  387. }
  388. }
  389. /**
  390. * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  391. * @adapter: the private adapter struct
  392. *
  393. * if the timestamp is valid, we convert it into the timecounter ns
  394. * value, then store that result into the shhwtstamps structure which
  395. * is passed up the network stack
  396. */
  397. static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
  398. {
  399. struct ixgbe_hw *hw = &adapter->hw;
  400. struct skb_shared_hwtstamps shhwtstamps;
  401. u64 regval = 0, ns;
  402. unsigned long flags;
  403. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
  404. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
  405. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  406. ns = timecounter_cyc2time(&adapter->tc, regval);
  407. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  408. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  409. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  410. skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
  411. dev_kfree_skb_any(adapter->ptp_tx_skb);
  412. adapter->ptp_tx_skb = NULL;
  413. clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
  414. }
  415. /**
  416. * ixgbe_ptp_tx_hwtstamp_work
  417. * @work: pointer to the work struct
  418. *
  419. * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
  420. * timestamp has been taken for the current skb. It is necessary, because the
  421. * descriptor's "done" bit does not correlate with the timestamp event.
  422. */
  423. static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
  424. {
  425. struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
  426. ptp_tx_work);
  427. struct ixgbe_hw *hw = &adapter->hw;
  428. bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
  429. IXGBE_PTP_TX_TIMEOUT);
  430. u32 tsynctxctl;
  431. if (timeout) {
  432. dev_kfree_skb_any(adapter->ptp_tx_skb);
  433. adapter->ptp_tx_skb = NULL;
  434. clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
  435. e_warn(drv, "clearing Tx Timestamp hang\n");
  436. return;
  437. }
  438. tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  439. if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID)
  440. ixgbe_ptp_tx_hwtstamp(adapter);
  441. else
  442. /* reschedule to keep checking if it's not available yet */
  443. schedule_work(&adapter->ptp_tx_work);
  444. }
  445. /**
  446. * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
  447. * @adapter: pointer to adapter struct
  448. * @skb: particular skb to send timestamp with
  449. *
  450. * if the timestamp is valid, we convert it into the timecounter ns
  451. * value, then store that result into the shhwtstamps structure which
  452. * is passed up the network stack
  453. */
  454. void ixgbe_ptp_rx_hwtstamp(struct ixgbe_adapter *adapter, struct sk_buff *skb)
  455. {
  456. struct ixgbe_hw *hw = &adapter->hw;
  457. struct skb_shared_hwtstamps *shhwtstamps;
  458. u64 regval = 0, ns;
  459. u32 tsyncrxctl;
  460. unsigned long flags;
  461. tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  462. if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
  463. return;
  464. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
  465. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
  466. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  467. ns = timecounter_cyc2time(&adapter->tc, regval);
  468. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  469. shhwtstamps = skb_hwtstamps(skb);
  470. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  471. /* Update the last_rx_timestamp timer in order to enable watchdog check
  472. * for error case of latched timestamp on a dropped packet.
  473. */
  474. adapter->last_rx_timestamp = jiffies;
  475. }
  476. int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
  477. {
  478. struct hwtstamp_config *config = &adapter->tstamp_config;
  479. return copy_to_user(ifr->ifr_data, config,
  480. sizeof(*config)) ? -EFAULT : 0;
  481. }
  482. /**
  483. * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
  484. * @adapter: the private ixgbe adapter structure
  485. * @config: the hwtstamp configuration requested
  486. *
  487. * Outgoing time stamping can be enabled and disabled. Play nice and
  488. * disable it when requested, although it shouldn't cause any overhead
  489. * when no packet needs it. At most one packet in the queue may be
  490. * marked for time stamping, otherwise it would be impossible to tell
  491. * for sure to which packet the hardware time stamp belongs.
  492. *
  493. * Incoming time stamping has to be configured via the hardware
  494. * filters. Not all combinations are supported, in particular event
  495. * type has to be specified. Matching the kind of event packet is
  496. * not supported, with the exception of "all V2 events regardless of
  497. * level 2 or 4".
  498. *
  499. * Since hardware always timestamps Path delay packets when timestamping V2
  500. * packets, regardless of the type specified in the register, only use V2
  501. * Event mode. This more accurately tells the user what the hardware is going
  502. * to do anyways.
  503. *
  504. * Note: this may modify the hwtstamp configuration towards a more general
  505. * mode, if required to support the specifically requested mode.
  506. */
  507. static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
  508. struct hwtstamp_config *config)
  509. {
  510. struct ixgbe_hw *hw = &adapter->hw;
  511. u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
  512. u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
  513. u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
  514. bool is_l2 = false;
  515. u32 regval;
  516. /* reserved for future extensions */
  517. if (config->flags)
  518. return -EINVAL;
  519. switch (config->tx_type) {
  520. case HWTSTAMP_TX_OFF:
  521. tsync_tx_ctl = 0;
  522. case HWTSTAMP_TX_ON:
  523. break;
  524. default:
  525. return -ERANGE;
  526. }
  527. switch (config->rx_filter) {
  528. case HWTSTAMP_FILTER_NONE:
  529. tsync_rx_ctl = 0;
  530. tsync_rx_mtrl = 0;
  531. break;
  532. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  533. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  534. tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
  535. break;
  536. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  537. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  538. tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
  539. break;
  540. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  541. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  542. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  543. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  544. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  545. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  546. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  547. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  548. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  549. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
  550. is_l2 = true;
  551. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  552. break;
  553. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  554. case HWTSTAMP_FILTER_ALL:
  555. default:
  556. /*
  557. * register RXMTRL must be set in order to do V1 packets,
  558. * therefore it is not possible to time stamp both V1 Sync and
  559. * Delay_Req messages and hardware does not support
  560. * timestamping all packets => return error
  561. */
  562. config->rx_filter = HWTSTAMP_FILTER_NONE;
  563. return -ERANGE;
  564. }
  565. if (hw->mac.type == ixgbe_mac_82598EB) {
  566. if (tsync_rx_ctl | tsync_tx_ctl)
  567. return -ERANGE;
  568. return 0;
  569. }
  570. /* define ethertype filter for timestamping L2 packets */
  571. if (is_l2)
  572. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
  573. (IXGBE_ETQF_FILTER_EN | /* enable filter */
  574. IXGBE_ETQF_1588 | /* enable timestamping */
  575. ETH_P_1588)); /* 1588 eth protocol type */
  576. else
  577. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
  578. /* enable/disable TX */
  579. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  580. regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
  581. regval |= tsync_tx_ctl;
  582. IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
  583. /* enable/disable RX */
  584. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  585. regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
  586. regval |= tsync_rx_ctl;
  587. IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
  588. /* define which PTP packets are time stamped */
  589. IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
  590. IXGBE_WRITE_FLUSH(hw);
  591. /* clear TX/RX time stamp registers, just to be sure */
  592. regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
  593. regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
  594. return 0;
  595. }
  596. /**
  597. * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
  598. * @adapter: pointer to adapter struct
  599. * @ifreq: ioctl data
  600. *
  601. * Set hardware to requested mode. If unsupported, return an error with no
  602. * changes. Otherwise, store the mode for future reference.
  603. */
  604. int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
  605. {
  606. struct hwtstamp_config config;
  607. int err;
  608. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  609. return -EFAULT;
  610. err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
  611. if (err)
  612. return err;
  613. /* save these settings for future reference */
  614. memcpy(&adapter->tstamp_config, &config,
  615. sizeof(adapter->tstamp_config));
  616. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  617. -EFAULT : 0;
  618. }
  619. /**
  620. * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
  621. * @adapter: pointer to the adapter structure
  622. *
  623. * This function should be called to set the proper values for the TIMINCA
  624. * register and tell the cyclecounter structure what the tick rate of SYSTIME
  625. * is. It does not directly modify SYSTIME registers or the timecounter
  626. * structure. It should be called whenever a new TIMINCA value is necessary,
  627. * such as during initialization or when the link speed changes.
  628. */
  629. void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
  630. {
  631. struct ixgbe_hw *hw = &adapter->hw;
  632. u32 incval = 0;
  633. u32 shift = 0;
  634. unsigned long flags;
  635. /**
  636. * Scale the NIC cycle counter by a large factor so that
  637. * relatively small corrections to the frequency can be added
  638. * or subtracted. The drawbacks of a large factor include
  639. * (a) the clock register overflows more quickly, (b) the cycle
  640. * counter structure must be able to convert the systime value
  641. * to nanoseconds using only a multiplier and a right-shift,
  642. * and (c) the value must fit within the timinca register space
  643. * => math based on internal DMA clock rate and available bits
  644. *
  645. * Note that when there is no link, internal DMA clock is same as when
  646. * link speed is 10Gb. Set the registers correctly even when link is
  647. * down to preserve the clock setting
  648. */
  649. switch (adapter->link_speed) {
  650. case IXGBE_LINK_SPEED_100_FULL:
  651. incval = IXGBE_INCVAL_100;
  652. shift = IXGBE_INCVAL_SHIFT_100;
  653. break;
  654. case IXGBE_LINK_SPEED_1GB_FULL:
  655. incval = IXGBE_INCVAL_1GB;
  656. shift = IXGBE_INCVAL_SHIFT_1GB;
  657. break;
  658. case IXGBE_LINK_SPEED_10GB_FULL:
  659. default:
  660. incval = IXGBE_INCVAL_10GB;
  661. shift = IXGBE_INCVAL_SHIFT_10GB;
  662. break;
  663. }
  664. /**
  665. * Modify the calculated values to fit within the correct
  666. * number of bits specified by the hardware. The 82599 doesn't
  667. * have the same space as the X540, so bitshift the calculated
  668. * values to fit.
  669. */
  670. switch (hw->mac.type) {
  671. case ixgbe_mac_X540:
  672. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
  673. break;
  674. case ixgbe_mac_82599EB:
  675. incval >>= IXGBE_INCVAL_SHIFT_82599;
  676. shift -= IXGBE_INCVAL_SHIFT_82599;
  677. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  678. (1 << IXGBE_INCPER_SHIFT_82599) |
  679. incval);
  680. break;
  681. default:
  682. /* other devices aren't supported */
  683. return;
  684. }
  685. /* update the base incval used to calculate frequency adjustment */
  686. ACCESS_ONCE(adapter->base_incval) = incval;
  687. smp_mb();
  688. /* need lock to prevent incorrect read while modifying cyclecounter */
  689. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  690. memset(&adapter->cc, 0, sizeof(adapter->cc));
  691. adapter->cc.read = ixgbe_ptp_read;
  692. adapter->cc.mask = CYCLECOUNTER_MASK(64);
  693. adapter->cc.shift = shift;
  694. adapter->cc.mult = 1;
  695. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  696. }
  697. /**
  698. * ixgbe_ptp_reset
  699. * @adapter: the ixgbe private board structure
  700. *
  701. * When the MAC resets, all the hardware bits for timesync are reset. This
  702. * function is used to re-enable the device for PTP based on current settings.
  703. * We do lose the current clock time, so just reset the cyclecounter to the
  704. * system real clock time.
  705. *
  706. * This function will maintain hwtstamp_config settings, and resets the SDP
  707. * output if it was enabled.
  708. */
  709. void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
  710. {
  711. struct ixgbe_hw *hw = &adapter->hw;
  712. unsigned long flags;
  713. /* set SYSTIME registers to 0 just in case */
  714. IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
  715. IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
  716. IXGBE_WRITE_FLUSH(hw);
  717. /* reset the hardware timestamping mode */
  718. ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
  719. ixgbe_ptp_start_cyclecounter(adapter);
  720. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  721. /* reset the ns time counter */
  722. timecounter_init(&adapter->tc, &adapter->cc,
  723. ktime_to_ns(ktime_get_real()));
  724. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  725. /*
  726. * Now that the shift has been calculated and the systime
  727. * registers reset, (re-)enable the Clock out feature
  728. */
  729. ixgbe_ptp_setup_sdp(adapter);
  730. }
  731. /**
  732. * ixgbe_ptp_create_clock
  733. * @adapter: the ixgbe private adapter structure
  734. *
  735. * This function performs setup of the user entry point function table and
  736. * initializes the PTP clock device, which is used to access the clock-like
  737. * features of the PTP core. It will be called by ixgbe_ptp_init, only if
  738. * there isn't already a clock device (such as after a suspend/resume cycle,
  739. * where the clock device wasn't destroyed).
  740. */
  741. static int ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
  742. {
  743. struct net_device *netdev = adapter->netdev;
  744. long err;
  745. /* do nothing if we already have a clock device */
  746. if (!IS_ERR_OR_NULL(adapter->ptp_clock))
  747. return 0;
  748. switch (adapter->hw.mac.type) {
  749. case ixgbe_mac_X540:
  750. snprintf(adapter->ptp_caps.name,
  751. sizeof(adapter->ptp_caps.name),
  752. "%s", netdev->name);
  753. adapter->ptp_caps.owner = THIS_MODULE;
  754. adapter->ptp_caps.max_adj = 250000000;
  755. adapter->ptp_caps.n_alarm = 0;
  756. adapter->ptp_caps.n_ext_ts = 0;
  757. adapter->ptp_caps.n_per_out = 0;
  758. adapter->ptp_caps.pps = 1;
  759. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
  760. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  761. adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
  762. adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
  763. adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
  764. break;
  765. case ixgbe_mac_82599EB:
  766. snprintf(adapter->ptp_caps.name,
  767. sizeof(adapter->ptp_caps.name),
  768. "%s", netdev->name);
  769. adapter->ptp_caps.owner = THIS_MODULE;
  770. adapter->ptp_caps.max_adj = 250000000;
  771. adapter->ptp_caps.n_alarm = 0;
  772. adapter->ptp_caps.n_ext_ts = 0;
  773. adapter->ptp_caps.n_per_out = 0;
  774. adapter->ptp_caps.pps = 0;
  775. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
  776. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  777. adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
  778. adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
  779. adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
  780. break;
  781. default:
  782. adapter->ptp_clock = NULL;
  783. return -EOPNOTSUPP;
  784. }
  785. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  786. &adapter->pdev->dev);
  787. if (IS_ERR(adapter->ptp_clock)) {
  788. err = PTR_ERR(adapter->ptp_clock);
  789. adapter->ptp_clock = NULL;
  790. e_dev_err("ptp_clock_register failed\n");
  791. return err;
  792. } else
  793. e_dev_info("registered PHC device on %s\n", netdev->name);
  794. /* set default timestamp mode to disabled here. We do this in
  795. * create_clock instead of init, because we don't want to override the
  796. * previous settings during a resume cycle.
  797. */
  798. adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  799. adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  800. return 0;
  801. }
  802. /**
  803. * ixgbe_ptp_init
  804. * @adapter: the ixgbe private adapter structure
  805. *
  806. * This function performs the required steps for enabling PTP
  807. * support. If PTP support has already been loaded it simply calls the
  808. * cyclecounter init routine and exits.
  809. */
  810. void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
  811. {
  812. /* initialize the spin lock first since we can't control when a user
  813. * will call the entry functions once we have initialized the clock
  814. * device
  815. */
  816. spin_lock_init(&adapter->tmreg_lock);
  817. /* obtain a PTP device, or re-use an existing device */
  818. if (ixgbe_ptp_create_clock(adapter))
  819. return;
  820. /* we have a clock so we can initialize work now */
  821. INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
  822. /* reset the PTP related hardware bits */
  823. ixgbe_ptp_reset(adapter);
  824. /* enter the IXGBE_PTP_RUNNING state */
  825. set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
  826. return;
  827. }
  828. /**
  829. * ixgbe_ptp_suspend - stop PTP work items
  830. * @ adapter: pointer to adapter struct
  831. *
  832. * this function suspends PTP activity, and prevents more PTP work from being
  833. * generated, but does not destroy the PTP clock device.
  834. */
  835. void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
  836. {
  837. /* Leave the IXGBE_PTP_RUNNING state. */
  838. if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
  839. return;
  840. /* since this might be called in suspend, we don't clear the state,
  841. * but simply reset the auxiliary PPS signal control register
  842. */
  843. IXGBE_WRITE_REG(&adapter->hw, IXGBE_TSAUXC, 0x0);
  844. /* ensure that we cancel any pending PTP Tx work item in progress */
  845. cancel_work_sync(&adapter->ptp_tx_work);
  846. if (adapter->ptp_tx_skb) {
  847. dev_kfree_skb_any(adapter->ptp_tx_skb);
  848. adapter->ptp_tx_skb = NULL;
  849. clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
  850. }
  851. }
  852. /**
  853. * ixgbe_ptp_stop - close the PTP device
  854. * @adapter: pointer to adapter struct
  855. *
  856. * completely destroy the PTP device, should only be called when the device is
  857. * being fully closed.
  858. */
  859. void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
  860. {
  861. /* first, suspend PTP activity */
  862. ixgbe_ptp_suspend(adapter);
  863. /* disable the PTP clock device */
  864. if (adapter->ptp_clock) {
  865. ptp_clock_unregister(adapter->ptp_clock);
  866. adapter->ptp_clock = NULL;
  867. e_dev_info("removed PHC on %s\n",
  868. adapter->netdev->name);
  869. }
  870. }