ixgbe_ptp.c 29 KB

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