igb_ptp.c 42 KB

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