igb_ptp.c 24 KB

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