igc_ptp.c 35 KB

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