igb_ptp.c 36 KB

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