ixgbe_ptp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2012 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. /*
  23. * The 82599 and the X540 do not have true 64bit nanosecond scale
  24. * counter registers. Instead, SYSTIME is defined by a fixed point
  25. * system which allows the user to define the scale counter increment
  26. * value at every level change of the oscillator driving the SYSTIME
  27. * value. For both devices the TIMINCA:IV field defines this
  28. * increment. On the X540 device, 31 bits are provided. However on the
  29. * 82599 only provides 24 bits. The time unit is determined by the
  30. * clock frequency of the oscillator in combination with the TIMINCA
  31. * register. When these devices link at 10Gb the oscillator has a
  32. * period of 6.4ns. In order to convert the scale counter into
  33. * nanoseconds the cyclecounter and timecounter structures are
  34. * used. The SYSTIME registers need to be converted to ns values by use
  35. * of only a right shift (division by power of 2). The following math
  36. * determines the largest incvalue that will fit into the available
  37. * bits in the TIMINCA register.
  38. *
  39. * PeriodWidth: Number of bits to store the clock period
  40. * MaxWidth: The maximum width value of the TIMINCA register
  41. * Period: The clock period for the oscillator
  42. * round(): discard the fractional portion of the calculation
  43. *
  44. * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
  45. *
  46. * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
  47. * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
  48. *
  49. * The period also changes based on the link speed:
  50. * At 10Gb link or no link, the period remains the same.
  51. * At 1Gb link, the period is multiplied by 10. (64ns)
  52. * At 100Mb link, the period is multiplied by 100. (640ns)
  53. *
  54. * The calculated value allows us to right shift the SYSTIME register
  55. * value in order to quickly convert it into a nanosecond clock,
  56. * while allowing for the maximum possible adjustment value.
  57. *
  58. * These diagrams are only for the 10Gb link period
  59. *
  60. * SYSTIMEH SYSTIMEL
  61. * +--------------+ +--------------+
  62. * X540 | 32 | | 1 | 3 | 28 |
  63. * *--------------+ +--------------+
  64. * \________ 36 bits ______/ fract
  65. *
  66. * +--------------+ +--------------+
  67. * 82599 | 32 | | 8 | 3 | 21 |
  68. * *--------------+ +--------------+
  69. * \________ 43 bits ______/ fract
  70. *
  71. * The 36 bit X540 SYSTIME overflows every
  72. * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
  73. *
  74. * The 43 bit 82599 SYSTIME overflows every
  75. * 2^43 * 10^-9 / 3600 = 2.4 hours
  76. */
  77. #define IXGBE_INCVAL_10GB 0x66666666
  78. #define IXGBE_INCVAL_1GB 0x40000000
  79. #define IXGBE_INCVAL_100 0x50000000
  80. #define IXGBE_INCVAL_SHIFT_10GB 28
  81. #define IXGBE_INCVAL_SHIFT_1GB 24
  82. #define IXGBE_INCVAL_SHIFT_100 21
  83. #define IXGBE_INCVAL_SHIFT_82599 7
  84. #define IXGBE_INCPER_SHIFT_82599 24
  85. #define IXGBE_MAX_TIMEADJ_VALUE 0x7FFFFFFFFFFFFFFFULL
  86. #define IXGBE_OVERFLOW_PERIOD (HZ * 30)
  87. #ifndef NSECS_PER_SEC
  88. #define NSECS_PER_SEC 1000000000ULL
  89. #endif
  90. /**
  91. * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
  92. * @cc - the cyclecounter structure
  93. *
  94. * this function reads the cyclecounter registers and is called by the
  95. * cyclecounter structure used to construct a ns counter from the
  96. * arbitrary fixed point registers
  97. */
  98. static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
  99. {
  100. struct ixgbe_adapter *adapter =
  101. container_of(cc, struct ixgbe_adapter, cc);
  102. struct ixgbe_hw *hw = &adapter->hw;
  103. u64 stamp = 0;
  104. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  105. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  106. return stamp;
  107. }
  108. /**
  109. * ixgbe_ptp_adjfreq
  110. * @ptp - the ptp clock structure
  111. * @ppb - parts per billion adjustment from base
  112. *
  113. * adjust the frequency of the ptp cycle counter by the
  114. * indicated ppb from the base frequency.
  115. */
  116. static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  117. {
  118. struct ixgbe_adapter *adapter =
  119. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  120. struct ixgbe_hw *hw = &adapter->hw;
  121. u64 freq;
  122. u32 diff, incval;
  123. int neg_adj = 0;
  124. if (ppb < 0) {
  125. neg_adj = 1;
  126. ppb = -ppb;
  127. }
  128. smp_mb();
  129. incval = ACCESS_ONCE(adapter->base_incval);
  130. freq = incval;
  131. freq *= ppb;
  132. diff = div_u64(freq, 1000000000ULL);
  133. incval = neg_adj ? (incval - diff) : (incval + diff);
  134. switch (hw->mac.type) {
  135. case ixgbe_mac_X540:
  136. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
  137. break;
  138. case ixgbe_mac_82599EB:
  139. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  140. (1 << IXGBE_INCPER_SHIFT_82599) |
  141. incval);
  142. break;
  143. default:
  144. break;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * ixgbe_ptp_adjtime
  150. * @ptp - the ptp clock structure
  151. * @delta - offset to adjust the cycle counter by
  152. *
  153. * adjust the timer by resetting the timecounter structure.
  154. */
  155. static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  156. {
  157. struct ixgbe_adapter *adapter =
  158. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  159. unsigned long flags;
  160. u64 now;
  161. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  162. now = timecounter_read(&adapter->tc);
  163. now += delta;
  164. /* reset the timecounter */
  165. timecounter_init(&adapter->tc,
  166. &adapter->cc,
  167. now);
  168. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  169. return 0;
  170. }
  171. /**
  172. * ixgbe_ptp_gettime
  173. * @ptp - the ptp clock structure
  174. * @ts - timespec structure to hold the current time value
  175. *
  176. * read the timecounter and return the correct value on ns,
  177. * after converting it into a struct timespec.
  178. */
  179. static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  180. {
  181. struct ixgbe_adapter *adapter =
  182. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  183. u64 ns;
  184. u32 remainder;
  185. unsigned long flags;
  186. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  187. ns = timecounter_read(&adapter->tc);
  188. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  189. ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
  190. ts->tv_nsec = remainder;
  191. return 0;
  192. }
  193. /**
  194. * ixgbe_ptp_settime
  195. * @ptp - the ptp clock structure
  196. * @ts - the timespec containing the new time for the cycle counter
  197. *
  198. * reset the timecounter to use a new base value instead of the kernel
  199. * wall timer value.
  200. */
  201. static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
  202. const struct timespec *ts)
  203. {
  204. struct ixgbe_adapter *adapter =
  205. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  206. u64 ns;
  207. unsigned long flags;
  208. ns = ts->tv_sec * 1000000000ULL;
  209. ns += ts->tv_nsec;
  210. /* reset the timecounter */
  211. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  212. timecounter_init(&adapter->tc, &adapter->cc, ns);
  213. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  214. return 0;
  215. }
  216. /**
  217. * ixgbe_ptp_enable
  218. * @ptp - the ptp clock structure
  219. * @rq - the requested feature to change
  220. * @on - whether to enable or disable the feature
  221. *
  222. * enable (or disable) ancillary features of the phc subsystem.
  223. * our driver only supports the PPS feature on the X540
  224. */
  225. static int ixgbe_ptp_enable(struct ptp_clock_info *ptp,
  226. struct ptp_clock_request *rq, int on)
  227. {
  228. struct ixgbe_adapter *adapter =
  229. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  230. /**
  231. * When PPS is enabled, unmask the interrupt for the ClockOut
  232. * feature, so that the interrupt handler can send the PPS
  233. * event when the clock SDP triggers. Clear mask when PPS is
  234. * disabled
  235. */
  236. if (rq->type == PTP_CLK_REQ_PPS) {
  237. switch (adapter->hw.mac.type) {
  238. case ixgbe_mac_X540:
  239. if (on)
  240. adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
  241. else
  242. adapter->flags2 &=
  243. ~IXGBE_FLAG2_PTP_PPS_ENABLED;
  244. return 0;
  245. default:
  246. break;
  247. }
  248. }
  249. return -ENOTSUPP;
  250. }
  251. /**
  252. * ixgbe_ptp_check_pps_event
  253. * @adapter - the private adapter structure
  254. * @eicr - the interrupt cause register value
  255. *
  256. * This function is called by the interrupt routine when checking for
  257. * interrupts. It will check and handle a pps event.
  258. */
  259. void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter, u32 eicr)
  260. {
  261. struct ixgbe_hw *hw = &adapter->hw;
  262. struct ptp_clock_event event;
  263. event.type = PTP_CLOCK_PPS;
  264. /* Make sure ptp clock is valid, and PPS event enabled */
  265. if (!adapter->ptp_clock ||
  266. !(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
  267. return;
  268. switch (hw->mac.type) {
  269. case ixgbe_mac_X540:
  270. if (eicr & IXGBE_EICR_TIMESYNC)
  271. ptp_clock_event(adapter->ptp_clock, &event);
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. /**
  278. * ixgbe_ptp_enable_sdp
  279. * @hw - the hardware private structure
  280. * @shift - the clock shift for calculating nanoseconds
  281. *
  282. * this function enables the clock out feature on the sdp0 for the
  283. * X540 device. It will create a 1second periodic output that can be
  284. * used as the PPS (via an interrupt).
  285. *
  286. * It calculates when the systime will be on an exact second, and then
  287. * aligns the start of the PPS signal to that value. The shift is
  288. * necessary because it can change based on the link speed.
  289. */
  290. static void ixgbe_ptp_enable_sdp(struct ixgbe_hw *hw, int shift)
  291. {
  292. u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh;
  293. u64 clock_edge = 0;
  294. u32 rem;
  295. switch (hw->mac.type) {
  296. case ixgbe_mac_X540:
  297. esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
  298. /*
  299. * enable the SDP0 pin as output, and connected to the native
  300. * function for Timesync (ClockOut)
  301. */
  302. esdp |= (IXGBE_ESDP_SDP0_DIR |
  303. IXGBE_ESDP_SDP0_NATIVE);
  304. /*
  305. * enable the Clock Out feature on SDP0, and allow interrupts
  306. * to occur when the pin changes
  307. */
  308. tsauxc = (IXGBE_TSAUXC_EN_CLK |
  309. IXGBE_TSAUXC_SYNCLK |
  310. IXGBE_TSAUXC_SDP0_INT);
  311. /* clock period (or pulse length) */
  312. clktiml = (u32)(NSECS_PER_SEC << shift);
  313. clktimh = (u32)((NSECS_PER_SEC << shift) >> 32);
  314. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  315. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  316. /*
  317. * account for the fact that we can't do u64 division
  318. * with remainder, by converting the clock values into
  319. * nanoseconds first
  320. */
  321. clock_edge >>= shift;
  322. div_u64_rem(clock_edge, NSECS_PER_SEC, &rem);
  323. clock_edge += (NSECS_PER_SEC - rem);
  324. clock_edge <<= shift;
  325. /* specify the initial clock start time */
  326. trgttiml = (u32)clock_edge;
  327. trgttimh = (u32)(clock_edge >> 32);
  328. IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
  329. IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
  330. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
  331. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
  332. IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
  333. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
  334. IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_TIMESYNC);
  335. break;
  336. default:
  337. break;
  338. }
  339. }
  340. /**
  341. * ixgbe_ptp_disable_sdp
  342. * @hw - the private hardware structure
  343. *
  344. * this function disables the auxiliary SDP clock out feature
  345. */
  346. static void ixgbe_ptp_disable_sdp(struct ixgbe_hw *hw)
  347. {
  348. IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_TIMESYNC);
  349. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0);
  350. }
  351. /**
  352. * ixgbe_ptp_overflow_check - delayed work to detect SYSTIME overflow
  353. * @work: structure containing information about this work task
  354. *
  355. * this work function is scheduled to continue reading the timecounter
  356. * in order to prevent missing when the system time registers wrap
  357. * around. This needs to be run approximately twice a minute when no
  358. * PTP activity is occurring.
  359. */
  360. void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
  361. {
  362. unsigned long elapsed_jiffies = adapter->last_overflow_check - jiffies;
  363. struct timespec ts;
  364. if ((adapter->flags2 & IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED) &&
  365. (elapsed_jiffies >= IXGBE_OVERFLOW_PERIOD)) {
  366. ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
  367. adapter->last_overflow_check = jiffies;
  368. }
  369. }
  370. /**
  371. * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  372. * @q_vector: structure containing interrupt and ring information
  373. * @skb: particular skb to send timestamp with
  374. *
  375. * if the timestamp is valid, we convert it into the timecounter ns
  376. * value, then store that result into the shhwtstamps structure which
  377. * is passed up the network stack
  378. */
  379. void ixgbe_ptp_tx_hwtstamp(struct ixgbe_q_vector *q_vector,
  380. struct sk_buff *skb)
  381. {
  382. struct ixgbe_adapter *adapter;
  383. struct ixgbe_hw *hw;
  384. struct skb_shared_hwtstamps shhwtstamps;
  385. u64 regval = 0, ns;
  386. u32 tsynctxctl;
  387. unsigned long flags;
  388. /* we cannot process timestamps on a ring without a q_vector */
  389. if (!q_vector || !q_vector->adapter)
  390. return;
  391. adapter = q_vector->adapter;
  392. hw = &adapter->hw;
  393. tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  394. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
  395. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
  396. /*
  397. * if TX timestamp is not valid, exit after clearing the
  398. * timestamp registers
  399. */
  400. if (!(tsynctxctl & IXGBE_TSYNCTXCTL_VALID))
  401. return;
  402. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  403. ns = timecounter_cyc2time(&adapter->tc, regval);
  404. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  405. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  406. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  407. skb_tstamp_tx(skb, &shhwtstamps);
  408. }
  409. /**
  410. * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
  411. * @q_vector: structure containing interrupt and ring information
  412. * @skb: particular skb to send timestamp with
  413. *
  414. * if the timestamp is valid, we convert it into the timecounter ns
  415. * value, then store that result into the shhwtstamps structure which
  416. * is passed up the network stack
  417. */
  418. void ixgbe_ptp_rx_hwtstamp(struct ixgbe_q_vector *q_vector,
  419. struct sk_buff *skb)
  420. {
  421. struct ixgbe_adapter *adapter;
  422. struct ixgbe_hw *hw;
  423. struct skb_shared_hwtstamps *shhwtstamps;
  424. u64 regval = 0, ns;
  425. u32 tsyncrxctl;
  426. unsigned long flags;
  427. /* we cannot process timestamps on a ring without a q_vector */
  428. if (!q_vector || !q_vector->adapter)
  429. return;
  430. adapter = q_vector->adapter;
  431. hw = &adapter->hw;
  432. tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  433. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
  434. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
  435. /*
  436. * If this bit is set, then the RX registers contain the time stamp. No
  437. * other packet will be time stamped until we read these registers, so
  438. * read the registers to make them available again. Because only one
  439. * packet can be time stamped at a time, we know that the register
  440. * values must belong to this one here and therefore we don't need to
  441. * compare any of the additional attributes stored for it.
  442. *
  443. * If nothing went wrong, then it should have a skb_shared_tx that we
  444. * can turn into a skb_shared_hwtstamps.
  445. */
  446. if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
  447. return;
  448. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  449. ns = timecounter_cyc2time(&adapter->tc, regval);
  450. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  451. shhwtstamps = skb_hwtstamps(skb);
  452. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  453. }
  454. /**
  455. * ixgbe_ptp_hwtstamp_ioctl - control hardware time stamping
  456. * @adapter: pointer to adapter struct
  457. * @ifreq: ioctl data
  458. * @cmd: particular ioctl requested
  459. *
  460. * Outgoing time stamping can be enabled and disabled. Play nice and
  461. * disable it when requested, although it shouldn't case any overhead
  462. * when no packet needs it. At most one packet in the queue may be
  463. * marked for time stamping, otherwise it would be impossible to tell
  464. * for sure to which packet the hardware time stamp belongs.
  465. *
  466. * Incoming time stamping has to be configured via the hardware
  467. * filters. Not all combinations are supported, in particular event
  468. * type has to be specified. Matching the kind of event packet is
  469. * not supported, with the exception of "all V2 events regardless of
  470. * level 2 or 4".
  471. */
  472. int ixgbe_ptp_hwtstamp_ioctl(struct ixgbe_adapter *adapter,
  473. struct ifreq *ifr, int cmd)
  474. {
  475. struct ixgbe_hw *hw = &adapter->hw;
  476. struct hwtstamp_config config;
  477. u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
  478. u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
  479. u32 tsync_rx_mtrl = 0;
  480. bool is_l4 = false;
  481. bool is_l2 = false;
  482. u32 regval;
  483. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  484. return -EFAULT;
  485. /* reserved for future extensions */
  486. if (config.flags)
  487. return -EINVAL;
  488. switch (config.tx_type) {
  489. case HWTSTAMP_TX_OFF:
  490. tsync_tx_ctl = 0;
  491. case HWTSTAMP_TX_ON:
  492. break;
  493. default:
  494. return -ERANGE;
  495. }
  496. switch (config.rx_filter) {
  497. case HWTSTAMP_FILTER_NONE:
  498. tsync_rx_ctl = 0;
  499. break;
  500. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  501. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  502. tsync_rx_mtrl = IXGBE_RXMTRL_V1_SYNC_MSG;
  503. is_l4 = true;
  504. break;
  505. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  506. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  507. tsync_rx_mtrl = IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
  508. is_l4 = true;
  509. break;
  510. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  511. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  512. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  513. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L2_L4_V2;
  514. tsync_rx_mtrl = IXGBE_RXMTRL_V2_SYNC_MSG;
  515. is_l2 = true;
  516. is_l4 = true;
  517. config.rx_filter = HWTSTAMP_FILTER_SOME;
  518. break;
  519. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  520. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  521. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  522. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L2_L4_V2;
  523. tsync_rx_mtrl = IXGBE_RXMTRL_V2_DELAY_REQ_MSG;
  524. is_l2 = true;
  525. is_l4 = true;
  526. config.rx_filter = HWTSTAMP_FILTER_SOME;
  527. break;
  528. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  529. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  530. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  531. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
  532. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  533. is_l2 = true;
  534. is_l4 = true;
  535. break;
  536. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  537. case HWTSTAMP_FILTER_ALL:
  538. default:
  539. /*
  540. * register RXMTRL must be set, therefore it is not
  541. * possible to time stamp both V1 Sync and Delay_Req messages
  542. * and hardware does not support timestamping all packets
  543. * => return error
  544. */
  545. return -ERANGE;
  546. }
  547. if (hw->mac.type == ixgbe_mac_82598EB) {
  548. if (tsync_rx_ctl | tsync_tx_ctl)
  549. return -ERANGE;
  550. return 0;
  551. }
  552. /* define ethertype filter for timestamped packets */
  553. if (is_l2)
  554. IXGBE_WRITE_REG(hw, IXGBE_ETQF(3),
  555. (IXGBE_ETQF_FILTER_EN | /* enable filter */
  556. IXGBE_ETQF_1588 | /* enable timestamping */
  557. ETH_P_1588)); /* 1588 eth protocol type */
  558. else
  559. IXGBE_WRITE_REG(hw, IXGBE_ETQF(3), 0);
  560. #define PTP_PORT 319
  561. /* L4 Queue Filter[3]: filter by destination port and protocol */
  562. if (is_l4) {
  563. u32 ftqf = (IXGBE_FTQF_PROTOCOL_UDP /* UDP */
  564. | IXGBE_FTQF_POOL_MASK_EN /* Pool not compared */
  565. | IXGBE_FTQF_QUEUE_ENABLE);
  566. ftqf |= ((IXGBE_FTQF_PROTOCOL_COMP_MASK /* protocol check */
  567. & IXGBE_FTQF_DEST_PORT_MASK /* dest check */
  568. & IXGBE_FTQF_SOURCE_PORT_MASK) /* source check */
  569. << IXGBE_FTQF_5TUPLE_MASK_SHIFT);
  570. IXGBE_WRITE_REG(hw, IXGBE_L34T_IMIR(3),
  571. (3 << IXGBE_IMIR_RX_QUEUE_SHIFT_82599 |
  572. IXGBE_IMIR_SIZE_BP_82599));
  573. /* enable port check */
  574. IXGBE_WRITE_REG(hw, IXGBE_SDPQF(3),
  575. (htons(PTP_PORT) |
  576. htons(PTP_PORT) << 16));
  577. IXGBE_WRITE_REG(hw, IXGBE_FTQF(3), ftqf);
  578. tsync_rx_mtrl |= PTP_PORT << 16;
  579. } else {
  580. IXGBE_WRITE_REG(hw, IXGBE_FTQF(3), 0);
  581. }
  582. /* enable/disable TX */
  583. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  584. regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
  585. regval |= tsync_tx_ctl;
  586. IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
  587. /* enable/disable RX */
  588. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  589. regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
  590. regval |= tsync_rx_ctl;
  591. IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
  592. /* define which PTP packets are time stamped */
  593. IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
  594. IXGBE_WRITE_FLUSH(hw);
  595. /* clear TX/RX time stamp registers, just to be sure */
  596. regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
  597. regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
  598. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  599. -EFAULT : 0;
  600. }
  601. /**
  602. * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
  603. * @adapter - pointer to the adapter structure
  604. *
  605. * this function initializes the timecounter and cyclecounter
  606. * structures for use in generated a ns counter from the arbitrary
  607. * fixed point cycles registers in the hardware.
  608. *
  609. * A change in link speed impacts the frequency of the DMA clock on
  610. * the device, which is used to generate the cycle counter
  611. * registers. Therefor this function is called whenever the link speed
  612. * changes.
  613. *
  614. * This function also turns on the SDP pin for clock out feature (X540
  615. * only), because this is where the shift is first calculated.
  616. */
  617. void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
  618. {
  619. struct ixgbe_hw *hw = &adapter->hw;
  620. u32 incval = 0;
  621. u32 shift = 0;
  622. u32 cycle_speed;
  623. unsigned long flags;
  624. /**
  625. * Determine what speed we need to set the cyclecounter
  626. * for. It should be different for 100Mb, 1Gb, and 10Gb. Treat
  627. * unknown speeds as 10Gb. (Hence why we can't just copy the
  628. * link_speed.
  629. */
  630. switch (adapter->link_speed) {
  631. case IXGBE_LINK_SPEED_100_FULL:
  632. case IXGBE_LINK_SPEED_1GB_FULL:
  633. case IXGBE_LINK_SPEED_10GB_FULL:
  634. cycle_speed = adapter->link_speed;
  635. break;
  636. default:
  637. /* cycle speed should be 10Gb when there is no link */
  638. cycle_speed = IXGBE_LINK_SPEED_10GB_FULL;
  639. break;
  640. }
  641. /* Bail if the cycle speed didn't change */
  642. if (adapter->cycle_speed == cycle_speed)
  643. return;
  644. /* disable the SDP clock out */
  645. ixgbe_ptp_disable_sdp(hw);
  646. /**
  647. * Scale the NIC cycle counter by a large factor so that
  648. * relatively small corrections to the frequency can be added
  649. * or subtracted. The drawbacks of a large factor include
  650. * (a) the clock register overflows more quickly, (b) the cycle
  651. * counter structure must be able to convert the systime value
  652. * to nanoseconds using only a multiplier and a right-shift,
  653. * and (c) the value must fit within the timinca register space
  654. * => math based on internal DMA clock rate and available bits
  655. */
  656. switch (cycle_speed) {
  657. case IXGBE_LINK_SPEED_100_FULL:
  658. incval = IXGBE_INCVAL_100;
  659. shift = IXGBE_INCVAL_SHIFT_100;
  660. break;
  661. case IXGBE_LINK_SPEED_1GB_FULL:
  662. incval = IXGBE_INCVAL_1GB;
  663. shift = IXGBE_INCVAL_SHIFT_1GB;
  664. break;
  665. case IXGBE_LINK_SPEED_10GB_FULL:
  666. incval = IXGBE_INCVAL_10GB;
  667. shift = IXGBE_INCVAL_SHIFT_10GB;
  668. break;
  669. }
  670. /**
  671. * Modify the calculated values to fit within the correct
  672. * number of bits specified by the hardware. The 82599 doesn't
  673. * have the same space as the X540, so bitshift the calculated
  674. * values to fit.
  675. */
  676. switch (hw->mac.type) {
  677. case ixgbe_mac_X540:
  678. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
  679. break;
  680. case ixgbe_mac_82599EB:
  681. incval >>= IXGBE_INCVAL_SHIFT_82599;
  682. shift -= IXGBE_INCVAL_SHIFT_82599;
  683. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  684. (1 << IXGBE_INCPER_SHIFT_82599) |
  685. incval);
  686. break;
  687. default:
  688. /* other devices aren't supported */
  689. return;
  690. }
  691. /* reset the system time registers */
  692. IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
  693. IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
  694. IXGBE_WRITE_FLUSH(hw);
  695. /* now that the shift has been calculated and the systime
  696. * registers reset, (re-)enable the Clock out feature*/
  697. ixgbe_ptp_enable_sdp(hw, shift);
  698. /* store the new cycle speed */
  699. adapter->cycle_speed = cycle_speed;
  700. ACCESS_ONCE(adapter->base_incval) = incval;
  701. smp_mb();
  702. /* grab the ptp lock */
  703. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  704. memset(&adapter->cc, 0, sizeof(adapter->cc));
  705. adapter->cc.read = ixgbe_ptp_read;
  706. adapter->cc.mask = CLOCKSOURCE_MASK(64);
  707. adapter->cc.shift = shift;
  708. adapter->cc.mult = 1;
  709. /* reset the ns time counter */
  710. timecounter_init(&adapter->tc, &adapter->cc,
  711. ktime_to_ns(ktime_get_real()));
  712. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  713. }
  714. /**
  715. * ixgbe_ptp_init
  716. * @adapter - the ixgbe private adapter structure
  717. *
  718. * This function performs the required steps for enabling ptp
  719. * support. If ptp support has already been loaded it simply calls the
  720. * cyclecounter init routine and exits.
  721. */
  722. void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
  723. {
  724. struct net_device *netdev = adapter->netdev;
  725. switch (adapter->hw.mac.type) {
  726. case ixgbe_mac_X540:
  727. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  728. adapter->ptp_caps.owner = THIS_MODULE;
  729. adapter->ptp_caps.max_adj = 250000000;
  730. adapter->ptp_caps.n_alarm = 0;
  731. adapter->ptp_caps.n_ext_ts = 0;
  732. adapter->ptp_caps.n_per_out = 0;
  733. adapter->ptp_caps.pps = 1;
  734. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
  735. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  736. adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
  737. adapter->ptp_caps.settime = ixgbe_ptp_settime;
  738. adapter->ptp_caps.enable = ixgbe_ptp_enable;
  739. break;
  740. case ixgbe_mac_82599EB:
  741. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  742. adapter->ptp_caps.owner = THIS_MODULE;
  743. adapter->ptp_caps.max_adj = 250000000;
  744. adapter->ptp_caps.n_alarm = 0;
  745. adapter->ptp_caps.n_ext_ts = 0;
  746. adapter->ptp_caps.n_per_out = 0;
  747. adapter->ptp_caps.pps = 0;
  748. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
  749. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  750. adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
  751. adapter->ptp_caps.settime = ixgbe_ptp_settime;
  752. adapter->ptp_caps.enable = ixgbe_ptp_enable;
  753. break;
  754. default:
  755. adapter->ptp_clock = NULL;
  756. return;
  757. }
  758. spin_lock_init(&adapter->tmreg_lock);
  759. ixgbe_ptp_start_cyclecounter(adapter);
  760. /* (Re)start the overflow check */
  761. adapter->flags2 |= IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED;
  762. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps);
  763. if (IS_ERR(adapter->ptp_clock)) {
  764. adapter->ptp_clock = NULL;
  765. e_dev_err("ptp_clock_register failed\n");
  766. } else
  767. e_dev_info("registered PHC device on %s\n", netdev->name);
  768. return;
  769. }
  770. /**
  771. * ixgbe_ptp_stop - disable ptp device and stop the overflow check
  772. * @adapter: pointer to adapter struct
  773. *
  774. * this function stops the ptp support, and cancels the delayed work.
  775. */
  776. void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
  777. {
  778. ixgbe_ptp_disable_sdp(&adapter->hw);
  779. /* stop the overflow check task */
  780. adapter->flags2 &= ~IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED;
  781. if (adapter->ptp_clock) {
  782. ptp_clock_unregister(adapter->ptp_clock);
  783. adapter->ptp_clock = NULL;
  784. e_dev_info("removed PHC on %s\n",
  785. adapter->netdev->name);
  786. }
  787. }