igb_ptp.c 25 KB

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