ptp.c 64 KB

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