igb_ptp.c 24 KB

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