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. } else if (ptp->phc_clock) {
  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. }
  1111. ptp->nic_ts_enabled = false;
  1112. return 0;
  1113. fail4:
  1114. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1115. fail3:
  1116. destroy_workqueue(efx->ptp_data->workwq);
  1117. fail2:
  1118. efx_nic_free_buffer(efx, &ptp->start);
  1119. fail1:
  1120. kfree(efx->ptp_data);
  1121. efx->ptp_data = NULL;
  1122. return rc;
  1123. }
  1124. /* Initialise PTP channel.
  1125. *
  1126. * Setting core_index to zero causes the queue to be initialised and doesn't
  1127. * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
  1128. */
  1129. static int efx_ptp_probe_channel(struct efx_channel *channel)
  1130. {
  1131. struct efx_nic *efx = channel->efx;
  1132. channel->irq_moderation_us = 0;
  1133. channel->rx_queue.core_index = 0;
  1134. return efx_ptp_probe(efx, channel);
  1135. }
  1136. void efx_ptp_remove(struct efx_nic *efx)
  1137. {
  1138. if (!efx->ptp_data)
  1139. return;
  1140. (void)efx_ptp_disable(efx);
  1141. cancel_work_sync(&efx->ptp_data->work);
  1142. cancel_work_sync(&efx->ptp_data->pps_work);
  1143. skb_queue_purge(&efx->ptp_data->rxq);
  1144. skb_queue_purge(&efx->ptp_data->txq);
  1145. if (efx->ptp_data->phc_clock) {
  1146. destroy_workqueue(efx->ptp_data->pps_workwq);
  1147. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1148. }
  1149. destroy_workqueue(efx->ptp_data->workwq);
  1150. efx_nic_free_buffer(efx, &efx->ptp_data->start);
  1151. kfree(efx->ptp_data);
  1152. }
  1153. static void efx_ptp_remove_channel(struct efx_channel *channel)
  1154. {
  1155. efx_ptp_remove(channel->efx);
  1156. }
  1157. static void efx_ptp_get_channel_name(struct efx_channel *channel,
  1158. char *buf, size_t len)
  1159. {
  1160. snprintf(buf, len, "%s-ptp", channel->efx->name);
  1161. }
  1162. /* Determine whether this packet should be processed by the PTP module
  1163. * or transmitted conventionally.
  1164. */
  1165. bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1166. {
  1167. return efx->ptp_data &&
  1168. efx->ptp_data->enabled &&
  1169. skb->len >= PTP_MIN_LENGTH &&
  1170. skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
  1171. likely(skb->protocol == htons(ETH_P_IP)) &&
  1172. skb_transport_header_was_set(skb) &&
  1173. skb_network_header_len(skb) >= sizeof(struct iphdr) &&
  1174. ip_hdr(skb)->protocol == IPPROTO_UDP &&
  1175. skb_headlen(skb) >=
  1176. skb_transport_offset(skb) + sizeof(struct udphdr) &&
  1177. udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
  1178. }
  1179. /* Receive a PTP packet. Packets are queued until the arrival of
  1180. * the receive timestamp from the MC - this will probably occur after the
  1181. * packet arrival because of the processing in the MC.
  1182. */
  1183. static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
  1184. {
  1185. struct efx_nic *efx = channel->efx;
  1186. struct efx_ptp_data *ptp = efx->ptp_data;
  1187. struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
  1188. u8 *match_data_012, *match_data_345;
  1189. unsigned int version;
  1190. u8 *data;
  1191. match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1192. /* Correct version? */
  1193. if (ptp->mode == MC_CMD_PTP_MODE_V1) {
  1194. if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
  1195. return false;
  1196. }
  1197. data = skb->data;
  1198. version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
  1199. if (version != PTP_VERSION_V1) {
  1200. return false;
  1201. }
  1202. /* PTP V1 uses all six bytes of the UUID to match the packet
  1203. * to the timestamp
  1204. */
  1205. match_data_012 = data + PTP_V1_UUID_OFFSET;
  1206. match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
  1207. } else {
  1208. if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
  1209. return false;
  1210. }
  1211. data = skb->data;
  1212. version = data[PTP_V2_VERSION_OFFSET];
  1213. if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
  1214. return false;
  1215. }
  1216. /* The original V2 implementation uses bytes 2-7 of
  1217. * the UUID to match the packet to the timestamp. This
  1218. * discards two of the bytes of the MAC address used
  1219. * to create the UUID (SF bug 33070). The PTP V2
  1220. * enhanced mode fixes this issue and uses bytes 0-2
  1221. * and byte 5-7 of the UUID.
  1222. */
  1223. match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
  1224. if (ptp->mode == MC_CMD_PTP_MODE_V2) {
  1225. match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
  1226. } else {
  1227. match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
  1228. BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
  1229. }
  1230. }
  1231. /* Does this packet require timestamping? */
  1232. if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
  1233. match->state = PTP_PACKET_STATE_UNMATCHED;
  1234. /* We expect the sequence number to be in the same position in
  1235. * the packet for PTP V1 and V2
  1236. */
  1237. BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
  1238. BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
  1239. /* Extract UUID/Sequence information */
  1240. match->words[0] = (match_data_012[0] |
  1241. (match_data_012[1] << 8) |
  1242. (match_data_012[2] << 16) |
  1243. (match_data_345[0] << 24));
  1244. match->words[1] = (match_data_345[1] |
  1245. (match_data_345[2] << 8) |
  1246. (data[PTP_V1_SEQUENCE_OFFSET +
  1247. PTP_V1_SEQUENCE_LENGTH - 1] <<
  1248. 16));
  1249. } else {
  1250. match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
  1251. }
  1252. skb_queue_tail(&ptp->rxq, skb);
  1253. queue_work(ptp->workwq, &ptp->work);
  1254. return true;
  1255. }
  1256. /* Transmit a PTP packet. This has to be transmitted by the MC
  1257. * itself, through an MCDI call. MCDI calls aren't permitted
  1258. * in the transmit path so defer the actual transmission to a suitable worker.
  1259. */
  1260. int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1261. {
  1262. struct efx_ptp_data *ptp = efx->ptp_data;
  1263. skb_queue_tail(&ptp->txq, skb);
  1264. if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
  1265. (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
  1266. efx_xmit_hwtstamp_pending(skb);
  1267. queue_work(ptp->workwq, &ptp->work);
  1268. return NETDEV_TX_OK;
  1269. }
  1270. int efx_ptp_get_mode(struct efx_nic *efx)
  1271. {
  1272. return efx->ptp_data->mode;
  1273. }
  1274. int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
  1275. unsigned int new_mode)
  1276. {
  1277. if ((enable_wanted != efx->ptp_data->enabled) ||
  1278. (enable_wanted && (efx->ptp_data->mode != new_mode))) {
  1279. int rc = 0;
  1280. if (enable_wanted) {
  1281. /* Change of mode requires disable */
  1282. if (efx->ptp_data->enabled &&
  1283. (efx->ptp_data->mode != new_mode)) {
  1284. efx->ptp_data->enabled = false;
  1285. rc = efx_ptp_stop(efx);
  1286. if (rc != 0)
  1287. return rc;
  1288. }
  1289. /* Set new operating mode and establish
  1290. * baseline synchronisation, which must
  1291. * succeed.
  1292. */
  1293. efx->ptp_data->mode = new_mode;
  1294. if (netif_running(efx->net_dev))
  1295. rc = efx_ptp_start(efx);
  1296. if (rc == 0) {
  1297. rc = efx_ptp_synchronize(efx,
  1298. PTP_SYNC_ATTEMPTS * 2);
  1299. if (rc != 0)
  1300. efx_ptp_stop(efx);
  1301. }
  1302. } else {
  1303. rc = efx_ptp_stop(efx);
  1304. }
  1305. if (rc != 0)
  1306. return rc;
  1307. efx->ptp_data->enabled = enable_wanted;
  1308. }
  1309. return 0;
  1310. }
  1311. static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
  1312. {
  1313. int rc;
  1314. if (init->flags)
  1315. return -EINVAL;
  1316. if ((init->tx_type != HWTSTAMP_TX_OFF) &&
  1317. (init->tx_type != HWTSTAMP_TX_ON))
  1318. return -ERANGE;
  1319. rc = efx->type->ptp_set_ts_config(efx, init);
  1320. if (rc)
  1321. return rc;
  1322. efx->ptp_data->config = *init;
  1323. return 0;
  1324. }
  1325. void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
  1326. {
  1327. struct efx_ptp_data *ptp = efx->ptp_data;
  1328. struct efx_nic *primary = efx->primary;
  1329. ASSERT_RTNL();
  1330. if (!ptp)
  1331. return;
  1332. ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
  1333. SOF_TIMESTAMPING_RX_HARDWARE |
  1334. SOF_TIMESTAMPING_RAW_HARDWARE);
  1335. if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
  1336. ts_info->phc_index =
  1337. ptp_clock_index(primary->ptp_data->phc_clock);
  1338. ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
  1339. ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
  1340. }
  1341. int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1342. {
  1343. struct hwtstamp_config config;
  1344. int rc;
  1345. /* Not a PTP enabled port */
  1346. if (!efx->ptp_data)
  1347. return -EOPNOTSUPP;
  1348. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1349. return -EFAULT;
  1350. rc = efx_ptp_ts_init(efx, &config);
  1351. if (rc != 0)
  1352. return rc;
  1353. return copy_to_user(ifr->ifr_data, &config, sizeof(config))
  1354. ? -EFAULT : 0;
  1355. }
  1356. int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1357. {
  1358. if (!efx->ptp_data)
  1359. return -EOPNOTSUPP;
  1360. return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
  1361. sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
  1362. }
  1363. static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
  1364. {
  1365. struct efx_ptp_data *ptp = efx->ptp_data;
  1366. netif_err(efx, hw, efx->net_dev,
  1367. "PTP unexpected event length: got %d expected %d\n",
  1368. ptp->evt_frag_idx, expected_frag_len);
  1369. ptp->reset_required = true;
  1370. queue_work(ptp->workwq, &ptp->work);
  1371. }
  1372. /* Process a completed receive event. Put it on the event queue and
  1373. * start worker thread. This is required because event and their
  1374. * correspoding packets may come in either order.
  1375. */
  1376. static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1377. {
  1378. struct efx_ptp_event_rx *evt = NULL;
  1379. if (WARN_ON_ONCE(ptp->rx_ts_inline))
  1380. return;
  1381. if (ptp->evt_frag_idx != 3) {
  1382. ptp_event_failure(efx, 3);
  1383. return;
  1384. }
  1385. spin_lock_bh(&ptp->evt_lock);
  1386. if (!list_empty(&ptp->evt_free_list)) {
  1387. evt = list_first_entry(&ptp->evt_free_list,
  1388. struct efx_ptp_event_rx, link);
  1389. list_del(&evt->link);
  1390. evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
  1391. evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
  1392. MCDI_EVENT_SRC) |
  1393. (EFX_QWORD_FIELD(ptp->evt_frags[1],
  1394. MCDI_EVENT_SRC) << 8) |
  1395. (EFX_QWORD_FIELD(ptp->evt_frags[0],
  1396. MCDI_EVENT_SRC) << 16));
  1397. evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
  1398. EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
  1399. EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
  1400. ptp->ts_corrections.rx);
  1401. evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1402. list_add_tail(&evt->link, &ptp->evt_list);
  1403. queue_work(ptp->workwq, &ptp->work);
  1404. } else if (net_ratelimit()) {
  1405. /* Log a rate-limited warning message. */
  1406. netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
  1407. }
  1408. spin_unlock_bh(&ptp->evt_lock);
  1409. }
  1410. static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1411. {
  1412. int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
  1413. if (ptp->evt_frag_idx != 1) {
  1414. ptp_event_failure(efx, 1);
  1415. return;
  1416. }
  1417. netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
  1418. }
  1419. static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1420. {
  1421. if (ptp->nic_ts_enabled)
  1422. queue_work(ptp->pps_workwq, &ptp->pps_work);
  1423. }
  1424. void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
  1425. {
  1426. struct efx_ptp_data *ptp = efx->ptp_data;
  1427. int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
  1428. if (!ptp) {
  1429. if (net_ratelimit())
  1430. netif_warn(efx, drv, efx->net_dev,
  1431. "Received PTP event but PTP not set up\n");
  1432. return;
  1433. }
  1434. if (!ptp->enabled)
  1435. return;
  1436. if (ptp->evt_frag_idx == 0) {
  1437. ptp->evt_code = code;
  1438. } else if (ptp->evt_code != code) {
  1439. netif_err(efx, hw, efx->net_dev,
  1440. "PTP out of sequence event %d\n", code);
  1441. ptp->evt_frag_idx = 0;
  1442. }
  1443. ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
  1444. if (!MCDI_EVENT_FIELD(*ev, CONT)) {
  1445. /* Process resulting event */
  1446. switch (code) {
  1447. case MCDI_EVENT_CODE_PTP_RX:
  1448. ptp_event_rx(efx, ptp);
  1449. break;
  1450. case MCDI_EVENT_CODE_PTP_FAULT:
  1451. ptp_event_fault(efx, ptp);
  1452. break;
  1453. case MCDI_EVENT_CODE_PTP_PPS:
  1454. ptp_event_pps(efx, ptp);
  1455. break;
  1456. default:
  1457. netif_err(efx, hw, efx->net_dev,
  1458. "PTP unknown event %d\n", code);
  1459. break;
  1460. }
  1461. ptp->evt_frag_idx = 0;
  1462. } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
  1463. netif_err(efx, hw, efx->net_dev,
  1464. "PTP too many event fragments\n");
  1465. ptp->evt_frag_idx = 0;
  1466. }
  1467. }
  1468. void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
  1469. {
  1470. channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
  1471. channel->sync_timestamp_minor =
  1472. MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_26_19) << 19;
  1473. /* if sync events have been disabled then we want to silently ignore
  1474. * this event, so throw away result.
  1475. */
  1476. (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
  1477. SYNC_EVENTS_VALID);
  1478. }
  1479. /* make some assumptions about the time representation rather than abstract it,
  1480. * since we currently only support one type of inline timestamping and only on
  1481. * EF10.
  1482. */
  1483. #define MINOR_TICKS_PER_SECOND 0x8000000
  1484. /* Fuzz factor for sync events to be out of order with RX events */
  1485. #define FUZZ (MINOR_TICKS_PER_SECOND / 10)
  1486. #define EXPECTED_SYNC_EVENTS_PER_SECOND 4
  1487. static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
  1488. {
  1489. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  1490. return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
  1491. #else
  1492. const u8 *data = eh + efx->rx_packet_ts_offset;
  1493. return (u32)data[0] |
  1494. (u32)data[1] << 8 |
  1495. (u32)data[2] << 16 |
  1496. (u32)data[3] << 24;
  1497. #endif
  1498. }
  1499. void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
  1500. struct sk_buff *skb)
  1501. {
  1502. struct efx_nic *efx = channel->efx;
  1503. u32 pkt_timestamp_major, pkt_timestamp_minor;
  1504. u32 diff, carry;
  1505. struct skb_shared_hwtstamps *timestamps;
  1506. pkt_timestamp_minor = (efx_rx_buf_timestamp_minor(efx,
  1507. skb_mac_header(skb)) +
  1508. (u32) efx->ptp_data->ts_corrections.rx) &
  1509. (MINOR_TICKS_PER_SECOND - 1);
  1510. /* get the difference between the packet and sync timestamps,
  1511. * modulo one second
  1512. */
  1513. diff = (pkt_timestamp_minor - channel->sync_timestamp_minor) &
  1514. (MINOR_TICKS_PER_SECOND - 1);
  1515. /* do we roll over a second boundary and need to carry the one? */
  1516. carry = channel->sync_timestamp_minor + diff > MINOR_TICKS_PER_SECOND ?
  1517. 1 : 0;
  1518. if (diff <= MINOR_TICKS_PER_SECOND / EXPECTED_SYNC_EVENTS_PER_SECOND +
  1519. FUZZ) {
  1520. /* packet is ahead of the sync event by a quarter of a second or
  1521. * less (allowing for fuzz)
  1522. */
  1523. pkt_timestamp_major = channel->sync_timestamp_major + carry;
  1524. } else if (diff >= MINOR_TICKS_PER_SECOND - FUZZ) {
  1525. /* packet is behind the sync event but within the fuzz factor.
  1526. * This means the RX packet and sync event crossed as they were
  1527. * placed on the event queue, which can sometimes happen.
  1528. */
  1529. pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
  1530. } else {
  1531. /* it's outside tolerance in both directions. this might be
  1532. * indicative of us missing sync events for some reason, so
  1533. * we'll call it an error rather than risk giving a bogus
  1534. * timestamp.
  1535. */
  1536. netif_vdbg(efx, drv, efx->net_dev,
  1537. "packet timestamp %x too far from sync event %x:%x\n",
  1538. pkt_timestamp_minor, channel->sync_timestamp_major,
  1539. channel->sync_timestamp_minor);
  1540. return;
  1541. }
  1542. /* attach the timestamps to the skb */
  1543. timestamps = skb_hwtstamps(skb);
  1544. timestamps->hwtstamp =
  1545. efx_ptp_s27_to_ktime(pkt_timestamp_major, pkt_timestamp_minor);
  1546. }
  1547. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  1548. {
  1549. struct efx_ptp_data *ptp_data = container_of(ptp,
  1550. struct efx_ptp_data,
  1551. phc_clock_info);
  1552. struct efx_nic *efx = ptp_data->efx;
  1553. MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
  1554. s64 adjustment_ns;
  1555. int rc;
  1556. if (delta > MAX_PPB)
  1557. delta = MAX_PPB;
  1558. else if (delta < -MAX_PPB)
  1559. delta = -MAX_PPB;
  1560. /* Convert ppb to fixed point ns. */
  1561. adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
  1562. (PPB_EXTRA_BITS + MAX_PPB_BITS));
  1563. MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1564. MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
  1565. MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
  1566. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
  1567. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
  1568. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
  1569. NULL, 0, NULL);
  1570. if (rc != 0)
  1571. return rc;
  1572. ptp_data->current_adjfreq = adjustment_ns;
  1573. return 0;
  1574. }
  1575. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1576. {
  1577. u32 nic_major, nic_minor;
  1578. struct efx_ptp_data *ptp_data = container_of(ptp,
  1579. struct efx_ptp_data,
  1580. phc_clock_info);
  1581. struct efx_nic *efx = ptp_data->efx;
  1582. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
  1583. efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
  1584. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1585. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1586. MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
  1587. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
  1588. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
  1589. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1590. NULL, 0, NULL);
  1591. }
  1592. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  1593. {
  1594. struct efx_ptp_data *ptp_data = container_of(ptp,
  1595. struct efx_ptp_data,
  1596. phc_clock_info);
  1597. struct efx_nic *efx = ptp_data->efx;
  1598. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
  1599. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
  1600. int rc;
  1601. ktime_t kt;
  1602. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
  1603. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1604. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1605. outbuf, sizeof(outbuf), NULL);
  1606. if (rc != 0)
  1607. return rc;
  1608. kt = ptp_data->nic_to_kernel_time(
  1609. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
  1610. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
  1611. *ts = ktime_to_timespec64(kt);
  1612. return 0;
  1613. }
  1614. static int efx_phc_settime(struct ptp_clock_info *ptp,
  1615. const struct timespec64 *e_ts)
  1616. {
  1617. /* Get the current NIC time, efx_phc_gettime.
  1618. * Subtract from the desired time to get the offset
  1619. * call efx_phc_adjtime with the offset
  1620. */
  1621. int rc;
  1622. struct timespec64 time_now;
  1623. struct timespec64 delta;
  1624. rc = efx_phc_gettime(ptp, &time_now);
  1625. if (rc != 0)
  1626. return rc;
  1627. delta = timespec64_sub(*e_ts, time_now);
  1628. rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
  1629. if (rc != 0)
  1630. return rc;
  1631. return 0;
  1632. }
  1633. static int efx_phc_enable(struct ptp_clock_info *ptp,
  1634. struct ptp_clock_request *request,
  1635. int enable)
  1636. {
  1637. struct efx_ptp_data *ptp_data = container_of(ptp,
  1638. struct efx_ptp_data,
  1639. phc_clock_info);
  1640. if (request->type != PTP_CLK_REQ_PPS)
  1641. return -EOPNOTSUPP;
  1642. ptp_data->nic_ts_enabled = !!enable;
  1643. return 0;
  1644. }
  1645. static const struct efx_channel_type efx_ptp_channel_type = {
  1646. .handle_no_channel = efx_ptp_handle_no_channel,
  1647. .pre_probe = efx_ptp_probe_channel,
  1648. .post_remove = efx_ptp_remove_channel,
  1649. .get_name = efx_ptp_get_channel_name,
  1650. /* no copy operation; there is no need to reallocate this channel */
  1651. .receive_skb = efx_ptp_rx,
  1652. .keep_eventq = false,
  1653. };
  1654. void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
  1655. {
  1656. /* Check whether PTP is implemented on this NIC. The DISABLE
  1657. * operation will succeed if and only if it is implemented.
  1658. */
  1659. if (efx_ptp_disable(efx) == 0)
  1660. efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
  1661. &efx_ptp_channel_type;
  1662. }
  1663. void efx_ptp_start_datapath(struct efx_nic *efx)
  1664. {
  1665. if (efx_ptp_restart(efx))
  1666. netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
  1667. /* re-enable timestamping if it was previously enabled */
  1668. if (efx->type->ptp_set_ts_sync_events)
  1669. efx->type->ptp_set_ts_sync_events(efx, true, true);
  1670. }
  1671. void efx_ptp_stop_datapath(struct efx_nic *efx)
  1672. {
  1673. /* temporarily disable timestamping */
  1674. if (efx->type->ptp_set_ts_sync_events)
  1675. efx->type->ptp_set_ts_sync_events(efx, false, true);
  1676. efx_ptp_stop(efx);
  1677. }