igb_ptp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /* PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
  2. *
  3. * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/pci.h>
  21. #include <linux/ptp_classify.h>
  22. #include "igb.h"
  23. #define INCVALUE_MASK 0x7fffffff
  24. #define ISGN 0x80000000
  25. /* The 82580 timesync updates the system timer every 8ns by 8ns,
  26. * and this update value cannot be reprogrammed.
  27. *
  28. * Neither the 82576 nor the 82580 offer registers wide enough to hold
  29. * nanoseconds time values for very long. For the 82580, SYSTIM always
  30. * counts nanoseconds, but the upper 24 bits are not availible. The
  31. * frequency is adjusted by changing the 32 bit fractional nanoseconds
  32. * register, TIMINCA.
  33. *
  34. * For the 82576, the SYSTIM register time unit is affect by the
  35. * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
  36. * field are needed to provide the nominal 16 nanosecond period,
  37. * leaving 19 bits for fractional nanoseconds.
  38. *
  39. * We scale the NIC clock cycle by a large factor so that relatively
  40. * small clock corrections can be added or subtracted at each clock
  41. * tick. The drawbacks of a large factor are a) that the clock
  42. * register overflows more quickly (not such a big deal) and b) that
  43. * the increment per tick has to fit into 24 bits. As a result we
  44. * need to use a shift of 19 so we can fit a value of 16 into the
  45. * TIMINCA register.
  46. *
  47. *
  48. * SYSTIMH SYSTIML
  49. * +--------------+ +---+---+------+
  50. * 82576 | 32 | | 8 | 5 | 19 |
  51. * +--------------+ +---+---+------+
  52. * \________ 45 bits _______/ fract
  53. *
  54. * +----------+---+ +--------------+
  55. * 82580 | 24 | 8 | | 32 |
  56. * +----------+---+ +--------------+
  57. * reserved \______ 40 bits _____/
  58. *
  59. *
  60. * The 45 bit 82576 SYSTIM overflows every
  61. * 2^45 * 10^-9 / 3600 = 9.77 hours.
  62. *
  63. * The 40 bit 82580 SYSTIM overflows every
  64. * 2^40 * 10^-9 / 60 = 18.3 minutes.
  65. */
  66. #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9)
  67. #define IGB_PTP_TX_TIMEOUT (HZ * 15)
  68. #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT)
  69. #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
  70. #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT)
  71. #define IGB_NBITS_82580 40
  72. static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter);
  73. /* SYSTIM read access for the 82576 */
  74. static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc)
  75. {
  76. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  77. struct e1000_hw *hw = &igb->hw;
  78. u64 val;
  79. u32 lo, hi;
  80. lo = rd32(E1000_SYSTIML);
  81. hi = rd32(E1000_SYSTIMH);
  82. val = ((u64) hi) << 32;
  83. val |= lo;
  84. return val;
  85. }
  86. /* SYSTIM read access for the 82580 */
  87. static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc)
  88. {
  89. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  90. struct e1000_hw *hw = &igb->hw;
  91. u32 lo, hi;
  92. u64 val;
  93. /* The timestamp latches on lowest register read. For the 82580
  94. * the lowest register is SYSTIMR instead of SYSTIML. However we only
  95. * need to provide nanosecond resolution, so we just ignore it.
  96. */
  97. rd32(E1000_SYSTIMR);
  98. lo = rd32(E1000_SYSTIML);
  99. hi = rd32(E1000_SYSTIMH);
  100. val = ((u64) hi) << 32;
  101. val |= lo;
  102. return val;
  103. }
  104. /* SYSTIM read access for I210/I211 */
  105. static void igb_ptp_read_i210(struct igb_adapter *adapter, struct timespec *ts)
  106. {
  107. struct e1000_hw *hw = &adapter->hw;
  108. u32 sec, nsec;
  109. /* The timestamp latches on lowest register read. For I210/I211, the
  110. * lowest register is SYSTIMR. Since we only need to provide nanosecond
  111. * resolution, we can ignore it.
  112. */
  113. rd32(E1000_SYSTIMR);
  114. nsec = rd32(E1000_SYSTIML);
  115. sec = rd32(E1000_SYSTIMH);
  116. ts->tv_sec = sec;
  117. ts->tv_nsec = nsec;
  118. }
  119. static void igb_ptp_write_i210(struct igb_adapter *adapter,
  120. const struct timespec *ts)
  121. {
  122. struct e1000_hw *hw = &adapter->hw;
  123. /* Writing the SYSTIMR register is not necessary as it only provides
  124. * sub-nanosecond resolution.
  125. */
  126. wr32(E1000_SYSTIML, ts->tv_nsec);
  127. wr32(E1000_SYSTIMH, ts->tv_sec);
  128. }
  129. /**
  130. * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
  131. * @adapter: board private structure
  132. * @hwtstamps: timestamp structure to update
  133. * @systim: unsigned 64bit system time value.
  134. *
  135. * We need to convert the system time value stored in the RX/TXSTMP registers
  136. * into a hwtstamp which can be used by the upper level timestamping functions.
  137. *
  138. * The 'tmreg_lock' spinlock is used to protect the consistency of the
  139. * system time value. This is needed because reading the 64 bit time
  140. * value involves reading two (or three) 32 bit registers. The first
  141. * read latches the value. Ditto for writing.
  142. *
  143. * In addition, here have extended the system time with an overflow
  144. * counter in software.
  145. **/
  146. static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
  147. struct skb_shared_hwtstamps *hwtstamps,
  148. u64 systim)
  149. {
  150. unsigned long flags;
  151. u64 ns;
  152. switch (adapter->hw.mac.type) {
  153. case e1000_82576:
  154. case e1000_82580:
  155. case e1000_i354:
  156. case e1000_i350:
  157. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  158. ns = timecounter_cyc2time(&adapter->tc, systim);
  159. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  160. memset(hwtstamps, 0, sizeof(*hwtstamps));
  161. hwtstamps->hwtstamp = ns_to_ktime(ns);
  162. break;
  163. case e1000_i210:
  164. case e1000_i211:
  165. memset(hwtstamps, 0, sizeof(*hwtstamps));
  166. /* Upper 32 bits contain s, lower 32 bits contain ns. */
  167. hwtstamps->hwtstamp = ktime_set(systim >> 32,
  168. systim & 0xFFFFFFFF);
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. /* PTP clock operations */
  175. static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
  176. {
  177. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  178. ptp_caps);
  179. struct e1000_hw *hw = &igb->hw;
  180. int neg_adj = 0;
  181. u64 rate;
  182. u32 incvalue;
  183. if (ppb < 0) {
  184. neg_adj = 1;
  185. ppb = -ppb;
  186. }
  187. rate = ppb;
  188. rate <<= 14;
  189. rate = div_u64(rate, 1953125);
  190. incvalue = 16 << IGB_82576_TSYNC_SHIFT;
  191. if (neg_adj)
  192. incvalue -= rate;
  193. else
  194. incvalue += rate;
  195. wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
  196. return 0;
  197. }
  198. static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
  199. {
  200. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  201. ptp_caps);
  202. struct e1000_hw *hw = &igb->hw;
  203. int neg_adj = 0;
  204. u64 rate;
  205. u32 inca;
  206. if (ppb < 0) {
  207. neg_adj = 1;
  208. ppb = -ppb;
  209. }
  210. rate = ppb;
  211. rate <<= 26;
  212. rate = div_u64(rate, 1953125);
  213. inca = rate & INCVALUE_MASK;
  214. if (neg_adj)
  215. inca |= ISGN;
  216. wr32(E1000_TIMINCA, inca);
  217. return 0;
  218. }
  219. static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
  220. {
  221. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  222. ptp_caps);
  223. unsigned long flags;
  224. s64 now;
  225. spin_lock_irqsave(&igb->tmreg_lock, flags);
  226. now = timecounter_read(&igb->tc);
  227. now += delta;
  228. timecounter_init(&igb->tc, &igb->cc, now);
  229. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  230. return 0;
  231. }
  232. static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
  233. {
  234. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  235. ptp_caps);
  236. unsigned long flags;
  237. struct timespec now, then = ns_to_timespec(delta);
  238. spin_lock_irqsave(&igb->tmreg_lock, flags);
  239. igb_ptp_read_i210(igb, &now);
  240. now = timespec_add(now, then);
  241. igb_ptp_write_i210(igb, (const struct timespec *)&now);
  242. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  243. return 0;
  244. }
  245. static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
  246. struct timespec *ts)
  247. {
  248. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  249. ptp_caps);
  250. unsigned long flags;
  251. u64 ns;
  252. u32 remainder;
  253. spin_lock_irqsave(&igb->tmreg_lock, flags);
  254. ns = timecounter_read(&igb->tc);
  255. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  256. ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
  257. ts->tv_nsec = remainder;
  258. return 0;
  259. }
  260. static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
  261. struct timespec *ts)
  262. {
  263. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  264. ptp_caps);
  265. unsigned long flags;
  266. spin_lock_irqsave(&igb->tmreg_lock, flags);
  267. igb_ptp_read_i210(igb, ts);
  268. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  269. return 0;
  270. }
  271. static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
  272. const struct timespec *ts)
  273. {
  274. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  275. ptp_caps);
  276. unsigned long flags;
  277. u64 ns;
  278. ns = ts->tv_sec * 1000000000ULL;
  279. ns += ts->tv_nsec;
  280. spin_lock_irqsave(&igb->tmreg_lock, flags);
  281. timecounter_init(&igb->tc, &igb->cc, ns);
  282. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  283. return 0;
  284. }
  285. static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
  286. const struct timespec *ts)
  287. {
  288. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  289. ptp_caps);
  290. unsigned long flags;
  291. spin_lock_irqsave(&igb->tmreg_lock, flags);
  292. igb_ptp_write_i210(igb, ts);
  293. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  294. return 0;
  295. }
  296. static int igb_ptp_feature_enable(struct ptp_clock_info *ptp,
  297. struct ptp_clock_request *rq, int on)
  298. {
  299. return -EOPNOTSUPP;
  300. }
  301. /**
  302. * igb_ptp_tx_work
  303. * @work: pointer to work struct
  304. *
  305. * This work function polls the TSYNCTXCTL valid bit to determine when a
  306. * timestamp has been taken for the current stored skb.
  307. **/
  308. static void igb_ptp_tx_work(struct work_struct *work)
  309. {
  310. struct igb_adapter *adapter = container_of(work, struct igb_adapter,
  311. ptp_tx_work);
  312. struct e1000_hw *hw = &adapter->hw;
  313. u32 tsynctxctl;
  314. if (!adapter->ptp_tx_skb)
  315. return;
  316. if (time_is_before_jiffies(adapter->ptp_tx_start +
  317. IGB_PTP_TX_TIMEOUT)) {
  318. dev_kfree_skb_any(adapter->ptp_tx_skb);
  319. adapter->ptp_tx_skb = NULL;
  320. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  321. adapter->tx_hwtstamp_timeouts++;
  322. dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
  323. return;
  324. }
  325. tsynctxctl = rd32(E1000_TSYNCTXCTL);
  326. if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
  327. igb_ptp_tx_hwtstamp(adapter);
  328. else
  329. /* reschedule to check later */
  330. schedule_work(&adapter->ptp_tx_work);
  331. }
  332. static void igb_ptp_overflow_check(struct work_struct *work)
  333. {
  334. struct igb_adapter *igb =
  335. container_of(work, struct igb_adapter, ptp_overflow_work.work);
  336. struct timespec ts;
  337. igb->ptp_caps.gettime(&igb->ptp_caps, &ts);
  338. pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  339. schedule_delayed_work(&igb->ptp_overflow_work,
  340. IGB_SYSTIM_OVERFLOW_PERIOD);
  341. }
  342. /**
  343. * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
  344. * @adapter: private network adapter structure
  345. *
  346. * This watchdog task is scheduled to detect error case where hardware has
  347. * dropped an Rx packet that was timestamped when the ring is full. The
  348. * particular error is rare but leaves the device in a state unable to timestamp
  349. * any future packets.
  350. **/
  351. void igb_ptp_rx_hang(struct igb_adapter *adapter)
  352. {
  353. struct e1000_hw *hw = &adapter->hw;
  354. u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
  355. unsigned long rx_event;
  356. if (hw->mac.type != e1000_82576)
  357. return;
  358. /* If we don't have a valid timestamp in the registers, just update the
  359. * timeout counter and exit
  360. */
  361. if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
  362. adapter->last_rx_ptp_check = jiffies;
  363. return;
  364. }
  365. /* Determine the most recent watchdog or rx_timestamp event */
  366. rx_event = adapter->last_rx_ptp_check;
  367. if (time_after(adapter->last_rx_timestamp, rx_event))
  368. rx_event = adapter->last_rx_timestamp;
  369. /* Only need to read the high RXSTMP register to clear the lock */
  370. if (time_is_before_jiffies(rx_event + 5 * HZ)) {
  371. rd32(E1000_RXSTMPH);
  372. adapter->last_rx_ptp_check = jiffies;
  373. adapter->rx_hwtstamp_cleared++;
  374. dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang\n");
  375. }
  376. }
  377. /**
  378. * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  379. * @adapter: Board private structure.
  380. *
  381. * If we were asked to do hardware stamping and such a time stamp is
  382. * available, then it must have been for this skb here because we only
  383. * allow only one such packet into the queue.
  384. **/
  385. static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
  386. {
  387. struct e1000_hw *hw = &adapter->hw;
  388. struct skb_shared_hwtstamps shhwtstamps;
  389. u64 regval;
  390. regval = rd32(E1000_TXSTMPL);
  391. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  392. igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  393. skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
  394. dev_kfree_skb_any(adapter->ptp_tx_skb);
  395. adapter->ptp_tx_skb = NULL;
  396. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  397. }
  398. /**
  399. * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
  400. * @q_vector: Pointer to interrupt specific structure
  401. * @va: Pointer to address containing Rx buffer
  402. * @skb: Buffer containing timestamp and packet
  403. *
  404. * This function is meant to retrieve a timestamp from the first buffer of an
  405. * incoming frame. The value is stored in little endian format starting on
  406. * byte 8.
  407. **/
  408. void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
  409. unsigned char *va,
  410. struct sk_buff *skb)
  411. {
  412. __le64 *regval = (__le64 *)va;
  413. /* The timestamp is recorded in little endian format.
  414. * DWORD: 0 1 2 3
  415. * Field: Reserved Reserved SYSTIML SYSTIMH
  416. */
  417. igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
  418. le64_to_cpu(regval[1]));
  419. }
  420. /**
  421. * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
  422. * @q_vector: Pointer to interrupt specific structure
  423. * @skb: Buffer containing timestamp and packet
  424. *
  425. * This function is meant to retrieve a timestamp from the internal registers
  426. * of the adapter and store it in the skb.
  427. **/
  428. void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
  429. struct sk_buff *skb)
  430. {
  431. struct igb_adapter *adapter = q_vector->adapter;
  432. struct e1000_hw *hw = &adapter->hw;
  433. u64 regval;
  434. /* If this bit is set, then the RX registers contain the time stamp. No
  435. * other packet will be time stamped until we read these registers, so
  436. * read the registers to make them available again. Because only one
  437. * packet can be time stamped at a time, we know that the register
  438. * values must belong to this one here and therefore we don't need to
  439. * compare any of the additional attributes stored for it.
  440. *
  441. * If nothing went wrong, then it should have a shared tx_flags that we
  442. * can turn into a skb_shared_hwtstamps.
  443. */
  444. if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  445. return;
  446. regval = rd32(E1000_RXSTMPL);
  447. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  448. igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  449. /* Update the last_rx_timestamp timer in order to enable watchdog check
  450. * for error case of latched timestamp on a dropped packet.
  451. */
  452. adapter->last_rx_timestamp = jiffies;
  453. }
  454. /**
  455. * igb_ptp_get_ts_config - get hardware time stamping config
  456. * @netdev:
  457. * @ifreq:
  458. *
  459. * Get the hwtstamp_config settings to return to the user. Rather than attempt
  460. * to deconstruct the settings from the registers, just return a shadow copy
  461. * of the last known settings.
  462. **/
  463. int igb_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
  464. {
  465. struct igb_adapter *adapter = netdev_priv(netdev);
  466. struct hwtstamp_config *config = &adapter->tstamp_config;
  467. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  468. -EFAULT : 0;
  469. }
  470. /**
  471. * igb_ptp_set_timestamp_mode - setup hardware for timestamping
  472. * @adapter: networking device structure
  473. * @config: hwtstamp configuration
  474. *
  475. * Outgoing time stamping can be enabled and disabled. Play nice and
  476. * disable it when requested, although it shouldn't case any overhead
  477. * when no packet needs it. At most one packet in the queue may be
  478. * marked for time stamping, otherwise it would be impossible to tell
  479. * for sure to which packet the hardware time stamp belongs.
  480. *
  481. * Incoming time stamping has to be configured via the hardware
  482. * filters. Not all combinations are supported, in particular event
  483. * type has to be specified. Matching the kind of event packet is
  484. * not supported, with the exception of "all V2 events regardless of
  485. * level 2 or 4".
  486. */
  487. static int igb_ptp_set_timestamp_mode(struct igb_adapter *adapter,
  488. struct hwtstamp_config *config)
  489. {
  490. struct e1000_hw *hw = &adapter->hw;
  491. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  492. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  493. u32 tsync_rx_cfg = 0;
  494. bool is_l4 = false;
  495. bool is_l2 = false;
  496. u32 regval;
  497. /* reserved for future extensions */
  498. if (config->flags)
  499. return -EINVAL;
  500. switch (config->tx_type) {
  501. case HWTSTAMP_TX_OFF:
  502. tsync_tx_ctl = 0;
  503. case HWTSTAMP_TX_ON:
  504. break;
  505. default:
  506. return -ERANGE;
  507. }
  508. switch (config->rx_filter) {
  509. case HWTSTAMP_FILTER_NONE:
  510. tsync_rx_ctl = 0;
  511. break;
  512. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  513. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  514. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  515. is_l4 = true;
  516. break;
  517. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  518. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  519. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  520. is_l4 = true;
  521. break;
  522. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  523. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  524. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  525. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  526. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  527. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  528. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  529. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  530. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  531. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  532. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  533. is_l2 = true;
  534. is_l4 = true;
  535. break;
  536. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  537. case HWTSTAMP_FILTER_ALL:
  538. /* 82576 cannot timestamp all packets, which it needs to do to
  539. * support both V1 Sync and Delay_Req messages
  540. */
  541. if (hw->mac.type != e1000_82576) {
  542. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  543. config->rx_filter = HWTSTAMP_FILTER_ALL;
  544. break;
  545. }
  546. /* fall through */
  547. default:
  548. config->rx_filter = HWTSTAMP_FILTER_NONE;
  549. return -ERANGE;
  550. }
  551. if (hw->mac.type == e1000_82575) {
  552. if (tsync_rx_ctl | tsync_tx_ctl)
  553. return -EINVAL;
  554. return 0;
  555. }
  556. /* Per-packet timestamping only works if all packets are
  557. * timestamped, so enable timestamping in all packets as
  558. * long as one Rx filter was configured.
  559. */
  560. if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
  561. tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  562. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  563. config->rx_filter = HWTSTAMP_FILTER_ALL;
  564. is_l2 = true;
  565. is_l4 = true;
  566. if ((hw->mac.type == e1000_i210) ||
  567. (hw->mac.type == e1000_i211)) {
  568. regval = rd32(E1000_RXPBS);
  569. regval |= E1000_RXPBS_CFG_TS_EN;
  570. wr32(E1000_RXPBS, regval);
  571. }
  572. }
  573. /* enable/disable TX */
  574. regval = rd32(E1000_TSYNCTXCTL);
  575. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  576. regval |= tsync_tx_ctl;
  577. wr32(E1000_TSYNCTXCTL, regval);
  578. /* enable/disable RX */
  579. regval = rd32(E1000_TSYNCRXCTL);
  580. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  581. regval |= tsync_rx_ctl;
  582. wr32(E1000_TSYNCRXCTL, regval);
  583. /* define which PTP packets are time stamped */
  584. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  585. /* define ethertype filter for timestamped packets */
  586. if (is_l2)
  587. wr32(E1000_ETQF(3),
  588. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  589. E1000_ETQF_1588 | /* enable timestamping */
  590. ETH_P_1588)); /* 1588 eth protocol type */
  591. else
  592. wr32(E1000_ETQF(3), 0);
  593. /* L4 Queue Filter[3]: filter by destination port and protocol */
  594. if (is_l4) {
  595. u32 ftqf = (IPPROTO_UDP /* UDP */
  596. | E1000_FTQF_VF_BP /* VF not compared */
  597. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  598. | E1000_FTQF_MASK); /* mask all inputs */
  599. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  600. wr32(E1000_IMIR(3), htons(PTP_EV_PORT));
  601. wr32(E1000_IMIREXT(3),
  602. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  603. if (hw->mac.type == e1000_82576) {
  604. /* enable source port check */
  605. wr32(E1000_SPQF(3), htons(PTP_EV_PORT));
  606. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  607. }
  608. wr32(E1000_FTQF(3), ftqf);
  609. } else {
  610. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  611. }
  612. wrfl();
  613. /* clear TX/RX time stamp registers, just to be sure */
  614. regval = rd32(E1000_TXSTMPL);
  615. regval = rd32(E1000_TXSTMPH);
  616. regval = rd32(E1000_RXSTMPL);
  617. regval = rd32(E1000_RXSTMPH);
  618. return 0;
  619. }
  620. /**
  621. * igb_ptp_set_ts_config - set hardware time stamping config
  622. * @netdev:
  623. * @ifreq:
  624. *
  625. **/
  626. int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
  627. {
  628. struct igb_adapter *adapter = netdev_priv(netdev);
  629. struct hwtstamp_config config;
  630. int err;
  631. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  632. return -EFAULT;
  633. err = igb_ptp_set_timestamp_mode(adapter, &config);
  634. if (err)
  635. return err;
  636. /* save these settings for future reference */
  637. memcpy(&adapter->tstamp_config, &config,
  638. sizeof(adapter->tstamp_config));
  639. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  640. -EFAULT : 0;
  641. }
  642. void igb_ptp_init(struct igb_adapter *adapter)
  643. {
  644. struct e1000_hw *hw = &adapter->hw;
  645. struct net_device *netdev = adapter->netdev;
  646. switch (hw->mac.type) {
  647. case e1000_82576:
  648. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  649. adapter->ptp_caps.owner = THIS_MODULE;
  650. adapter->ptp_caps.max_adj = 999999881;
  651. adapter->ptp_caps.n_ext_ts = 0;
  652. adapter->ptp_caps.pps = 0;
  653. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
  654. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  655. adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
  656. adapter->ptp_caps.settime = igb_ptp_settime_82576;
  657. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  658. adapter->cc.read = igb_ptp_read_82576;
  659. adapter->cc.mask = CLOCKSOURCE_MASK(64);
  660. adapter->cc.mult = 1;
  661. adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
  662. /* Dial the nominal frequency. */
  663. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  664. break;
  665. case e1000_82580:
  666. case e1000_i354:
  667. case e1000_i350:
  668. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  669. adapter->ptp_caps.owner = THIS_MODULE;
  670. adapter->ptp_caps.max_adj = 62499999;
  671. adapter->ptp_caps.n_ext_ts = 0;
  672. adapter->ptp_caps.pps = 0;
  673. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  674. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  675. adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
  676. adapter->ptp_caps.settime = igb_ptp_settime_82576;
  677. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  678. adapter->cc.read = igb_ptp_read_82580;
  679. adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580);
  680. adapter->cc.mult = 1;
  681. adapter->cc.shift = 0;
  682. /* Enable the timer functions by clearing bit 31. */
  683. wr32(E1000_TSAUXC, 0x0);
  684. break;
  685. case e1000_i210:
  686. case e1000_i211:
  687. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  688. adapter->ptp_caps.owner = THIS_MODULE;
  689. adapter->ptp_caps.max_adj = 62499999;
  690. adapter->ptp_caps.n_ext_ts = 0;
  691. adapter->ptp_caps.pps = 0;
  692. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  693. adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
  694. adapter->ptp_caps.gettime = igb_ptp_gettime_i210;
  695. adapter->ptp_caps.settime = igb_ptp_settime_i210;
  696. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  697. /* Enable the timer functions by clearing bit 31. */
  698. wr32(E1000_TSAUXC, 0x0);
  699. break;
  700. default:
  701. adapter->ptp_clock = NULL;
  702. return;
  703. }
  704. wrfl();
  705. spin_lock_init(&adapter->tmreg_lock);
  706. INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
  707. /* Initialize the clock and overflow work for devices that need it. */
  708. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  709. struct timespec ts = ktime_to_timespec(ktime_get_real());
  710. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  711. } else {
  712. timecounter_init(&adapter->tc, &adapter->cc,
  713. ktime_to_ns(ktime_get_real()));
  714. INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
  715. igb_ptp_overflow_check);
  716. schedule_delayed_work(&adapter->ptp_overflow_work,
  717. IGB_SYSTIM_OVERFLOW_PERIOD);
  718. }
  719. /* Initialize the time sync interrupts for devices that support it. */
  720. if (hw->mac.type >= e1000_82580) {
  721. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  722. wr32(E1000_IMS, E1000_IMS_TS);
  723. }
  724. adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  725. adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  726. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  727. &adapter->pdev->dev);
  728. if (IS_ERR(adapter->ptp_clock)) {
  729. adapter->ptp_clock = NULL;
  730. dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
  731. } else {
  732. dev_info(&adapter->pdev->dev, "added PHC on %s\n",
  733. adapter->netdev->name);
  734. adapter->flags |= IGB_FLAG_PTP;
  735. }
  736. }
  737. /**
  738. * igb_ptp_stop - Disable PTP device and stop the overflow check.
  739. * @adapter: Board private structure.
  740. *
  741. * This function stops the PTP support and cancels the delayed work.
  742. **/
  743. void igb_ptp_stop(struct igb_adapter *adapter)
  744. {
  745. switch (adapter->hw.mac.type) {
  746. case e1000_82576:
  747. case e1000_82580:
  748. case e1000_i354:
  749. case e1000_i350:
  750. cancel_delayed_work_sync(&adapter->ptp_overflow_work);
  751. break;
  752. case e1000_i210:
  753. case e1000_i211:
  754. /* No delayed work to cancel. */
  755. break;
  756. default:
  757. return;
  758. }
  759. cancel_work_sync(&adapter->ptp_tx_work);
  760. if (adapter->ptp_tx_skb) {
  761. dev_kfree_skb_any(adapter->ptp_tx_skb);
  762. adapter->ptp_tx_skb = NULL;
  763. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  764. }
  765. if (adapter->ptp_clock) {
  766. ptp_clock_unregister(adapter->ptp_clock);
  767. dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
  768. adapter->netdev->name);
  769. adapter->flags &= ~IGB_FLAG_PTP;
  770. }
  771. }
  772. /**
  773. * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
  774. * @adapter: Board private structure.
  775. *
  776. * This function handles the reset work required to re-enable the PTP device.
  777. **/
  778. void igb_ptp_reset(struct igb_adapter *adapter)
  779. {
  780. struct e1000_hw *hw = &adapter->hw;
  781. if (!(adapter->flags & IGB_FLAG_PTP))
  782. return;
  783. /* reset the tstamp_config */
  784. igb_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
  785. switch (adapter->hw.mac.type) {
  786. case e1000_82576:
  787. /* Dial the nominal frequency. */
  788. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  789. break;
  790. case e1000_82580:
  791. case e1000_i354:
  792. case e1000_i350:
  793. case e1000_i210:
  794. case e1000_i211:
  795. /* Enable the timer functions and interrupts. */
  796. wr32(E1000_TSAUXC, 0x0);
  797. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  798. wr32(E1000_IMS, E1000_IMS_TS);
  799. break;
  800. default:
  801. /* No work to do. */
  802. return;
  803. }
  804. /* Re-initialize the timer. */
  805. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  806. struct timespec ts = ktime_to_timespec(ktime_get_real());
  807. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  808. } else {
  809. timecounter_init(&adapter->tc, &adapter->cc,
  810. ktime_to_ns(ktime_get_real()));
  811. }
  812. }