igb_ptp.c 36 KB

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