ixgbe_ptp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include "ixgbe.h"
  21. #include <linux/export.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. #ifndef NSECS_PER_SEC
  90. #define NSECS_PER_SEC 1000000000ULL
  91. #endif
  92. /**
  93. * ixgbe_ptp_setup_sdp
  94. * @hw: the hardware private structure
  95. *
  96. * this function enables or disables the clock out feature on SDP0 for
  97. * the X540 device. It will create a 1second periodic output that can
  98. * be used as the PPS (via an interrupt).
  99. *
  100. * It calculates when the systime will be on an exact second, and then
  101. * aligns the start of the PPS signal to that value. The shift is
  102. * necessary because it can change based on the link speed.
  103. */
  104. static void ixgbe_ptp_setup_sdp(struct ixgbe_adapter *adapter)
  105. {
  106. struct ixgbe_hw *hw = &adapter->hw;
  107. int shift = adapter->cc.shift;
  108. u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
  109. u64 ns = 0, clock_edge = 0;
  110. if ((adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED) &&
  111. (hw->mac.type == ixgbe_mac_X540)) {
  112. /* disable the pin first */
  113. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
  114. IXGBE_WRITE_FLUSH(hw);
  115. esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
  116. /*
  117. * enable the SDP0 pin as output, and connected to the
  118. * native function for Timesync (ClockOut)
  119. */
  120. esdp |= (IXGBE_ESDP_SDP0_DIR |
  121. IXGBE_ESDP_SDP0_NATIVE);
  122. /*
  123. * enable the Clock Out feature on SDP0, and allow
  124. * interrupts to occur when the pin changes
  125. */
  126. tsauxc = (IXGBE_TSAUXC_EN_CLK |
  127. IXGBE_TSAUXC_SYNCLK |
  128. IXGBE_TSAUXC_SDP0_INT);
  129. /* clock period (or pulse length) */
  130. clktiml = (u32)(NSECS_PER_SEC << shift);
  131. clktimh = (u32)((NSECS_PER_SEC << shift) >> 32);
  132. /*
  133. * Account for the cyclecounter wrap-around value by
  134. * using the converted ns value of the current time to
  135. * check for when the next aligned second would occur.
  136. */
  137. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  138. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  139. ns = timecounter_cyc2time(&adapter->tc, clock_edge);
  140. div_u64_rem(ns, NSECS_PER_SEC, &rem);
  141. clock_edge += ((NSECS_PER_SEC - (u64)rem) << shift);
  142. /* specify the initial clock start time */
  143. trgttiml = (u32)clock_edge;
  144. trgttimh = (u32)(clock_edge >> 32);
  145. IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
  146. IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
  147. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
  148. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
  149. IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
  150. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
  151. } else {
  152. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
  153. }
  154. IXGBE_WRITE_FLUSH(hw);
  155. }
  156. /**
  157. * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
  158. * @cc: the cyclecounter structure
  159. *
  160. * this function reads the cyclecounter registers and is called by the
  161. * cyclecounter structure used to construct a ns counter from the
  162. * arbitrary fixed point registers
  163. */
  164. static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
  165. {
  166. struct ixgbe_adapter *adapter =
  167. container_of(cc, struct ixgbe_adapter, cc);
  168. struct ixgbe_hw *hw = &adapter->hw;
  169. u64 stamp = 0;
  170. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  171. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  172. return stamp;
  173. }
  174. /**
  175. * ixgbe_ptp_adjfreq
  176. * @ptp: the ptp clock structure
  177. * @ppb: parts per billion adjustment from base
  178. *
  179. * adjust the frequency of the ptp cycle counter by the
  180. * indicated ppb from the base frequency.
  181. */
  182. static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  183. {
  184. struct ixgbe_adapter *adapter =
  185. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  186. struct ixgbe_hw *hw = &adapter->hw;
  187. u64 freq;
  188. u32 diff, incval;
  189. int neg_adj = 0;
  190. if (ppb < 0) {
  191. neg_adj = 1;
  192. ppb = -ppb;
  193. }
  194. smp_mb();
  195. incval = ACCESS_ONCE(adapter->base_incval);
  196. freq = incval;
  197. freq *= ppb;
  198. diff = div_u64(freq, 1000000000ULL);
  199. incval = neg_adj ? (incval - diff) : (incval + diff);
  200. switch (hw->mac.type) {
  201. case ixgbe_mac_X540:
  202. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
  203. break;
  204. case ixgbe_mac_82599EB:
  205. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  206. (1 << IXGBE_INCPER_SHIFT_82599) |
  207. incval);
  208. break;
  209. default:
  210. break;
  211. }
  212. return 0;
  213. }
  214. /**
  215. * ixgbe_ptp_adjtime
  216. * @ptp: the ptp clock structure
  217. * @delta: offset to adjust the cycle counter by
  218. *
  219. * adjust the timer by resetting the timecounter structure.
  220. */
  221. static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  222. {
  223. struct ixgbe_adapter *adapter =
  224. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  225. unsigned long flags;
  226. u64 now;
  227. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  228. now = timecounter_read(&adapter->tc);
  229. now += delta;
  230. /* reset the timecounter */
  231. timecounter_init(&adapter->tc,
  232. &adapter->cc,
  233. now);
  234. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  235. ixgbe_ptp_setup_sdp(adapter);
  236. return 0;
  237. }
  238. /**
  239. * ixgbe_ptp_gettime
  240. * @ptp: the ptp clock structure
  241. * @ts: timespec structure to hold the current time value
  242. *
  243. * read the timecounter and return the correct value on ns,
  244. * after converting it into a struct timespec.
  245. */
  246. static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  247. {
  248. struct ixgbe_adapter *adapter =
  249. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  250. u64 ns;
  251. u32 remainder;
  252. unsigned long flags;
  253. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  254. ns = timecounter_read(&adapter->tc);
  255. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  256. ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
  257. ts->tv_nsec = remainder;
  258. return 0;
  259. }
  260. /**
  261. * ixgbe_ptp_settime
  262. * @ptp: the ptp clock structure
  263. * @ts: the timespec containing the new time for the cycle counter
  264. *
  265. * reset the timecounter to use a new base value instead of the kernel
  266. * wall timer value.
  267. */
  268. static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
  269. const struct timespec *ts)
  270. {
  271. struct ixgbe_adapter *adapter =
  272. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  273. u64 ns;
  274. unsigned long flags;
  275. ns = ts->tv_sec * 1000000000ULL;
  276. ns += ts->tv_nsec;
  277. /* reset the timecounter */
  278. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  279. timecounter_init(&adapter->tc, &adapter->cc, ns);
  280. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  281. ixgbe_ptp_setup_sdp(adapter);
  282. return 0;
  283. }
  284. /**
  285. * ixgbe_ptp_enable
  286. * @ptp: the ptp clock structure
  287. * @rq: the requested feature to change
  288. * @on: whether to enable or disable the feature
  289. *
  290. * enable (or disable) ancillary features of the phc subsystem.
  291. * our driver only supports the PPS feature on the X540
  292. */
  293. static int ixgbe_ptp_enable(struct ptp_clock_info *ptp,
  294. struct ptp_clock_request *rq, int on)
  295. {
  296. struct ixgbe_adapter *adapter =
  297. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  298. /**
  299. * When PPS is enabled, unmask the interrupt for the ClockOut
  300. * feature, so that the interrupt handler can send the PPS
  301. * event when the clock SDP triggers. Clear mask when PPS is
  302. * disabled
  303. */
  304. if (rq->type == PTP_CLK_REQ_PPS) {
  305. switch (adapter->hw.mac.type) {
  306. case ixgbe_mac_X540:
  307. if (on)
  308. adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
  309. else
  310. adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
  311. ixgbe_ptp_setup_sdp(adapter);
  312. return 0;
  313. default:
  314. break;
  315. }
  316. }
  317. return -ENOTSUPP;
  318. }
  319. /**
  320. * ixgbe_ptp_check_pps_event
  321. * @adapter: the private adapter structure
  322. * @eicr: the interrupt cause register value
  323. *
  324. * This function is called by the interrupt routine when checking for
  325. * interrupts. It will check and handle a pps event.
  326. */
  327. void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
  328. {
  329. struct ixgbe_hw *hw = &adapter->hw;
  330. struct ptp_clock_event event;
  331. event.type = PTP_CLOCK_PPS;
  332. /* this check is necessary in case the interrupt was enabled via some
  333. * alternative means (ex. debug_fs). Better to check here than
  334. * everywhere that calls this function.
  335. */
  336. if (!adapter->ptp_clock)
  337. return;
  338. switch (hw->mac.type) {
  339. case ixgbe_mac_X540:
  340. ptp_clock_event(adapter->ptp_clock, &event);
  341. break;
  342. default:
  343. break;
  344. }
  345. }
  346. /**
  347. * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
  348. * @adapter: private adapter struct
  349. *
  350. * this watchdog task periodically reads the timecounter
  351. * in order to prevent missing when the system time registers wrap
  352. * around. This needs to be run approximately twice a minute.
  353. */
  354. void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
  355. {
  356. bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
  357. IXGBE_OVERFLOW_PERIOD);
  358. struct timespec ts;
  359. if (timeout) {
  360. ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
  361. adapter->last_overflow_check = jiffies;
  362. }
  363. }
  364. /**
  365. * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
  366. * @adapter: private network adapter structure
  367. *
  368. * this watchdog task is scheduled to detect error case where hardware has
  369. * dropped an Rx packet that was timestamped when the ring is full. The
  370. * particular error is rare but leaves the device in a state unable to timestamp
  371. * any future packets.
  372. */
  373. void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
  374. {
  375. struct ixgbe_hw *hw = &adapter->hw;
  376. struct ixgbe_ring *rx_ring;
  377. u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  378. unsigned long rx_event;
  379. int n;
  380. /* if we don't have a valid timestamp in the registers, just update the
  381. * timeout counter and exit
  382. */
  383. if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
  384. adapter->last_rx_ptp_check = jiffies;
  385. return;
  386. }
  387. /* determine the most recent watchdog or rx_timestamp event */
  388. rx_event = adapter->last_rx_ptp_check;
  389. for (n = 0; n < adapter->num_rx_queues; n++) {
  390. rx_ring = adapter->rx_ring[n];
  391. if (time_after(rx_ring->last_rx_timestamp, rx_event))
  392. rx_event = rx_ring->last_rx_timestamp;
  393. }
  394. /* only need to read the high RXSTMP register to clear the lock */
  395. if (time_is_before_jiffies(rx_event + 5*HZ)) {
  396. IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
  397. adapter->last_rx_ptp_check = jiffies;
  398. e_warn(drv, "clearing RX Timestamp hang");
  399. }
  400. }
  401. /**
  402. * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  403. * @adapter: the private adapter struct
  404. *
  405. * if the timestamp is valid, we convert it into the timecounter ns
  406. * value, then store that result into the shhwtstamps structure which
  407. * is passed up the network stack
  408. */
  409. static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
  410. {
  411. struct ixgbe_hw *hw = &adapter->hw;
  412. struct skb_shared_hwtstamps shhwtstamps;
  413. u64 regval = 0, ns;
  414. unsigned long flags;
  415. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
  416. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
  417. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  418. ns = timecounter_cyc2time(&adapter->tc, regval);
  419. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  420. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  421. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  422. skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
  423. dev_kfree_skb_any(adapter->ptp_tx_skb);
  424. adapter->ptp_tx_skb = NULL;
  425. }
  426. /**
  427. * ixgbe_ptp_tx_hwtstamp_work
  428. * @work: pointer to the work struct
  429. *
  430. * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
  431. * timestamp has been taken for the current skb. It is necesary, because the
  432. * descriptor's "done" bit does not correlate with the timestamp event.
  433. */
  434. static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
  435. {
  436. struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
  437. ptp_tx_work);
  438. struct ixgbe_hw *hw = &adapter->hw;
  439. bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
  440. IXGBE_PTP_TX_TIMEOUT);
  441. u32 tsynctxctl;
  442. /* we have to have a valid skb */
  443. if (!adapter->ptp_tx_skb)
  444. return;
  445. if (timeout) {
  446. dev_kfree_skb_any(adapter->ptp_tx_skb);
  447. adapter->ptp_tx_skb = NULL;
  448. e_warn(drv, "clearing Tx Timestamp hang");
  449. return;
  450. }
  451. tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  452. if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID)
  453. ixgbe_ptp_tx_hwtstamp(adapter);
  454. else
  455. /* reschedule to keep checking if it's not available yet */
  456. schedule_work(&adapter->ptp_tx_work);
  457. }
  458. /**
  459. * __ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
  460. * @q_vector: structure containing interrupt and ring information
  461. * @skb: particular skb to send timestamp with
  462. *
  463. * if the timestamp is valid, we convert it into the timecounter ns
  464. * value, then store that result into the shhwtstamps structure which
  465. * is passed up the network stack
  466. */
  467. void __ixgbe_ptp_rx_hwtstamp(struct ixgbe_q_vector *q_vector,
  468. struct sk_buff *skb)
  469. {
  470. struct ixgbe_adapter *adapter;
  471. struct ixgbe_hw *hw;
  472. struct skb_shared_hwtstamps *shhwtstamps;
  473. u64 regval = 0, ns;
  474. u32 tsyncrxctl;
  475. unsigned long flags;
  476. /* we cannot process timestamps on a ring without a q_vector */
  477. if (!q_vector || !q_vector->adapter)
  478. return;
  479. adapter = q_vector->adapter;
  480. hw = &adapter->hw;
  481. /*
  482. * Read the tsyncrxctl register afterwards in order to prevent taking an
  483. * I/O hit on every packet.
  484. */
  485. tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  486. if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
  487. return;
  488. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
  489. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
  490. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  491. ns = timecounter_cyc2time(&adapter->tc, regval);
  492. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  493. shhwtstamps = skb_hwtstamps(skb);
  494. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  495. }
  496. /**
  497. * ixgbe_ptp_hwtstamp_ioctl - control hardware time stamping
  498. * @adapter: pointer to adapter struct
  499. * @ifreq: ioctl data
  500. * @cmd: particular ioctl requested
  501. *
  502. * Outgoing time stamping can be enabled and disabled. Play nice and
  503. * disable it when requested, although it shouldn't case any overhead
  504. * when no packet needs it. At most one packet in the queue may be
  505. * marked for time stamping, otherwise it would be impossible to tell
  506. * for sure to which packet the hardware time stamp belongs.
  507. *
  508. * Incoming time stamping has to be configured via the hardware
  509. * filters. Not all combinations are supported, in particular event
  510. * type has to be specified. Matching the kind of event packet is
  511. * not supported, with the exception of "all V2 events regardless of
  512. * level 2 or 4".
  513. *
  514. * Since hardware always timestamps Path delay packets when timestamping V2
  515. * packets, regardless of the type specified in the register, only use V2
  516. * Event mode. This more accurately tells the user what the hardware is going
  517. * to do anyways.
  518. */
  519. int ixgbe_ptp_hwtstamp_ioctl(struct ixgbe_adapter *adapter,
  520. struct ifreq *ifr, int cmd)
  521. {
  522. struct ixgbe_hw *hw = &adapter->hw;
  523. struct hwtstamp_config config;
  524. u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
  525. u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
  526. u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
  527. bool is_l2 = false;
  528. u32 regval;
  529. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  530. return -EFAULT;
  531. /* reserved for future extensions */
  532. if (config.flags)
  533. return -EINVAL;
  534. switch (config.tx_type) {
  535. case HWTSTAMP_TX_OFF:
  536. tsync_tx_ctl = 0;
  537. case HWTSTAMP_TX_ON:
  538. break;
  539. default:
  540. return -ERANGE;
  541. }
  542. switch (config.rx_filter) {
  543. case HWTSTAMP_FILTER_NONE:
  544. tsync_rx_ctl = 0;
  545. tsync_rx_mtrl = 0;
  546. break;
  547. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  548. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  549. tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
  550. break;
  551. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  552. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  553. tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
  554. break;
  555. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  556. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  557. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  558. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  559. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  560. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  561. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  562. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  563. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  564. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
  565. is_l2 = true;
  566. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  567. break;
  568. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  569. case HWTSTAMP_FILTER_ALL:
  570. default:
  571. /*
  572. * register RXMTRL must be set in order to do V1 packets,
  573. * therefore it is not possible to time stamp both V1 Sync and
  574. * Delay_Req messages and hardware does not support
  575. * timestamping all packets => return error
  576. */
  577. config.rx_filter = HWTSTAMP_FILTER_NONE;
  578. return -ERANGE;
  579. }
  580. if (hw->mac.type == ixgbe_mac_82598EB) {
  581. if (tsync_rx_ctl | tsync_tx_ctl)
  582. return -ERANGE;
  583. return 0;
  584. }
  585. /* define ethertype filter for timestamping L2 packets */
  586. if (is_l2)
  587. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
  588. (IXGBE_ETQF_FILTER_EN | /* enable filter */
  589. IXGBE_ETQF_1588 | /* enable timestamping */
  590. ETH_P_1588)); /* 1588 eth protocol type */
  591. else
  592. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
  593. /* enable/disable TX */
  594. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  595. regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
  596. regval |= tsync_tx_ctl;
  597. IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
  598. /* enable/disable RX */
  599. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  600. regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
  601. regval |= tsync_rx_ctl;
  602. IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
  603. /* define which PTP packets are time stamped */
  604. IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
  605. IXGBE_WRITE_FLUSH(hw);
  606. /* clear TX/RX time stamp registers, just to be sure */
  607. regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
  608. regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
  609. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  610. -EFAULT : 0;
  611. }
  612. /**
  613. * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
  614. * @adapter: pointer to the adapter structure
  615. *
  616. * This function should be called to set the proper values for the TIMINCA
  617. * register and tell the cyclecounter structure what the tick rate of SYSTIME
  618. * is. It does not directly modify SYSTIME registers or the timecounter
  619. * structure. It should be called whenever a new TIMINCA value is necessary,
  620. * such as during initialization or when the link speed changes.
  621. */
  622. void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
  623. {
  624. struct ixgbe_hw *hw = &adapter->hw;
  625. u32 incval = 0;
  626. u32 shift = 0;
  627. unsigned long flags;
  628. /**
  629. * Scale the NIC cycle counter by a large factor so that
  630. * relatively small corrections to the frequency can be added
  631. * or subtracted. The drawbacks of a large factor include
  632. * (a) the clock register overflows more quickly, (b) the cycle
  633. * counter structure must be able to convert the systime value
  634. * to nanoseconds using only a multiplier and a right-shift,
  635. * and (c) the value must fit within the timinca register space
  636. * => math based on internal DMA clock rate and available bits
  637. *
  638. * Note that when there is no link, internal DMA clock is same as when
  639. * link speed is 10Gb. Set the registers correctly even when link is
  640. * down to preserve the clock setting
  641. */
  642. switch (adapter->link_speed) {
  643. case IXGBE_LINK_SPEED_100_FULL:
  644. incval = IXGBE_INCVAL_100;
  645. shift = IXGBE_INCVAL_SHIFT_100;
  646. break;
  647. case IXGBE_LINK_SPEED_1GB_FULL:
  648. incval = IXGBE_INCVAL_1GB;
  649. shift = IXGBE_INCVAL_SHIFT_1GB;
  650. break;
  651. case IXGBE_LINK_SPEED_10GB_FULL:
  652. default:
  653. incval = IXGBE_INCVAL_10GB;
  654. shift = IXGBE_INCVAL_SHIFT_10GB;
  655. break;
  656. }
  657. /**
  658. * Modify the calculated values to fit within the correct
  659. * number of bits specified by the hardware. The 82599 doesn't
  660. * have the same space as the X540, so bitshift the calculated
  661. * values to fit.
  662. */
  663. switch (hw->mac.type) {
  664. case ixgbe_mac_X540:
  665. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
  666. break;
  667. case ixgbe_mac_82599EB:
  668. incval >>= IXGBE_INCVAL_SHIFT_82599;
  669. shift -= IXGBE_INCVAL_SHIFT_82599;
  670. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  671. (1 << IXGBE_INCPER_SHIFT_82599) |
  672. incval);
  673. break;
  674. default:
  675. /* other devices aren't supported */
  676. return;
  677. }
  678. /* update the base incval used to calculate frequency adjustment */
  679. ACCESS_ONCE(adapter->base_incval) = incval;
  680. smp_mb();
  681. /* need lock to prevent incorrect read while modifying cyclecounter */
  682. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  683. memset(&adapter->cc, 0, sizeof(adapter->cc));
  684. adapter->cc.read = ixgbe_ptp_read;
  685. adapter->cc.mask = CLOCKSOURCE_MASK(64);
  686. adapter->cc.shift = shift;
  687. adapter->cc.mult = 1;
  688. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  689. }
  690. /**
  691. * ixgbe_ptp_reset
  692. * @adapter: the ixgbe private board structure
  693. *
  694. * When the MAC resets, all timesync features are reset. This function should be
  695. * called to re-enable the PTP clock structure. It will re-init the timecounter
  696. * structure based on the kernel time as well as setup the cycle counter data.
  697. */
  698. void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
  699. {
  700. struct ixgbe_hw *hw = &adapter->hw;
  701. unsigned long flags;
  702. /* set SYSTIME registers to 0 just in case */
  703. IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
  704. IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
  705. IXGBE_WRITE_FLUSH(hw);
  706. ixgbe_ptp_start_cyclecounter(adapter);
  707. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  708. /* reset the ns time counter */
  709. timecounter_init(&adapter->tc, &adapter->cc,
  710. ktime_to_ns(ktime_get_real()));
  711. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  712. /*
  713. * Now that the shift has been calculated and the systime
  714. * registers reset, (re-)enable the Clock out feature
  715. */
  716. ixgbe_ptp_setup_sdp(adapter);
  717. }
  718. /**
  719. * ixgbe_ptp_init
  720. * @adapter: the ixgbe private adapter structure
  721. *
  722. * This function performs the required steps for enabling ptp
  723. * support. If ptp support has already been loaded it simply calls the
  724. * cyclecounter init routine and exits.
  725. */
  726. void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
  727. {
  728. struct net_device *netdev = adapter->netdev;
  729. switch (adapter->hw.mac.type) {
  730. case ixgbe_mac_X540:
  731. snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
  732. adapter->ptp_caps.owner = THIS_MODULE;
  733. adapter->ptp_caps.max_adj = 250000000;
  734. adapter->ptp_caps.n_alarm = 0;
  735. adapter->ptp_caps.n_ext_ts = 0;
  736. adapter->ptp_caps.n_per_out = 0;
  737. adapter->ptp_caps.pps = 1;
  738. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
  739. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  740. adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
  741. adapter->ptp_caps.settime = ixgbe_ptp_settime;
  742. adapter->ptp_caps.enable = ixgbe_ptp_enable;
  743. break;
  744. case ixgbe_mac_82599EB:
  745. snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
  746. adapter->ptp_caps.owner = THIS_MODULE;
  747. adapter->ptp_caps.max_adj = 250000000;
  748. adapter->ptp_caps.n_alarm = 0;
  749. adapter->ptp_caps.n_ext_ts = 0;
  750. adapter->ptp_caps.n_per_out = 0;
  751. adapter->ptp_caps.pps = 0;
  752. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
  753. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  754. adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
  755. adapter->ptp_caps.settime = ixgbe_ptp_settime;
  756. adapter->ptp_caps.enable = ixgbe_ptp_enable;
  757. break;
  758. default:
  759. adapter->ptp_clock = NULL;
  760. return;
  761. }
  762. spin_lock_init(&adapter->tmreg_lock);
  763. INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
  764. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  765. &adapter->pdev->dev);
  766. if (IS_ERR(adapter->ptp_clock)) {
  767. adapter->ptp_clock = NULL;
  768. e_dev_err("ptp_clock_register failed\n");
  769. } else
  770. e_dev_info("registered PHC device on %s\n", netdev->name);
  771. ixgbe_ptp_reset(adapter);
  772. /* set the flag that PTP has been enabled */
  773. adapter->flags2 |= IXGBE_FLAG2_PTP_ENABLED;
  774. return;
  775. }
  776. /**
  777. * ixgbe_ptp_stop - disable ptp device and stop the overflow check
  778. * @adapter: pointer to adapter struct
  779. *
  780. * this function stops the ptp support, and cancels the delayed work.
  781. */
  782. void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
  783. {
  784. /* stop the overflow check task */
  785. adapter->flags2 &= ~(IXGBE_FLAG2_PTP_ENABLED |
  786. IXGBE_FLAG2_PTP_PPS_ENABLED);
  787. ixgbe_ptp_setup_sdp(adapter);
  788. cancel_work_sync(&adapter->ptp_tx_work);
  789. if (adapter->ptp_tx_skb) {
  790. dev_kfree_skb_any(adapter->ptp_tx_skb);
  791. adapter->ptp_tx_skb = NULL;
  792. }
  793. if (adapter->ptp_clock) {
  794. ptp_clock_unregister(adapter->ptp_clock);
  795. adapter->ptp_clock = NULL;
  796. e_dev_info("removed PHC on %s\n",
  797. adapter->netdev->name);
  798. }
  799. }