ptp.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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. netif_err(efx, hw, efx->net_dev,
  350. "MC_CMD_PTP_OP_STATUS failed (%d)\n", rc);
  351. memset(outbuf, 0, sizeof(outbuf));
  352. }
  353. efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
  354. efx_ptp_stat_mask,
  355. stats, _MCDI_PTR(outbuf, 0), false);
  356. return PTP_STAT_COUNT;
  357. }
  358. /* For Siena platforms NIC time is s and ns */
  359. static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
  360. {
  361. struct timespec ts = ns_to_timespec(ns);
  362. *nic_major = ts.tv_sec;
  363. *nic_minor = ts.tv_nsec;
  364. }
  365. static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
  366. s32 correction)
  367. {
  368. ktime_t kt = ktime_set(nic_major, nic_minor);
  369. if (correction >= 0)
  370. kt = ktime_add_ns(kt, (u64)correction);
  371. else
  372. kt = ktime_sub_ns(kt, (u64)-correction);
  373. return kt;
  374. }
  375. /* To convert from s27 format to ns we multiply then divide by a power of 2.
  376. * For the conversion from ns to s27, the operation is also converted to a
  377. * multiply and shift.
  378. */
  379. #define S27_TO_NS_SHIFT (27)
  380. #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
  381. #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
  382. #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
  383. /* For Huntington platforms NIC time is in seconds and fractions of a second
  384. * where the minor register only uses 27 bits in units of 2^-27s.
  385. */
  386. static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
  387. {
  388. struct timespec ts = ns_to_timespec(ns);
  389. u32 maj = ts.tv_sec;
  390. u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
  391. (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
  392. /* The conversion can result in the minor value exceeding the maximum.
  393. * In this case, round up to the next second.
  394. */
  395. if (min >= S27_MINOR_MAX) {
  396. min -= S27_MINOR_MAX;
  397. maj++;
  398. }
  399. *nic_major = maj;
  400. *nic_minor = min;
  401. }
  402. static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
  403. {
  404. u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
  405. (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
  406. return ktime_set(nic_major, ns);
  407. }
  408. static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
  409. s32 correction)
  410. {
  411. /* Apply the correction and deal with carry */
  412. nic_minor += correction;
  413. if ((s32)nic_minor < 0) {
  414. nic_minor += S27_MINOR_MAX;
  415. nic_major--;
  416. } else if (nic_minor >= S27_MINOR_MAX) {
  417. nic_minor -= S27_MINOR_MAX;
  418. nic_major++;
  419. }
  420. return efx_ptp_s27_to_ktime(nic_major, nic_minor);
  421. }
  422. /* Get PTP attributes and set up time conversions */
  423. static int efx_ptp_get_attributes(struct efx_nic *efx)
  424. {
  425. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
  426. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
  427. struct efx_ptp_data *ptp = efx->ptp_data;
  428. int rc;
  429. u32 fmt;
  430. size_t out_len;
  431. /* Get the PTP attributes. If the NIC doesn't support the operation we
  432. * use the default format for compatibility with older NICs i.e.
  433. * seconds and nanoseconds.
  434. */
  435. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
  436. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  437. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  438. outbuf, sizeof(outbuf), &out_len);
  439. if (rc == 0)
  440. fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
  441. else if (rc == -EINVAL)
  442. fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
  443. else
  444. return rc;
  445. if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION) {
  446. ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
  447. ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
  448. } else if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS) {
  449. ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
  450. ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
  451. } else {
  452. return -ERANGE;
  453. }
  454. ptp->time_format = fmt;
  455. /* MC_CMD_PTP_OP_GET_ATTRIBUTES is an extended version of an older
  456. * operation MC_CMD_PTP_OP_GET_TIME_FORMAT that also returns a value
  457. * to use for the minimum acceptable corrected synchronization window.
  458. * If we have the extra information store it. For older firmware that
  459. * does not implement the extended command use the default value.
  460. */
  461. if (rc == 0 && out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
  462. ptp->min_synchronisation_ns =
  463. MCDI_DWORD(outbuf,
  464. PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
  465. else
  466. ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
  467. return 0;
  468. }
  469. /* Get PTP timestamp corrections */
  470. static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
  471. {
  472. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
  473. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_LEN);
  474. int rc;
  475. /* Get the timestamp corrections from the NIC. If this operation is
  476. * not supported (older NICs) then no correction is required.
  477. */
  478. MCDI_SET_DWORD(inbuf, PTP_IN_OP,
  479. MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
  480. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  481. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  482. outbuf, sizeof(outbuf), NULL);
  483. if (rc == 0) {
  484. efx->ptp_data->ts_corrections.tx = MCDI_DWORD(outbuf,
  485. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
  486. efx->ptp_data->ts_corrections.rx = MCDI_DWORD(outbuf,
  487. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
  488. efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
  489. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
  490. efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
  491. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
  492. } else if (rc == -EINVAL) {
  493. efx->ptp_data->ts_corrections.tx = 0;
  494. efx->ptp_data->ts_corrections.rx = 0;
  495. efx->ptp_data->ts_corrections.pps_out = 0;
  496. efx->ptp_data->ts_corrections.pps_in = 0;
  497. } else {
  498. return rc;
  499. }
  500. return 0;
  501. }
  502. /* Enable MCDI PTP support. */
  503. static int efx_ptp_enable(struct efx_nic *efx)
  504. {
  505. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
  506. MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
  507. int rc;
  508. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
  509. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  510. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
  511. efx->ptp_data->channel ?
  512. efx->ptp_data->channel->channel : 0);
  513. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
  514. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  515. outbuf, sizeof(outbuf), NULL);
  516. rc = (rc == -EALREADY) ? 0 : rc;
  517. if (rc)
  518. efx_mcdi_display_error(efx, MC_CMD_PTP,
  519. MC_CMD_PTP_IN_ENABLE_LEN,
  520. outbuf, sizeof(outbuf), rc);
  521. return rc;
  522. }
  523. /* Disable MCDI PTP support.
  524. *
  525. * Note that this function should never rely on the presence of ptp_data -
  526. * may be called before that exists.
  527. */
  528. static int efx_ptp_disable(struct efx_nic *efx)
  529. {
  530. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
  531. MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
  532. int rc;
  533. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
  534. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  535. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  536. outbuf, sizeof(outbuf), NULL);
  537. rc = (rc == -EALREADY) ? 0 : rc;
  538. /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
  539. * should only have been called during probe.
  540. */
  541. if (rc == -ENOSYS || rc == -EPERM)
  542. netif_info(efx, probe, efx->net_dev, "no PTP support\n");
  543. else if (rc)
  544. efx_mcdi_display_error(efx, MC_CMD_PTP,
  545. MC_CMD_PTP_IN_DISABLE_LEN,
  546. outbuf, sizeof(outbuf), rc);
  547. return rc;
  548. }
  549. static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
  550. {
  551. struct sk_buff *skb;
  552. while ((skb = skb_dequeue(q))) {
  553. local_bh_disable();
  554. netif_receive_skb(skb);
  555. local_bh_enable();
  556. }
  557. }
  558. static void efx_ptp_handle_no_channel(struct efx_nic *efx)
  559. {
  560. netif_err(efx, drv, efx->net_dev,
  561. "ERROR: PTP requires MSI-X and 1 additional interrupt"
  562. "vector. PTP disabled\n");
  563. }
  564. /* Repeatedly send the host time to the MC which will capture the hardware
  565. * time.
  566. */
  567. static void efx_ptp_send_times(struct efx_nic *efx,
  568. struct pps_event_time *last_time)
  569. {
  570. struct pps_event_time now;
  571. struct timespec limit;
  572. struct efx_ptp_data *ptp = efx->ptp_data;
  573. struct timespec start;
  574. int *mc_running = ptp->start.addr;
  575. pps_get_ts(&now);
  576. start = now.ts_real;
  577. limit = now.ts_real;
  578. timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
  579. /* Write host time for specified period or until MC is done */
  580. while ((timespec_compare(&now.ts_real, &limit) < 0) &&
  581. ACCESS_ONCE(*mc_running)) {
  582. struct timespec update_time;
  583. unsigned int host_time;
  584. /* Don't update continuously to avoid saturating the PCIe bus */
  585. update_time = now.ts_real;
  586. timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
  587. do {
  588. pps_get_ts(&now);
  589. } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
  590. ACCESS_ONCE(*mc_running));
  591. /* Synchronise NIC with single word of time only */
  592. host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
  593. now.ts_real.tv_nsec);
  594. /* Update host time in NIC memory */
  595. efx->type->ptp_write_host_time(efx, host_time);
  596. }
  597. *last_time = now;
  598. }
  599. /* Read a timeset from the MC's results and partial process. */
  600. static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
  601. struct efx_ptp_timeset *timeset)
  602. {
  603. unsigned start_ns, end_ns;
  604. timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
  605. timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
  606. timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
  607. timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
  608. timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
  609. /* Ignore seconds */
  610. start_ns = timeset->host_start & MC_NANOSECOND_MASK;
  611. end_ns = timeset->host_end & MC_NANOSECOND_MASK;
  612. /* Allow for rollover */
  613. if (end_ns < start_ns)
  614. end_ns += NSEC_PER_SEC;
  615. /* Determine duration of operation */
  616. timeset->window = end_ns - start_ns;
  617. }
  618. /* Process times received from MC.
  619. *
  620. * Extract times from returned results, and establish the minimum value
  621. * seen. The minimum value represents the "best" possible time and events
  622. * too much greater than this are rejected - the machine is, perhaps, too
  623. * busy. A number of readings are taken so that, hopefully, at least one good
  624. * synchronisation will be seen in the results.
  625. */
  626. static int
  627. efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
  628. size_t response_length,
  629. const struct pps_event_time *last_time)
  630. {
  631. unsigned number_readings =
  632. MCDI_VAR_ARRAY_LEN(response_length,
  633. PTP_OUT_SYNCHRONIZE_TIMESET);
  634. unsigned i;
  635. unsigned ngood = 0;
  636. unsigned last_good = 0;
  637. struct efx_ptp_data *ptp = efx->ptp_data;
  638. u32 last_sec;
  639. u32 start_sec;
  640. struct timespec delta;
  641. ktime_t mc_time;
  642. if (number_readings == 0)
  643. return -EAGAIN;
  644. /* Read the set of results and find the last good host-MC
  645. * synchronization result. The MC times when it finishes reading the
  646. * host time so the corrected window time should be fairly constant
  647. * for a given platform. Increment stats for any results that appear
  648. * to be erroneous.
  649. */
  650. for (i = 0; i < number_readings; i++) {
  651. s32 window, corrected;
  652. struct timespec wait;
  653. efx_ptp_read_timeset(
  654. MCDI_ARRAY_STRUCT_PTR(synch_buf,
  655. PTP_OUT_SYNCHRONIZE_TIMESET, i),
  656. &ptp->timeset[i]);
  657. wait = ktime_to_timespec(
  658. ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
  659. window = ptp->timeset[i].window;
  660. corrected = window - wait.tv_nsec;
  661. /* We expect the uncorrected synchronization window to be at
  662. * least as large as the interval between host start and end
  663. * times. If it is smaller than this then this is mostly likely
  664. * to be a consequence of the host's time being adjusted.
  665. * Check that the corrected sync window is in a reasonable
  666. * range. If it is out of range it is likely to be because an
  667. * interrupt or other delay occurred between reading the system
  668. * time and writing it to MC memory.
  669. */
  670. if (window < SYNCHRONISATION_GRANULARITY_NS) {
  671. ++ptp->invalid_sync_windows;
  672. } else if (corrected >= MAX_SYNCHRONISATION_NS) {
  673. ++ptp->oversize_sync_windows;
  674. } else if (corrected < ptp->min_synchronisation_ns) {
  675. ++ptp->undersize_sync_windows;
  676. } else {
  677. ngood++;
  678. last_good = i;
  679. }
  680. }
  681. if (ngood == 0) {
  682. netif_warn(efx, drv, efx->net_dev,
  683. "PTP no suitable synchronisations\n");
  684. return -EAGAIN;
  685. }
  686. /* Calculate delay from last good sync (host time) to last_time.
  687. * It is possible that the seconds rolled over between taking
  688. * the start reading and the last value written by the host. The
  689. * timescales are such that a gap of more than one second is never
  690. * expected. delta is *not* normalised.
  691. */
  692. start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
  693. last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
  694. if (start_sec != last_sec &&
  695. ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
  696. netif_warn(efx, hw, efx->net_dev,
  697. "PTP bad synchronisation seconds\n");
  698. return -EAGAIN;
  699. }
  700. delta.tv_sec = (last_sec - start_sec) & 1;
  701. delta.tv_nsec =
  702. last_time->ts_real.tv_nsec -
  703. (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
  704. /* Convert the NIC time at last good sync into kernel time.
  705. * No correction is required - this time is the output of a
  706. * firmware process.
  707. */
  708. mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
  709. ptp->timeset[last_good].minor, 0);
  710. /* Calculate delay from NIC top of second to last_time */
  711. delta.tv_nsec += ktime_to_timespec(mc_time).tv_nsec;
  712. /* Set PPS timestamp to match NIC top of second */
  713. ptp->host_time_pps = *last_time;
  714. pps_sub_ts(&ptp->host_time_pps, delta);
  715. return 0;
  716. }
  717. /* Synchronize times between the host and the MC */
  718. static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
  719. {
  720. struct efx_ptp_data *ptp = efx->ptp_data;
  721. MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
  722. size_t response_length;
  723. int rc;
  724. unsigned long timeout;
  725. struct pps_event_time last_time = {};
  726. unsigned int loops = 0;
  727. int *start = ptp->start.addr;
  728. MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
  729. MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
  730. MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
  731. num_readings);
  732. MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
  733. ptp->start.dma_addr);
  734. /* Clear flag that signals MC ready */
  735. ACCESS_ONCE(*start) = 0;
  736. rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
  737. MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
  738. EFX_BUG_ON_PARANOID(rc);
  739. /* Wait for start from MCDI (or timeout) */
  740. timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
  741. while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
  742. udelay(20); /* Usually start MCDI execution quickly */
  743. loops++;
  744. }
  745. if (loops <= 1)
  746. ++ptp->fast_syncs;
  747. if (!time_before(jiffies, timeout))
  748. ++ptp->sync_timeouts;
  749. if (ACCESS_ONCE(*start))
  750. efx_ptp_send_times(efx, &last_time);
  751. /* Collect results */
  752. rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
  753. MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
  754. synch_buf, sizeof(synch_buf),
  755. &response_length);
  756. if (rc == 0) {
  757. rc = efx_ptp_process_times(efx, synch_buf, response_length,
  758. &last_time);
  759. if (rc == 0)
  760. ++ptp->good_syncs;
  761. else
  762. ++ptp->no_time_syncs;
  763. }
  764. /* Increment the bad syncs counter if the synchronize fails, whatever
  765. * the reason.
  766. */
  767. if (rc != 0)
  768. ++ptp->bad_syncs;
  769. return rc;
  770. }
  771. /* Transmit a PTP packet, via the MCDI interface, to the wire. */
  772. static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
  773. {
  774. struct efx_ptp_data *ptp_data = efx->ptp_data;
  775. struct skb_shared_hwtstamps timestamps;
  776. int rc = -EIO;
  777. MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
  778. size_t len;
  779. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
  780. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
  781. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
  782. if (skb_shinfo(skb)->nr_frags != 0) {
  783. rc = skb_linearize(skb);
  784. if (rc != 0)
  785. goto fail;
  786. }
  787. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  788. rc = skb_checksum_help(skb);
  789. if (rc != 0)
  790. goto fail;
  791. }
  792. skb_copy_from_linear_data(skb,
  793. MCDI_PTR(ptp_data->txbuf,
  794. PTP_IN_TRANSMIT_PACKET),
  795. skb->len);
  796. rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
  797. ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
  798. txtime, sizeof(txtime), &len);
  799. if (rc != 0)
  800. goto fail;
  801. memset(&timestamps, 0, sizeof(timestamps));
  802. timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
  803. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
  804. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
  805. ptp_data->ts_corrections.tx);
  806. skb_tstamp_tx(skb, &timestamps);
  807. rc = 0;
  808. fail:
  809. dev_kfree_skb(skb);
  810. return rc;
  811. }
  812. static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
  813. {
  814. struct efx_ptp_data *ptp = efx->ptp_data;
  815. struct list_head *cursor;
  816. struct list_head *next;
  817. if (ptp->rx_ts_inline)
  818. return;
  819. /* Drop time-expired events */
  820. spin_lock_bh(&ptp->evt_lock);
  821. if (!list_empty(&ptp->evt_list)) {
  822. list_for_each_safe(cursor, next, &ptp->evt_list) {
  823. struct efx_ptp_event_rx *evt;
  824. evt = list_entry(cursor, struct efx_ptp_event_rx,
  825. link);
  826. if (time_after(jiffies, evt->expiry)) {
  827. list_move(&evt->link, &ptp->evt_free_list);
  828. netif_warn(efx, hw, efx->net_dev,
  829. "PTP rx event dropped\n");
  830. }
  831. }
  832. }
  833. spin_unlock_bh(&ptp->evt_lock);
  834. }
  835. static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
  836. struct sk_buff *skb)
  837. {
  838. struct efx_ptp_data *ptp = efx->ptp_data;
  839. bool evts_waiting;
  840. struct list_head *cursor;
  841. struct list_head *next;
  842. struct efx_ptp_match *match;
  843. enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
  844. WARN_ON_ONCE(ptp->rx_ts_inline);
  845. spin_lock_bh(&ptp->evt_lock);
  846. evts_waiting = !list_empty(&ptp->evt_list);
  847. spin_unlock_bh(&ptp->evt_lock);
  848. if (!evts_waiting)
  849. return PTP_PACKET_STATE_UNMATCHED;
  850. match = (struct efx_ptp_match *)skb->cb;
  851. /* Look for a matching timestamp in the event queue */
  852. spin_lock_bh(&ptp->evt_lock);
  853. list_for_each_safe(cursor, next, &ptp->evt_list) {
  854. struct efx_ptp_event_rx *evt;
  855. evt = list_entry(cursor, struct efx_ptp_event_rx, link);
  856. if ((evt->seq0 == match->words[0]) &&
  857. (evt->seq1 == match->words[1])) {
  858. struct skb_shared_hwtstamps *timestamps;
  859. /* Match - add in hardware timestamp */
  860. timestamps = skb_hwtstamps(skb);
  861. timestamps->hwtstamp = evt->hwtimestamp;
  862. match->state = PTP_PACKET_STATE_MATCHED;
  863. rc = PTP_PACKET_STATE_MATCHED;
  864. list_move(&evt->link, &ptp->evt_free_list);
  865. break;
  866. }
  867. }
  868. spin_unlock_bh(&ptp->evt_lock);
  869. return rc;
  870. }
  871. /* Process any queued receive events and corresponding packets
  872. *
  873. * q is returned with all the packets that are ready for delivery.
  874. */
  875. static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
  876. {
  877. struct efx_ptp_data *ptp = efx->ptp_data;
  878. struct sk_buff *skb;
  879. while ((skb = skb_dequeue(&ptp->rxq))) {
  880. struct efx_ptp_match *match;
  881. match = (struct efx_ptp_match *)skb->cb;
  882. if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
  883. __skb_queue_tail(q, skb);
  884. } else if (efx_ptp_match_rx(efx, skb) ==
  885. PTP_PACKET_STATE_MATCHED) {
  886. __skb_queue_tail(q, skb);
  887. } else if (time_after(jiffies, match->expiry)) {
  888. match->state = PTP_PACKET_STATE_TIMED_OUT;
  889. ++ptp->rx_no_timestamp;
  890. __skb_queue_tail(q, skb);
  891. } else {
  892. /* Replace unprocessed entry and stop */
  893. skb_queue_head(&ptp->rxq, skb);
  894. break;
  895. }
  896. }
  897. }
  898. /* Complete processing of a received packet */
  899. static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
  900. {
  901. local_bh_disable();
  902. netif_receive_skb(skb);
  903. local_bh_enable();
  904. }
  905. static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
  906. {
  907. struct efx_ptp_data *ptp = efx->ptp_data;
  908. if (ptp->rxfilter_installed) {
  909. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  910. ptp->rxfilter_general);
  911. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  912. ptp->rxfilter_event);
  913. ptp->rxfilter_installed = false;
  914. }
  915. }
  916. static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
  917. {
  918. struct efx_ptp_data *ptp = efx->ptp_data;
  919. struct efx_filter_spec rxfilter;
  920. int rc;
  921. if (!ptp->channel || ptp->rxfilter_installed)
  922. return 0;
  923. /* Must filter on both event and general ports to ensure
  924. * that there is no packet re-ordering.
  925. */
  926. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  927. efx_rx_queue_index(
  928. efx_channel_get_rx_queue(ptp->channel)));
  929. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  930. htonl(PTP_ADDRESS),
  931. htons(PTP_EVENT_PORT));
  932. if (rc != 0)
  933. return rc;
  934. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  935. if (rc < 0)
  936. return rc;
  937. ptp->rxfilter_event = rc;
  938. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  939. efx_rx_queue_index(
  940. efx_channel_get_rx_queue(ptp->channel)));
  941. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  942. htonl(PTP_ADDRESS),
  943. htons(PTP_GENERAL_PORT));
  944. if (rc != 0)
  945. goto fail;
  946. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  947. if (rc < 0)
  948. goto fail;
  949. ptp->rxfilter_general = rc;
  950. ptp->rxfilter_installed = true;
  951. return 0;
  952. fail:
  953. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  954. ptp->rxfilter_event);
  955. return rc;
  956. }
  957. static int efx_ptp_start(struct efx_nic *efx)
  958. {
  959. struct efx_ptp_data *ptp = efx->ptp_data;
  960. int rc;
  961. ptp->reset_required = false;
  962. rc = efx_ptp_insert_multicast_filters(efx);
  963. if (rc)
  964. return rc;
  965. rc = efx_ptp_enable(efx);
  966. if (rc != 0)
  967. goto fail;
  968. ptp->evt_frag_idx = 0;
  969. ptp->current_adjfreq = 0;
  970. return 0;
  971. fail:
  972. efx_ptp_remove_multicast_filters(efx);
  973. return rc;
  974. }
  975. static int efx_ptp_stop(struct efx_nic *efx)
  976. {
  977. struct efx_ptp_data *ptp = efx->ptp_data;
  978. struct list_head *cursor;
  979. struct list_head *next;
  980. int rc;
  981. if (ptp == NULL)
  982. return 0;
  983. rc = efx_ptp_disable(efx);
  984. efx_ptp_remove_multicast_filters(efx);
  985. /* Make sure RX packets are really delivered */
  986. efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
  987. skb_queue_purge(&efx->ptp_data->txq);
  988. /* Drop any pending receive events */
  989. spin_lock_bh(&efx->ptp_data->evt_lock);
  990. list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
  991. list_move(cursor, &efx->ptp_data->evt_free_list);
  992. }
  993. spin_unlock_bh(&efx->ptp_data->evt_lock);
  994. return rc;
  995. }
  996. static int efx_ptp_restart(struct efx_nic *efx)
  997. {
  998. if (efx->ptp_data && efx->ptp_data->enabled)
  999. return efx_ptp_start(efx);
  1000. return 0;
  1001. }
  1002. static void efx_ptp_pps_worker(struct work_struct *work)
  1003. {
  1004. struct efx_ptp_data *ptp =
  1005. container_of(work, struct efx_ptp_data, pps_work);
  1006. struct efx_nic *efx = ptp->efx;
  1007. struct ptp_clock_event ptp_evt;
  1008. if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
  1009. return;
  1010. ptp_evt.type = PTP_CLOCK_PPSUSR;
  1011. ptp_evt.pps_times = ptp->host_time_pps;
  1012. ptp_clock_event(ptp->phc_clock, &ptp_evt);
  1013. }
  1014. static void efx_ptp_worker(struct work_struct *work)
  1015. {
  1016. struct efx_ptp_data *ptp_data =
  1017. container_of(work, struct efx_ptp_data, work);
  1018. struct efx_nic *efx = ptp_data->efx;
  1019. struct sk_buff *skb;
  1020. struct sk_buff_head tempq;
  1021. if (ptp_data->reset_required) {
  1022. efx_ptp_stop(efx);
  1023. efx_ptp_start(efx);
  1024. return;
  1025. }
  1026. efx_ptp_drop_time_expired_events(efx);
  1027. __skb_queue_head_init(&tempq);
  1028. efx_ptp_process_events(efx, &tempq);
  1029. while ((skb = skb_dequeue(&ptp_data->txq)))
  1030. efx_ptp_xmit_skb(efx, skb);
  1031. while ((skb = __skb_dequeue(&tempq)))
  1032. efx_ptp_process_rx(efx, skb);
  1033. }
  1034. static const struct ptp_clock_info efx_phc_clock_info = {
  1035. .owner = THIS_MODULE,
  1036. .name = "sfc",
  1037. .max_adj = MAX_PPB,
  1038. .n_alarm = 0,
  1039. .n_ext_ts = 0,
  1040. .n_per_out = 0,
  1041. .n_pins = 0,
  1042. .pps = 1,
  1043. .adjfreq = efx_phc_adjfreq,
  1044. .adjtime = efx_phc_adjtime,
  1045. .gettime64 = efx_phc_gettime,
  1046. .settime64 = efx_phc_settime,
  1047. .enable = efx_phc_enable,
  1048. };
  1049. /* Initialise PTP state. */
  1050. int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
  1051. {
  1052. struct efx_ptp_data *ptp;
  1053. int rc = 0;
  1054. unsigned int pos;
  1055. ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
  1056. efx->ptp_data = ptp;
  1057. if (!efx->ptp_data)
  1058. return -ENOMEM;
  1059. ptp->efx = efx;
  1060. ptp->channel = channel;
  1061. ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
  1062. rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
  1063. if (rc != 0)
  1064. goto fail1;
  1065. skb_queue_head_init(&ptp->rxq);
  1066. skb_queue_head_init(&ptp->txq);
  1067. ptp->workwq = create_singlethread_workqueue("sfc_ptp");
  1068. if (!ptp->workwq) {
  1069. rc = -ENOMEM;
  1070. goto fail2;
  1071. }
  1072. INIT_WORK(&ptp->work, efx_ptp_worker);
  1073. ptp->config.flags = 0;
  1074. ptp->config.tx_type = HWTSTAMP_TX_OFF;
  1075. ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
  1076. INIT_LIST_HEAD(&ptp->evt_list);
  1077. INIT_LIST_HEAD(&ptp->evt_free_list);
  1078. spin_lock_init(&ptp->evt_lock);
  1079. for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
  1080. list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
  1081. /* Get the NIC PTP attributes and set up time conversions */
  1082. rc = efx_ptp_get_attributes(efx);
  1083. if (rc < 0)
  1084. goto fail3;
  1085. /* Get the timestamp corrections */
  1086. rc = efx_ptp_get_timestamp_corrections(efx);
  1087. if (rc < 0)
  1088. goto fail3;
  1089. if (efx->mcdi->fn_flags &
  1090. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
  1091. ptp->phc_clock_info = efx_phc_clock_info;
  1092. ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
  1093. &efx->pci_dev->dev);
  1094. if (IS_ERR(ptp->phc_clock)) {
  1095. rc = PTR_ERR(ptp->phc_clock);
  1096. goto fail3;
  1097. }
  1098. INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
  1099. ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
  1100. if (!ptp->pps_workwq) {
  1101. rc = -ENOMEM;
  1102. goto fail4;
  1103. }
  1104. }
  1105. ptp->nic_ts_enabled = false;
  1106. return 0;
  1107. fail4:
  1108. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1109. fail3:
  1110. destroy_workqueue(efx->ptp_data->workwq);
  1111. fail2:
  1112. efx_nic_free_buffer(efx, &ptp->start);
  1113. fail1:
  1114. kfree(efx->ptp_data);
  1115. efx->ptp_data = NULL;
  1116. return rc;
  1117. }
  1118. /* Initialise PTP channel.
  1119. *
  1120. * Setting core_index to zero causes the queue to be initialised and doesn't
  1121. * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
  1122. */
  1123. static int efx_ptp_probe_channel(struct efx_channel *channel)
  1124. {
  1125. struct efx_nic *efx = channel->efx;
  1126. channel->irq_moderation = 0;
  1127. channel->rx_queue.core_index = 0;
  1128. return efx_ptp_probe(efx, channel);
  1129. }
  1130. void efx_ptp_remove(struct efx_nic *efx)
  1131. {
  1132. if (!efx->ptp_data)
  1133. return;
  1134. (void)efx_ptp_disable(efx);
  1135. cancel_work_sync(&efx->ptp_data->work);
  1136. cancel_work_sync(&efx->ptp_data->pps_work);
  1137. skb_queue_purge(&efx->ptp_data->rxq);
  1138. skb_queue_purge(&efx->ptp_data->txq);
  1139. if (efx->ptp_data->phc_clock) {
  1140. destroy_workqueue(efx->ptp_data->pps_workwq);
  1141. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1142. }
  1143. destroy_workqueue(efx->ptp_data->workwq);
  1144. efx_nic_free_buffer(efx, &efx->ptp_data->start);
  1145. kfree(efx->ptp_data);
  1146. }
  1147. static void efx_ptp_remove_channel(struct efx_channel *channel)
  1148. {
  1149. efx_ptp_remove(channel->efx);
  1150. }
  1151. static void efx_ptp_get_channel_name(struct efx_channel *channel,
  1152. char *buf, size_t len)
  1153. {
  1154. snprintf(buf, len, "%s-ptp", channel->efx->name);
  1155. }
  1156. /* Determine whether this packet should be processed by the PTP module
  1157. * or transmitted conventionally.
  1158. */
  1159. bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1160. {
  1161. return efx->ptp_data &&
  1162. efx->ptp_data->enabled &&
  1163. skb->len >= PTP_MIN_LENGTH &&
  1164. skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
  1165. likely(skb->protocol == htons(ETH_P_IP)) &&
  1166. skb_transport_header_was_set(skb) &&
  1167. skb_network_header_len(skb) >= sizeof(struct iphdr) &&
  1168. ip_hdr(skb)->protocol == IPPROTO_UDP &&
  1169. skb_headlen(skb) >=
  1170. skb_transport_offset(skb) + sizeof(struct udphdr) &&
  1171. udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
  1172. }
  1173. /* Receive a PTP packet. Packets are queued until the arrival of
  1174. * the receive timestamp from the MC - this will probably occur after the
  1175. * packet arrival because of the processing in the MC.
  1176. */
  1177. static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
  1178. {
  1179. struct efx_nic *efx = channel->efx;
  1180. struct efx_ptp_data *ptp = efx->ptp_data;
  1181. struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
  1182. u8 *match_data_012, *match_data_345;
  1183. unsigned int version;
  1184. u8 *data;
  1185. match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1186. /* Correct version? */
  1187. if (ptp->mode == MC_CMD_PTP_MODE_V1) {
  1188. if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
  1189. return false;
  1190. }
  1191. data = skb->data;
  1192. version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
  1193. if (version != PTP_VERSION_V1) {
  1194. return false;
  1195. }
  1196. /* PTP V1 uses all six bytes of the UUID to match the packet
  1197. * to the timestamp
  1198. */
  1199. match_data_012 = data + PTP_V1_UUID_OFFSET;
  1200. match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
  1201. } else {
  1202. if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
  1203. return false;
  1204. }
  1205. data = skb->data;
  1206. version = data[PTP_V2_VERSION_OFFSET];
  1207. if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
  1208. return false;
  1209. }
  1210. /* The original V2 implementation uses bytes 2-7 of
  1211. * the UUID to match the packet to the timestamp. This
  1212. * discards two of the bytes of the MAC address used
  1213. * to create the UUID (SF bug 33070). The PTP V2
  1214. * enhanced mode fixes this issue and uses bytes 0-2
  1215. * and byte 5-7 of the UUID.
  1216. */
  1217. match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
  1218. if (ptp->mode == MC_CMD_PTP_MODE_V2) {
  1219. match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
  1220. } else {
  1221. match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
  1222. BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
  1223. }
  1224. }
  1225. /* Does this packet require timestamping? */
  1226. if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
  1227. match->state = PTP_PACKET_STATE_UNMATCHED;
  1228. /* We expect the sequence number to be in the same position in
  1229. * the packet for PTP V1 and V2
  1230. */
  1231. BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
  1232. BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
  1233. /* Extract UUID/Sequence information */
  1234. match->words[0] = (match_data_012[0] |
  1235. (match_data_012[1] << 8) |
  1236. (match_data_012[2] << 16) |
  1237. (match_data_345[0] << 24));
  1238. match->words[1] = (match_data_345[1] |
  1239. (match_data_345[2] << 8) |
  1240. (data[PTP_V1_SEQUENCE_OFFSET +
  1241. PTP_V1_SEQUENCE_LENGTH - 1] <<
  1242. 16));
  1243. } else {
  1244. match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
  1245. }
  1246. skb_queue_tail(&ptp->rxq, skb);
  1247. queue_work(ptp->workwq, &ptp->work);
  1248. return true;
  1249. }
  1250. /* Transmit a PTP packet. This has to be transmitted by the MC
  1251. * itself, through an MCDI call. MCDI calls aren't permitted
  1252. * in the transmit path so defer the actual transmission to a suitable worker.
  1253. */
  1254. int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1255. {
  1256. struct efx_ptp_data *ptp = efx->ptp_data;
  1257. skb_queue_tail(&ptp->txq, skb);
  1258. if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
  1259. (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
  1260. efx_xmit_hwtstamp_pending(skb);
  1261. queue_work(ptp->workwq, &ptp->work);
  1262. return NETDEV_TX_OK;
  1263. }
  1264. int efx_ptp_get_mode(struct efx_nic *efx)
  1265. {
  1266. return efx->ptp_data->mode;
  1267. }
  1268. int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
  1269. unsigned int new_mode)
  1270. {
  1271. if ((enable_wanted != efx->ptp_data->enabled) ||
  1272. (enable_wanted && (efx->ptp_data->mode != new_mode))) {
  1273. int rc = 0;
  1274. if (enable_wanted) {
  1275. /* Change of mode requires disable */
  1276. if (efx->ptp_data->enabled &&
  1277. (efx->ptp_data->mode != new_mode)) {
  1278. efx->ptp_data->enabled = false;
  1279. rc = efx_ptp_stop(efx);
  1280. if (rc != 0)
  1281. return rc;
  1282. }
  1283. /* Set new operating mode and establish
  1284. * baseline synchronisation, which must
  1285. * succeed.
  1286. */
  1287. efx->ptp_data->mode = new_mode;
  1288. if (netif_running(efx->net_dev))
  1289. rc = efx_ptp_start(efx);
  1290. if (rc == 0) {
  1291. rc = efx_ptp_synchronize(efx,
  1292. PTP_SYNC_ATTEMPTS * 2);
  1293. if (rc != 0)
  1294. efx_ptp_stop(efx);
  1295. }
  1296. } else {
  1297. rc = efx_ptp_stop(efx);
  1298. }
  1299. if (rc != 0)
  1300. return rc;
  1301. efx->ptp_data->enabled = enable_wanted;
  1302. }
  1303. return 0;
  1304. }
  1305. static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
  1306. {
  1307. int rc;
  1308. if (init->flags)
  1309. return -EINVAL;
  1310. if ((init->tx_type != HWTSTAMP_TX_OFF) &&
  1311. (init->tx_type != HWTSTAMP_TX_ON))
  1312. return -ERANGE;
  1313. rc = efx->type->ptp_set_ts_config(efx, init);
  1314. if (rc)
  1315. return rc;
  1316. efx->ptp_data->config = *init;
  1317. return 0;
  1318. }
  1319. void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
  1320. {
  1321. struct efx_ptp_data *ptp = efx->ptp_data;
  1322. struct efx_nic *primary = efx->primary;
  1323. ASSERT_RTNL();
  1324. if (!ptp)
  1325. return;
  1326. ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
  1327. SOF_TIMESTAMPING_RX_HARDWARE |
  1328. SOF_TIMESTAMPING_RAW_HARDWARE);
  1329. if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
  1330. ts_info->phc_index =
  1331. ptp_clock_index(primary->ptp_data->phc_clock);
  1332. ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
  1333. ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
  1334. }
  1335. int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1336. {
  1337. struct hwtstamp_config config;
  1338. int rc;
  1339. /* Not a PTP enabled port */
  1340. if (!efx->ptp_data)
  1341. return -EOPNOTSUPP;
  1342. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1343. return -EFAULT;
  1344. rc = efx_ptp_ts_init(efx, &config);
  1345. if (rc != 0)
  1346. return rc;
  1347. return copy_to_user(ifr->ifr_data, &config, sizeof(config))
  1348. ? -EFAULT : 0;
  1349. }
  1350. int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1351. {
  1352. if (!efx->ptp_data)
  1353. return -EOPNOTSUPP;
  1354. return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
  1355. sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
  1356. }
  1357. static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
  1358. {
  1359. struct efx_ptp_data *ptp = efx->ptp_data;
  1360. netif_err(efx, hw, efx->net_dev,
  1361. "PTP unexpected event length: got %d expected %d\n",
  1362. ptp->evt_frag_idx, expected_frag_len);
  1363. ptp->reset_required = true;
  1364. queue_work(ptp->workwq, &ptp->work);
  1365. }
  1366. /* Process a completed receive event. Put it on the event queue and
  1367. * start worker thread. This is required because event and their
  1368. * correspoding packets may come in either order.
  1369. */
  1370. static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1371. {
  1372. struct efx_ptp_event_rx *evt = NULL;
  1373. if (WARN_ON_ONCE(ptp->rx_ts_inline))
  1374. return;
  1375. if (ptp->evt_frag_idx != 3) {
  1376. ptp_event_failure(efx, 3);
  1377. return;
  1378. }
  1379. spin_lock_bh(&ptp->evt_lock);
  1380. if (!list_empty(&ptp->evt_free_list)) {
  1381. evt = list_first_entry(&ptp->evt_free_list,
  1382. struct efx_ptp_event_rx, link);
  1383. list_del(&evt->link);
  1384. evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
  1385. evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
  1386. MCDI_EVENT_SRC) |
  1387. (EFX_QWORD_FIELD(ptp->evt_frags[1],
  1388. MCDI_EVENT_SRC) << 8) |
  1389. (EFX_QWORD_FIELD(ptp->evt_frags[0],
  1390. MCDI_EVENT_SRC) << 16));
  1391. evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
  1392. EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
  1393. EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
  1394. ptp->ts_corrections.rx);
  1395. evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1396. list_add_tail(&evt->link, &ptp->evt_list);
  1397. queue_work(ptp->workwq, &ptp->work);
  1398. } else if (net_ratelimit()) {
  1399. /* Log a rate-limited warning message. */
  1400. netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
  1401. }
  1402. spin_unlock_bh(&ptp->evt_lock);
  1403. }
  1404. static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1405. {
  1406. int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
  1407. if (ptp->evt_frag_idx != 1) {
  1408. ptp_event_failure(efx, 1);
  1409. return;
  1410. }
  1411. netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
  1412. }
  1413. static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1414. {
  1415. if (ptp->nic_ts_enabled)
  1416. queue_work(ptp->pps_workwq, &ptp->pps_work);
  1417. }
  1418. void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
  1419. {
  1420. struct efx_ptp_data *ptp = efx->ptp_data;
  1421. int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
  1422. if (!ptp) {
  1423. if (net_ratelimit())
  1424. netif_warn(efx, drv, efx->net_dev,
  1425. "Received PTP event but PTP not set up\n");
  1426. return;
  1427. }
  1428. if (!ptp->enabled)
  1429. return;
  1430. if (ptp->evt_frag_idx == 0) {
  1431. ptp->evt_code = code;
  1432. } else if (ptp->evt_code != code) {
  1433. netif_err(efx, hw, efx->net_dev,
  1434. "PTP out of sequence event %d\n", code);
  1435. ptp->evt_frag_idx = 0;
  1436. }
  1437. ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
  1438. if (!MCDI_EVENT_FIELD(*ev, CONT)) {
  1439. /* Process resulting event */
  1440. switch (code) {
  1441. case MCDI_EVENT_CODE_PTP_RX:
  1442. ptp_event_rx(efx, ptp);
  1443. break;
  1444. case MCDI_EVENT_CODE_PTP_FAULT:
  1445. ptp_event_fault(efx, ptp);
  1446. break;
  1447. case MCDI_EVENT_CODE_PTP_PPS:
  1448. ptp_event_pps(efx, ptp);
  1449. break;
  1450. default:
  1451. netif_err(efx, hw, efx->net_dev,
  1452. "PTP unknown event %d\n", code);
  1453. break;
  1454. }
  1455. ptp->evt_frag_idx = 0;
  1456. } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
  1457. netif_err(efx, hw, efx->net_dev,
  1458. "PTP too many event fragments\n");
  1459. ptp->evt_frag_idx = 0;
  1460. }
  1461. }
  1462. void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
  1463. {
  1464. channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
  1465. channel->sync_timestamp_minor =
  1466. MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_26_19) << 19;
  1467. /* if sync events have been disabled then we want to silently ignore
  1468. * this event, so throw away result.
  1469. */
  1470. (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
  1471. SYNC_EVENTS_VALID);
  1472. }
  1473. /* make some assumptions about the time representation rather than abstract it,
  1474. * since we currently only support one type of inline timestamping and only on
  1475. * EF10.
  1476. */
  1477. #define MINOR_TICKS_PER_SECOND 0x8000000
  1478. /* Fuzz factor for sync events to be out of order with RX events */
  1479. #define FUZZ (MINOR_TICKS_PER_SECOND / 10)
  1480. #define EXPECTED_SYNC_EVENTS_PER_SECOND 4
  1481. static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
  1482. {
  1483. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  1484. return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
  1485. #else
  1486. const u8 *data = eh + efx->rx_packet_ts_offset;
  1487. return (u32)data[0] |
  1488. (u32)data[1] << 8 |
  1489. (u32)data[2] << 16 |
  1490. (u32)data[3] << 24;
  1491. #endif
  1492. }
  1493. void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
  1494. struct sk_buff *skb)
  1495. {
  1496. struct efx_nic *efx = channel->efx;
  1497. u32 pkt_timestamp_major, pkt_timestamp_minor;
  1498. u32 diff, carry;
  1499. struct skb_shared_hwtstamps *timestamps;
  1500. pkt_timestamp_minor = (efx_rx_buf_timestamp_minor(efx,
  1501. skb_mac_header(skb)) +
  1502. (u32) efx->ptp_data->ts_corrections.rx) &
  1503. (MINOR_TICKS_PER_SECOND - 1);
  1504. /* get the difference between the packet and sync timestamps,
  1505. * modulo one second
  1506. */
  1507. diff = (pkt_timestamp_minor - channel->sync_timestamp_minor) &
  1508. (MINOR_TICKS_PER_SECOND - 1);
  1509. /* do we roll over a second boundary and need to carry the one? */
  1510. carry = channel->sync_timestamp_minor + diff > MINOR_TICKS_PER_SECOND ?
  1511. 1 : 0;
  1512. if (diff <= MINOR_TICKS_PER_SECOND / EXPECTED_SYNC_EVENTS_PER_SECOND +
  1513. FUZZ) {
  1514. /* packet is ahead of the sync event by a quarter of a second or
  1515. * less (allowing for fuzz)
  1516. */
  1517. pkt_timestamp_major = channel->sync_timestamp_major + carry;
  1518. } else if (diff >= MINOR_TICKS_PER_SECOND - FUZZ) {
  1519. /* packet is behind the sync event but within the fuzz factor.
  1520. * This means the RX packet and sync event crossed as they were
  1521. * placed on the event queue, which can sometimes happen.
  1522. */
  1523. pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
  1524. } else {
  1525. /* it's outside tolerance in both directions. this might be
  1526. * indicative of us missing sync events for some reason, so
  1527. * we'll call it an error rather than risk giving a bogus
  1528. * timestamp.
  1529. */
  1530. netif_vdbg(efx, drv, efx->net_dev,
  1531. "packet timestamp %x too far from sync event %x:%x\n",
  1532. pkt_timestamp_minor, channel->sync_timestamp_major,
  1533. channel->sync_timestamp_minor);
  1534. return;
  1535. }
  1536. /* attach the timestamps to the skb */
  1537. timestamps = skb_hwtstamps(skb);
  1538. timestamps->hwtstamp =
  1539. efx_ptp_s27_to_ktime(pkt_timestamp_major, pkt_timestamp_minor);
  1540. }
  1541. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  1542. {
  1543. struct efx_ptp_data *ptp_data = container_of(ptp,
  1544. struct efx_ptp_data,
  1545. phc_clock_info);
  1546. struct efx_nic *efx = ptp_data->efx;
  1547. MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
  1548. s64 adjustment_ns;
  1549. int rc;
  1550. if (delta > MAX_PPB)
  1551. delta = MAX_PPB;
  1552. else if (delta < -MAX_PPB)
  1553. delta = -MAX_PPB;
  1554. /* Convert ppb to fixed point ns. */
  1555. adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
  1556. (PPB_EXTRA_BITS + MAX_PPB_BITS));
  1557. MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1558. MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
  1559. MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
  1560. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
  1561. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
  1562. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
  1563. NULL, 0, NULL);
  1564. if (rc != 0)
  1565. return rc;
  1566. ptp_data->current_adjfreq = adjustment_ns;
  1567. return 0;
  1568. }
  1569. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1570. {
  1571. u32 nic_major, nic_minor;
  1572. struct efx_ptp_data *ptp_data = container_of(ptp,
  1573. struct efx_ptp_data,
  1574. phc_clock_info);
  1575. struct efx_nic *efx = ptp_data->efx;
  1576. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
  1577. efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
  1578. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1579. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1580. MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
  1581. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
  1582. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
  1583. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1584. NULL, 0, NULL);
  1585. }
  1586. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  1587. {
  1588. struct efx_ptp_data *ptp_data = container_of(ptp,
  1589. struct efx_ptp_data,
  1590. phc_clock_info);
  1591. struct efx_nic *efx = ptp_data->efx;
  1592. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
  1593. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
  1594. int rc;
  1595. ktime_t kt;
  1596. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
  1597. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1598. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1599. outbuf, sizeof(outbuf), NULL);
  1600. if (rc != 0)
  1601. return rc;
  1602. kt = ptp_data->nic_to_kernel_time(
  1603. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
  1604. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
  1605. *ts = ktime_to_timespec64(kt);
  1606. return 0;
  1607. }
  1608. static int efx_phc_settime(struct ptp_clock_info *ptp,
  1609. const struct timespec64 *e_ts)
  1610. {
  1611. /* Get the current NIC time, efx_phc_gettime.
  1612. * Subtract from the desired time to get the offset
  1613. * call efx_phc_adjtime with the offset
  1614. */
  1615. int rc;
  1616. struct timespec64 time_now;
  1617. struct timespec64 delta;
  1618. rc = efx_phc_gettime(ptp, &time_now);
  1619. if (rc != 0)
  1620. return rc;
  1621. delta = timespec64_sub(*e_ts, time_now);
  1622. rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
  1623. if (rc != 0)
  1624. return rc;
  1625. return 0;
  1626. }
  1627. static int efx_phc_enable(struct ptp_clock_info *ptp,
  1628. struct ptp_clock_request *request,
  1629. int enable)
  1630. {
  1631. struct efx_ptp_data *ptp_data = container_of(ptp,
  1632. struct efx_ptp_data,
  1633. phc_clock_info);
  1634. if (request->type != PTP_CLK_REQ_PPS)
  1635. return -EOPNOTSUPP;
  1636. ptp_data->nic_ts_enabled = !!enable;
  1637. return 0;
  1638. }
  1639. static const struct efx_channel_type efx_ptp_channel_type = {
  1640. .handle_no_channel = efx_ptp_handle_no_channel,
  1641. .pre_probe = efx_ptp_probe_channel,
  1642. .post_remove = efx_ptp_remove_channel,
  1643. .get_name = efx_ptp_get_channel_name,
  1644. /* no copy operation; there is no need to reallocate this channel */
  1645. .receive_skb = efx_ptp_rx,
  1646. .keep_eventq = false,
  1647. };
  1648. void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
  1649. {
  1650. /* Check whether PTP is implemented on this NIC. The DISABLE
  1651. * operation will succeed if and only if it is implemented.
  1652. */
  1653. if (efx_ptp_disable(efx) == 0)
  1654. efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
  1655. &efx_ptp_channel_type;
  1656. }
  1657. void efx_ptp_start_datapath(struct efx_nic *efx)
  1658. {
  1659. if (efx_ptp_restart(efx))
  1660. netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
  1661. /* re-enable timestamping if it was previously enabled */
  1662. if (efx->type->ptp_set_ts_sync_events)
  1663. efx->type->ptp_set_ts_sync_events(efx, true, true);
  1664. }
  1665. void efx_ptp_stop_datapath(struct efx_nic *efx)
  1666. {
  1667. /* temporarily disable timestamping */
  1668. if (efx->type->ptp_set_ts_sync_events)
  1669. efx->type->ptp_set_ts_sync_events(efx, false, true);
  1670. efx_ptp_stop(efx);
  1671. }