ptp.c 56 KB

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