ixgbe_ptp.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "ixgbe.h"
  4. #include <linux/ptp_classify.h>
  5. #include <linux/clocksource.h>
  6. /*
  7. * The 82599 and the X540 do not have true 64bit nanosecond scale
  8. * counter registers. Instead, SYSTIME is defined by a fixed point
  9. * system which allows the user to define the scale counter increment
  10. * value at every level change of the oscillator driving the SYSTIME
  11. * value. For both devices the TIMINCA:IV field defines this
  12. * increment. On the X540 device, 31 bits are provided. However on the
  13. * 82599 only provides 24 bits. The time unit is determined by the
  14. * clock frequency of the oscillator in combination with the TIMINCA
  15. * register. When these devices link at 10Gb the oscillator has a
  16. * period of 6.4ns. In order to convert the scale counter into
  17. * nanoseconds the cyclecounter and timecounter structures are
  18. * used. The SYSTIME registers need to be converted to ns values by use
  19. * of only a right shift (division by power of 2). The following math
  20. * determines the largest incvalue that will fit into the available
  21. * bits in the TIMINCA register.
  22. *
  23. * PeriodWidth: Number of bits to store the clock period
  24. * MaxWidth: The maximum width value of the TIMINCA register
  25. * Period: The clock period for the oscillator
  26. * round(): discard the fractional portion of the calculation
  27. *
  28. * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
  29. *
  30. * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
  31. * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
  32. *
  33. * The period also changes based on the link speed:
  34. * At 10Gb link or no link, the period remains the same.
  35. * At 1Gb link, the period is multiplied by 10. (64ns)
  36. * At 100Mb link, the period is multiplied by 100. (640ns)
  37. *
  38. * The calculated value allows us to right shift the SYSTIME register
  39. * value in order to quickly convert it into a nanosecond clock,
  40. * while allowing for the maximum possible adjustment value.
  41. *
  42. * These diagrams are only for the 10Gb link period
  43. *
  44. * SYSTIMEH SYSTIMEL
  45. * +--------------+ +--------------+
  46. * X540 | 32 | | 1 | 3 | 28 |
  47. * *--------------+ +--------------+
  48. * \________ 36 bits ______/ fract
  49. *
  50. * +--------------+ +--------------+
  51. * 82599 | 32 | | 8 | 3 | 21 |
  52. * *--------------+ +--------------+
  53. * \________ 43 bits ______/ fract
  54. *
  55. * The 36 bit X540 SYSTIME overflows every
  56. * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
  57. *
  58. * The 43 bit 82599 SYSTIME overflows every
  59. * 2^43 * 10^-9 / 3600 = 2.4 hours
  60. */
  61. #define IXGBE_INCVAL_10GB 0x66666666
  62. #define IXGBE_INCVAL_1GB 0x40000000
  63. #define IXGBE_INCVAL_100 0x50000000
  64. #define IXGBE_INCVAL_SHIFT_10GB 28
  65. #define IXGBE_INCVAL_SHIFT_1GB 24
  66. #define IXGBE_INCVAL_SHIFT_100 21
  67. #define IXGBE_INCVAL_SHIFT_82599 7
  68. #define IXGBE_INCPER_SHIFT_82599 24
  69. #define IXGBE_OVERFLOW_PERIOD (HZ * 30)
  70. #define IXGBE_PTP_TX_TIMEOUT (HZ * 15)
  71. /* half of a one second clock period, for use with PPS signal. We have to use
  72. * this instead of something pre-defined like IXGBE_PTP_PPS_HALF_SECOND, in
  73. * order to force at least 64bits of precision for shifting
  74. */
  75. #define IXGBE_PTP_PPS_HALF_SECOND 500000000ULL
  76. /* In contrast, the X550 controller has two registers, SYSTIMEH and SYSTIMEL
  77. * which contain measurements of seconds and nanoseconds respectively. This
  78. * matches the standard linux representation of time in the kernel. In addition,
  79. * the X550 also has a SYSTIMER register which represents residue, or
  80. * subnanosecond overflow adjustments. To control clock adjustment, the TIMINCA
  81. * register is used, but it is unlike the X540 and 82599 devices. TIMINCA
  82. * represents units of 2^-32 nanoseconds, and uses 31 bits for this, with the
  83. * high bit representing whether the adjustent is positive or negative. Every
  84. * clock cycle, the X550 will add 12.5 ns + TIMINCA which can result in a range
  85. * of 12 to 13 nanoseconds adjustment. Unlike the 82599 and X540 devices, the
  86. * X550's clock for purposes of SYSTIME generation is constant and not dependent
  87. * on the link speed.
  88. *
  89. * SYSTIMEH SYSTIMEL SYSTIMER
  90. * +--------------+ +--------------+ +-------------+
  91. * X550 | 32 | | 32 | | 32 |
  92. * *--------------+ +--------------+ +-------------+
  93. * \____seconds___/ \_nanoseconds_/ \__2^-32 ns__/
  94. *
  95. * This results in a full 96 bits to represent the clock, with 32 bits for
  96. * seconds, 32 bits for nanoseconds (largest value is 0d999999999 or just under
  97. * 1 second) and an additional 32 bits to measure sub nanosecond adjustments for
  98. * underflow of adjustments.
  99. *
  100. * The 32 bits of seconds for the X550 overflows every
  101. * 2^32 / ( 365.25 * 24 * 60 * 60 ) = ~136 years.
  102. *
  103. * In order to adjust the clock frequency for the X550, the TIMINCA register is
  104. * provided. This register represents a + or minus nearly 0.5 ns adjustment to
  105. * the base frequency. It is measured in 2^-32 ns units, with the high bit being
  106. * the sign bit. This register enables software to calculate frequency
  107. * adjustments and apply them directly to the clock rate.
  108. *
  109. * The math for converting ppb into TIMINCA values is fairly straightforward.
  110. * TIMINCA value = ( Base_Frequency * ppb ) / 1000000000ULL
  111. *
  112. * This assumes that ppb is never high enough to create a value bigger than
  113. * TIMINCA's 31 bits can store. This is ensured by the stack. Calculating this
  114. * value is also simple.
  115. * Max ppb = ( Max Adjustment / Base Frequency ) / 1000000000ULL
  116. *
  117. * For the X550, the Max adjustment is +/- 0.5 ns, and the base frequency is
  118. * 12.5 nanoseconds. This means that the Max ppb is 39999999
  119. * Note: We subtract one in order to ensure no overflow, because the TIMINCA
  120. * register can only hold slightly under 0.5 nanoseconds.
  121. *
  122. * Because TIMINCA is measured in 2^-32 ns units, we have to convert 12.5 ns
  123. * into 2^-32 units, which is
  124. *
  125. * 12.5 * 2^32 = C80000000
  126. *
  127. * Some revisions of hardware have a faster base frequency than the registers
  128. * were defined for. To fix this, we use a timecounter structure with the
  129. * proper mult and shift to convert the cycles into nanoseconds of time.
  130. */
  131. #define IXGBE_X550_BASE_PERIOD 0xC80000000ULL
  132. #define INCVALUE_MASK 0x7FFFFFFF
  133. #define ISGN 0x80000000
  134. #define MAX_TIMADJ 0x7FFFFFFF
  135. /**
  136. * ixgbe_ptp_setup_sdp_x540
  137. * @adapter: private adapter structure
  138. *
  139. * this function enables or disables the clock out feature on SDP0 for
  140. * the X540 device. It will create a 1second periodic output that can
  141. * be used as the PPS (via an interrupt).
  142. *
  143. * It calculates when the systime will be on an exact second, and then
  144. * aligns the start of the PPS signal to that value. The shift is
  145. * necessary because it can change based on the link speed.
  146. */
  147. static void ixgbe_ptp_setup_sdp_x540(struct ixgbe_adapter *adapter)
  148. {
  149. struct ixgbe_hw *hw = &adapter->hw;
  150. int shift = adapter->hw_cc.shift;
  151. u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
  152. u64 ns = 0, clock_edge = 0;
  153. /* disable the pin first */
  154. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
  155. IXGBE_WRITE_FLUSH(hw);
  156. if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
  157. return;
  158. esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
  159. /* enable the SDP0 pin as output, and connected to the
  160. * native function for Timesync (ClockOut)
  161. */
  162. esdp |= IXGBE_ESDP_SDP0_DIR |
  163. IXGBE_ESDP_SDP0_NATIVE;
  164. /* enable the Clock Out feature on SDP0, and allow
  165. * interrupts to occur when the pin changes
  166. */
  167. tsauxc = IXGBE_TSAUXC_EN_CLK |
  168. IXGBE_TSAUXC_SYNCLK |
  169. IXGBE_TSAUXC_SDP0_INT;
  170. /* clock period (or pulse length) */
  171. clktiml = (u32)(IXGBE_PTP_PPS_HALF_SECOND << shift);
  172. clktimh = (u32)((IXGBE_PTP_PPS_HALF_SECOND << shift) >> 32);
  173. /* Account for the cyclecounter wrap-around value by
  174. * using the converted ns value of the current time to
  175. * check for when the next aligned second would occur.
  176. */
  177. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  178. clock_edge |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  179. ns = timecounter_cyc2time(&adapter->hw_tc, clock_edge);
  180. div_u64_rem(ns, IXGBE_PTP_PPS_HALF_SECOND, &rem);
  181. clock_edge += ((IXGBE_PTP_PPS_HALF_SECOND - (u64)rem) << shift);
  182. /* specify the initial clock start time */
  183. trgttiml = (u32)clock_edge;
  184. trgttimh = (u32)(clock_edge >> 32);
  185. IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
  186. IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
  187. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
  188. IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
  189. IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
  190. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
  191. IXGBE_WRITE_FLUSH(hw);
  192. }
  193. /**
  194. * ixgbe_ptp_read_X550 - read cycle counter value
  195. * @hw_cc: cyclecounter structure
  196. *
  197. * This function reads SYSTIME registers. It is called by the cyclecounter
  198. * structure to convert from internal representation into nanoseconds. We need
  199. * this for X550 since some skews do not have expected clock frequency and
  200. * result of SYSTIME is 32bits of "billions of cycles" and 32 bits of
  201. * "cycles", rather than seconds and nanoseconds.
  202. */
  203. static u64 ixgbe_ptp_read_X550(const struct cyclecounter *hw_cc)
  204. {
  205. struct ixgbe_adapter *adapter =
  206. container_of(hw_cc, struct ixgbe_adapter, hw_cc);
  207. struct ixgbe_hw *hw = &adapter->hw;
  208. struct timespec64 ts;
  209. /* storage is 32 bits of 'billions of cycles' and 32 bits of 'cycles'.
  210. * Some revisions of hardware run at a higher frequency and so the
  211. * cycles are not guaranteed to be nanoseconds. The timespec64 created
  212. * here is used for its math/conversions but does not necessarily
  213. * represent nominal time.
  214. *
  215. * It should be noted that this cyclecounter will overflow at a
  216. * non-bitmask field since we have to convert our billions of cycles
  217. * into an actual cycles count. This results in some possible weird
  218. * situations at high cycle counter stamps. However given that 32 bits
  219. * of "seconds" is ~138 years this isn't a problem. Even at the
  220. * increased frequency of some revisions, this is still ~103 years.
  221. * Since the SYSTIME values start at 0 and we never write them, it is
  222. * highly unlikely for the cyclecounter to overflow in practice.
  223. */
  224. IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
  225. ts.tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  226. ts.tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
  227. return (u64)timespec64_to_ns(&ts);
  228. }
  229. /**
  230. * ixgbe_ptp_read_82599 - read raw cycle counter (to be used by time counter)
  231. * @cc: the cyclecounter structure
  232. *
  233. * this function reads the cyclecounter registers and is called by the
  234. * cyclecounter structure used to construct a ns counter from the
  235. * arbitrary fixed point registers
  236. */
  237. static u64 ixgbe_ptp_read_82599(const struct cyclecounter *cc)
  238. {
  239. struct ixgbe_adapter *adapter =
  240. container_of(cc, struct ixgbe_adapter, hw_cc);
  241. struct ixgbe_hw *hw = &adapter->hw;
  242. u64 stamp = 0;
  243. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
  244. stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
  245. return stamp;
  246. }
  247. /**
  248. * ixgbe_ptp_convert_to_hwtstamp - convert register value to hw timestamp
  249. * @adapter: private adapter structure
  250. * @hwtstamp: stack timestamp structure
  251. * @timestamp: unsigned 64bit system time value
  252. *
  253. * We need to convert the adapter's RX/TXSTMP registers into a hwtstamp value
  254. * which can be used by the stack's ptp functions.
  255. *
  256. * The lock is used to protect consistency of the cyclecounter and the SYSTIME
  257. * registers. However, it does not need to protect against the Rx or Tx
  258. * timestamp registers, as there can't be a new timestamp until the old one is
  259. * unlatched by reading.
  260. *
  261. * In addition to the timestamp in hardware, some controllers need a software
  262. * overflow cyclecounter, and this function takes this into account as well.
  263. **/
  264. static void ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter *adapter,
  265. struct skb_shared_hwtstamps *hwtstamp,
  266. u64 timestamp)
  267. {
  268. unsigned long flags;
  269. struct timespec64 systime;
  270. u64 ns;
  271. memset(hwtstamp, 0, sizeof(*hwtstamp));
  272. switch (adapter->hw.mac.type) {
  273. /* X550 and later hardware supposedly represent time using a seconds
  274. * and nanoseconds counter, instead of raw 64bits nanoseconds. We need
  275. * to convert the timestamp into cycles before it can be fed to the
  276. * cyclecounter. We need an actual cyclecounter because some revisions
  277. * of hardware run at a higher frequency and thus the counter does
  278. * not represent seconds/nanoseconds. Instead it can be thought of as
  279. * cycles and billions of cycles.
  280. */
  281. case ixgbe_mac_X550:
  282. case ixgbe_mac_X550EM_x:
  283. case ixgbe_mac_x550em_a:
  284. /* Upper 32 bits represent billions of cycles, lower 32 bits
  285. * represent cycles. However, we use timespec64_to_ns for the
  286. * correct math even though the units haven't been corrected
  287. * yet.
  288. */
  289. systime.tv_sec = timestamp >> 32;
  290. systime.tv_nsec = timestamp & 0xFFFFFFFF;
  291. timestamp = timespec64_to_ns(&systime);
  292. break;
  293. default:
  294. break;
  295. }
  296. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  297. ns = timecounter_cyc2time(&adapter->hw_tc, timestamp);
  298. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  299. hwtstamp->hwtstamp = ns_to_ktime(ns);
  300. }
  301. /**
  302. * ixgbe_ptp_adjfreq_82599
  303. * @ptp: the ptp clock structure
  304. * @ppb: parts per billion adjustment from base
  305. *
  306. * adjust the frequency of the ptp cycle counter by the
  307. * indicated ppb from the base frequency.
  308. */
  309. static int ixgbe_ptp_adjfreq_82599(struct ptp_clock_info *ptp, s32 ppb)
  310. {
  311. struct ixgbe_adapter *adapter =
  312. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  313. struct ixgbe_hw *hw = &adapter->hw;
  314. u64 freq, incval;
  315. u32 diff;
  316. int neg_adj = 0;
  317. if (ppb < 0) {
  318. neg_adj = 1;
  319. ppb = -ppb;
  320. }
  321. smp_mb();
  322. incval = READ_ONCE(adapter->base_incval);
  323. freq = incval;
  324. freq *= ppb;
  325. diff = div_u64(freq, 1000000000ULL);
  326. incval = neg_adj ? (incval - diff) : (incval + diff);
  327. switch (hw->mac.type) {
  328. case ixgbe_mac_X540:
  329. if (incval > 0xFFFFFFFFULL)
  330. e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
  331. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, (u32)incval);
  332. break;
  333. case ixgbe_mac_82599EB:
  334. if (incval > 0x00FFFFFFULL)
  335. e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
  336. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  337. BIT(IXGBE_INCPER_SHIFT_82599) |
  338. ((u32)incval & 0x00FFFFFFUL));
  339. break;
  340. default:
  341. break;
  342. }
  343. return 0;
  344. }
  345. /**
  346. * ixgbe_ptp_adjfreq_X550
  347. * @ptp: the ptp clock structure
  348. * @ppb: parts per billion adjustment from base
  349. *
  350. * adjust the frequency of the SYSTIME registers by the indicated ppb from base
  351. * frequency
  352. */
  353. static int ixgbe_ptp_adjfreq_X550(struct ptp_clock_info *ptp, s32 ppb)
  354. {
  355. struct ixgbe_adapter *adapter =
  356. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  357. struct ixgbe_hw *hw = &adapter->hw;
  358. int neg_adj = 0;
  359. u64 rate = IXGBE_X550_BASE_PERIOD;
  360. u32 inca;
  361. if (ppb < 0) {
  362. neg_adj = 1;
  363. ppb = -ppb;
  364. }
  365. rate *= ppb;
  366. rate = div_u64(rate, 1000000000ULL);
  367. /* warn if rate is too large */
  368. if (rate >= INCVALUE_MASK)
  369. e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
  370. inca = rate & INCVALUE_MASK;
  371. if (neg_adj)
  372. inca |= ISGN;
  373. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, inca);
  374. return 0;
  375. }
  376. /**
  377. * ixgbe_ptp_adjtime
  378. * @ptp: the ptp clock structure
  379. * @delta: offset to adjust the cycle counter by
  380. *
  381. * adjust the timer by resetting the timecounter structure.
  382. */
  383. static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  384. {
  385. struct ixgbe_adapter *adapter =
  386. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  387. unsigned long flags;
  388. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  389. timecounter_adjtime(&adapter->hw_tc, delta);
  390. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  391. if (adapter->ptp_setup_sdp)
  392. adapter->ptp_setup_sdp(adapter);
  393. return 0;
  394. }
  395. /**
  396. * ixgbe_ptp_gettime
  397. * @ptp: the ptp clock structure
  398. * @ts: timespec structure to hold the current time value
  399. *
  400. * read the timecounter and return the correct value on ns,
  401. * after converting it into a struct timespec.
  402. */
  403. static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  404. {
  405. struct ixgbe_adapter *adapter =
  406. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  407. unsigned long flags;
  408. u64 ns;
  409. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  410. ns = timecounter_read(&adapter->hw_tc);
  411. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  412. *ts = ns_to_timespec64(ns);
  413. return 0;
  414. }
  415. /**
  416. * ixgbe_ptp_settime
  417. * @ptp: the ptp clock structure
  418. * @ts: the timespec containing the new time for the cycle counter
  419. *
  420. * reset the timecounter to use a new base value instead of the kernel
  421. * wall timer value.
  422. */
  423. static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
  424. const struct timespec64 *ts)
  425. {
  426. struct ixgbe_adapter *adapter =
  427. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  428. unsigned long flags;
  429. u64 ns = timespec64_to_ns(ts);
  430. /* reset the timecounter */
  431. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  432. timecounter_init(&adapter->hw_tc, &adapter->hw_cc, ns);
  433. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  434. if (adapter->ptp_setup_sdp)
  435. adapter->ptp_setup_sdp(adapter);
  436. return 0;
  437. }
  438. /**
  439. * ixgbe_ptp_feature_enable
  440. * @ptp: the ptp clock structure
  441. * @rq: the requested feature to change
  442. * @on: whether to enable or disable the feature
  443. *
  444. * enable (or disable) ancillary features of the phc subsystem.
  445. * our driver only supports the PPS feature on the X540
  446. */
  447. static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
  448. struct ptp_clock_request *rq, int on)
  449. {
  450. struct ixgbe_adapter *adapter =
  451. container_of(ptp, struct ixgbe_adapter, ptp_caps);
  452. /**
  453. * When PPS is enabled, unmask the interrupt for the ClockOut
  454. * feature, so that the interrupt handler can send the PPS
  455. * event when the clock SDP triggers. Clear mask when PPS is
  456. * disabled
  457. */
  458. if (rq->type != PTP_CLK_REQ_PPS || !adapter->ptp_setup_sdp)
  459. return -ENOTSUPP;
  460. if (on)
  461. adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
  462. else
  463. adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
  464. adapter->ptp_setup_sdp(adapter);
  465. return 0;
  466. }
  467. /**
  468. * ixgbe_ptp_check_pps_event
  469. * @adapter: the private adapter structure
  470. *
  471. * This function is called by the interrupt routine when checking for
  472. * interrupts. It will check and handle a pps event.
  473. */
  474. void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter)
  475. {
  476. struct ixgbe_hw *hw = &adapter->hw;
  477. struct ptp_clock_event event;
  478. event.type = PTP_CLOCK_PPS;
  479. /* this check is necessary in case the interrupt was enabled via some
  480. * alternative means (ex. debug_fs). Better to check here than
  481. * everywhere that calls this function.
  482. */
  483. if (!adapter->ptp_clock)
  484. return;
  485. switch (hw->mac.type) {
  486. case ixgbe_mac_X540:
  487. ptp_clock_event(adapter->ptp_clock, &event);
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. /**
  494. * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow
  495. * @adapter: private adapter struct
  496. *
  497. * this watchdog task periodically reads the timecounter
  498. * in order to prevent missing when the system time registers wrap
  499. * around. This needs to be run approximately twice a minute.
  500. */
  501. void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
  502. {
  503. bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
  504. IXGBE_OVERFLOW_PERIOD);
  505. struct timespec64 ts;
  506. if (timeout) {
  507. ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
  508. adapter->last_overflow_check = jiffies;
  509. }
  510. }
  511. /**
  512. * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched
  513. * @adapter: private network adapter structure
  514. *
  515. * this watchdog task is scheduled to detect error case where hardware has
  516. * dropped an Rx packet that was timestamped when the ring is full. The
  517. * particular error is rare but leaves the device in a state unable to timestamp
  518. * any future packets.
  519. */
  520. void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
  521. {
  522. struct ixgbe_hw *hw = &adapter->hw;
  523. u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  524. struct ixgbe_ring *rx_ring;
  525. unsigned long rx_event;
  526. int n;
  527. /* if we don't have a valid timestamp in the registers, just update the
  528. * timeout counter and exit
  529. */
  530. if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
  531. adapter->last_rx_ptp_check = jiffies;
  532. return;
  533. }
  534. /* determine the most recent watchdog or rx_timestamp event */
  535. rx_event = adapter->last_rx_ptp_check;
  536. for (n = 0; n < adapter->num_rx_queues; n++) {
  537. rx_ring = adapter->rx_ring[n];
  538. if (time_after(rx_ring->last_rx_timestamp, rx_event))
  539. rx_event = rx_ring->last_rx_timestamp;
  540. }
  541. /* only need to read the high RXSTMP register to clear the lock */
  542. if (time_is_before_jiffies(rx_event + 5 * HZ)) {
  543. IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
  544. adapter->last_rx_ptp_check = jiffies;
  545. adapter->rx_hwtstamp_cleared++;
  546. e_warn(drv, "clearing RX Timestamp hang\n");
  547. }
  548. }
  549. /**
  550. * ixgbe_ptp_clear_tx_timestamp - utility function to clear Tx timestamp state
  551. * @adapter: the private adapter structure
  552. *
  553. * This function should be called whenever the state related to a Tx timestamp
  554. * needs to be cleared. This helps ensure that all related bits are reset for
  555. * the next Tx timestamp event.
  556. */
  557. static void ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter *adapter)
  558. {
  559. struct ixgbe_hw *hw = &adapter->hw;
  560. IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
  561. if (adapter->ptp_tx_skb) {
  562. dev_kfree_skb_any(adapter->ptp_tx_skb);
  563. adapter->ptp_tx_skb = NULL;
  564. }
  565. clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
  566. }
  567. /**
  568. * ixgbe_ptp_tx_hang - detect error case where Tx timestamp never finishes
  569. * @adapter: private network adapter structure
  570. */
  571. void ixgbe_ptp_tx_hang(struct ixgbe_adapter *adapter)
  572. {
  573. bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
  574. IXGBE_PTP_TX_TIMEOUT);
  575. if (!adapter->ptp_tx_skb)
  576. return;
  577. if (!test_bit(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state))
  578. return;
  579. /* If we haven't received a timestamp within the timeout, it is
  580. * reasonable to assume that it will never occur, so we can unlock the
  581. * timestamp bit when this occurs.
  582. */
  583. if (timeout) {
  584. cancel_work_sync(&adapter->ptp_tx_work);
  585. ixgbe_ptp_clear_tx_timestamp(adapter);
  586. adapter->tx_hwtstamp_timeouts++;
  587. e_warn(drv, "clearing Tx timestamp hang\n");
  588. }
  589. }
  590. /**
  591. * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  592. * @adapter: the private adapter struct
  593. *
  594. * if the timestamp is valid, we convert it into the timecounter ns
  595. * value, then store that result into the shhwtstamps structure which
  596. * is passed up the network stack
  597. */
  598. static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
  599. {
  600. struct sk_buff *skb = adapter->ptp_tx_skb;
  601. struct ixgbe_hw *hw = &adapter->hw;
  602. struct skb_shared_hwtstamps shhwtstamps;
  603. u64 regval = 0;
  604. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
  605. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
  606. ixgbe_ptp_convert_to_hwtstamp(adapter, &shhwtstamps, regval);
  607. /* Handle cleanup of the ptp_tx_skb ourselves, and unlock the state
  608. * bit prior to notifying the stack via skb_tstamp_tx(). This prevents
  609. * well behaved applications from attempting to timestamp again prior
  610. * to the lock bit being clear.
  611. */
  612. adapter->ptp_tx_skb = NULL;
  613. clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
  614. /* Notify the stack and then free the skb after we've unlocked */
  615. skb_tstamp_tx(skb, &shhwtstamps);
  616. dev_kfree_skb_any(skb);
  617. }
  618. /**
  619. * ixgbe_ptp_tx_hwtstamp_work
  620. * @work: pointer to the work struct
  621. *
  622. * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware
  623. * timestamp has been taken for the current skb. It is necessary, because the
  624. * descriptor's "done" bit does not correlate with the timestamp event.
  625. */
  626. static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
  627. {
  628. struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
  629. ptp_tx_work);
  630. struct ixgbe_hw *hw = &adapter->hw;
  631. bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
  632. IXGBE_PTP_TX_TIMEOUT);
  633. u32 tsynctxctl;
  634. /* we have to have a valid skb to poll for a timestamp */
  635. if (!adapter->ptp_tx_skb) {
  636. ixgbe_ptp_clear_tx_timestamp(adapter);
  637. return;
  638. }
  639. /* stop polling once we have a valid timestamp */
  640. tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  641. if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID) {
  642. ixgbe_ptp_tx_hwtstamp(adapter);
  643. return;
  644. }
  645. if (timeout) {
  646. ixgbe_ptp_clear_tx_timestamp(adapter);
  647. adapter->tx_hwtstamp_timeouts++;
  648. e_warn(drv, "clearing Tx Timestamp hang\n");
  649. } else {
  650. /* reschedule to keep checking if it's not available yet */
  651. schedule_work(&adapter->ptp_tx_work);
  652. }
  653. }
  654. /**
  655. * ixgbe_ptp_rx_pktstamp - utility function to get RX time stamp from buffer
  656. * @q_vector: structure containing interrupt and ring information
  657. * @skb: the packet
  658. *
  659. * This function will be called by the Rx routine of the timestamp for this
  660. * packet is stored in the buffer. The value is stored in little endian format
  661. * starting at the end of the packet data.
  662. */
  663. void ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector *q_vector,
  664. struct sk_buff *skb)
  665. {
  666. __le64 regval;
  667. /* copy the bits out of the skb, and then trim the skb length */
  668. skb_copy_bits(skb, skb->len - IXGBE_TS_HDR_LEN, &regval,
  669. IXGBE_TS_HDR_LEN);
  670. __pskb_trim(skb, skb->len - IXGBE_TS_HDR_LEN);
  671. /* The timestamp is recorded in little endian format, and is stored at
  672. * the end of the packet.
  673. *
  674. * DWORD: N N + 1 N + 2
  675. * Field: End of Packet SYSTIMH SYSTIML
  676. */
  677. ixgbe_ptp_convert_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
  678. le64_to_cpu(regval));
  679. }
  680. /**
  681. * ixgbe_ptp_rx_rgtstamp - utility function which checks for RX time stamp
  682. * @q_vector: structure containing interrupt and ring information
  683. * @skb: particular skb to send timestamp with
  684. *
  685. * if the timestamp is valid, we convert it into the timecounter ns
  686. * value, then store that result into the shhwtstamps structure which
  687. * is passed up the network stack
  688. */
  689. void ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector *q_vector,
  690. struct sk_buff *skb)
  691. {
  692. struct ixgbe_adapter *adapter;
  693. struct ixgbe_hw *hw;
  694. u64 regval = 0;
  695. u32 tsyncrxctl;
  696. /* we cannot process timestamps on a ring without a q_vector */
  697. if (!q_vector || !q_vector->adapter)
  698. return;
  699. adapter = q_vector->adapter;
  700. hw = &adapter->hw;
  701. /* Read the tsyncrxctl register afterwards in order to prevent taking an
  702. * I/O hit on every packet.
  703. */
  704. tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  705. if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
  706. return;
  707. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
  708. regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
  709. ixgbe_ptp_convert_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  710. }
  711. int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
  712. {
  713. struct hwtstamp_config *config = &adapter->tstamp_config;
  714. return copy_to_user(ifr->ifr_data, config,
  715. sizeof(*config)) ? -EFAULT : 0;
  716. }
  717. /**
  718. * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode
  719. * @adapter: the private ixgbe adapter structure
  720. * @config: the hwtstamp configuration requested
  721. *
  722. * Outgoing time stamping can be enabled and disabled. Play nice and
  723. * disable it when requested, although it shouldn't cause any overhead
  724. * when no packet needs it. At most one packet in the queue may be
  725. * marked for time stamping, otherwise it would be impossible to tell
  726. * for sure to which packet the hardware time stamp belongs.
  727. *
  728. * Incoming time stamping has to be configured via the hardware
  729. * filters. Not all combinations are supported, in particular event
  730. * type has to be specified. Matching the kind of event packet is
  731. * not supported, with the exception of "all V2 events regardless of
  732. * level 2 or 4".
  733. *
  734. * Since hardware always timestamps Path delay packets when timestamping V2
  735. * packets, regardless of the type specified in the register, only use V2
  736. * Event mode. This more accurately tells the user what the hardware is going
  737. * to do anyways.
  738. *
  739. * Note: this may modify the hwtstamp configuration towards a more general
  740. * mode, if required to support the specifically requested mode.
  741. */
  742. static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
  743. struct hwtstamp_config *config)
  744. {
  745. struct ixgbe_hw *hw = &adapter->hw;
  746. u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
  747. u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
  748. u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
  749. bool is_l2 = false;
  750. u32 regval;
  751. /* reserved for future extensions */
  752. if (config->flags)
  753. return -EINVAL;
  754. switch (config->tx_type) {
  755. case HWTSTAMP_TX_OFF:
  756. tsync_tx_ctl = 0;
  757. case HWTSTAMP_TX_ON:
  758. break;
  759. default:
  760. return -ERANGE;
  761. }
  762. switch (config->rx_filter) {
  763. case HWTSTAMP_FILTER_NONE:
  764. tsync_rx_ctl = 0;
  765. tsync_rx_mtrl = 0;
  766. adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
  767. IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
  768. break;
  769. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  770. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  771. tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
  772. adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
  773. IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
  774. break;
  775. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  776. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
  777. tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
  778. adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
  779. IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
  780. break;
  781. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  782. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  783. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  784. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  785. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  786. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  787. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  788. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  789. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  790. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
  791. is_l2 = true;
  792. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  793. adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
  794. IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
  795. break;
  796. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  797. case HWTSTAMP_FILTER_NTP_ALL:
  798. case HWTSTAMP_FILTER_ALL:
  799. /* The X550 controller is capable of timestamping all packets,
  800. * which allows it to accept any filter.
  801. */
  802. if (hw->mac.type >= ixgbe_mac_X550) {
  803. tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_ALL;
  804. config->rx_filter = HWTSTAMP_FILTER_ALL;
  805. adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
  806. break;
  807. }
  808. /* fall through */
  809. default:
  810. /*
  811. * register RXMTRL must be set in order to do V1 packets,
  812. * therefore it is not possible to time stamp both V1 Sync and
  813. * Delay_Req messages and hardware does not support
  814. * timestamping all packets => return error
  815. */
  816. adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
  817. IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
  818. config->rx_filter = HWTSTAMP_FILTER_NONE;
  819. return -ERANGE;
  820. }
  821. if (hw->mac.type == ixgbe_mac_82598EB) {
  822. adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
  823. IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
  824. if (tsync_rx_ctl | tsync_tx_ctl)
  825. return -ERANGE;
  826. return 0;
  827. }
  828. /* Per-packet timestamping only works if the filter is set to all
  829. * packets. Since this is desired, always timestamp all packets as long
  830. * as any Rx filter was configured.
  831. */
  832. switch (hw->mac.type) {
  833. case ixgbe_mac_X550:
  834. case ixgbe_mac_X550EM_x:
  835. case ixgbe_mac_x550em_a:
  836. /* enable timestamping all packets only if at least some
  837. * packets were requested. Otherwise, play nice and disable
  838. * timestamping
  839. */
  840. if (config->rx_filter == HWTSTAMP_FILTER_NONE)
  841. break;
  842. tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED |
  843. IXGBE_TSYNCRXCTL_TYPE_ALL |
  844. IXGBE_TSYNCRXCTL_TSIP_UT_EN;
  845. config->rx_filter = HWTSTAMP_FILTER_ALL;
  846. adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
  847. adapter->flags &= ~IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER;
  848. is_l2 = true;
  849. break;
  850. default:
  851. break;
  852. }
  853. /* define ethertype filter for timestamping L2 packets */
  854. if (is_l2)
  855. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
  856. (IXGBE_ETQF_FILTER_EN | /* enable filter */
  857. IXGBE_ETQF_1588 | /* enable timestamping */
  858. ETH_P_1588)); /* 1588 eth protocol type */
  859. else
  860. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
  861. /* enable/disable TX */
  862. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
  863. regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
  864. regval |= tsync_tx_ctl;
  865. IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
  866. /* enable/disable RX */
  867. regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
  868. regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
  869. regval |= tsync_rx_ctl;
  870. IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
  871. /* define which PTP packets are time stamped */
  872. IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
  873. IXGBE_WRITE_FLUSH(hw);
  874. /* clear TX/RX time stamp registers, just to be sure */
  875. ixgbe_ptp_clear_tx_timestamp(adapter);
  876. IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
  877. return 0;
  878. }
  879. /**
  880. * ixgbe_ptp_set_ts_config - user entry point for timestamp mode
  881. * @adapter: pointer to adapter struct
  882. * @ifr: ioctl data
  883. *
  884. * Set hardware to requested mode. If unsupported, return an error with no
  885. * changes. Otherwise, store the mode for future reference.
  886. */
  887. int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
  888. {
  889. struct hwtstamp_config config;
  890. int err;
  891. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  892. return -EFAULT;
  893. err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
  894. if (err)
  895. return err;
  896. /* save these settings for future reference */
  897. memcpy(&adapter->tstamp_config, &config,
  898. sizeof(adapter->tstamp_config));
  899. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  900. -EFAULT : 0;
  901. }
  902. static void ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter *adapter,
  903. u32 *shift, u32 *incval)
  904. {
  905. /**
  906. * Scale the NIC cycle counter by a large factor so that
  907. * relatively small corrections to the frequency can be added
  908. * or subtracted. The drawbacks of a large factor include
  909. * (a) the clock register overflows more quickly, (b) the cycle
  910. * counter structure must be able to convert the systime value
  911. * to nanoseconds using only a multiplier and a right-shift,
  912. * and (c) the value must fit within the timinca register space
  913. * => math based on internal DMA clock rate and available bits
  914. *
  915. * Note that when there is no link, internal DMA clock is same as when
  916. * link speed is 10Gb. Set the registers correctly even when link is
  917. * down to preserve the clock setting
  918. */
  919. switch (adapter->link_speed) {
  920. case IXGBE_LINK_SPEED_100_FULL:
  921. *shift = IXGBE_INCVAL_SHIFT_100;
  922. *incval = IXGBE_INCVAL_100;
  923. break;
  924. case IXGBE_LINK_SPEED_1GB_FULL:
  925. *shift = IXGBE_INCVAL_SHIFT_1GB;
  926. *incval = IXGBE_INCVAL_1GB;
  927. break;
  928. case IXGBE_LINK_SPEED_10GB_FULL:
  929. default:
  930. *shift = IXGBE_INCVAL_SHIFT_10GB;
  931. *incval = IXGBE_INCVAL_10GB;
  932. break;
  933. }
  934. }
  935. /**
  936. * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
  937. * @adapter: pointer to the adapter structure
  938. *
  939. * This function should be called to set the proper values for the TIMINCA
  940. * register and tell the cyclecounter structure what the tick rate of SYSTIME
  941. * is. It does not directly modify SYSTIME registers or the timecounter
  942. * structure. It should be called whenever a new TIMINCA value is necessary,
  943. * such as during initialization or when the link speed changes.
  944. */
  945. void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
  946. {
  947. struct ixgbe_hw *hw = &adapter->hw;
  948. struct cyclecounter cc;
  949. unsigned long flags;
  950. u32 incval = 0;
  951. u32 tsauxc = 0;
  952. u32 fuse0 = 0;
  953. /* For some of the boards below this mask is technically incorrect.
  954. * The timestamp mask overflows at approximately 61bits. However the
  955. * particular hardware does not overflow on an even bitmask value.
  956. * Instead, it overflows due to conversion of upper 32bits billions of
  957. * cycles. Timecounters are not really intended for this purpose so
  958. * they do not properly function if the overflow point isn't 2^N-1.
  959. * However, the actual SYSTIME values in question take ~138 years to
  960. * overflow. In practice this means they won't actually overflow. A
  961. * proper fix to this problem would require modification of the
  962. * timecounter delta calculations.
  963. */
  964. cc.mask = CLOCKSOURCE_MASK(64);
  965. cc.mult = 1;
  966. cc.shift = 0;
  967. switch (hw->mac.type) {
  968. case ixgbe_mac_X550EM_x:
  969. /* SYSTIME assumes X550EM_x board frequency is 300Mhz, and is
  970. * designed to represent seconds and nanoseconds when this is
  971. * the case. However, some revisions of hardware have a 400Mhz
  972. * clock and we have to compensate for this frequency
  973. * variation using corrected mult and shift values.
  974. */
  975. fuse0 = IXGBE_READ_REG(hw, IXGBE_FUSES0_GROUP(0));
  976. if (!(fuse0 & IXGBE_FUSES0_300MHZ)) {
  977. cc.mult = 3;
  978. cc.shift = 2;
  979. }
  980. /* fallthrough */
  981. case ixgbe_mac_x550em_a:
  982. case ixgbe_mac_X550:
  983. cc.read = ixgbe_ptp_read_X550;
  984. /* enable SYSTIME counter */
  985. IXGBE_WRITE_REG(hw, IXGBE_SYSTIMR, 0);
  986. IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0);
  987. IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0);
  988. tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC);
  989. IXGBE_WRITE_REG(hw, IXGBE_TSAUXC,
  990. tsauxc & ~IXGBE_TSAUXC_DISABLE_SYSTIME);
  991. IXGBE_WRITE_REG(hw, IXGBE_TSIM, IXGBE_TSIM_TXTS);
  992. IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_TIMESYNC);
  993. IXGBE_WRITE_FLUSH(hw);
  994. break;
  995. case ixgbe_mac_X540:
  996. cc.read = ixgbe_ptp_read_82599;
  997. ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
  998. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
  999. break;
  1000. case ixgbe_mac_82599EB:
  1001. cc.read = ixgbe_ptp_read_82599;
  1002. ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
  1003. incval >>= IXGBE_INCVAL_SHIFT_82599;
  1004. cc.shift -= IXGBE_INCVAL_SHIFT_82599;
  1005. IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
  1006. BIT(IXGBE_INCPER_SHIFT_82599) | incval);
  1007. break;
  1008. default:
  1009. /* other devices aren't supported */
  1010. return;
  1011. }
  1012. /* update the base incval used to calculate frequency adjustment */
  1013. WRITE_ONCE(adapter->base_incval, incval);
  1014. smp_mb();
  1015. /* need lock to prevent incorrect read while modifying cyclecounter */
  1016. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  1017. memcpy(&adapter->hw_cc, &cc, sizeof(adapter->hw_cc));
  1018. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  1019. }
  1020. /**
  1021. * ixgbe_ptp_reset
  1022. * @adapter: the ixgbe private board structure
  1023. *
  1024. * When the MAC resets, all the hardware bits for timesync are reset. This
  1025. * function is used to re-enable the device for PTP based on current settings.
  1026. * We do lose the current clock time, so just reset the cyclecounter to the
  1027. * system real clock time.
  1028. *
  1029. * This function will maintain hwtstamp_config settings, and resets the SDP
  1030. * output if it was enabled.
  1031. */
  1032. void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
  1033. {
  1034. struct ixgbe_hw *hw = &adapter->hw;
  1035. unsigned long flags;
  1036. /* reset the hardware timestamping mode */
  1037. ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
  1038. /* 82598 does not support PTP */
  1039. if (hw->mac.type == ixgbe_mac_82598EB)
  1040. return;
  1041. ixgbe_ptp_start_cyclecounter(adapter);
  1042. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  1043. timecounter_init(&adapter->hw_tc, &adapter->hw_cc,
  1044. ktime_to_ns(ktime_get_real()));
  1045. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  1046. adapter->last_overflow_check = jiffies;
  1047. /* Now that the shift has been calculated and the systime
  1048. * registers reset, (re-)enable the Clock out feature
  1049. */
  1050. if (adapter->ptp_setup_sdp)
  1051. adapter->ptp_setup_sdp(adapter);
  1052. }
  1053. /**
  1054. * ixgbe_ptp_create_clock
  1055. * @adapter: the ixgbe private adapter structure
  1056. *
  1057. * This function performs setup of the user entry point function table and
  1058. * initializes the PTP clock device, which is used to access the clock-like
  1059. * features of the PTP core. It will be called by ixgbe_ptp_init, and may
  1060. * reuse a previously initialized clock (such as during a suspend/resume
  1061. * cycle).
  1062. */
  1063. static long ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
  1064. {
  1065. struct net_device *netdev = adapter->netdev;
  1066. long err;
  1067. /* do nothing if we already have a clock device */
  1068. if (!IS_ERR_OR_NULL(adapter->ptp_clock))
  1069. return 0;
  1070. switch (adapter->hw.mac.type) {
  1071. case ixgbe_mac_X540:
  1072. snprintf(adapter->ptp_caps.name,
  1073. sizeof(adapter->ptp_caps.name),
  1074. "%s", netdev->name);
  1075. adapter->ptp_caps.owner = THIS_MODULE;
  1076. adapter->ptp_caps.max_adj = 250000000;
  1077. adapter->ptp_caps.n_alarm = 0;
  1078. adapter->ptp_caps.n_ext_ts = 0;
  1079. adapter->ptp_caps.n_per_out = 0;
  1080. adapter->ptp_caps.pps = 1;
  1081. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
  1082. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  1083. adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
  1084. adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
  1085. adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
  1086. adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_x540;
  1087. break;
  1088. case ixgbe_mac_82599EB:
  1089. snprintf(adapter->ptp_caps.name,
  1090. sizeof(adapter->ptp_caps.name),
  1091. "%s", netdev->name);
  1092. adapter->ptp_caps.owner = THIS_MODULE;
  1093. adapter->ptp_caps.max_adj = 250000000;
  1094. adapter->ptp_caps.n_alarm = 0;
  1095. adapter->ptp_caps.n_ext_ts = 0;
  1096. adapter->ptp_caps.n_per_out = 0;
  1097. adapter->ptp_caps.pps = 0;
  1098. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
  1099. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  1100. adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
  1101. adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
  1102. adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
  1103. break;
  1104. case ixgbe_mac_X550:
  1105. case ixgbe_mac_X550EM_x:
  1106. case ixgbe_mac_x550em_a:
  1107. snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
  1108. adapter->ptp_caps.owner = THIS_MODULE;
  1109. adapter->ptp_caps.max_adj = 30000000;
  1110. adapter->ptp_caps.n_alarm = 0;
  1111. adapter->ptp_caps.n_ext_ts = 0;
  1112. adapter->ptp_caps.n_per_out = 0;
  1113. adapter->ptp_caps.pps = 0;
  1114. adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_X550;
  1115. adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
  1116. adapter->ptp_caps.gettime64 = ixgbe_ptp_gettime;
  1117. adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
  1118. adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
  1119. adapter->ptp_setup_sdp = NULL;
  1120. break;
  1121. default:
  1122. adapter->ptp_clock = NULL;
  1123. adapter->ptp_setup_sdp = NULL;
  1124. return -EOPNOTSUPP;
  1125. }
  1126. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  1127. &adapter->pdev->dev);
  1128. if (IS_ERR(adapter->ptp_clock)) {
  1129. err = PTR_ERR(adapter->ptp_clock);
  1130. adapter->ptp_clock = NULL;
  1131. e_dev_err("ptp_clock_register failed\n");
  1132. return err;
  1133. } else if (adapter->ptp_clock)
  1134. e_dev_info("registered PHC device on %s\n", netdev->name);
  1135. /* set default timestamp mode to disabled here. We do this in
  1136. * create_clock instead of init, because we don't want to override the
  1137. * previous settings during a resume cycle.
  1138. */
  1139. adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  1140. adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  1141. return 0;
  1142. }
  1143. /**
  1144. * ixgbe_ptp_init
  1145. * @adapter: the ixgbe private adapter structure
  1146. *
  1147. * This function performs the required steps for enabling PTP
  1148. * support. If PTP support has already been loaded it simply calls the
  1149. * cyclecounter init routine and exits.
  1150. */
  1151. void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
  1152. {
  1153. /* initialize the spin lock first since we can't control when a user
  1154. * will call the entry functions once we have initialized the clock
  1155. * device
  1156. */
  1157. spin_lock_init(&adapter->tmreg_lock);
  1158. /* obtain a PTP device, or re-use an existing device */
  1159. if (ixgbe_ptp_create_clock(adapter))
  1160. return;
  1161. /* we have a clock so we can initialize work now */
  1162. INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
  1163. /* reset the PTP related hardware bits */
  1164. ixgbe_ptp_reset(adapter);
  1165. /* enter the IXGBE_PTP_RUNNING state */
  1166. set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
  1167. return;
  1168. }
  1169. /**
  1170. * ixgbe_ptp_suspend - stop PTP work items
  1171. * @adapter: pointer to adapter struct
  1172. *
  1173. * this function suspends PTP activity, and prevents more PTP work from being
  1174. * generated, but does not destroy the PTP clock device.
  1175. */
  1176. void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
  1177. {
  1178. /* Leave the IXGBE_PTP_RUNNING state. */
  1179. if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
  1180. return;
  1181. adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
  1182. if (adapter->ptp_setup_sdp)
  1183. adapter->ptp_setup_sdp(adapter);
  1184. /* ensure that we cancel any pending PTP Tx work item in progress */
  1185. cancel_work_sync(&adapter->ptp_tx_work);
  1186. ixgbe_ptp_clear_tx_timestamp(adapter);
  1187. }
  1188. /**
  1189. * ixgbe_ptp_stop - close the PTP device
  1190. * @adapter: pointer to adapter struct
  1191. *
  1192. * completely destroy the PTP device, should only be called when the device is
  1193. * being fully closed.
  1194. */
  1195. void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
  1196. {
  1197. /* first, suspend PTP activity */
  1198. ixgbe_ptp_suspend(adapter);
  1199. /* disable the PTP clock device */
  1200. if (adapter->ptp_clock) {
  1201. ptp_clock_unregister(adapter->ptp_clock);
  1202. adapter->ptp_clock = NULL;
  1203. e_dev_info("removed PHC on %s\n",
  1204. adapter->netdev->name);
  1205. }
  1206. }