igb_ptp.c 32 KB

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