ptp.c 57 KB

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