ixgbe_ptp.c 29 KB

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