igb_ptp.c 36 KB

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