igb_ptp.c 33 KB

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