ptp.c 57 KB

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