ixgbe_ptp.c 43 KB

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