ptp.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2011-2013 Solarflare Communications Inc.
  5. */
  6. /* Theory of operation:
  7. *
  8. * PTP support is assisted by firmware running on the MC, which provides
  9. * the hardware timestamping capabilities. Both transmitted and received
  10. * PTP event packets are queued onto internal queues for subsequent processing;
  11. * this is because the MC operations are relatively long and would block
  12. * block NAPI/interrupt operation.
  13. *
  14. * Receive event processing:
  15. * The event contains the packet's UUID and sequence number, together
  16. * with the hardware timestamp. The PTP receive packet queue is searched
  17. * for this UUID/sequence number and, if found, put on a pending queue.
  18. * Packets not matching are delivered without timestamps (MCDI events will
  19. * always arrive after the actual packet).
  20. * It is important for the operation of the PTP protocol that the ordering
  21. * of packets between the event and general port is maintained.
  22. *
  23. * Work queue processing:
  24. * If work waiting, synchronise host/hardware time
  25. *
  26. * Transmit: send packet through MC, which returns the transmission time
  27. * that is converted to an appropriate timestamp.
  28. *
  29. * Receive: the packet's reception time is converted to an appropriate
  30. * timestamp.
  31. */
  32. #include <linux/ip.h>
  33. #include <linux/udp.h>
  34. #include <linux/time.h>
  35. #include <linux/ktime.h>
  36. #include <linux/module.h>
  37. #include <linux/net_tstamp.h>
  38. #include <linux/pps_kernel.h>
  39. #include <linux/ptp_clock_kernel.h>
  40. #include "net_driver.h"
  41. #include "efx.h"
  42. #include "mcdi.h"
  43. #include "mcdi_pcol.h"
  44. #include "io.h"
  45. #include "farch_regs.h"
  46. #include "nic.h"
  47. /* Maximum number of events expected to make up a PTP event */
  48. #define MAX_EVENT_FRAGS 3
  49. /* Maximum delay, ms, to begin synchronisation */
  50. #define MAX_SYNCHRONISE_WAIT_MS 2
  51. /* How long, at most, to spend synchronising */
  52. #define SYNCHRONISE_PERIOD_NS 250000
  53. /* How often to update the shared memory time */
  54. #define SYNCHRONISATION_GRANULARITY_NS 200
  55. /* Minimum permitted length of a (corrected) synchronisation time */
  56. #define DEFAULT_MIN_SYNCHRONISATION_NS 120
  57. /* Maximum permitted length of a (corrected) synchronisation time */
  58. #define MAX_SYNCHRONISATION_NS 1000
  59. /* How many (MC) receive events that can be queued */
  60. #define MAX_RECEIVE_EVENTS 8
  61. /* Length of (modified) moving average. */
  62. #define AVERAGE_LENGTH 16
  63. /* How long an unmatched event or packet can be held */
  64. #define PKT_EVENT_LIFETIME_MS 10
  65. /* Offsets into PTP packet for identification. These offsets are from the
  66. * start of the IP header, not the MAC header. Note that neither PTP V1 nor
  67. * PTP V2 permit the use of IPV4 options.
  68. */
  69. #define PTP_DPORT_OFFSET 22
  70. #define PTP_V1_VERSION_LENGTH 2
  71. #define PTP_V1_VERSION_OFFSET 28
  72. #define PTP_V1_UUID_LENGTH 6
  73. #define PTP_V1_UUID_OFFSET 50
  74. #define PTP_V1_SEQUENCE_LENGTH 2
  75. #define PTP_V1_SEQUENCE_OFFSET 58
  76. /* The minimum length of a PTP V1 packet for offsets, etc. to be valid:
  77. * includes IP header.
  78. */
  79. #define PTP_V1_MIN_LENGTH 64
  80. #define PTP_V2_VERSION_LENGTH 1
  81. #define PTP_V2_VERSION_OFFSET 29
  82. #define PTP_V2_UUID_LENGTH 8
  83. #define PTP_V2_UUID_OFFSET 48
  84. /* Although PTP V2 UUIDs are comprised a ClockIdentity (8) and PortNumber (2),
  85. * the MC only captures the last six bytes of the clock identity. These values
  86. * reflect those, not the ones used in the standard. The standard permits
  87. * mapping of V1 UUIDs to V2 UUIDs with these same values.
  88. */
  89. #define PTP_V2_MC_UUID_LENGTH 6
  90. #define PTP_V2_MC_UUID_OFFSET 50
  91. #define PTP_V2_SEQUENCE_LENGTH 2
  92. #define PTP_V2_SEQUENCE_OFFSET 58
  93. /* The minimum length of a PTP V2 packet for offsets, etc. to be valid:
  94. * includes IP header.
  95. */
  96. #define PTP_V2_MIN_LENGTH 63
  97. #define PTP_MIN_LENGTH 63
  98. #define PTP_ADDRESS 0xe0000181 /* 224.0.1.129 */
  99. #define PTP_EVENT_PORT 319
  100. #define PTP_GENERAL_PORT 320
  101. /* Annoyingly the format of the version numbers are different between
  102. * versions 1 and 2 so it isn't possible to simply look for 1 or 2.
  103. */
  104. #define PTP_VERSION_V1 1
  105. #define PTP_VERSION_V2 2
  106. #define PTP_VERSION_V2_MASK 0x0f
  107. enum ptp_packet_state {
  108. PTP_PACKET_STATE_UNMATCHED = 0,
  109. PTP_PACKET_STATE_MATCHED,
  110. PTP_PACKET_STATE_TIMED_OUT,
  111. PTP_PACKET_STATE_MATCH_UNWANTED
  112. };
  113. /* NIC synchronised with single word of time only comprising
  114. * partial seconds and full nanoseconds: 10^9 ~ 2^30 so 2 bits for seconds.
  115. */
  116. #define MC_NANOSECOND_BITS 30
  117. #define MC_NANOSECOND_MASK ((1 << MC_NANOSECOND_BITS) - 1)
  118. #define MC_SECOND_MASK ((1 << (32 - MC_NANOSECOND_BITS)) - 1)
  119. /* Maximum parts-per-billion adjustment that is acceptable */
  120. #define MAX_PPB 1000000
  121. /* Precalculate scale word to avoid long long division at runtime */
  122. /* This is equivalent to 2^66 / 10^9. */
  123. #define PPB_SCALE_WORD ((1LL << (57)) / 1953125LL)
  124. /* How much to shift down after scaling to convert to FP40 */
  125. #define PPB_SHIFT_FP40 26
  126. /* ... and FP44. */
  127. #define PPB_SHIFT_FP44 22
  128. #define PTP_SYNC_ATTEMPTS 4
  129. /**
  130. * struct efx_ptp_match - Matching structure, stored in sk_buff's cb area.
  131. * @words: UUID and (partial) sequence number
  132. * @expiry: Time after which the packet should be delivered irrespective of
  133. * event arrival.
  134. * @state: The state of the packet - whether it is ready for processing or
  135. * whether that is of no interest.
  136. */
  137. struct efx_ptp_match {
  138. u32 words[DIV_ROUND_UP(PTP_V1_UUID_LENGTH, 4)];
  139. unsigned long expiry;
  140. enum ptp_packet_state state;
  141. };
  142. /**
  143. * struct efx_ptp_event_rx - A PTP receive event (from MC)
  144. * @seq0: First part of (PTP) UUID
  145. * @seq1: Second part of (PTP) UUID and sequence number
  146. * @hwtimestamp: Event timestamp
  147. */
  148. struct efx_ptp_event_rx {
  149. struct list_head link;
  150. u32 seq0;
  151. u32 seq1;
  152. ktime_t hwtimestamp;
  153. unsigned long expiry;
  154. };
  155. /**
  156. * struct efx_ptp_timeset - Synchronisation between host and MC
  157. * @host_start: Host time immediately before hardware timestamp taken
  158. * @major: Hardware timestamp, major
  159. * @minor: Hardware timestamp, minor
  160. * @host_end: Host time immediately after hardware timestamp taken
  161. * @wait: Number of NIC clock ticks between hardware timestamp being read and
  162. * host end time being seen
  163. * @window: Difference of host_end and host_start
  164. * @valid: Whether this timeset is valid
  165. */
  166. struct efx_ptp_timeset {
  167. u32 host_start;
  168. u32 major;
  169. u32 minor;
  170. u32 host_end;
  171. u32 wait;
  172. u32 window; /* Derived: end - start, allowing for wrap */
  173. };
  174. /**
  175. * struct efx_ptp_data - Precision Time Protocol (PTP) state
  176. * @efx: The NIC context
  177. * @channel: The PTP channel (Siena only)
  178. * @rx_ts_inline: Flag for whether RX timestamps are inline (else they are
  179. * separate events)
  180. * @rxq: Receive SKB queue (awaiting timestamps)
  181. * @txq: Transmit SKB queue
  182. * @evt_list: List of MC receive events awaiting packets
  183. * @evt_free_list: List of free events
  184. * @evt_lock: Lock for manipulating evt_list and evt_free_list
  185. * @rx_evts: Instantiated events (on evt_list and evt_free_list)
  186. * @workwq: Work queue for processing pending PTP operations
  187. * @work: Work task
  188. * @reset_required: A serious error has occurred and the PTP task needs to be
  189. * reset (disable, enable).
  190. * @rxfilter_event: Receive filter when operating
  191. * @rxfilter_general: Receive filter when operating
  192. * @config: Current timestamp configuration
  193. * @enabled: PTP operation enabled
  194. * @mode: Mode in which PTP operating (PTP version)
  195. * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time
  196. * @nic_to_kernel_time: Function to convert from NIC to kernel time
  197. * @nic_time.minor_max: Wrap point for NIC minor times
  198. * @nic_time.sync_event_diff_min: Minimum acceptable difference between time
  199. * in packet prefix and last MCDI time sync event i.e. how much earlier than
  200. * the last sync event time a packet timestamp can be.
  201. * @nic_time.sync_event_diff_max: Maximum acceptable difference between time
  202. * in packet prefix and last MCDI time sync event i.e. how much later than
  203. * the last sync event time a packet timestamp can be.
  204. * @nic_time.sync_event_minor_shift: Shift required to make minor time from
  205. * field in MCDI time sync event.
  206. * @min_synchronisation_ns: Minimum acceptable corrected sync window
  207. * @capabilities: Capabilities flags from the NIC
  208. * @ts_corrections.ptp_tx: Required driver correction of PTP packet transmit
  209. * timestamps
  210. * @ts_corrections.ptp_rx: Required driver correction of PTP packet receive
  211. * timestamps
  212. * @ts_corrections.pps_out: PPS output error (information only)
  213. * @ts_corrections.pps_in: Required driver correction of PPS input timestamps
  214. * @ts_corrections.general_tx: Required driver correction of general packet
  215. * transmit timestamps
  216. * @ts_corrections.general_rx: Required driver correction of general packet
  217. * receive timestamps
  218. * @evt_frags: Partly assembled PTP events
  219. * @evt_frag_idx: Current fragment number
  220. * @evt_code: Last event code
  221. * @start: Address at which MC indicates ready for synchronisation
  222. * @host_time_pps: Host time at last PPS
  223. * @adjfreq_ppb_shift: Shift required to convert scaled parts-per-billion
  224. * frequency adjustment into a fixed point fractional nanosecond format.
  225. * @current_adjfreq: Current ppb adjustment.
  226. * @phc_clock: Pointer to registered phc device (if primary function)
  227. * @phc_clock_info: Registration structure for phc device
  228. * @pps_work: pps work task for handling pps events
  229. * @pps_workwq: pps work queue
  230. * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
  231. * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
  232. * allocations in main data path).
  233. * @good_syncs: Number of successful synchronisations.
  234. * @fast_syncs: Number of synchronisations requiring short delay
  235. * @bad_syncs: Number of failed synchronisations.
  236. * @sync_timeouts: Number of synchronisation timeouts
  237. * @no_time_syncs: Number of synchronisations with no good times.
  238. * @invalid_sync_windows: Number of sync windows with bad durations.
  239. * @undersize_sync_windows: Number of corrected sync windows that are too small
  240. * @oversize_sync_windows: Number of corrected sync windows that are too large
  241. * @rx_no_timestamp: Number of packets received without a timestamp.
  242. * @timeset: Last set of synchronisation statistics.
  243. * @xmit_skb: Transmit SKB function.
  244. */
  245. struct efx_ptp_data {
  246. struct efx_nic *efx;
  247. struct efx_channel *channel;
  248. bool rx_ts_inline;
  249. struct sk_buff_head rxq;
  250. struct sk_buff_head txq;
  251. struct list_head evt_list;
  252. struct list_head evt_free_list;
  253. spinlock_t evt_lock;
  254. struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
  255. struct workqueue_struct *workwq;
  256. struct work_struct work;
  257. bool reset_required;
  258. u32 rxfilter_event;
  259. u32 rxfilter_general;
  260. bool rxfilter_installed;
  261. struct hwtstamp_config config;
  262. bool enabled;
  263. unsigned int mode;
  264. void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
  265. ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
  266. s32 correction);
  267. struct {
  268. u32 minor_max;
  269. u32 sync_event_diff_min;
  270. u32 sync_event_diff_max;
  271. unsigned int sync_event_minor_shift;
  272. } nic_time;
  273. unsigned int min_synchronisation_ns;
  274. unsigned int capabilities;
  275. struct {
  276. s32 ptp_tx;
  277. s32 ptp_rx;
  278. s32 pps_out;
  279. s32 pps_in;
  280. s32 general_tx;
  281. s32 general_rx;
  282. } ts_corrections;
  283. efx_qword_t evt_frags[MAX_EVENT_FRAGS];
  284. int evt_frag_idx;
  285. int evt_code;
  286. struct efx_buffer start;
  287. struct pps_event_time host_time_pps;
  288. unsigned int adjfreq_ppb_shift;
  289. s64 current_adjfreq;
  290. struct ptp_clock *phc_clock;
  291. struct ptp_clock_info phc_clock_info;
  292. struct work_struct pps_work;
  293. struct workqueue_struct *pps_workwq;
  294. bool nic_ts_enabled;
  295. _MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
  296. unsigned int good_syncs;
  297. unsigned int fast_syncs;
  298. unsigned int bad_syncs;
  299. unsigned int sync_timeouts;
  300. unsigned int no_time_syncs;
  301. unsigned int invalid_sync_windows;
  302. unsigned int undersize_sync_windows;
  303. unsigned int oversize_sync_windows;
  304. unsigned int rx_no_timestamp;
  305. struct efx_ptp_timeset
  306. timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
  307. void (*xmit_skb)(struct efx_nic *efx, struct sk_buff *skb);
  308. };
  309. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
  310. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
  311. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
  312. static int efx_phc_settime(struct ptp_clock_info *ptp,
  313. const struct timespec64 *e_ts);
  314. static int efx_phc_enable(struct ptp_clock_info *ptp,
  315. struct ptp_clock_request *request, int on);
  316. bool efx_ptp_use_mac_tx_timestamps(struct efx_nic *efx)
  317. {
  318. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  319. return ((efx_nic_rev(efx) >= EFX_REV_HUNT_A0) &&
  320. (nic_data->datapath_caps2 &
  321. (1 << MC_CMD_GET_CAPABILITIES_V2_OUT_TX_MAC_TIMESTAMPING_LBN)
  322. ));
  323. }
  324. /* PTP 'extra' channel is still a traffic channel, but we only create TX queues
  325. * if PTP uses MAC TX timestamps, not if PTP uses the MC directly to transmit.
  326. */
  327. static bool efx_ptp_want_txqs(struct efx_channel *channel)
  328. {
  329. return efx_ptp_use_mac_tx_timestamps(channel->efx);
  330. }
  331. #define PTP_SW_STAT(ext_name, field_name) \
  332. { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
  333. #define PTP_MC_STAT(ext_name, mcdi_name) \
  334. { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
  335. static const struct efx_hw_stat_desc efx_ptp_stat_desc[] = {
  336. PTP_SW_STAT(ptp_good_syncs, good_syncs),
  337. PTP_SW_STAT(ptp_fast_syncs, fast_syncs),
  338. PTP_SW_STAT(ptp_bad_syncs, bad_syncs),
  339. PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts),
  340. PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs),
  341. PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows),
  342. PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows),
  343. PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows),
  344. PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp),
  345. PTP_MC_STAT(ptp_tx_timestamp_packets, TX),
  346. PTP_MC_STAT(ptp_rx_timestamp_packets, RX),
  347. PTP_MC_STAT(ptp_timestamp_packets, TS),
  348. PTP_MC_STAT(ptp_filter_matches, FM),
  349. PTP_MC_STAT(ptp_non_filter_matches, NFM),
  350. };
  351. #define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
  352. static const unsigned long efx_ptp_stat_mask[] = {
  353. [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
  354. };
  355. size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
  356. {
  357. if (!efx->ptp_data)
  358. return 0;
  359. return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
  360. efx_ptp_stat_mask, strings);
  361. }
  362. size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
  363. {
  364. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
  365. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
  366. size_t i;
  367. int rc;
  368. if (!efx->ptp_data)
  369. return 0;
  370. /* Copy software statistics */
  371. for (i = 0; i < PTP_STAT_COUNT; i++) {
  372. if (efx_ptp_stat_desc[i].dma_width)
  373. continue;
  374. stats[i] = *(unsigned int *)((char *)efx->ptp_data +
  375. efx_ptp_stat_desc[i].offset);
  376. }
  377. /* Fetch MC statistics. We *must* fill in all statistics or
  378. * risk leaking kernel memory to userland, so if the MCDI
  379. * request fails we pretend we got zeroes.
  380. */
  381. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS);
  382. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  383. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  384. outbuf, sizeof(outbuf), NULL);
  385. if (rc)
  386. memset(outbuf, 0, sizeof(outbuf));
  387. efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
  388. efx_ptp_stat_mask,
  389. stats, _MCDI_PTR(outbuf, 0), false);
  390. return PTP_STAT_COUNT;
  391. }
  392. /* For Siena platforms NIC time is s and ns */
  393. static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
  394. {
  395. struct timespec64 ts = ns_to_timespec64(ns);
  396. *nic_major = (u32)ts.tv_sec;
  397. *nic_minor = ts.tv_nsec;
  398. }
  399. static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
  400. s32 correction)
  401. {
  402. ktime_t kt = ktime_set(nic_major, nic_minor);
  403. if (correction >= 0)
  404. kt = ktime_add_ns(kt, (u64)correction);
  405. else
  406. kt = ktime_sub_ns(kt, (u64)-correction);
  407. return kt;
  408. }
  409. /* To convert from s27 format to ns we multiply then divide by a power of 2.
  410. * For the conversion from ns to s27, the operation is also converted to a
  411. * multiply and shift.
  412. */
  413. #define S27_TO_NS_SHIFT (27)
  414. #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
  415. #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
  416. #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
  417. /* For Huntington platforms NIC time is in seconds and fractions of a second
  418. * where the minor register only uses 27 bits in units of 2^-27s.
  419. */
  420. static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
  421. {
  422. struct timespec64 ts = ns_to_timespec64(ns);
  423. u32 maj = (u32)ts.tv_sec;
  424. u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
  425. (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
  426. /* The conversion can result in the minor value exceeding the maximum.
  427. * In this case, round up to the next second.
  428. */
  429. if (min >= S27_MINOR_MAX) {
  430. min -= S27_MINOR_MAX;
  431. maj++;
  432. }
  433. *nic_major = maj;
  434. *nic_minor = min;
  435. }
  436. static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
  437. {
  438. u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
  439. (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
  440. return ktime_set(nic_major, ns);
  441. }
  442. static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
  443. s32 correction)
  444. {
  445. /* Apply the correction and deal with carry */
  446. nic_minor += correction;
  447. if ((s32)nic_minor < 0) {
  448. nic_minor += S27_MINOR_MAX;
  449. nic_major--;
  450. } else if (nic_minor >= S27_MINOR_MAX) {
  451. nic_minor -= S27_MINOR_MAX;
  452. nic_major++;
  453. }
  454. return efx_ptp_s27_to_ktime(nic_major, nic_minor);
  455. }
  456. /* For Medford2 platforms the time is in seconds and quarter nanoseconds. */
  457. static void efx_ptp_ns_to_s_qns(s64 ns, u32 *nic_major, u32 *nic_minor)
  458. {
  459. struct timespec64 ts = ns_to_timespec64(ns);
  460. *nic_major = (u32)ts.tv_sec;
  461. *nic_minor = ts.tv_nsec * 4;
  462. }
  463. static ktime_t efx_ptp_s_qns_to_ktime_correction(u32 nic_major, u32 nic_minor,
  464. s32 correction)
  465. {
  466. ktime_t kt;
  467. nic_minor = DIV_ROUND_CLOSEST(nic_minor, 4);
  468. correction = DIV_ROUND_CLOSEST(correction, 4);
  469. kt = ktime_set(nic_major, nic_minor);
  470. if (correction >= 0)
  471. kt = ktime_add_ns(kt, (u64)correction);
  472. else
  473. kt = ktime_sub_ns(kt, (u64)-correction);
  474. return kt;
  475. }
  476. struct efx_channel *efx_ptp_channel(struct efx_nic *efx)
  477. {
  478. return efx->ptp_data ? efx->ptp_data->channel : NULL;
  479. }
  480. static u32 last_sync_timestamp_major(struct efx_nic *efx)
  481. {
  482. struct efx_channel *channel = efx_ptp_channel(efx);
  483. u32 major = 0;
  484. if (channel)
  485. major = channel->sync_timestamp_major;
  486. return major;
  487. }
  488. /* The 8000 series and later can provide the time from the MAC, which is only
  489. * 48 bits long and provides meta-information in the top 2 bits.
  490. */
  491. static ktime_t
  492. efx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx,
  493. struct efx_ptp_data *ptp,
  494. u32 nic_major, u32 nic_minor,
  495. s32 correction)
  496. {
  497. u32 sync_timestamp;
  498. ktime_t kt = { 0 };
  499. s16 delta;
  500. if (!(nic_major & 0x80000000)) {
  501. WARN_ON_ONCE(nic_major >> 16);
  502. /* Medford provides 48 bits of timestamp, so we must get the top
  503. * 16 bits from the timesync event state.
  504. *
  505. * We only have the lower 16 bits of the time now, but we do
  506. * have a full resolution timestamp at some point in past. As
  507. * long as the difference between the (real) now and the sync
  508. * is less than 2^15, then we can reconstruct the difference
  509. * between those two numbers using only the lower 16 bits of
  510. * each.
  511. *
  512. * Put another way
  513. *
  514. * a - b = ((a mod k) - b) mod k
  515. *
  516. * when -k/2 < (a-b) < k/2. In our case k is 2^16. We know
  517. * (a mod k) and b, so can calculate the delta, a - b.
  518. *
  519. */
  520. sync_timestamp = last_sync_timestamp_major(efx);
  521. /* Because delta is s16 this does an implicit mask down to
  522. * 16 bits which is what we need, assuming
  523. * MEDFORD_TX_SECS_EVENT_BITS is 16. delta is signed so that
  524. * we can deal with the (unlikely) case of sync timestamps
  525. * arriving from the future.
  526. */
  527. delta = nic_major - sync_timestamp;
  528. /* Recover the fully specified time now, by applying the offset
  529. * to the (fully specified) sync time.
  530. */
  531. nic_major = sync_timestamp + delta;
  532. kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
  533. correction);
  534. }
  535. return kt;
  536. }
  537. ktime_t efx_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue)
  538. {
  539. struct efx_nic *efx = tx_queue->efx;
  540. struct efx_ptp_data *ptp = efx->ptp_data;
  541. ktime_t kt;
  542. if (efx_ptp_use_mac_tx_timestamps(efx))
  543. kt = efx_ptp_mac_nic_to_ktime_correction(efx, ptp,
  544. tx_queue->completed_timestamp_major,
  545. tx_queue->completed_timestamp_minor,
  546. ptp->ts_corrections.general_tx);
  547. else
  548. kt = ptp->nic_to_kernel_time(
  549. tx_queue->completed_timestamp_major,
  550. tx_queue->completed_timestamp_minor,
  551. ptp->ts_corrections.general_tx);
  552. return kt;
  553. }
  554. /* Get PTP attributes and set up time conversions */
  555. static int efx_ptp_get_attributes(struct efx_nic *efx)
  556. {
  557. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
  558. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
  559. struct efx_ptp_data *ptp = efx->ptp_data;
  560. int rc;
  561. u32 fmt;
  562. size_t out_len;
  563. /* Get the PTP attributes. If the NIC doesn't support the operation we
  564. * use the default format for compatibility with older NICs i.e.
  565. * seconds and nanoseconds.
  566. */
  567. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
  568. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  569. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  570. outbuf, sizeof(outbuf), &out_len);
  571. if (rc == 0) {
  572. fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
  573. } else if (rc == -EINVAL) {
  574. fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
  575. } else if (rc == -EPERM) {
  576. netif_info(efx, probe, efx->net_dev, "no PTP support\n");
  577. return rc;
  578. } else {
  579. efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf),
  580. outbuf, sizeof(outbuf), rc);
  581. return rc;
  582. }
  583. switch (fmt) {
  584. case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION:
  585. ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
  586. ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
  587. ptp->nic_time.minor_max = 1 << 27;
  588. ptp->nic_time.sync_event_minor_shift = 19;
  589. break;
  590. case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS:
  591. ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
  592. ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
  593. ptp->nic_time.minor_max = 1000000000;
  594. ptp->nic_time.sync_event_minor_shift = 22;
  595. break;
  596. case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS:
  597. ptp->ns_to_nic_time = efx_ptp_ns_to_s_qns;
  598. ptp->nic_to_kernel_time = efx_ptp_s_qns_to_ktime_correction;
  599. ptp->nic_time.minor_max = 4000000000UL;
  600. ptp->nic_time.sync_event_minor_shift = 24;
  601. break;
  602. default:
  603. return -ERANGE;
  604. }
  605. /* Precalculate acceptable difference between the minor time in the
  606. * packet prefix and the last MCDI time sync event. We expect the
  607. * packet prefix timestamp to be after of sync event by up to one
  608. * sync event interval (0.25s) but we allow it to exceed this by a
  609. * fuzz factor of (0.1s)
  610. */
  611. ptp->nic_time.sync_event_diff_min = ptp->nic_time.minor_max
  612. - (ptp->nic_time.minor_max / 10);
  613. ptp->nic_time.sync_event_diff_max = (ptp->nic_time.minor_max / 4)
  614. + (ptp->nic_time.minor_max / 10);
  615. /* MC_CMD_PTP_OP_GET_ATTRIBUTES has been extended twice from an older
  616. * operation MC_CMD_PTP_OP_GET_TIME_FORMAT. The function now may return
  617. * a value to use for the minimum acceptable corrected synchronization
  618. * window and may return further capabilities.
  619. * If we have the extra information store it. For older firmware that
  620. * does not implement the extended command use the default value.
  621. */
  622. if (rc == 0 &&
  623. out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST)
  624. ptp->min_synchronisation_ns =
  625. MCDI_DWORD(outbuf,
  626. PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
  627. else
  628. ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
  629. if (rc == 0 &&
  630. out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
  631. ptp->capabilities = MCDI_DWORD(outbuf,
  632. PTP_OUT_GET_ATTRIBUTES_CAPABILITIES);
  633. else
  634. ptp->capabilities = 0;
  635. /* Set up the shift for conversion between frequency
  636. * adjustments in parts-per-billion and the fixed-point
  637. * fractional ns format that the adapter uses.
  638. */
  639. if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN))
  640. ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44;
  641. else
  642. ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40;
  643. return 0;
  644. }
  645. /* Get PTP timestamp corrections */
  646. static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
  647. {
  648. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
  649. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN);
  650. int rc;
  651. size_t out_len;
  652. /* Get the timestamp corrections from the NIC. If this operation is
  653. * not supported (older NICs) then no correction is required.
  654. */
  655. MCDI_SET_DWORD(inbuf, PTP_IN_OP,
  656. MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
  657. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  658. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  659. outbuf, sizeof(outbuf), &out_len);
  660. if (rc == 0) {
  661. efx->ptp_data->ts_corrections.ptp_tx = MCDI_DWORD(outbuf,
  662. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
  663. efx->ptp_data->ts_corrections.ptp_rx = MCDI_DWORD(outbuf,
  664. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
  665. efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
  666. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
  667. efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
  668. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
  669. if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) {
  670. efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD(
  671. outbuf,
  672. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX);
  673. efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD(
  674. outbuf,
  675. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX);
  676. } else {
  677. efx->ptp_data->ts_corrections.general_tx =
  678. efx->ptp_data->ts_corrections.ptp_tx;
  679. efx->ptp_data->ts_corrections.general_rx =
  680. efx->ptp_data->ts_corrections.ptp_rx;
  681. }
  682. } else if (rc == -EINVAL) {
  683. efx->ptp_data->ts_corrections.ptp_tx = 0;
  684. efx->ptp_data->ts_corrections.ptp_rx = 0;
  685. efx->ptp_data->ts_corrections.pps_out = 0;
  686. efx->ptp_data->ts_corrections.pps_in = 0;
  687. efx->ptp_data->ts_corrections.general_tx = 0;
  688. efx->ptp_data->ts_corrections.general_rx = 0;
  689. } else {
  690. efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), outbuf,
  691. sizeof(outbuf), rc);
  692. return rc;
  693. }
  694. return 0;
  695. }
  696. /* Enable MCDI PTP support. */
  697. static int efx_ptp_enable(struct efx_nic *efx)
  698. {
  699. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
  700. MCDI_DECLARE_BUF_ERR(outbuf);
  701. int rc;
  702. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
  703. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  704. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
  705. efx->ptp_data->channel ?
  706. efx->ptp_data->channel->channel : 0);
  707. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
  708. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  709. outbuf, sizeof(outbuf), NULL);
  710. rc = (rc == -EALREADY) ? 0 : rc;
  711. if (rc)
  712. efx_mcdi_display_error(efx, MC_CMD_PTP,
  713. MC_CMD_PTP_IN_ENABLE_LEN,
  714. outbuf, sizeof(outbuf), rc);
  715. return rc;
  716. }
  717. /* Disable MCDI PTP support.
  718. *
  719. * Note that this function should never rely on the presence of ptp_data -
  720. * may be called before that exists.
  721. */
  722. static int efx_ptp_disable(struct efx_nic *efx)
  723. {
  724. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
  725. MCDI_DECLARE_BUF_ERR(outbuf);
  726. int rc;
  727. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
  728. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  729. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  730. outbuf, sizeof(outbuf), NULL);
  731. rc = (rc == -EALREADY) ? 0 : rc;
  732. /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
  733. * should only have been called during probe.
  734. */
  735. if (rc == -ENOSYS || rc == -EPERM)
  736. netif_info(efx, probe, efx->net_dev, "no PTP support\n");
  737. else if (rc)
  738. efx_mcdi_display_error(efx, MC_CMD_PTP,
  739. MC_CMD_PTP_IN_DISABLE_LEN,
  740. outbuf, sizeof(outbuf), rc);
  741. return rc;
  742. }
  743. static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
  744. {
  745. struct sk_buff *skb;
  746. while ((skb = skb_dequeue(q))) {
  747. local_bh_disable();
  748. netif_receive_skb(skb);
  749. local_bh_enable();
  750. }
  751. }
  752. static void efx_ptp_handle_no_channel(struct efx_nic *efx)
  753. {
  754. netif_err(efx, drv, efx->net_dev,
  755. "ERROR: PTP requires MSI-X and 1 additional interrupt"
  756. "vector. PTP disabled\n");
  757. }
  758. /* Repeatedly send the host time to the MC which will capture the hardware
  759. * time.
  760. */
  761. static void efx_ptp_send_times(struct efx_nic *efx,
  762. struct pps_event_time *last_time)
  763. {
  764. struct pps_event_time now;
  765. struct timespec64 limit;
  766. struct efx_ptp_data *ptp = efx->ptp_data;
  767. int *mc_running = ptp->start.addr;
  768. pps_get_ts(&now);
  769. limit = now.ts_real;
  770. timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
  771. /* Write host time for specified period or until MC is done */
  772. while ((timespec64_compare(&now.ts_real, &limit) < 0) &&
  773. READ_ONCE(*mc_running)) {
  774. struct timespec64 update_time;
  775. unsigned int host_time;
  776. /* Don't update continuously to avoid saturating the PCIe bus */
  777. update_time = now.ts_real;
  778. timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
  779. do {
  780. pps_get_ts(&now);
  781. } while ((timespec64_compare(&now.ts_real, &update_time) < 0) &&
  782. READ_ONCE(*mc_running));
  783. /* Synchronise NIC with single word of time only */
  784. host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
  785. now.ts_real.tv_nsec);
  786. /* Update host time in NIC memory */
  787. efx->type->ptp_write_host_time(efx, host_time);
  788. }
  789. *last_time = now;
  790. }
  791. /* Read a timeset from the MC's results and partial process. */
  792. static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
  793. struct efx_ptp_timeset *timeset)
  794. {
  795. unsigned start_ns, end_ns;
  796. timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
  797. timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
  798. timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
  799. timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
  800. timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
  801. /* Ignore seconds */
  802. start_ns = timeset->host_start & MC_NANOSECOND_MASK;
  803. end_ns = timeset->host_end & MC_NANOSECOND_MASK;
  804. /* Allow for rollover */
  805. if (end_ns < start_ns)
  806. end_ns += NSEC_PER_SEC;
  807. /* Determine duration of operation */
  808. timeset->window = end_ns - start_ns;
  809. }
  810. /* Process times received from MC.
  811. *
  812. * Extract times from returned results, and establish the minimum value
  813. * seen. The minimum value represents the "best" possible time and events
  814. * too much greater than this are rejected - the machine is, perhaps, too
  815. * busy. A number of readings are taken so that, hopefully, at least one good
  816. * synchronisation will be seen in the results.
  817. */
  818. static int
  819. efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
  820. size_t response_length,
  821. const struct pps_event_time *last_time)
  822. {
  823. unsigned number_readings =
  824. MCDI_VAR_ARRAY_LEN(response_length,
  825. PTP_OUT_SYNCHRONIZE_TIMESET);
  826. unsigned i;
  827. unsigned ngood = 0;
  828. unsigned last_good = 0;
  829. struct efx_ptp_data *ptp = efx->ptp_data;
  830. u32 last_sec;
  831. u32 start_sec;
  832. struct timespec64 delta;
  833. ktime_t mc_time;
  834. if (number_readings == 0)
  835. return -EAGAIN;
  836. /* Read the set of results and find the last good host-MC
  837. * synchronization result. The MC times when it finishes reading the
  838. * host time so the corrected window time should be fairly constant
  839. * for a given platform. Increment stats for any results that appear
  840. * to be erroneous.
  841. */
  842. for (i = 0; i < number_readings; i++) {
  843. s32 window, corrected;
  844. struct timespec64 wait;
  845. efx_ptp_read_timeset(
  846. MCDI_ARRAY_STRUCT_PTR(synch_buf,
  847. PTP_OUT_SYNCHRONIZE_TIMESET, i),
  848. &ptp->timeset[i]);
  849. wait = ktime_to_timespec64(
  850. ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
  851. window = ptp->timeset[i].window;
  852. corrected = window - wait.tv_nsec;
  853. /* We expect the uncorrected synchronization window to be at
  854. * least as large as the interval between host start and end
  855. * times. If it is smaller than this then this is mostly likely
  856. * to be a consequence of the host's time being adjusted.
  857. * Check that the corrected sync window is in a reasonable
  858. * range. If it is out of range it is likely to be because an
  859. * interrupt or other delay occurred between reading the system
  860. * time and writing it to MC memory.
  861. */
  862. if (window < SYNCHRONISATION_GRANULARITY_NS) {
  863. ++ptp->invalid_sync_windows;
  864. } else if (corrected >= MAX_SYNCHRONISATION_NS) {
  865. ++ptp->oversize_sync_windows;
  866. } else if (corrected < ptp->min_synchronisation_ns) {
  867. ++ptp->undersize_sync_windows;
  868. } else {
  869. ngood++;
  870. last_good = i;
  871. }
  872. }
  873. if (ngood == 0) {
  874. netif_warn(efx, drv, efx->net_dev,
  875. "PTP no suitable synchronisations\n");
  876. return -EAGAIN;
  877. }
  878. /* Calculate delay from last good sync (host time) to last_time.
  879. * It is possible that the seconds rolled over between taking
  880. * the start reading and the last value written by the host. The
  881. * timescales are such that a gap of more than one second is never
  882. * expected. delta is *not* normalised.
  883. */
  884. start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
  885. last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
  886. if (start_sec != last_sec &&
  887. ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
  888. netif_warn(efx, hw, efx->net_dev,
  889. "PTP bad synchronisation seconds\n");
  890. return -EAGAIN;
  891. }
  892. delta.tv_sec = (last_sec - start_sec) & 1;
  893. delta.tv_nsec =
  894. last_time->ts_real.tv_nsec -
  895. (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
  896. /* Convert the NIC time at last good sync into kernel time.
  897. * No correction is required - this time is the output of a
  898. * firmware process.
  899. */
  900. mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
  901. ptp->timeset[last_good].minor, 0);
  902. /* Calculate delay from NIC top of second to last_time */
  903. delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec;
  904. /* Set PPS timestamp to match NIC top of second */
  905. ptp->host_time_pps = *last_time;
  906. pps_sub_ts(&ptp->host_time_pps, delta);
  907. return 0;
  908. }
  909. /* Synchronize times between the host and the MC */
  910. static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
  911. {
  912. struct efx_ptp_data *ptp = efx->ptp_data;
  913. MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
  914. size_t response_length;
  915. int rc;
  916. unsigned long timeout;
  917. struct pps_event_time last_time = {};
  918. unsigned int loops = 0;
  919. int *start = ptp->start.addr;
  920. MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
  921. MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
  922. MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
  923. num_readings);
  924. MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
  925. ptp->start.dma_addr);
  926. /* Clear flag that signals MC ready */
  927. WRITE_ONCE(*start, 0);
  928. rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
  929. MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
  930. EFX_WARN_ON_ONCE_PARANOID(rc);
  931. /* Wait for start from MCDI (or timeout) */
  932. timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
  933. while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) {
  934. udelay(20); /* Usually start MCDI execution quickly */
  935. loops++;
  936. }
  937. if (loops <= 1)
  938. ++ptp->fast_syncs;
  939. if (!time_before(jiffies, timeout))
  940. ++ptp->sync_timeouts;
  941. if (READ_ONCE(*start))
  942. efx_ptp_send_times(efx, &last_time);
  943. /* Collect results */
  944. rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
  945. MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
  946. synch_buf, sizeof(synch_buf),
  947. &response_length);
  948. if (rc == 0) {
  949. rc = efx_ptp_process_times(efx, synch_buf, response_length,
  950. &last_time);
  951. if (rc == 0)
  952. ++ptp->good_syncs;
  953. else
  954. ++ptp->no_time_syncs;
  955. }
  956. /* Increment the bad syncs counter if the synchronize fails, whatever
  957. * the reason.
  958. */
  959. if (rc != 0)
  960. ++ptp->bad_syncs;
  961. return rc;
  962. }
  963. /* Transmit a PTP packet via the dedicated hardware timestamped queue. */
  964. static void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb)
  965. {
  966. struct efx_ptp_data *ptp_data = efx->ptp_data;
  967. struct efx_tx_queue *tx_queue;
  968. u8 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
  969. tx_queue = &ptp_data->channel->tx_queue[type];
  970. if (tx_queue && tx_queue->timestamping) {
  971. efx_enqueue_skb(tx_queue, skb);
  972. } else {
  973. WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
  974. dev_kfree_skb_any(skb);
  975. }
  976. }
  977. /* Transmit a PTP packet, via the MCDI interface, to the wire. */
  978. static void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb)
  979. {
  980. struct efx_ptp_data *ptp_data = efx->ptp_data;
  981. struct skb_shared_hwtstamps timestamps;
  982. int rc = -EIO;
  983. MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
  984. size_t len;
  985. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
  986. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
  987. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
  988. if (skb_shinfo(skb)->nr_frags != 0) {
  989. rc = skb_linearize(skb);
  990. if (rc != 0)
  991. goto fail;
  992. }
  993. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  994. rc = skb_checksum_help(skb);
  995. if (rc != 0)
  996. goto fail;
  997. }
  998. skb_copy_from_linear_data(skb,
  999. MCDI_PTR(ptp_data->txbuf,
  1000. PTP_IN_TRANSMIT_PACKET),
  1001. skb->len);
  1002. rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
  1003. ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
  1004. txtime, sizeof(txtime), &len);
  1005. if (rc != 0)
  1006. goto fail;
  1007. memset(&timestamps, 0, sizeof(timestamps));
  1008. timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
  1009. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
  1010. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
  1011. ptp_data->ts_corrections.ptp_tx);
  1012. skb_tstamp_tx(skb, &timestamps);
  1013. rc = 0;
  1014. fail:
  1015. dev_kfree_skb_any(skb);
  1016. return;
  1017. }
  1018. static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
  1019. {
  1020. struct efx_ptp_data *ptp = efx->ptp_data;
  1021. struct list_head *cursor;
  1022. struct list_head *next;
  1023. if (ptp->rx_ts_inline)
  1024. return;
  1025. /* Drop time-expired events */
  1026. spin_lock_bh(&ptp->evt_lock);
  1027. if (!list_empty(&ptp->evt_list)) {
  1028. list_for_each_safe(cursor, next, &ptp->evt_list) {
  1029. struct efx_ptp_event_rx *evt;
  1030. evt = list_entry(cursor, struct efx_ptp_event_rx,
  1031. link);
  1032. if (time_after(jiffies, evt->expiry)) {
  1033. list_move(&evt->link, &ptp->evt_free_list);
  1034. netif_warn(efx, hw, efx->net_dev,
  1035. "PTP rx event dropped\n");
  1036. }
  1037. }
  1038. }
  1039. spin_unlock_bh(&ptp->evt_lock);
  1040. }
  1041. static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
  1042. struct sk_buff *skb)
  1043. {
  1044. struct efx_ptp_data *ptp = efx->ptp_data;
  1045. bool evts_waiting;
  1046. struct list_head *cursor;
  1047. struct list_head *next;
  1048. struct efx_ptp_match *match;
  1049. enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
  1050. WARN_ON_ONCE(ptp->rx_ts_inline);
  1051. spin_lock_bh(&ptp->evt_lock);
  1052. evts_waiting = !list_empty(&ptp->evt_list);
  1053. spin_unlock_bh(&ptp->evt_lock);
  1054. if (!evts_waiting)
  1055. return PTP_PACKET_STATE_UNMATCHED;
  1056. match = (struct efx_ptp_match *)skb->cb;
  1057. /* Look for a matching timestamp in the event queue */
  1058. spin_lock_bh(&ptp->evt_lock);
  1059. list_for_each_safe(cursor, next, &ptp->evt_list) {
  1060. struct efx_ptp_event_rx *evt;
  1061. evt = list_entry(cursor, struct efx_ptp_event_rx, link);
  1062. if ((evt->seq0 == match->words[0]) &&
  1063. (evt->seq1 == match->words[1])) {
  1064. struct skb_shared_hwtstamps *timestamps;
  1065. /* Match - add in hardware timestamp */
  1066. timestamps = skb_hwtstamps(skb);
  1067. timestamps->hwtstamp = evt->hwtimestamp;
  1068. match->state = PTP_PACKET_STATE_MATCHED;
  1069. rc = PTP_PACKET_STATE_MATCHED;
  1070. list_move(&evt->link, &ptp->evt_free_list);
  1071. break;
  1072. }
  1073. }
  1074. spin_unlock_bh(&ptp->evt_lock);
  1075. return rc;
  1076. }
  1077. /* Process any queued receive events and corresponding packets
  1078. *
  1079. * q is returned with all the packets that are ready for delivery.
  1080. */
  1081. static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
  1082. {
  1083. struct efx_ptp_data *ptp = efx->ptp_data;
  1084. struct sk_buff *skb;
  1085. while ((skb = skb_dequeue(&ptp->rxq))) {
  1086. struct efx_ptp_match *match;
  1087. match = (struct efx_ptp_match *)skb->cb;
  1088. if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
  1089. __skb_queue_tail(q, skb);
  1090. } else if (efx_ptp_match_rx(efx, skb) ==
  1091. PTP_PACKET_STATE_MATCHED) {
  1092. __skb_queue_tail(q, skb);
  1093. } else if (time_after(jiffies, match->expiry)) {
  1094. match->state = PTP_PACKET_STATE_TIMED_OUT;
  1095. ++ptp->rx_no_timestamp;
  1096. __skb_queue_tail(q, skb);
  1097. } else {
  1098. /* Replace unprocessed entry and stop */
  1099. skb_queue_head(&ptp->rxq, skb);
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. /* Complete processing of a received packet */
  1105. static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
  1106. {
  1107. local_bh_disable();
  1108. netif_receive_skb(skb);
  1109. local_bh_enable();
  1110. }
  1111. static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
  1112. {
  1113. struct efx_ptp_data *ptp = efx->ptp_data;
  1114. if (ptp->rxfilter_installed) {
  1115. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  1116. ptp->rxfilter_general);
  1117. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  1118. ptp->rxfilter_event);
  1119. ptp->rxfilter_installed = false;
  1120. }
  1121. }
  1122. static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
  1123. {
  1124. struct efx_ptp_data *ptp = efx->ptp_data;
  1125. struct efx_filter_spec rxfilter;
  1126. int rc;
  1127. if (!ptp->channel || ptp->rxfilter_installed)
  1128. return 0;
  1129. /* Must filter on both event and general ports to ensure
  1130. * that there is no packet re-ordering.
  1131. */
  1132. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  1133. efx_rx_queue_index(
  1134. efx_channel_get_rx_queue(ptp->channel)));
  1135. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  1136. htonl(PTP_ADDRESS),
  1137. htons(PTP_EVENT_PORT));
  1138. if (rc != 0)
  1139. return rc;
  1140. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  1141. if (rc < 0)
  1142. return rc;
  1143. ptp->rxfilter_event = rc;
  1144. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  1145. efx_rx_queue_index(
  1146. efx_channel_get_rx_queue(ptp->channel)));
  1147. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  1148. htonl(PTP_ADDRESS),
  1149. htons(PTP_GENERAL_PORT));
  1150. if (rc != 0)
  1151. goto fail;
  1152. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  1153. if (rc < 0)
  1154. goto fail;
  1155. ptp->rxfilter_general = rc;
  1156. ptp->rxfilter_installed = true;
  1157. return 0;
  1158. fail:
  1159. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  1160. ptp->rxfilter_event);
  1161. return rc;
  1162. }
  1163. static int efx_ptp_start(struct efx_nic *efx)
  1164. {
  1165. struct efx_ptp_data *ptp = efx->ptp_data;
  1166. int rc;
  1167. ptp->reset_required = false;
  1168. rc = efx_ptp_insert_multicast_filters(efx);
  1169. if (rc)
  1170. return rc;
  1171. rc = efx_ptp_enable(efx);
  1172. if (rc != 0)
  1173. goto fail;
  1174. ptp->evt_frag_idx = 0;
  1175. ptp->current_adjfreq = 0;
  1176. return 0;
  1177. fail:
  1178. efx_ptp_remove_multicast_filters(efx);
  1179. return rc;
  1180. }
  1181. static int efx_ptp_stop(struct efx_nic *efx)
  1182. {
  1183. struct efx_ptp_data *ptp = efx->ptp_data;
  1184. struct list_head *cursor;
  1185. struct list_head *next;
  1186. int rc;
  1187. if (ptp == NULL)
  1188. return 0;
  1189. rc = efx_ptp_disable(efx);
  1190. efx_ptp_remove_multicast_filters(efx);
  1191. /* Make sure RX packets are really delivered */
  1192. efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
  1193. skb_queue_purge(&efx->ptp_data->txq);
  1194. /* Drop any pending receive events */
  1195. spin_lock_bh(&efx->ptp_data->evt_lock);
  1196. list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
  1197. list_move(cursor, &efx->ptp_data->evt_free_list);
  1198. }
  1199. spin_unlock_bh(&efx->ptp_data->evt_lock);
  1200. return rc;
  1201. }
  1202. static int efx_ptp_restart(struct efx_nic *efx)
  1203. {
  1204. if (efx->ptp_data && efx->ptp_data->enabled)
  1205. return efx_ptp_start(efx);
  1206. return 0;
  1207. }
  1208. static void efx_ptp_pps_worker(struct work_struct *work)
  1209. {
  1210. struct efx_ptp_data *ptp =
  1211. container_of(work, struct efx_ptp_data, pps_work);
  1212. struct efx_nic *efx = ptp->efx;
  1213. struct ptp_clock_event ptp_evt;
  1214. if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
  1215. return;
  1216. ptp_evt.type = PTP_CLOCK_PPSUSR;
  1217. ptp_evt.pps_times = ptp->host_time_pps;
  1218. ptp_clock_event(ptp->phc_clock, &ptp_evt);
  1219. }
  1220. static void efx_ptp_worker(struct work_struct *work)
  1221. {
  1222. struct efx_ptp_data *ptp_data =
  1223. container_of(work, struct efx_ptp_data, work);
  1224. struct efx_nic *efx = ptp_data->efx;
  1225. struct sk_buff *skb;
  1226. struct sk_buff_head tempq;
  1227. if (ptp_data->reset_required) {
  1228. efx_ptp_stop(efx);
  1229. efx_ptp_start(efx);
  1230. return;
  1231. }
  1232. efx_ptp_drop_time_expired_events(efx);
  1233. __skb_queue_head_init(&tempq);
  1234. efx_ptp_process_events(efx, &tempq);
  1235. while ((skb = skb_dequeue(&ptp_data->txq)))
  1236. ptp_data->xmit_skb(efx, skb);
  1237. while ((skb = __skb_dequeue(&tempq)))
  1238. efx_ptp_process_rx(efx, skb);
  1239. }
  1240. static const struct ptp_clock_info efx_phc_clock_info = {
  1241. .owner = THIS_MODULE,
  1242. .name = "sfc",
  1243. .max_adj = MAX_PPB,
  1244. .n_alarm = 0,
  1245. .n_ext_ts = 0,
  1246. .n_per_out = 0,
  1247. .n_pins = 0,
  1248. .pps = 1,
  1249. .adjfreq = efx_phc_adjfreq,
  1250. .adjtime = efx_phc_adjtime,
  1251. .gettime64 = efx_phc_gettime,
  1252. .settime64 = efx_phc_settime,
  1253. .enable = efx_phc_enable,
  1254. };
  1255. /* Initialise PTP state. */
  1256. int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
  1257. {
  1258. struct efx_ptp_data *ptp;
  1259. int rc = 0;
  1260. unsigned int pos;
  1261. ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
  1262. efx->ptp_data = ptp;
  1263. if (!efx->ptp_data)
  1264. return -ENOMEM;
  1265. ptp->efx = efx;
  1266. ptp->channel = channel;
  1267. ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
  1268. rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
  1269. if (rc != 0)
  1270. goto fail1;
  1271. skb_queue_head_init(&ptp->rxq);
  1272. skb_queue_head_init(&ptp->txq);
  1273. ptp->workwq = create_singlethread_workqueue("sfc_ptp");
  1274. if (!ptp->workwq) {
  1275. rc = -ENOMEM;
  1276. goto fail2;
  1277. }
  1278. if (efx_ptp_use_mac_tx_timestamps(efx)) {
  1279. ptp->xmit_skb = efx_ptp_xmit_skb_queue;
  1280. /* Request sync events on this channel. */
  1281. channel->sync_events_state = SYNC_EVENTS_QUIESCENT;
  1282. } else {
  1283. ptp->xmit_skb = efx_ptp_xmit_skb_mc;
  1284. }
  1285. INIT_WORK(&ptp->work, efx_ptp_worker);
  1286. ptp->config.flags = 0;
  1287. ptp->config.tx_type = HWTSTAMP_TX_OFF;
  1288. ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
  1289. INIT_LIST_HEAD(&ptp->evt_list);
  1290. INIT_LIST_HEAD(&ptp->evt_free_list);
  1291. spin_lock_init(&ptp->evt_lock);
  1292. for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
  1293. list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
  1294. /* Get the NIC PTP attributes and set up time conversions */
  1295. rc = efx_ptp_get_attributes(efx);
  1296. if (rc < 0)
  1297. goto fail3;
  1298. /* Get the timestamp corrections */
  1299. rc = efx_ptp_get_timestamp_corrections(efx);
  1300. if (rc < 0)
  1301. goto fail3;
  1302. if (efx->mcdi->fn_flags &
  1303. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
  1304. ptp->phc_clock_info = efx_phc_clock_info;
  1305. ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
  1306. &efx->pci_dev->dev);
  1307. if (IS_ERR(ptp->phc_clock)) {
  1308. rc = PTR_ERR(ptp->phc_clock);
  1309. goto fail3;
  1310. } else if (ptp->phc_clock) {
  1311. INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
  1312. ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
  1313. if (!ptp->pps_workwq) {
  1314. rc = -ENOMEM;
  1315. goto fail4;
  1316. }
  1317. }
  1318. }
  1319. ptp->nic_ts_enabled = false;
  1320. return 0;
  1321. fail4:
  1322. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1323. fail3:
  1324. destroy_workqueue(efx->ptp_data->workwq);
  1325. fail2:
  1326. efx_nic_free_buffer(efx, &ptp->start);
  1327. fail1:
  1328. kfree(efx->ptp_data);
  1329. efx->ptp_data = NULL;
  1330. return rc;
  1331. }
  1332. /* Initialise PTP channel.
  1333. *
  1334. * Setting core_index to zero causes the queue to be initialised and doesn't
  1335. * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
  1336. */
  1337. static int efx_ptp_probe_channel(struct efx_channel *channel)
  1338. {
  1339. struct efx_nic *efx = channel->efx;
  1340. int rc;
  1341. channel->irq_moderation_us = 0;
  1342. channel->rx_queue.core_index = 0;
  1343. rc = efx_ptp_probe(efx, channel);
  1344. /* Failure to probe PTP is not fatal; this channel will just not be
  1345. * used for anything.
  1346. * In the case of EPERM, efx_ptp_probe will print its own message (in
  1347. * efx_ptp_get_attributes()), so we don't need to.
  1348. */
  1349. if (rc && rc != -EPERM)
  1350. netif_warn(efx, drv, efx->net_dev,
  1351. "Failed to probe PTP, rc=%d\n", rc);
  1352. return 0;
  1353. }
  1354. void efx_ptp_remove(struct efx_nic *efx)
  1355. {
  1356. if (!efx->ptp_data)
  1357. return;
  1358. (void)efx_ptp_disable(efx);
  1359. cancel_work_sync(&efx->ptp_data->work);
  1360. if (efx->ptp_data->pps_workwq)
  1361. cancel_work_sync(&efx->ptp_data->pps_work);
  1362. skb_queue_purge(&efx->ptp_data->rxq);
  1363. skb_queue_purge(&efx->ptp_data->txq);
  1364. if (efx->ptp_data->phc_clock) {
  1365. destroy_workqueue(efx->ptp_data->pps_workwq);
  1366. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1367. }
  1368. destroy_workqueue(efx->ptp_data->workwq);
  1369. efx_nic_free_buffer(efx, &efx->ptp_data->start);
  1370. kfree(efx->ptp_data);
  1371. efx->ptp_data = NULL;
  1372. }
  1373. static void efx_ptp_remove_channel(struct efx_channel *channel)
  1374. {
  1375. efx_ptp_remove(channel->efx);
  1376. }
  1377. static void efx_ptp_get_channel_name(struct efx_channel *channel,
  1378. char *buf, size_t len)
  1379. {
  1380. snprintf(buf, len, "%s-ptp", channel->efx->name);
  1381. }
  1382. /* Determine whether this packet should be processed by the PTP module
  1383. * or transmitted conventionally.
  1384. */
  1385. bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1386. {
  1387. return efx->ptp_data &&
  1388. efx->ptp_data->enabled &&
  1389. skb->len >= PTP_MIN_LENGTH &&
  1390. skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
  1391. likely(skb->protocol == htons(ETH_P_IP)) &&
  1392. skb_transport_header_was_set(skb) &&
  1393. skb_network_header_len(skb) >= sizeof(struct iphdr) &&
  1394. ip_hdr(skb)->protocol == IPPROTO_UDP &&
  1395. skb_headlen(skb) >=
  1396. skb_transport_offset(skb) + sizeof(struct udphdr) &&
  1397. udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
  1398. }
  1399. /* Receive a PTP packet. Packets are queued until the arrival of
  1400. * the receive timestamp from the MC - this will probably occur after the
  1401. * packet arrival because of the processing in the MC.
  1402. */
  1403. static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
  1404. {
  1405. struct efx_nic *efx = channel->efx;
  1406. struct efx_ptp_data *ptp = efx->ptp_data;
  1407. struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
  1408. u8 *match_data_012, *match_data_345;
  1409. unsigned int version;
  1410. u8 *data;
  1411. match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1412. /* Correct version? */
  1413. if (ptp->mode == MC_CMD_PTP_MODE_V1) {
  1414. if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
  1415. return false;
  1416. }
  1417. data = skb->data;
  1418. version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
  1419. if (version != PTP_VERSION_V1) {
  1420. return false;
  1421. }
  1422. /* PTP V1 uses all six bytes of the UUID to match the packet
  1423. * to the timestamp
  1424. */
  1425. match_data_012 = data + PTP_V1_UUID_OFFSET;
  1426. match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
  1427. } else {
  1428. if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
  1429. return false;
  1430. }
  1431. data = skb->data;
  1432. version = data[PTP_V2_VERSION_OFFSET];
  1433. if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
  1434. return false;
  1435. }
  1436. /* The original V2 implementation uses bytes 2-7 of
  1437. * the UUID to match the packet to the timestamp. This
  1438. * discards two of the bytes of the MAC address used
  1439. * to create the UUID (SF bug 33070). The PTP V2
  1440. * enhanced mode fixes this issue and uses bytes 0-2
  1441. * and byte 5-7 of the UUID.
  1442. */
  1443. match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
  1444. if (ptp->mode == MC_CMD_PTP_MODE_V2) {
  1445. match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
  1446. } else {
  1447. match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
  1448. BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
  1449. }
  1450. }
  1451. /* Does this packet require timestamping? */
  1452. if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
  1453. match->state = PTP_PACKET_STATE_UNMATCHED;
  1454. /* We expect the sequence number to be in the same position in
  1455. * the packet for PTP V1 and V2
  1456. */
  1457. BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
  1458. BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
  1459. /* Extract UUID/Sequence information */
  1460. match->words[0] = (match_data_012[0] |
  1461. (match_data_012[1] << 8) |
  1462. (match_data_012[2] << 16) |
  1463. (match_data_345[0] << 24));
  1464. match->words[1] = (match_data_345[1] |
  1465. (match_data_345[2] << 8) |
  1466. (data[PTP_V1_SEQUENCE_OFFSET +
  1467. PTP_V1_SEQUENCE_LENGTH - 1] <<
  1468. 16));
  1469. } else {
  1470. match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
  1471. }
  1472. skb_queue_tail(&ptp->rxq, skb);
  1473. queue_work(ptp->workwq, &ptp->work);
  1474. return true;
  1475. }
  1476. /* Transmit a PTP packet. This has to be transmitted by the MC
  1477. * itself, through an MCDI call. MCDI calls aren't permitted
  1478. * in the transmit path so defer the actual transmission to a suitable worker.
  1479. */
  1480. int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1481. {
  1482. struct efx_ptp_data *ptp = efx->ptp_data;
  1483. skb_queue_tail(&ptp->txq, skb);
  1484. if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
  1485. (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
  1486. efx_xmit_hwtstamp_pending(skb);
  1487. queue_work(ptp->workwq, &ptp->work);
  1488. return NETDEV_TX_OK;
  1489. }
  1490. int efx_ptp_get_mode(struct efx_nic *efx)
  1491. {
  1492. return efx->ptp_data->mode;
  1493. }
  1494. int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
  1495. unsigned int new_mode)
  1496. {
  1497. if ((enable_wanted != efx->ptp_data->enabled) ||
  1498. (enable_wanted && (efx->ptp_data->mode != new_mode))) {
  1499. int rc = 0;
  1500. if (enable_wanted) {
  1501. /* Change of mode requires disable */
  1502. if (efx->ptp_data->enabled &&
  1503. (efx->ptp_data->mode != new_mode)) {
  1504. efx->ptp_data->enabled = false;
  1505. rc = efx_ptp_stop(efx);
  1506. if (rc != 0)
  1507. return rc;
  1508. }
  1509. /* Set new operating mode and establish
  1510. * baseline synchronisation, which must
  1511. * succeed.
  1512. */
  1513. efx->ptp_data->mode = new_mode;
  1514. if (netif_running(efx->net_dev))
  1515. rc = efx_ptp_start(efx);
  1516. if (rc == 0) {
  1517. rc = efx_ptp_synchronize(efx,
  1518. PTP_SYNC_ATTEMPTS * 2);
  1519. if (rc != 0)
  1520. efx_ptp_stop(efx);
  1521. }
  1522. } else {
  1523. rc = efx_ptp_stop(efx);
  1524. }
  1525. if (rc != 0)
  1526. return rc;
  1527. efx->ptp_data->enabled = enable_wanted;
  1528. }
  1529. return 0;
  1530. }
  1531. static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
  1532. {
  1533. int rc;
  1534. if (init->flags)
  1535. return -EINVAL;
  1536. if ((init->tx_type != HWTSTAMP_TX_OFF) &&
  1537. (init->tx_type != HWTSTAMP_TX_ON))
  1538. return -ERANGE;
  1539. rc = efx->type->ptp_set_ts_config(efx, init);
  1540. if (rc)
  1541. return rc;
  1542. efx->ptp_data->config = *init;
  1543. return 0;
  1544. }
  1545. void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
  1546. {
  1547. struct efx_ptp_data *ptp = efx->ptp_data;
  1548. struct efx_nic *primary = efx->primary;
  1549. ASSERT_RTNL();
  1550. if (!ptp)
  1551. return;
  1552. ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
  1553. SOF_TIMESTAMPING_RX_HARDWARE |
  1554. SOF_TIMESTAMPING_RAW_HARDWARE);
  1555. /* Check licensed features. If we don't have the license for TX
  1556. * timestamps, the NIC will not support them.
  1557. */
  1558. if (efx_ptp_use_mac_tx_timestamps(efx)) {
  1559. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1560. if (!(nic_data->licensed_features &
  1561. (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN)))
  1562. ts_info->so_timestamping &=
  1563. ~SOF_TIMESTAMPING_TX_HARDWARE;
  1564. }
  1565. if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
  1566. ts_info->phc_index =
  1567. ptp_clock_index(primary->ptp_data->phc_clock);
  1568. ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
  1569. ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
  1570. }
  1571. int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1572. {
  1573. struct hwtstamp_config config;
  1574. int rc;
  1575. /* Not a PTP enabled port */
  1576. if (!efx->ptp_data)
  1577. return -EOPNOTSUPP;
  1578. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1579. return -EFAULT;
  1580. rc = efx_ptp_ts_init(efx, &config);
  1581. if (rc != 0)
  1582. return rc;
  1583. return copy_to_user(ifr->ifr_data, &config, sizeof(config))
  1584. ? -EFAULT : 0;
  1585. }
  1586. int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1587. {
  1588. if (!efx->ptp_data)
  1589. return -EOPNOTSUPP;
  1590. return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
  1591. sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
  1592. }
  1593. static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
  1594. {
  1595. struct efx_ptp_data *ptp = efx->ptp_data;
  1596. netif_err(efx, hw, efx->net_dev,
  1597. "PTP unexpected event length: got %d expected %d\n",
  1598. ptp->evt_frag_idx, expected_frag_len);
  1599. ptp->reset_required = true;
  1600. queue_work(ptp->workwq, &ptp->work);
  1601. }
  1602. /* Process a completed receive event. Put it on the event queue and
  1603. * start worker thread. This is required because event and their
  1604. * correspoding packets may come in either order.
  1605. */
  1606. static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1607. {
  1608. struct efx_ptp_event_rx *evt = NULL;
  1609. if (WARN_ON_ONCE(ptp->rx_ts_inline))
  1610. return;
  1611. if (ptp->evt_frag_idx != 3) {
  1612. ptp_event_failure(efx, 3);
  1613. return;
  1614. }
  1615. spin_lock_bh(&ptp->evt_lock);
  1616. if (!list_empty(&ptp->evt_free_list)) {
  1617. evt = list_first_entry(&ptp->evt_free_list,
  1618. struct efx_ptp_event_rx, link);
  1619. list_del(&evt->link);
  1620. evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
  1621. evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
  1622. MCDI_EVENT_SRC) |
  1623. (EFX_QWORD_FIELD(ptp->evt_frags[1],
  1624. MCDI_EVENT_SRC) << 8) |
  1625. (EFX_QWORD_FIELD(ptp->evt_frags[0],
  1626. MCDI_EVENT_SRC) << 16));
  1627. evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
  1628. EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
  1629. EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
  1630. ptp->ts_corrections.ptp_rx);
  1631. evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1632. list_add_tail(&evt->link, &ptp->evt_list);
  1633. queue_work(ptp->workwq, &ptp->work);
  1634. } else if (net_ratelimit()) {
  1635. /* Log a rate-limited warning message. */
  1636. netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
  1637. }
  1638. spin_unlock_bh(&ptp->evt_lock);
  1639. }
  1640. static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1641. {
  1642. int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
  1643. if (ptp->evt_frag_idx != 1) {
  1644. ptp_event_failure(efx, 1);
  1645. return;
  1646. }
  1647. netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
  1648. }
  1649. static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1650. {
  1651. if (ptp->nic_ts_enabled)
  1652. queue_work(ptp->pps_workwq, &ptp->pps_work);
  1653. }
  1654. void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
  1655. {
  1656. struct efx_ptp_data *ptp = efx->ptp_data;
  1657. int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
  1658. if (!ptp) {
  1659. if (!efx->ptp_warned) {
  1660. netif_warn(efx, drv, efx->net_dev,
  1661. "Received PTP event but PTP not set up\n");
  1662. efx->ptp_warned = true;
  1663. }
  1664. return;
  1665. }
  1666. if (!ptp->enabled)
  1667. return;
  1668. if (ptp->evt_frag_idx == 0) {
  1669. ptp->evt_code = code;
  1670. } else if (ptp->evt_code != code) {
  1671. netif_err(efx, hw, efx->net_dev,
  1672. "PTP out of sequence event %d\n", code);
  1673. ptp->evt_frag_idx = 0;
  1674. }
  1675. ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
  1676. if (!MCDI_EVENT_FIELD(*ev, CONT)) {
  1677. /* Process resulting event */
  1678. switch (code) {
  1679. case MCDI_EVENT_CODE_PTP_RX:
  1680. ptp_event_rx(efx, ptp);
  1681. break;
  1682. case MCDI_EVENT_CODE_PTP_FAULT:
  1683. ptp_event_fault(efx, ptp);
  1684. break;
  1685. case MCDI_EVENT_CODE_PTP_PPS:
  1686. ptp_event_pps(efx, ptp);
  1687. break;
  1688. default:
  1689. netif_err(efx, hw, efx->net_dev,
  1690. "PTP unknown event %d\n", code);
  1691. break;
  1692. }
  1693. ptp->evt_frag_idx = 0;
  1694. } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
  1695. netif_err(efx, hw, efx->net_dev,
  1696. "PTP too many event fragments\n");
  1697. ptp->evt_frag_idx = 0;
  1698. }
  1699. }
  1700. void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
  1701. {
  1702. struct efx_nic *efx = channel->efx;
  1703. struct efx_ptp_data *ptp = efx->ptp_data;
  1704. /* When extracting the sync timestamp minor value, we should discard
  1705. * the least significant two bits. These are not required in order
  1706. * to reconstruct full-range timestamps and they are optionally used
  1707. * to report status depending on the options supplied when subscribing
  1708. * for sync events.
  1709. */
  1710. channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
  1711. channel->sync_timestamp_minor =
  1712. (MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC)
  1713. << ptp->nic_time.sync_event_minor_shift;
  1714. /* if sync events have been disabled then we want to silently ignore
  1715. * this event, so throw away result.
  1716. */
  1717. (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
  1718. SYNC_EVENTS_VALID);
  1719. }
  1720. static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
  1721. {
  1722. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  1723. return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
  1724. #else
  1725. const u8 *data = eh + efx->rx_packet_ts_offset;
  1726. return (u32)data[0] |
  1727. (u32)data[1] << 8 |
  1728. (u32)data[2] << 16 |
  1729. (u32)data[3] << 24;
  1730. #endif
  1731. }
  1732. void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
  1733. struct sk_buff *skb)
  1734. {
  1735. struct efx_nic *efx = channel->efx;
  1736. struct efx_ptp_data *ptp = efx->ptp_data;
  1737. u32 pkt_timestamp_major, pkt_timestamp_minor;
  1738. u32 diff, carry;
  1739. struct skb_shared_hwtstamps *timestamps;
  1740. if (channel->sync_events_state != SYNC_EVENTS_VALID)
  1741. return;
  1742. pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb));
  1743. /* get the difference between the packet and sync timestamps,
  1744. * modulo one second
  1745. */
  1746. diff = pkt_timestamp_minor - channel->sync_timestamp_minor;
  1747. if (pkt_timestamp_minor < channel->sync_timestamp_minor)
  1748. diff += ptp->nic_time.minor_max;
  1749. /* do we roll over a second boundary and need to carry the one? */
  1750. carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ?
  1751. 1 : 0;
  1752. if (diff <= ptp->nic_time.sync_event_diff_max) {
  1753. /* packet is ahead of the sync event by a quarter of a second or
  1754. * less (allowing for fuzz)
  1755. */
  1756. pkt_timestamp_major = channel->sync_timestamp_major + carry;
  1757. } else if (diff >= ptp->nic_time.sync_event_diff_min) {
  1758. /* packet is behind the sync event but within the fuzz factor.
  1759. * This means the RX packet and sync event crossed as they were
  1760. * placed on the event queue, which can sometimes happen.
  1761. */
  1762. pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
  1763. } else {
  1764. /* it's outside tolerance in both directions. this might be
  1765. * indicative of us missing sync events for some reason, so
  1766. * we'll call it an error rather than risk giving a bogus
  1767. * timestamp.
  1768. */
  1769. netif_vdbg(efx, drv, efx->net_dev,
  1770. "packet timestamp %x too far from sync event %x:%x\n",
  1771. pkt_timestamp_minor, channel->sync_timestamp_major,
  1772. channel->sync_timestamp_minor);
  1773. return;
  1774. }
  1775. /* attach the timestamps to the skb */
  1776. timestamps = skb_hwtstamps(skb);
  1777. timestamps->hwtstamp =
  1778. ptp->nic_to_kernel_time(pkt_timestamp_major,
  1779. pkt_timestamp_minor,
  1780. ptp->ts_corrections.general_rx);
  1781. }
  1782. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  1783. {
  1784. struct efx_ptp_data *ptp_data = container_of(ptp,
  1785. struct efx_ptp_data,
  1786. phc_clock_info);
  1787. struct efx_nic *efx = ptp_data->efx;
  1788. MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
  1789. s64 adjustment_ns;
  1790. int rc;
  1791. if (delta > MAX_PPB)
  1792. delta = MAX_PPB;
  1793. else if (delta < -MAX_PPB)
  1794. delta = -MAX_PPB;
  1795. /* Convert ppb to fixed point ns taking care to round correctly. */
  1796. adjustment_ns = ((s64)delta * PPB_SCALE_WORD +
  1797. (1 << (ptp_data->adjfreq_ppb_shift - 1))) >>
  1798. ptp_data->adjfreq_ppb_shift;
  1799. MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1800. MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
  1801. MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
  1802. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
  1803. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
  1804. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
  1805. NULL, 0, NULL);
  1806. if (rc != 0)
  1807. return rc;
  1808. ptp_data->current_adjfreq = adjustment_ns;
  1809. return 0;
  1810. }
  1811. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1812. {
  1813. u32 nic_major, nic_minor;
  1814. struct efx_ptp_data *ptp_data = container_of(ptp,
  1815. struct efx_ptp_data,
  1816. phc_clock_info);
  1817. struct efx_nic *efx = ptp_data->efx;
  1818. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
  1819. efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
  1820. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1821. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1822. MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
  1823. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
  1824. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
  1825. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1826. NULL, 0, NULL);
  1827. }
  1828. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  1829. {
  1830. struct efx_ptp_data *ptp_data = container_of(ptp,
  1831. struct efx_ptp_data,
  1832. phc_clock_info);
  1833. struct efx_nic *efx = ptp_data->efx;
  1834. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
  1835. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
  1836. int rc;
  1837. ktime_t kt;
  1838. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
  1839. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1840. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1841. outbuf, sizeof(outbuf), NULL);
  1842. if (rc != 0)
  1843. return rc;
  1844. kt = ptp_data->nic_to_kernel_time(
  1845. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
  1846. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
  1847. *ts = ktime_to_timespec64(kt);
  1848. return 0;
  1849. }
  1850. static int efx_phc_settime(struct ptp_clock_info *ptp,
  1851. const struct timespec64 *e_ts)
  1852. {
  1853. /* Get the current NIC time, efx_phc_gettime.
  1854. * Subtract from the desired time to get the offset
  1855. * call efx_phc_adjtime with the offset
  1856. */
  1857. int rc;
  1858. struct timespec64 time_now;
  1859. struct timespec64 delta;
  1860. rc = efx_phc_gettime(ptp, &time_now);
  1861. if (rc != 0)
  1862. return rc;
  1863. delta = timespec64_sub(*e_ts, time_now);
  1864. rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
  1865. if (rc != 0)
  1866. return rc;
  1867. return 0;
  1868. }
  1869. static int efx_phc_enable(struct ptp_clock_info *ptp,
  1870. struct ptp_clock_request *request,
  1871. int enable)
  1872. {
  1873. struct efx_ptp_data *ptp_data = container_of(ptp,
  1874. struct efx_ptp_data,
  1875. phc_clock_info);
  1876. if (request->type != PTP_CLK_REQ_PPS)
  1877. return -EOPNOTSUPP;
  1878. ptp_data->nic_ts_enabled = !!enable;
  1879. return 0;
  1880. }
  1881. static const struct efx_channel_type efx_ptp_channel_type = {
  1882. .handle_no_channel = efx_ptp_handle_no_channel,
  1883. .pre_probe = efx_ptp_probe_channel,
  1884. .post_remove = efx_ptp_remove_channel,
  1885. .get_name = efx_ptp_get_channel_name,
  1886. /* no copy operation; there is no need to reallocate this channel */
  1887. .receive_skb = efx_ptp_rx,
  1888. .want_txqs = efx_ptp_want_txqs,
  1889. .keep_eventq = false,
  1890. };
  1891. void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
  1892. {
  1893. /* Check whether PTP is implemented on this NIC. The DISABLE
  1894. * operation will succeed if and only if it is implemented.
  1895. */
  1896. if (efx_ptp_disable(efx) == 0)
  1897. efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
  1898. &efx_ptp_channel_type;
  1899. }
  1900. void efx_ptp_start_datapath(struct efx_nic *efx)
  1901. {
  1902. if (efx_ptp_restart(efx))
  1903. netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
  1904. /* re-enable timestamping if it was previously enabled */
  1905. if (efx->type->ptp_set_ts_sync_events)
  1906. efx->type->ptp_set_ts_sync_events(efx, true, true);
  1907. }
  1908. void efx_ptp_stop_datapath(struct efx_nic *efx)
  1909. {
  1910. /* temporarily disable timestamping */
  1911. if (efx->type->ptp_set_ts_sync_events)
  1912. efx->type->ptp_set_ts_sync_events(efx, false, true);
  1913. efx_ptp_stop(efx);
  1914. }