ptp.c 65 KB

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