igc_ptp.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Intel Corporation */
  3. #include "igc.h"
  4. #include <linux/module.h>
  5. #include <linux/device.h>
  6. #include <linux/pci.h>
  7. #include <linux/ptp_classify.h>
  8. #include <linux/clocksource.h>
  9. #include <linux/ktime.h>
  10. #include <linux/delay.h>
  11. #include <linux/iopoll.h>
  12. #include <net/xdp_sock_drv.h>
  13. #define INCVALUE_MASK 0x7fffffff
  14. #define ISGN 0x80000000
  15. #define IGC_PTP_TX_TIMEOUT (HZ * 15)
  16. #define IGC_PTM_STAT_SLEEP 2
  17. #define IGC_PTM_STAT_TIMEOUT 100
  18. /* SYSTIM read access for I225 */
  19. void igc_ptp_read(struct igc_adapter *adapter, struct timespec64 *ts)
  20. {
  21. struct igc_hw *hw = &adapter->hw;
  22. u32 sec, nsec;
  23. /* The timestamp is latched when SYSTIML is read. */
  24. nsec = rd32(IGC_SYSTIML);
  25. sec = rd32(IGC_SYSTIMH);
  26. ts->tv_sec = sec;
  27. ts->tv_nsec = nsec;
  28. }
  29. static void igc_ptp_write_i225(struct igc_adapter *adapter,
  30. const struct timespec64 *ts)
  31. {
  32. struct igc_hw *hw = &adapter->hw;
  33. wr32(IGC_SYSTIML, ts->tv_nsec);
  34. wr32(IGC_SYSTIMH, ts->tv_sec);
  35. }
  36. static int igc_ptp_adjfine_i225(struct ptp_clock_info *ptp, long scaled_ppm)
  37. {
  38. struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
  39. ptp_caps);
  40. struct igc_hw *hw = &igc->hw;
  41. int neg_adj = 0;
  42. u64 rate;
  43. u32 inca;
  44. if (scaled_ppm < 0) {
  45. neg_adj = 1;
  46. scaled_ppm = -scaled_ppm;
  47. }
  48. rate = scaled_ppm;
  49. rate <<= 14;
  50. rate = div_u64(rate, 78125);
  51. inca = rate & INCVALUE_MASK;
  52. if (neg_adj)
  53. inca |= ISGN;
  54. wr32(IGC_TIMINCA, inca);
  55. return 0;
  56. }
  57. static int igc_ptp_adjtime_i225(struct ptp_clock_info *ptp, s64 delta)
  58. {
  59. struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
  60. ptp_caps);
  61. struct timespec64 now, then = ns_to_timespec64(delta);
  62. unsigned long flags;
  63. spin_lock_irqsave(&igc->tmreg_lock, flags);
  64. igc_ptp_read(igc, &now);
  65. now = timespec64_add(now, then);
  66. igc_ptp_write_i225(igc, (const struct timespec64 *)&now);
  67. spin_unlock_irqrestore(&igc->tmreg_lock, flags);
  68. return 0;
  69. }
  70. static int igc_ptp_gettimex64_i225(struct ptp_clock_info *ptp,
  71. struct timespec64 *ts,
  72. struct ptp_system_timestamp *sts)
  73. {
  74. struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
  75. ptp_caps);
  76. struct igc_hw *hw = &igc->hw;
  77. unsigned long flags;
  78. spin_lock_irqsave(&igc->tmreg_lock, flags);
  79. ptp_read_system_prets(sts);
  80. ts->tv_nsec = rd32(IGC_SYSTIML);
  81. ts->tv_sec = rd32(IGC_SYSTIMH);
  82. ptp_read_system_postts(sts);
  83. spin_unlock_irqrestore(&igc->tmreg_lock, flags);
  84. return 0;
  85. }
  86. static int igc_ptp_settime_i225(struct ptp_clock_info *ptp,
  87. const struct timespec64 *ts)
  88. {
  89. struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
  90. ptp_caps);
  91. unsigned long flags;
  92. spin_lock_irqsave(&igc->tmreg_lock, flags);
  93. igc_ptp_write_i225(igc, ts);
  94. spin_unlock_irqrestore(&igc->tmreg_lock, flags);
  95. return 0;
  96. }
  97. static void igc_pin_direction(int pin, int input, u32 *ctrl, u32 *ctrl_ext)
  98. {
  99. u32 *ptr = pin < 2 ? ctrl : ctrl_ext;
  100. static const u32 mask[IGC_N_SDP] = {
  101. IGC_CTRL_SDP0_DIR,
  102. IGC_CTRL_SDP1_DIR,
  103. IGC_CTRL_EXT_SDP2_DIR,
  104. IGC_CTRL_EXT_SDP3_DIR,
  105. };
  106. if (input)
  107. *ptr &= ~mask[pin];
  108. else
  109. *ptr |= mask[pin];
  110. }
  111. static void igc_pin_perout(struct igc_adapter *igc, int chan, int pin, int freq)
  112. {
  113. static const u32 igc_aux0_sel_sdp[IGC_N_SDP] = {
  114. IGC_AUX0_SEL_SDP0, IGC_AUX0_SEL_SDP1, IGC_AUX0_SEL_SDP2, IGC_AUX0_SEL_SDP3,
  115. };
  116. static const u32 igc_aux1_sel_sdp[IGC_N_SDP] = {
  117. IGC_AUX1_SEL_SDP0, IGC_AUX1_SEL_SDP1, IGC_AUX1_SEL_SDP2, IGC_AUX1_SEL_SDP3,
  118. };
  119. static const u32 igc_ts_sdp_en[IGC_N_SDP] = {
  120. IGC_TS_SDP0_EN, IGC_TS_SDP1_EN, IGC_TS_SDP2_EN, IGC_TS_SDP3_EN,
  121. };
  122. static const u32 igc_ts_sdp_sel_tt0[IGC_N_SDP] = {
  123. IGC_TS_SDP0_SEL_TT0, IGC_TS_SDP1_SEL_TT0,
  124. IGC_TS_SDP2_SEL_TT0, IGC_TS_SDP3_SEL_TT0,
  125. };
  126. static const u32 igc_ts_sdp_sel_tt1[IGC_N_SDP] = {
  127. IGC_TS_SDP0_SEL_TT1, IGC_TS_SDP1_SEL_TT1,
  128. IGC_TS_SDP2_SEL_TT1, IGC_TS_SDP3_SEL_TT1,
  129. };
  130. static const u32 igc_ts_sdp_sel_fc0[IGC_N_SDP] = {
  131. IGC_TS_SDP0_SEL_FC0, IGC_TS_SDP1_SEL_FC0,
  132. IGC_TS_SDP2_SEL_FC0, IGC_TS_SDP3_SEL_FC0,
  133. };
  134. static const u32 igc_ts_sdp_sel_fc1[IGC_N_SDP] = {
  135. IGC_TS_SDP0_SEL_FC1, IGC_TS_SDP1_SEL_FC1,
  136. IGC_TS_SDP2_SEL_FC1, IGC_TS_SDP3_SEL_FC1,
  137. };
  138. static const u32 igc_ts_sdp_sel_clr[IGC_N_SDP] = {
  139. IGC_TS_SDP0_SEL_FC1, IGC_TS_SDP1_SEL_FC1,
  140. IGC_TS_SDP2_SEL_FC1, IGC_TS_SDP3_SEL_FC1,
  141. };
  142. struct igc_hw *hw = &igc->hw;
  143. u32 ctrl, ctrl_ext, tssdp = 0;
  144. ctrl = rd32(IGC_CTRL);
  145. ctrl_ext = rd32(IGC_CTRL_EXT);
  146. tssdp = rd32(IGC_TSSDP);
  147. igc_pin_direction(pin, 0, &ctrl, &ctrl_ext);
  148. /* Make sure this pin is not enabled as an input. */
  149. if ((tssdp & IGC_AUX0_SEL_SDP3) == igc_aux0_sel_sdp[pin])
  150. tssdp &= ~IGC_AUX0_TS_SDP_EN;
  151. if ((tssdp & IGC_AUX1_SEL_SDP3) == igc_aux1_sel_sdp[pin])
  152. tssdp &= ~IGC_AUX1_TS_SDP_EN;
  153. tssdp &= ~igc_ts_sdp_sel_clr[pin];
  154. if (freq) {
  155. if (chan == 1)
  156. tssdp |= igc_ts_sdp_sel_fc1[pin];
  157. else
  158. tssdp |= igc_ts_sdp_sel_fc0[pin];
  159. } else {
  160. if (chan == 1)
  161. tssdp |= igc_ts_sdp_sel_tt1[pin];
  162. else
  163. tssdp |= igc_ts_sdp_sel_tt0[pin];
  164. }
  165. tssdp |= igc_ts_sdp_en[pin];
  166. wr32(IGC_TSSDP, tssdp);
  167. wr32(IGC_CTRL, ctrl);
  168. wr32(IGC_CTRL_EXT, ctrl_ext);
  169. }
  170. static void igc_pin_extts(struct igc_adapter *igc, int chan, int pin)
  171. {
  172. static const u32 igc_aux0_sel_sdp[IGC_N_SDP] = {
  173. IGC_AUX0_SEL_SDP0, IGC_AUX0_SEL_SDP1, IGC_AUX0_SEL_SDP2, IGC_AUX0_SEL_SDP3,
  174. };
  175. static const u32 igc_aux1_sel_sdp[IGC_N_SDP] = {
  176. IGC_AUX1_SEL_SDP0, IGC_AUX1_SEL_SDP1, IGC_AUX1_SEL_SDP2, IGC_AUX1_SEL_SDP3,
  177. };
  178. static const u32 igc_ts_sdp_en[IGC_N_SDP] = {
  179. IGC_TS_SDP0_EN, IGC_TS_SDP1_EN, IGC_TS_SDP2_EN, IGC_TS_SDP3_EN,
  180. };
  181. struct igc_hw *hw = &igc->hw;
  182. u32 ctrl, ctrl_ext, tssdp = 0;
  183. ctrl = rd32(IGC_CTRL);
  184. ctrl_ext = rd32(IGC_CTRL_EXT);
  185. tssdp = rd32(IGC_TSSDP);
  186. igc_pin_direction(pin, 1, &ctrl, &ctrl_ext);
  187. /* Make sure this pin is not enabled as an output. */
  188. tssdp &= ~igc_ts_sdp_en[pin];
  189. if (chan == 1) {
  190. tssdp &= ~IGC_AUX1_SEL_SDP3;
  191. tssdp |= igc_aux1_sel_sdp[pin] | IGC_AUX1_TS_SDP_EN;
  192. } else {
  193. tssdp &= ~IGC_AUX0_SEL_SDP3;
  194. tssdp |= igc_aux0_sel_sdp[pin] | IGC_AUX0_TS_SDP_EN;
  195. }
  196. wr32(IGC_TSSDP, tssdp);
  197. wr32(IGC_CTRL, ctrl);
  198. wr32(IGC_CTRL_EXT, ctrl_ext);
  199. }
  200. static int igc_ptp_feature_enable_i225(struct ptp_clock_info *ptp,
  201. struct ptp_clock_request *rq, int on)
  202. {
  203. struct igc_adapter *igc =
  204. container_of(ptp, struct igc_adapter, ptp_caps);
  205. struct igc_hw *hw = &igc->hw;
  206. unsigned long flags;
  207. struct timespec64 ts;
  208. int use_freq = 0, pin = -1;
  209. u32 tsim, tsauxc, tsauxc_mask, tsim_mask, trgttiml, trgttimh, freqout;
  210. s64 ns;
  211. switch (rq->type) {
  212. case PTP_CLK_REQ_EXTTS:
  213. /* Reject requests failing to enable both edges. */
  214. if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
  215. (rq->extts.flags & PTP_ENABLE_FEATURE) &&
  216. (rq->extts.flags & PTP_EXTTS_EDGES) != PTP_EXTTS_EDGES)
  217. return -EOPNOTSUPP;
  218. if (on) {
  219. pin = ptp_find_pin(igc->ptp_clock, PTP_PF_EXTTS,
  220. rq->extts.index);
  221. if (pin < 0)
  222. return -EBUSY;
  223. }
  224. if (rq->extts.index == 1) {
  225. tsauxc_mask = IGC_TSAUXC_EN_TS1;
  226. tsim_mask = IGC_TSICR_AUTT1;
  227. } else {
  228. tsauxc_mask = IGC_TSAUXC_EN_TS0;
  229. tsim_mask = IGC_TSICR_AUTT0;
  230. }
  231. spin_lock_irqsave(&igc->tmreg_lock, flags);
  232. tsauxc = rd32(IGC_TSAUXC);
  233. tsim = rd32(IGC_TSIM);
  234. if (on) {
  235. igc_pin_extts(igc, rq->extts.index, pin);
  236. tsauxc |= tsauxc_mask;
  237. tsim |= tsim_mask;
  238. } else {
  239. tsauxc &= ~tsauxc_mask;
  240. tsim &= ~tsim_mask;
  241. }
  242. wr32(IGC_TSAUXC, tsauxc);
  243. wr32(IGC_TSIM, tsim);
  244. spin_unlock_irqrestore(&igc->tmreg_lock, flags);
  245. return 0;
  246. case PTP_CLK_REQ_PEROUT:
  247. if (on) {
  248. pin = ptp_find_pin(igc->ptp_clock, PTP_PF_PEROUT,
  249. rq->perout.index);
  250. if (pin < 0)
  251. return -EBUSY;
  252. }
  253. ts.tv_sec = rq->perout.period.sec;
  254. ts.tv_nsec = rq->perout.period.nsec;
  255. ns = timespec64_to_ns(&ts);
  256. ns = ns >> 1;
  257. if (on && (ns <= 70000000LL || ns == 125000000LL ||
  258. ns == 250000000LL || ns == 500000000LL)) {
  259. if (ns < 8LL)
  260. return -EINVAL;
  261. use_freq = 1;
  262. }
  263. ts = ns_to_timespec64(ns);
  264. if (rq->perout.index == 1) {
  265. if (use_freq) {
  266. tsauxc_mask = IGC_TSAUXC_EN_CLK1 | IGC_TSAUXC_ST1;
  267. tsim_mask = 0;
  268. } else {
  269. tsauxc_mask = IGC_TSAUXC_EN_TT1;
  270. tsim_mask = IGC_TSICR_TT1;
  271. }
  272. trgttiml = IGC_TRGTTIML1;
  273. trgttimh = IGC_TRGTTIMH1;
  274. freqout = IGC_FREQOUT1;
  275. } else {
  276. if (use_freq) {
  277. tsauxc_mask = IGC_TSAUXC_EN_CLK0 | IGC_TSAUXC_ST0;
  278. tsim_mask = 0;
  279. } else {
  280. tsauxc_mask = IGC_TSAUXC_EN_TT0;
  281. tsim_mask = IGC_TSICR_TT0;
  282. }
  283. trgttiml = IGC_TRGTTIML0;
  284. trgttimh = IGC_TRGTTIMH0;
  285. freqout = IGC_FREQOUT0;
  286. }
  287. spin_lock_irqsave(&igc->tmreg_lock, flags);
  288. tsauxc = rd32(IGC_TSAUXC);
  289. tsim = rd32(IGC_TSIM);
  290. if (rq->perout.index == 1) {
  291. tsauxc &= ~(IGC_TSAUXC_EN_TT1 | IGC_TSAUXC_EN_CLK1 |
  292. IGC_TSAUXC_ST1);
  293. tsim &= ~IGC_TSICR_TT1;
  294. } else {
  295. tsauxc &= ~(IGC_TSAUXC_EN_TT0 | IGC_TSAUXC_EN_CLK0 |
  296. IGC_TSAUXC_ST0);
  297. tsim &= ~IGC_TSICR_TT0;
  298. }
  299. if (on) {
  300. struct timespec64 safe_start;
  301. int i = rq->perout.index;
  302. igc_pin_perout(igc, i, pin, use_freq);
  303. igc_ptp_read(igc, &safe_start);
  304. /* PPS output start time is triggered by Target time(TT)
  305. * register. Programming any past time value into TT
  306. * register will cause PPS to never start. Need to make
  307. * sure we program the TT register a time ahead in
  308. * future. There isn't a stringent need to fire PPS out
  309. * right away. Adding +2 seconds should take care of
  310. * corner cases. Let's say if the SYSTIML is close to
  311. * wrap up and the timer keeps ticking as we program the
  312. * register, adding +2seconds is safe bet.
  313. */
  314. safe_start.tv_sec += 2;
  315. if (rq->perout.start.sec < safe_start.tv_sec)
  316. igc->perout[i].start.tv_sec = safe_start.tv_sec;
  317. else
  318. igc->perout[i].start.tv_sec = rq->perout.start.sec;
  319. igc->perout[i].start.tv_nsec = rq->perout.start.nsec;
  320. igc->perout[i].period.tv_sec = ts.tv_sec;
  321. igc->perout[i].period.tv_nsec = ts.tv_nsec;
  322. wr32(trgttimh, (u32)igc->perout[i].start.tv_sec);
  323. /* For now, always select timer 0 as source. */
  324. wr32(trgttiml, (u32)(igc->perout[i].start.tv_nsec |
  325. IGC_TT_IO_TIMER_SEL_SYSTIM0));
  326. if (use_freq)
  327. wr32(freqout, ns);
  328. tsauxc |= tsauxc_mask;
  329. tsim |= tsim_mask;
  330. }
  331. wr32(IGC_TSAUXC, tsauxc);
  332. wr32(IGC_TSIM, tsim);
  333. spin_unlock_irqrestore(&igc->tmreg_lock, flags);
  334. return 0;
  335. case PTP_CLK_REQ_PPS:
  336. spin_lock_irqsave(&igc->tmreg_lock, flags);
  337. tsim = rd32(IGC_TSIM);
  338. if (on)
  339. tsim |= IGC_TSICR_SYS_WRAP;
  340. else
  341. tsim &= ~IGC_TSICR_SYS_WRAP;
  342. igc->pps_sys_wrap_on = on;
  343. wr32(IGC_TSIM, tsim);
  344. spin_unlock_irqrestore(&igc->tmreg_lock, flags);
  345. return 0;
  346. default:
  347. break;
  348. }
  349. return -EOPNOTSUPP;
  350. }
  351. static int igc_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
  352. enum ptp_pin_function func, unsigned int chan)
  353. {
  354. switch (func) {
  355. case PTP_PF_NONE:
  356. case PTP_PF_EXTTS:
  357. case PTP_PF_PEROUT:
  358. break;
  359. case PTP_PF_PHYSYNC:
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. /**
  365. * igc_ptp_systim_to_hwtstamp - convert system time value to HW timestamp
  366. * @adapter: board private structure
  367. * @hwtstamps: timestamp structure to update
  368. * @systim: unsigned 64bit system time value
  369. *
  370. * We need to convert the system time value stored in the RX/TXSTMP registers
  371. * into a hwtstamp which can be used by the upper level timestamping functions.
  372. *
  373. * Returns 0 on success.
  374. **/
  375. static int igc_ptp_systim_to_hwtstamp(struct igc_adapter *adapter,
  376. struct skb_shared_hwtstamps *hwtstamps,
  377. u64 systim)
  378. {
  379. switch (adapter->hw.mac.type) {
  380. case igc_i225:
  381. memset(hwtstamps, 0, sizeof(*hwtstamps));
  382. /* Upper 32 bits contain s, lower 32 bits contain ns. */
  383. hwtstamps->hwtstamp = ktime_set(systim >> 32,
  384. systim & 0xFFFFFFFF);
  385. break;
  386. default:
  387. return -EINVAL;
  388. }
  389. return 0;
  390. }
  391. /**
  392. * igc_ptp_rx_pktstamp - Retrieve timestamp from Rx packet buffer
  393. * @adapter: Pointer to adapter the packet buffer belongs to
  394. * @buf: Pointer to start of timestamp in HW format (2 32-bit words)
  395. *
  396. * This function retrieves and converts the timestamp stored at @buf
  397. * to ktime_t, adjusting for hardware latencies.
  398. *
  399. * Returns timestamp value.
  400. */
  401. ktime_t igc_ptp_rx_pktstamp(struct igc_adapter *adapter, __le32 *buf)
  402. {
  403. ktime_t timestamp;
  404. u32 secs, nsecs;
  405. int adjust;
  406. nsecs = le32_to_cpu(buf[0]);
  407. secs = le32_to_cpu(buf[1]);
  408. timestamp = ktime_set(secs, nsecs);
  409. /* Adjust timestamp for the RX latency based on link speed */
  410. switch (adapter->link_speed) {
  411. case SPEED_10:
  412. adjust = IGC_I225_RX_LATENCY_10;
  413. break;
  414. case SPEED_100:
  415. adjust = IGC_I225_RX_LATENCY_100;
  416. break;
  417. case SPEED_1000:
  418. adjust = IGC_I225_RX_LATENCY_1000;
  419. break;
  420. case SPEED_2500:
  421. adjust = IGC_I225_RX_LATENCY_2500;
  422. break;
  423. default:
  424. adjust = 0;
  425. netdev_warn_once(adapter->netdev, "Imprecise timestamp\n");
  426. break;
  427. }
  428. return ktime_sub_ns(timestamp, adjust);
  429. }
  430. static void igc_ptp_disable_rx_timestamp(struct igc_adapter *adapter)
  431. {
  432. struct igc_hw *hw = &adapter->hw;
  433. u32 val;
  434. int i;
  435. wr32(IGC_TSYNCRXCTL, 0);
  436. for (i = 0; i < adapter->num_rx_queues; i++) {
  437. val = rd32(IGC_SRRCTL(i));
  438. val &= ~IGC_SRRCTL_TIMESTAMP;
  439. wr32(IGC_SRRCTL(i), val);
  440. }
  441. val = rd32(IGC_RXPBS);
  442. val &= ~IGC_RXPBS_CFG_TS_EN;
  443. wr32(IGC_RXPBS, val);
  444. }
  445. static void igc_ptp_enable_rx_timestamp(struct igc_adapter *adapter)
  446. {
  447. struct igc_hw *hw = &adapter->hw;
  448. u32 val;
  449. int i;
  450. val = rd32(IGC_RXPBS);
  451. val |= IGC_RXPBS_CFG_TS_EN;
  452. wr32(IGC_RXPBS, val);
  453. for (i = 0; i < adapter->num_rx_queues; i++) {
  454. val = rd32(IGC_SRRCTL(i));
  455. /* Enable retrieving timestamps from timer 0, the
  456. * "adjustable clock" and timer 1 the "free running
  457. * clock".
  458. */
  459. val |= IGC_SRRCTL_TIMER1SEL(1) | IGC_SRRCTL_TIMER0SEL(0) |
  460. IGC_SRRCTL_TIMESTAMP;
  461. wr32(IGC_SRRCTL(i), val);
  462. }
  463. val = IGC_TSYNCRXCTL_ENABLED | IGC_TSYNCRXCTL_TYPE_ALL |
  464. IGC_TSYNCRXCTL_RXSYNSIG;
  465. wr32(IGC_TSYNCRXCTL, val);
  466. }
  467. static void igc_ptp_free_tx_buffer(struct igc_adapter *adapter,
  468. struct igc_tx_timestamp_request *tstamp)
  469. {
  470. if (tstamp->buffer_type == IGC_TX_BUFFER_TYPE_XSK) {
  471. /* Release the transmit completion */
  472. tstamp->xsk_tx_buffer->xsk_pending_ts = false;
  473. /* Note: tstamp->skb and tstamp->xsk_tx_buffer are in union.
  474. * By setting tstamp->xsk_tx_buffer to NULL, tstamp->skb will
  475. * become NULL as well.
  476. */
  477. tstamp->xsk_tx_buffer = NULL;
  478. tstamp->buffer_type = 0;
  479. /* Trigger txrx interrupt for transmit completion */
  480. igc_xsk_wakeup(adapter->netdev, tstamp->xsk_queue_index,
  481. XDP_WAKEUP_TX);
  482. return;
  483. }
  484. dev_kfree_skb_any(tstamp->skb);
  485. tstamp->skb = NULL;
  486. }
  487. static void igc_ptp_clear_tx_tstamp(struct igc_adapter *adapter)
  488. {
  489. unsigned long flags;
  490. int i;
  491. spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
  492. for (i = 0; i < IGC_MAX_TX_TSTAMP_REGS; i++) {
  493. struct igc_tx_timestamp_request *tstamp = &adapter->tx_tstamp[i];
  494. if (tstamp->skb)
  495. igc_ptp_free_tx_buffer(adapter, tstamp);
  496. }
  497. spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
  498. }
  499. /**
  500. * igc_ptp_clear_xsk_tx_tstamp_queue - Clear pending XSK TX timestamps for a queue
  501. * @adapter: Board private structure
  502. * @queue_id: TX queue index to clear timestamps for
  503. *
  504. * Iterates over all TX timestamp registers and releases any pending
  505. * timestamp requests associated with the given TX queue. This is
  506. * called when an XDP pool is being disabled to ensure no stale
  507. * timestamp references remain.
  508. */
  509. void igc_ptp_clear_xsk_tx_tstamp_queue(struct igc_adapter *adapter, u16 queue_id)
  510. {
  511. unsigned long flags;
  512. int i;
  513. spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
  514. for (i = 0; i < IGC_MAX_TX_TSTAMP_REGS; i++) {
  515. struct igc_tx_timestamp_request *tstamp = &adapter->tx_tstamp[i];
  516. if (tstamp->buffer_type != IGC_TX_BUFFER_TYPE_XSK)
  517. continue;
  518. if (tstamp->xsk_queue_index != queue_id)
  519. continue;
  520. if (!tstamp->xsk_tx_buffer)
  521. continue;
  522. igc_ptp_free_tx_buffer(adapter, tstamp);
  523. }
  524. spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
  525. }
  526. static void igc_ptp_disable_tx_timestamp(struct igc_adapter *adapter)
  527. {
  528. struct igc_hw *hw = &adapter->hw;
  529. int i;
  530. /* Clear the flags first to avoid new packets to be enqueued
  531. * for TX timestamping.
  532. */
  533. for (i = 0; i < adapter->num_tx_queues; i++) {
  534. struct igc_ring *tx_ring = adapter->tx_ring[i];
  535. clear_bit(IGC_RING_FLAG_TX_HWTSTAMP, &tx_ring->flags);
  536. }
  537. /* Now we can clean the pending TX timestamp requests. */
  538. igc_ptp_clear_tx_tstamp(adapter);
  539. wr32(IGC_TSYNCTXCTL, 0);
  540. }
  541. static void igc_ptp_enable_tx_timestamp(struct igc_adapter *adapter)
  542. {
  543. struct igc_hw *hw = &adapter->hw;
  544. int i;
  545. wr32(IGC_TSYNCTXCTL, IGC_TSYNCTXCTL_ENABLED | IGC_TSYNCTXCTL_TXSYNSIG);
  546. /* Read TXSTMP registers to discard any timestamp previously stored. */
  547. rd32(IGC_TXSTMPL);
  548. rd32(IGC_TXSTMPH);
  549. /* The hardware is ready to accept TX timestamp requests,
  550. * notify the transmit path.
  551. */
  552. for (i = 0; i < adapter->num_tx_queues; i++) {
  553. struct igc_ring *tx_ring = adapter->tx_ring[i];
  554. set_bit(IGC_RING_FLAG_TX_HWTSTAMP, &tx_ring->flags);
  555. }
  556. }
  557. /**
  558. * igc_ptp_set_timestamp_mode - setup hardware for timestamping
  559. * @adapter: networking device structure
  560. * @config: hwtstamp configuration
  561. *
  562. * Return: 0 in case of success, negative errno code otherwise.
  563. */
  564. static int igc_ptp_set_timestamp_mode(struct igc_adapter *adapter,
  565. struct kernel_hwtstamp_config *config)
  566. {
  567. switch (config->tx_type) {
  568. case HWTSTAMP_TX_OFF:
  569. igc_ptp_disable_tx_timestamp(adapter);
  570. break;
  571. case HWTSTAMP_TX_ON:
  572. igc_ptp_enable_tx_timestamp(adapter);
  573. break;
  574. default:
  575. return -ERANGE;
  576. }
  577. switch (config->rx_filter) {
  578. case HWTSTAMP_FILTER_NONE:
  579. igc_ptp_disable_rx_timestamp(adapter);
  580. break;
  581. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  582. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  583. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  584. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  585. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  586. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  587. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  588. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  589. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  590. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  591. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  592. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  593. case HWTSTAMP_FILTER_NTP_ALL:
  594. case HWTSTAMP_FILTER_ALL:
  595. igc_ptp_enable_rx_timestamp(adapter);
  596. config->rx_filter = HWTSTAMP_FILTER_ALL;
  597. break;
  598. default:
  599. return -ERANGE;
  600. }
  601. return 0;
  602. }
  603. /* Requires adapter->ptp_tx_lock held by caller. */
  604. static void igc_ptp_tx_timeout(struct igc_adapter *adapter,
  605. struct igc_tx_timestamp_request *tstamp)
  606. {
  607. if (tstamp->skb)
  608. igc_ptp_free_tx_buffer(adapter, tstamp);
  609. adapter->tx_hwtstamp_timeouts++;
  610. netdev_warn(adapter->netdev, "Tx timestamp timeout\n");
  611. }
  612. void igc_ptp_tx_hang(struct igc_adapter *adapter)
  613. {
  614. struct igc_tx_timestamp_request *tstamp;
  615. struct igc_hw *hw = &adapter->hw;
  616. unsigned long flags;
  617. bool found = false;
  618. int i;
  619. spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
  620. for (i = 0; i < IGC_MAX_TX_TSTAMP_REGS; i++) {
  621. tstamp = &adapter->tx_tstamp[i];
  622. if (!tstamp->skb)
  623. continue;
  624. if (time_is_after_jiffies(tstamp->start + IGC_PTP_TX_TIMEOUT))
  625. continue;
  626. igc_ptp_tx_timeout(adapter, tstamp);
  627. found = true;
  628. }
  629. if (found) {
  630. /* Reading the high register of the first set of timestamp registers
  631. * clears all the equivalent bits in the TSYNCTXCTL register.
  632. */
  633. rd32(IGC_TXSTMPH_0);
  634. }
  635. spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
  636. }
  637. static void igc_ptp_tx_reg_to_stamp(struct igc_adapter *adapter,
  638. struct igc_tx_timestamp_request *tstamp, u64 regval)
  639. {
  640. struct skb_shared_hwtstamps shhwtstamps;
  641. struct sk_buff *skb;
  642. int adjust = 0;
  643. skb = tstamp->skb;
  644. if (!skb)
  645. return;
  646. if (igc_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval))
  647. return;
  648. switch (adapter->link_speed) {
  649. case SPEED_10:
  650. adjust = IGC_I225_TX_LATENCY_10;
  651. break;
  652. case SPEED_100:
  653. adjust = IGC_I225_TX_LATENCY_100;
  654. break;
  655. case SPEED_1000:
  656. adjust = IGC_I225_TX_LATENCY_1000;
  657. break;
  658. case SPEED_2500:
  659. adjust = IGC_I225_TX_LATENCY_2500;
  660. break;
  661. }
  662. shhwtstamps.hwtstamp =
  663. ktime_add_ns(shhwtstamps.hwtstamp, adjust);
  664. /* Copy the tx hardware timestamp into xdp metadata or skb */
  665. if (tstamp->buffer_type == IGC_TX_BUFFER_TYPE_XSK) {
  666. struct xsk_buff_pool *xsk_pool;
  667. xsk_pool = adapter->tx_ring[tstamp->xsk_queue_index]->xsk_pool;
  668. if (xsk_pool && xp_tx_metadata_enabled(xsk_pool)) {
  669. xsk_tx_metadata_complete(&tstamp->xsk_meta,
  670. &igc_xsk_tx_metadata_ops,
  671. &shhwtstamps.hwtstamp);
  672. }
  673. } else {
  674. skb_tstamp_tx(skb, &shhwtstamps);
  675. }
  676. igc_ptp_free_tx_buffer(adapter, tstamp);
  677. }
  678. /**
  679. * igc_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  680. * @adapter: Board private structure
  681. *
  682. * Check against the ready mask for which of the timestamp register
  683. * sets are ready to be retrieved, then retrieve that and notify the
  684. * rest of the stack.
  685. *
  686. * Context: Expects adapter->ptp_tx_lock to be held by caller.
  687. */
  688. static void igc_ptp_tx_hwtstamp(struct igc_adapter *adapter)
  689. {
  690. struct igc_hw *hw = &adapter->hw;
  691. u32 txstmpl_old;
  692. u64 regval;
  693. u32 mask;
  694. int i;
  695. /* Establish baseline of TXSTMPL_0 before checking TXTT_0.
  696. * This baseline is used to detect if a new timestamp arrives in
  697. * register 0 during the hardware bug workaround below.
  698. */
  699. txstmpl_old = rd32(IGC_TXSTMPL);
  700. mask = rd32(IGC_TSYNCTXCTL) & IGC_TSYNCTXCTL_TXTT_ANY;
  701. if (mask & IGC_TSYNCTXCTL_TXTT_0) {
  702. regval = rd32(IGC_TXSTMPL);
  703. regval |= (u64)rd32(IGC_TXSTMPH) << 32;
  704. } else {
  705. /* TXTT_0 not set - register 0 has no new timestamp initially.
  706. *
  707. * Hardware bug: Future timestamp interrupts won't fire unless
  708. * TXSTMPH_0 is read, even if the timestamp was captured in
  709. * registers 1-3.
  710. *
  711. * Workaround: Read TXSTMPH_0 here to enable future interrupts.
  712. * However, this read clears TXTT_0. If a timestamp arrives in
  713. * register 0 after checking TXTT_0 but before this read, it
  714. * would be lost.
  715. *
  716. * To detect this race: We saved a baseline read of TXSTMPL_0
  717. * before TXTT_0 check. After performing the workaround read of
  718. * TXSTMPH_0, we read TXSTMPL_0 again. Since consecutive
  719. * timestamps never share the same nanosecond value, a change
  720. * between the baseline and new TXSTMPL_0 indicates a timestamp
  721. * arrived during the race window. If so, read the complete
  722. * timestamp.
  723. */
  724. u32 txstmpl_new;
  725. rd32(IGC_TXSTMPH);
  726. txstmpl_new = rd32(IGC_TXSTMPL);
  727. if (txstmpl_old == txstmpl_new)
  728. goto done;
  729. regval = txstmpl_new;
  730. regval |= (u64)rd32(IGC_TXSTMPH) << 32;
  731. }
  732. igc_ptp_tx_reg_to_stamp(adapter, &adapter->tx_tstamp[0], regval);
  733. done:
  734. /* Now that the problematic first register was handled, we can
  735. * retrieve the timestamps from the other registers
  736. * (starting from '1') with less complications.
  737. */
  738. for (i = 1; i < IGC_MAX_TX_TSTAMP_REGS; i++) {
  739. struct igc_tx_timestamp_request *tstamp = &adapter->tx_tstamp[i];
  740. if (!(tstamp->mask & mask))
  741. continue;
  742. regval = rd32(tstamp->regl);
  743. regval |= (u64)rd32(tstamp->regh) << 32;
  744. igc_ptp_tx_reg_to_stamp(adapter, tstamp, regval);
  745. }
  746. }
  747. /**
  748. * igc_ptp_tx_tstamp_event
  749. * @adapter: board private structure
  750. *
  751. * Called when a TX timestamp interrupt happens to retrieve the
  752. * timestamp and send it up to the socket.
  753. */
  754. void igc_ptp_tx_tstamp_event(struct igc_adapter *adapter)
  755. {
  756. unsigned long flags;
  757. spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
  758. igc_ptp_tx_hwtstamp(adapter);
  759. spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
  760. }
  761. /**
  762. * igc_ptp_hwtstamp_set - set hardware time stamping config
  763. * @netdev: network interface device structure
  764. * @config: timestamping configuration structure
  765. * @extack: netlink extended ack structure for error reporting
  766. *
  767. **/
  768. int igc_ptp_hwtstamp_set(struct net_device *netdev,
  769. struct kernel_hwtstamp_config *config,
  770. struct netlink_ext_ack *extack)
  771. {
  772. struct igc_adapter *adapter = netdev_priv(netdev);
  773. int err;
  774. err = igc_ptp_set_timestamp_mode(adapter, config);
  775. if (err)
  776. return err;
  777. /* save these settings for future reference */
  778. adapter->tstamp_config = *config;
  779. return 0;
  780. }
  781. /**
  782. * igc_ptp_hwtstamp_get - get hardware time stamping config
  783. * @netdev: network interface device structure
  784. * @config: timestamping configuration structure
  785. *
  786. * Get the hwtstamp_config settings to return to the user. Rather than attempt
  787. * to deconstruct the settings from the registers, just return a shadow copy
  788. * of the last known settings.
  789. **/
  790. int igc_ptp_hwtstamp_get(struct net_device *netdev,
  791. struct kernel_hwtstamp_config *config)
  792. {
  793. struct igc_adapter *adapter = netdev_priv(netdev);
  794. *config = adapter->tstamp_config;
  795. return 0;
  796. }
  797. /* The two conditions below must be met for cross timestamping via
  798. * PCIe PTM:
  799. *
  800. * 1. We have an way to convert the timestamps in the PTM messages
  801. * to something related to the system clocks (right now, only
  802. * X86 systems with support for the Always Running Timer allow that);
  803. *
  804. * 2. We have PTM enabled in the path from the device to the PCIe root port.
  805. */
  806. static bool igc_is_crosststamp_supported(struct igc_adapter *adapter)
  807. {
  808. if (!IS_ENABLED(CONFIG_X86_TSC))
  809. return false;
  810. /* FIXME: it was noticed that enabling support for PCIe PTM in
  811. * some i225-V models could cause lockups when bringing the
  812. * interface up/down. There should be no downsides to
  813. * disabling crosstimestamping support for i225-V, as it
  814. * doesn't have any PTP support. That way we gain some time
  815. * while root causing the issue.
  816. */
  817. if (adapter->pdev->device == IGC_DEV_ID_I225_V)
  818. return false;
  819. return pcie_ptm_enabled(adapter->pdev);
  820. }
  821. static struct system_counterval_t igc_device_tstamp_to_system(u64 tstamp)
  822. {
  823. #if IS_ENABLED(CONFIG_X86_TSC) && !defined(CONFIG_UML)
  824. return (struct system_counterval_t) {
  825. .cs_id = CSID_X86_ART,
  826. .cycles = tstamp,
  827. .use_nsecs = true,
  828. };
  829. #else
  830. return (struct system_counterval_t) { };
  831. #endif
  832. }
  833. static void igc_ptm_log_error(struct igc_adapter *adapter, u32 ptm_stat)
  834. {
  835. struct net_device *netdev = adapter->netdev;
  836. switch (ptm_stat) {
  837. case IGC_PTM_STAT_RET_ERR:
  838. netdev_err(netdev, "PTM Error: Root port timeout\n");
  839. break;
  840. case IGC_PTM_STAT_BAD_PTM_RES:
  841. netdev_err(netdev, "PTM Error: Bad response, PTM Response Data expected\n");
  842. break;
  843. case IGC_PTM_STAT_T4M1_OVFL:
  844. netdev_err(netdev, "PTM Error: T4 minus T1 overflow\n");
  845. break;
  846. case IGC_PTM_STAT_ADJUST_1ST:
  847. netdev_err(netdev, "PTM Error: 1588 timer adjusted during first PTM cycle\n");
  848. break;
  849. case IGC_PTM_STAT_ADJUST_CYC:
  850. netdev_err(netdev, "PTM Error: 1588 timer adjusted during non-first PTM cycle\n");
  851. break;
  852. default:
  853. netdev_err(netdev, "PTM Error: Unknown error (%#x)\n", ptm_stat);
  854. break;
  855. }
  856. }
  857. /* The PTM lock: adapter->ptm_lock must be held when calling igc_ptm_trigger() */
  858. static void igc_ptm_trigger(struct igc_hw *hw)
  859. {
  860. u32 ctrl;
  861. /* To "manually" start the PTM cycle we need to set the
  862. * trigger (TRIG) bit
  863. */
  864. ctrl = rd32(IGC_PTM_CTRL);
  865. ctrl |= IGC_PTM_CTRL_TRIG;
  866. wr32(IGC_PTM_CTRL, ctrl);
  867. /* Perform flush after write to CTRL register otherwise
  868. * transaction may not start
  869. */
  870. wrfl();
  871. }
  872. /* The PTM lock: adapter->ptm_lock must be held when calling igc_ptm_reset() */
  873. static void igc_ptm_reset(struct igc_hw *hw)
  874. {
  875. u32 ctrl;
  876. ctrl = rd32(IGC_PTM_CTRL);
  877. ctrl &= ~IGC_PTM_CTRL_TRIG;
  878. wr32(IGC_PTM_CTRL, ctrl);
  879. /* Write to clear all status */
  880. wr32(IGC_PTM_STAT, IGC_PTM_STAT_ALL);
  881. }
  882. static int igc_phc_get_syncdevicetime(ktime_t *device,
  883. struct system_counterval_t *system,
  884. void *ctx)
  885. {
  886. struct igc_adapter *adapter = ctx;
  887. struct igc_hw *hw = &adapter->hw;
  888. u32 stat, t2_curr_h, t2_curr_l;
  889. int err, count = 100;
  890. ktime_t t1, t2_curr;
  891. /* Doing this in a loop because in the event of a
  892. * badly timed (ha!) system clock adjustment, we may
  893. * get PTM errors from the PCI root, but these errors
  894. * are transitory. Repeating the process returns valid
  895. * data eventually.
  896. */
  897. do {
  898. /* Get a snapshot of system clocks to use as historic value. */
  899. ktime_get_snapshot(&adapter->snapshot);
  900. igc_ptm_trigger(hw);
  901. err = readx_poll_timeout(rd32, IGC_PTM_STAT, stat,
  902. stat, IGC_PTM_STAT_SLEEP,
  903. IGC_PTM_STAT_TIMEOUT);
  904. igc_ptm_reset(hw);
  905. if (err < 0) {
  906. netdev_err(adapter->netdev, "Timeout reading IGC_PTM_STAT register\n");
  907. return err;
  908. }
  909. if ((stat & IGC_PTM_STAT_VALID) == IGC_PTM_STAT_VALID)
  910. break;
  911. igc_ptm_log_error(adapter, stat);
  912. } while (--count);
  913. if (!count) {
  914. netdev_err(adapter->netdev, "Exceeded number of tries for PTM cycle\n");
  915. return -ETIMEDOUT;
  916. }
  917. t1 = ktime_set(rd32(IGC_PTM_T1_TIM0_H), rd32(IGC_PTM_T1_TIM0_L));
  918. t2_curr_l = rd32(IGC_PTM_CURR_T2_L);
  919. t2_curr_h = rd32(IGC_PTM_CURR_T2_H);
  920. /* FIXME: When the register that tells the endianness of the
  921. * PTM registers are implemented, check them here and add the
  922. * appropriate conversion.
  923. */
  924. t2_curr_h = swab32(t2_curr_h);
  925. t2_curr = ((s64)t2_curr_h << 32 | t2_curr_l);
  926. *device = t1;
  927. *system = igc_device_tstamp_to_system(t2_curr);
  928. return 0;
  929. }
  930. static int igc_ptp_getcrosststamp(struct ptp_clock_info *ptp,
  931. struct system_device_crosststamp *cts)
  932. {
  933. struct igc_adapter *adapter = container_of(ptp, struct igc_adapter,
  934. ptp_caps);
  935. int ret;
  936. /* This blocks until any in progress PTM transactions complete */
  937. mutex_lock(&adapter->ptm_lock);
  938. ret = get_device_system_crosststamp(igc_phc_get_syncdevicetime,
  939. adapter, &adapter->snapshot, cts);
  940. mutex_unlock(&adapter->ptm_lock);
  941. return ret;
  942. }
  943. static int igc_ptp_getcyclesx64(struct ptp_clock_info *ptp,
  944. struct timespec64 *ts,
  945. struct ptp_system_timestamp *sts)
  946. {
  947. struct igc_adapter *igc = container_of(ptp, struct igc_adapter, ptp_caps);
  948. struct igc_hw *hw = &igc->hw;
  949. unsigned long flags;
  950. spin_lock_irqsave(&igc->free_timer_lock, flags);
  951. ptp_read_system_prets(sts);
  952. ts->tv_nsec = rd32(IGC_SYSTIML_1);
  953. ts->tv_sec = rd32(IGC_SYSTIMH_1);
  954. ptp_read_system_postts(sts);
  955. spin_unlock_irqrestore(&igc->free_timer_lock, flags);
  956. return 0;
  957. }
  958. /**
  959. * igc_ptp_init - Initialize PTP functionality
  960. * @adapter: Board private structure
  961. *
  962. * This function is called at device probe to initialize the PTP
  963. * functionality.
  964. */
  965. void igc_ptp_init(struct igc_adapter *adapter)
  966. {
  967. struct net_device *netdev = adapter->netdev;
  968. struct igc_tx_timestamp_request *tstamp;
  969. struct igc_hw *hw = &adapter->hw;
  970. int i;
  971. tstamp = &adapter->tx_tstamp[0];
  972. tstamp->mask = IGC_TSYNCTXCTL_TXTT_0;
  973. tstamp->regl = IGC_TXSTMPL_0;
  974. tstamp->regh = IGC_TXSTMPH_0;
  975. tstamp->flags = 0;
  976. tstamp = &adapter->tx_tstamp[1];
  977. tstamp->mask = IGC_TSYNCTXCTL_TXTT_1;
  978. tstamp->regl = IGC_TXSTMPL_1;
  979. tstamp->regh = IGC_TXSTMPH_1;
  980. tstamp->flags = IGC_TX_FLAGS_TSTAMP_1;
  981. tstamp = &adapter->tx_tstamp[2];
  982. tstamp->mask = IGC_TSYNCTXCTL_TXTT_2;
  983. tstamp->regl = IGC_TXSTMPL_2;
  984. tstamp->regh = IGC_TXSTMPH_2;
  985. tstamp->flags = IGC_TX_FLAGS_TSTAMP_2;
  986. tstamp = &adapter->tx_tstamp[3];
  987. tstamp->mask = IGC_TSYNCTXCTL_TXTT_3;
  988. tstamp->regl = IGC_TXSTMPL_3;
  989. tstamp->regh = IGC_TXSTMPH_3;
  990. tstamp->flags = IGC_TX_FLAGS_TSTAMP_3;
  991. switch (hw->mac.type) {
  992. case igc_i225:
  993. for (i = 0; i < IGC_N_SDP; i++) {
  994. struct ptp_pin_desc *ppd = &adapter->sdp_config[i];
  995. snprintf(ppd->name, sizeof(ppd->name), "SDP%d", i);
  996. ppd->index = i;
  997. ppd->func = PTP_PF_NONE;
  998. }
  999. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  1000. adapter->ptp_caps.owner = THIS_MODULE;
  1001. adapter->ptp_caps.max_adj = 62499999;
  1002. adapter->ptp_caps.adjfine = igc_ptp_adjfine_i225;
  1003. adapter->ptp_caps.adjtime = igc_ptp_adjtime_i225;
  1004. adapter->ptp_caps.gettimex64 = igc_ptp_gettimex64_i225;
  1005. adapter->ptp_caps.getcyclesx64 = igc_ptp_getcyclesx64;
  1006. adapter->ptp_caps.settime64 = igc_ptp_settime_i225;
  1007. adapter->ptp_caps.enable = igc_ptp_feature_enable_i225;
  1008. adapter->ptp_caps.pps = 1;
  1009. adapter->ptp_caps.pin_config = adapter->sdp_config;
  1010. adapter->ptp_caps.n_ext_ts = IGC_N_EXTTS;
  1011. adapter->ptp_caps.n_per_out = IGC_N_PEROUT;
  1012. adapter->ptp_caps.supported_extts_flags = PTP_RISING_EDGE |
  1013. PTP_FALLING_EDGE |
  1014. PTP_STRICT_FLAGS;
  1015. adapter->ptp_caps.n_pins = IGC_N_SDP;
  1016. adapter->ptp_caps.verify = igc_ptp_verify_pin;
  1017. if (!igc_is_crosststamp_supported(adapter))
  1018. break;
  1019. adapter->ptp_caps.getcrosststamp = igc_ptp_getcrosststamp;
  1020. break;
  1021. default:
  1022. adapter->ptp_clock = NULL;
  1023. return;
  1024. }
  1025. spin_lock_init(&adapter->ptp_tx_lock);
  1026. spin_lock_init(&adapter->free_timer_lock);
  1027. spin_lock_init(&adapter->tmreg_lock);
  1028. mutex_init(&adapter->ptm_lock);
  1029. adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  1030. adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  1031. adapter->prev_ptp_time = ktime_to_timespec64(ktime_get_real());
  1032. adapter->ptp_reset_start = ktime_get();
  1033. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  1034. &adapter->pdev->dev);
  1035. if (IS_ERR(adapter->ptp_clock)) {
  1036. adapter->ptp_clock = NULL;
  1037. netdev_err(netdev, "ptp_clock_register failed\n");
  1038. mutex_destroy(&adapter->ptm_lock);
  1039. } else if (adapter->ptp_clock) {
  1040. netdev_info(netdev, "PHC added\n");
  1041. adapter->ptp_flags |= IGC_PTP_ENABLED;
  1042. }
  1043. }
  1044. static void igc_ptp_time_save(struct igc_adapter *adapter)
  1045. {
  1046. igc_ptp_read(adapter, &adapter->prev_ptp_time);
  1047. adapter->ptp_reset_start = ktime_get();
  1048. }
  1049. static void igc_ptp_time_restore(struct igc_adapter *adapter)
  1050. {
  1051. struct timespec64 ts = adapter->prev_ptp_time;
  1052. ktime_t delta;
  1053. delta = ktime_sub(ktime_get(), adapter->ptp_reset_start);
  1054. timespec64_add_ns(&ts, ktime_to_ns(delta));
  1055. igc_ptp_write_i225(adapter, &ts);
  1056. }
  1057. static void igc_ptm_stop(struct igc_adapter *adapter)
  1058. {
  1059. struct igc_hw *hw = &adapter->hw;
  1060. u32 ctrl;
  1061. mutex_lock(&adapter->ptm_lock);
  1062. ctrl = rd32(IGC_PTM_CTRL);
  1063. ctrl &= ~IGC_PTM_CTRL_EN;
  1064. wr32(IGC_PTM_CTRL, ctrl);
  1065. mutex_unlock(&adapter->ptm_lock);
  1066. }
  1067. /**
  1068. * igc_ptp_suspend - Disable PTP work items and prepare for suspend
  1069. * @adapter: Board private structure
  1070. *
  1071. * This function stops the overflow check work and PTP Tx timestamp work, and
  1072. * will prepare the device for OS suspend.
  1073. */
  1074. void igc_ptp_suspend(struct igc_adapter *adapter)
  1075. {
  1076. if (!(adapter->ptp_flags & IGC_PTP_ENABLED))
  1077. return;
  1078. igc_ptp_clear_tx_tstamp(adapter);
  1079. if (pci_device_is_present(adapter->pdev)) {
  1080. igc_ptp_time_save(adapter);
  1081. igc_ptm_stop(adapter);
  1082. }
  1083. }
  1084. /**
  1085. * igc_ptp_stop - Disable PTP device and stop the overflow check.
  1086. * @adapter: Board private structure.
  1087. *
  1088. * This function stops the PTP support and cancels the delayed work.
  1089. **/
  1090. void igc_ptp_stop(struct igc_adapter *adapter)
  1091. {
  1092. if (!(adapter->ptp_flags & IGC_PTP_ENABLED))
  1093. return;
  1094. igc_ptp_suspend(adapter);
  1095. adapter->ptp_flags &= ~IGC_PTP_ENABLED;
  1096. if (adapter->ptp_clock) {
  1097. ptp_clock_unregister(adapter->ptp_clock);
  1098. netdev_info(adapter->netdev, "PHC removed\n");
  1099. adapter->ptp_flags &= ~IGC_PTP_ENABLED;
  1100. }
  1101. mutex_destroy(&adapter->ptm_lock);
  1102. }
  1103. /**
  1104. * igc_ptp_reset - Re-enable the adapter for PTP following a reset.
  1105. * @adapter: Board private structure.
  1106. *
  1107. * This function handles the reset work required to re-enable the PTP device.
  1108. **/
  1109. void igc_ptp_reset(struct igc_adapter *adapter)
  1110. {
  1111. struct igc_hw *hw = &adapter->hw;
  1112. u32 cycle_ctrl, ctrl, stat;
  1113. unsigned long flags;
  1114. u32 timadj;
  1115. if (!(adapter->ptp_flags & IGC_PTP_ENABLED))
  1116. return;
  1117. /* reset the tstamp_config */
  1118. igc_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
  1119. mutex_lock(&adapter->ptm_lock);
  1120. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  1121. switch (adapter->hw.mac.type) {
  1122. case igc_i225:
  1123. timadj = rd32(IGC_TIMADJ);
  1124. timadj |= IGC_TIMADJ_ADJUST_METH;
  1125. wr32(IGC_TIMADJ, timadj);
  1126. wr32(IGC_TSAUXC, 0x0);
  1127. wr32(IGC_TSSDP, 0x0);
  1128. wr32(IGC_TSIM,
  1129. IGC_TSICR_INTERRUPTS |
  1130. (adapter->pps_sys_wrap_on ? IGC_TSICR_SYS_WRAP : 0));
  1131. wr32(IGC_IMS, IGC_IMS_TS);
  1132. if (!igc_is_crosststamp_supported(adapter))
  1133. break;
  1134. wr32(IGC_PCIE_DIG_DELAY, IGC_PCIE_DIG_DELAY_DEFAULT);
  1135. wr32(IGC_PCIE_PHY_DELAY, IGC_PCIE_PHY_DELAY_DEFAULT);
  1136. cycle_ctrl = IGC_PTM_CYCLE_CTRL_CYC_TIME(IGC_PTM_CYC_TIME_DEFAULT);
  1137. wr32(IGC_PTM_CYCLE_CTRL, cycle_ctrl);
  1138. ctrl = IGC_PTM_CTRL_EN |
  1139. IGC_PTM_CTRL_START_NOW |
  1140. IGC_PTM_CTRL_SHRT_CYC(IGC_PTM_SHORT_CYC_DEFAULT) |
  1141. IGC_PTM_CTRL_PTM_TO(IGC_PTM_TIMEOUT_DEFAULT);
  1142. wr32(IGC_PTM_CTRL, ctrl);
  1143. /* Force the first cycle to run. */
  1144. igc_ptm_trigger(hw);
  1145. if (readx_poll_timeout_atomic(rd32, IGC_PTM_STAT, stat,
  1146. stat, IGC_PTM_STAT_SLEEP,
  1147. IGC_PTM_STAT_TIMEOUT))
  1148. netdev_err(adapter->netdev, "Timeout reading IGC_PTM_STAT register\n");
  1149. igc_ptm_reset(hw);
  1150. break;
  1151. default:
  1152. /* No work to do. */
  1153. goto out;
  1154. }
  1155. /* Re-initialize the timer. */
  1156. if (hw->mac.type == igc_i225) {
  1157. igc_ptp_time_restore(adapter);
  1158. } else {
  1159. timecounter_init(&adapter->tc, &adapter->cc,
  1160. ktime_to_ns(ktime_get_real()));
  1161. }
  1162. out:
  1163. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  1164. mutex_unlock(&adapter->ptm_lock);
  1165. wrfl();
  1166. }