ptp.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. u32 sync_timestamp;
  501. ktime_t kt = { 0 };
  502. s16 delta;
  503. if (!(nic_major & 0x80000000)) {
  504. WARN_ON_ONCE(nic_major >> 16);
  505. /* Medford provides 48 bits of timestamp, so we must get the top
  506. * 16 bits from the timesync event state.
  507. *
  508. * We only have the lower 16 bits of the time now, but we do
  509. * have a full resolution timestamp at some point in past. As
  510. * long as the difference between the (real) now and the sync
  511. * is less than 2^15, then we can reconstruct the difference
  512. * between those two numbers using only the lower 16 bits of
  513. * each.
  514. *
  515. * Put another way
  516. *
  517. * a - b = ((a mod k) - b) mod k
  518. *
  519. * when -k/2 < (a-b) < k/2. In our case k is 2^16. We know
  520. * (a mod k) and b, so can calculate the delta, a - b.
  521. *
  522. */
  523. sync_timestamp = last_sync_timestamp_major(efx);
  524. /* Because delta is s16 this does an implicit mask down to
  525. * 16 bits which is what we need, assuming
  526. * MEDFORD_TX_SECS_EVENT_BITS is 16. delta is signed so that
  527. * we can deal with the (unlikely) case of sync timestamps
  528. * arriving from the future.
  529. */
  530. delta = nic_major - sync_timestamp;
  531. /* Recover the fully specified time now, by applying the offset
  532. * to the (fully specified) sync time.
  533. */
  534. nic_major = sync_timestamp + delta;
  535. kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
  536. correction);
  537. }
  538. return kt;
  539. }
  540. ktime_t efx_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue)
  541. {
  542. struct efx_nic *efx = tx_queue->efx;
  543. struct efx_ptp_data *ptp = efx->ptp_data;
  544. ktime_t kt;
  545. if (efx_ptp_use_mac_tx_timestamps(efx))
  546. kt = efx_ptp_mac_nic_to_ktime_correction(efx, ptp,
  547. tx_queue->completed_timestamp_major,
  548. tx_queue->completed_timestamp_minor,
  549. ptp->ts_corrections.general_tx);
  550. else
  551. kt = ptp->nic_to_kernel_time(
  552. tx_queue->completed_timestamp_major,
  553. tx_queue->completed_timestamp_minor,
  554. ptp->ts_corrections.general_tx);
  555. return kt;
  556. }
  557. /* Get PTP attributes and set up time conversions */
  558. static int efx_ptp_get_attributes(struct efx_nic *efx)
  559. {
  560. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
  561. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
  562. struct efx_ptp_data *ptp = efx->ptp_data;
  563. int rc;
  564. u32 fmt;
  565. size_t out_len;
  566. /* Get the PTP attributes. If the NIC doesn't support the operation we
  567. * use the default format for compatibility with older NICs i.e.
  568. * seconds and nanoseconds.
  569. */
  570. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
  571. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  572. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  573. outbuf, sizeof(outbuf), &out_len);
  574. if (rc == 0) {
  575. fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
  576. } else if (rc == -EINVAL) {
  577. fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
  578. } else if (rc == -EPERM) {
  579. netif_info(efx, probe, efx->net_dev, "no PTP support\n");
  580. return rc;
  581. } else {
  582. efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf),
  583. outbuf, sizeof(outbuf), rc);
  584. return rc;
  585. }
  586. switch (fmt) {
  587. case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION:
  588. ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
  589. ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
  590. ptp->nic_time.minor_max = 1 << 27;
  591. ptp->nic_time.sync_event_minor_shift = 19;
  592. break;
  593. case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS:
  594. ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
  595. ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
  596. ptp->nic_time.minor_max = 1000000000;
  597. ptp->nic_time.sync_event_minor_shift = 22;
  598. break;
  599. case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS:
  600. ptp->ns_to_nic_time = efx_ptp_ns_to_s_qns;
  601. ptp->nic_to_kernel_time = efx_ptp_s_qns_to_ktime_correction;
  602. ptp->nic_time.minor_max = 4000000000UL;
  603. ptp->nic_time.sync_event_minor_shift = 24;
  604. break;
  605. default:
  606. return -ERANGE;
  607. }
  608. /* Precalculate acceptable difference between the minor time in the
  609. * packet prefix and the last MCDI time sync event. We expect the
  610. * packet prefix timestamp to be after of sync event by up to one
  611. * sync event interval (0.25s) but we allow it to exceed this by a
  612. * fuzz factor of (0.1s)
  613. */
  614. ptp->nic_time.sync_event_diff_min = ptp->nic_time.minor_max
  615. - (ptp->nic_time.minor_max / 10);
  616. ptp->nic_time.sync_event_diff_max = (ptp->nic_time.minor_max / 4)
  617. + (ptp->nic_time.minor_max / 10);
  618. /* MC_CMD_PTP_OP_GET_ATTRIBUTES has been extended twice from an older
  619. * operation MC_CMD_PTP_OP_GET_TIME_FORMAT. The function now may return
  620. * a value to use for the minimum acceptable corrected synchronization
  621. * window and may return further capabilities.
  622. * If we have the extra information store it. For older firmware that
  623. * does not implement the extended command use the default value.
  624. */
  625. if (rc == 0 &&
  626. out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST)
  627. ptp->min_synchronisation_ns =
  628. MCDI_DWORD(outbuf,
  629. PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
  630. else
  631. ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
  632. if (rc == 0 &&
  633. out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
  634. ptp->capabilities = MCDI_DWORD(outbuf,
  635. PTP_OUT_GET_ATTRIBUTES_CAPABILITIES);
  636. else
  637. ptp->capabilities = 0;
  638. /* Set up the shift for conversion between frequency
  639. * adjustments in parts-per-billion and the fixed-point
  640. * fractional ns format that the adapter uses.
  641. */
  642. if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN))
  643. ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44;
  644. else
  645. ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40;
  646. return 0;
  647. }
  648. /* Get PTP timestamp corrections */
  649. static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
  650. {
  651. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
  652. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN);
  653. int rc;
  654. size_t out_len;
  655. /* Get the timestamp corrections from the NIC. If this operation is
  656. * not supported (older NICs) then no correction is required.
  657. */
  658. MCDI_SET_DWORD(inbuf, PTP_IN_OP,
  659. MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
  660. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  661. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  662. outbuf, sizeof(outbuf), &out_len);
  663. if (rc == 0) {
  664. efx->ptp_data->ts_corrections.ptp_tx = MCDI_DWORD(outbuf,
  665. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
  666. efx->ptp_data->ts_corrections.ptp_rx = MCDI_DWORD(outbuf,
  667. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
  668. efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
  669. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
  670. efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
  671. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
  672. if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) {
  673. efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD(
  674. outbuf,
  675. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX);
  676. efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD(
  677. outbuf,
  678. PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX);
  679. } else {
  680. efx->ptp_data->ts_corrections.general_tx =
  681. efx->ptp_data->ts_corrections.ptp_tx;
  682. efx->ptp_data->ts_corrections.general_rx =
  683. efx->ptp_data->ts_corrections.ptp_rx;
  684. }
  685. } else if (rc == -EINVAL) {
  686. efx->ptp_data->ts_corrections.ptp_tx = 0;
  687. efx->ptp_data->ts_corrections.ptp_rx = 0;
  688. efx->ptp_data->ts_corrections.pps_out = 0;
  689. efx->ptp_data->ts_corrections.pps_in = 0;
  690. efx->ptp_data->ts_corrections.general_tx = 0;
  691. efx->ptp_data->ts_corrections.general_rx = 0;
  692. } else {
  693. efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), outbuf,
  694. sizeof(outbuf), rc);
  695. return rc;
  696. }
  697. return 0;
  698. }
  699. /* Enable MCDI PTP support. */
  700. static int efx_ptp_enable(struct efx_nic *efx)
  701. {
  702. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
  703. MCDI_DECLARE_BUF_ERR(outbuf);
  704. int rc;
  705. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
  706. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  707. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
  708. efx->ptp_data->channel ?
  709. efx->ptp_data->channel->channel : 0);
  710. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
  711. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  712. outbuf, sizeof(outbuf), NULL);
  713. rc = (rc == -EALREADY) ? 0 : rc;
  714. if (rc)
  715. efx_mcdi_display_error(efx, MC_CMD_PTP,
  716. MC_CMD_PTP_IN_ENABLE_LEN,
  717. outbuf, sizeof(outbuf), rc);
  718. return rc;
  719. }
  720. /* Disable MCDI PTP support.
  721. *
  722. * Note that this function should never rely on the presence of ptp_data -
  723. * may be called before that exists.
  724. */
  725. static int efx_ptp_disable(struct efx_nic *efx)
  726. {
  727. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
  728. MCDI_DECLARE_BUF_ERR(outbuf);
  729. int rc;
  730. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
  731. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  732. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  733. outbuf, sizeof(outbuf), NULL);
  734. rc = (rc == -EALREADY) ? 0 : rc;
  735. /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
  736. * should only have been called during probe.
  737. */
  738. if (rc == -ENOSYS || rc == -EPERM)
  739. netif_info(efx, probe, efx->net_dev, "no PTP support\n");
  740. else if (rc)
  741. efx_mcdi_display_error(efx, MC_CMD_PTP,
  742. MC_CMD_PTP_IN_DISABLE_LEN,
  743. outbuf, sizeof(outbuf), rc);
  744. return rc;
  745. }
  746. static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
  747. {
  748. struct sk_buff *skb;
  749. while ((skb = skb_dequeue(q))) {
  750. local_bh_disable();
  751. netif_receive_skb(skb);
  752. local_bh_enable();
  753. }
  754. }
  755. static void efx_ptp_handle_no_channel(struct efx_nic *efx)
  756. {
  757. netif_err(efx, drv, efx->net_dev,
  758. "ERROR: PTP requires MSI-X and 1 additional interrupt"
  759. "vector. PTP disabled\n");
  760. }
  761. /* Repeatedly send the host time to the MC which will capture the hardware
  762. * time.
  763. */
  764. static void efx_ptp_send_times(struct efx_nic *efx,
  765. struct pps_event_time *last_time)
  766. {
  767. struct pps_event_time now;
  768. struct timespec64 limit;
  769. struct efx_ptp_data *ptp = efx->ptp_data;
  770. int *mc_running = ptp->start.addr;
  771. pps_get_ts(&now);
  772. limit = now.ts_real;
  773. timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
  774. /* Write host time for specified period or until MC is done */
  775. while ((timespec64_compare(&now.ts_real, &limit) < 0) &&
  776. READ_ONCE(*mc_running)) {
  777. struct timespec64 update_time;
  778. unsigned int host_time;
  779. /* Don't update continuously to avoid saturating the PCIe bus */
  780. update_time = now.ts_real;
  781. timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
  782. do {
  783. pps_get_ts(&now);
  784. } while ((timespec64_compare(&now.ts_real, &update_time) < 0) &&
  785. READ_ONCE(*mc_running));
  786. /* Synchronise NIC with single word of time only */
  787. host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
  788. now.ts_real.tv_nsec);
  789. /* Update host time in NIC memory */
  790. efx->type->ptp_write_host_time(efx, host_time);
  791. }
  792. *last_time = now;
  793. }
  794. /* Read a timeset from the MC's results and partial process. */
  795. static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
  796. struct efx_ptp_timeset *timeset)
  797. {
  798. unsigned start_ns, end_ns;
  799. timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
  800. timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
  801. timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
  802. timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
  803. timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
  804. /* Ignore seconds */
  805. start_ns = timeset->host_start & MC_NANOSECOND_MASK;
  806. end_ns = timeset->host_end & MC_NANOSECOND_MASK;
  807. /* Allow for rollover */
  808. if (end_ns < start_ns)
  809. end_ns += NSEC_PER_SEC;
  810. /* Determine duration of operation */
  811. timeset->window = end_ns - start_ns;
  812. }
  813. /* Process times received from MC.
  814. *
  815. * Extract times from returned results, and establish the minimum value
  816. * seen. The minimum value represents the "best" possible time and events
  817. * too much greater than this are rejected - the machine is, perhaps, too
  818. * busy. A number of readings are taken so that, hopefully, at least one good
  819. * synchronisation will be seen in the results.
  820. */
  821. static int
  822. efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
  823. size_t response_length,
  824. const struct pps_event_time *last_time)
  825. {
  826. unsigned number_readings =
  827. MCDI_VAR_ARRAY_LEN(response_length,
  828. PTP_OUT_SYNCHRONIZE_TIMESET);
  829. unsigned i;
  830. unsigned ngood = 0;
  831. unsigned last_good = 0;
  832. struct efx_ptp_data *ptp = efx->ptp_data;
  833. u32 last_sec;
  834. u32 start_sec;
  835. struct timespec64 delta;
  836. ktime_t mc_time;
  837. if (number_readings == 0)
  838. return -EAGAIN;
  839. /* Read the set of results and find the last good host-MC
  840. * synchronization result. The MC times when it finishes reading the
  841. * host time so the corrected window time should be fairly constant
  842. * for a given platform. Increment stats for any results that appear
  843. * to be erroneous.
  844. */
  845. for (i = 0; i < number_readings; i++) {
  846. s32 window, corrected;
  847. struct timespec64 wait;
  848. efx_ptp_read_timeset(
  849. MCDI_ARRAY_STRUCT_PTR(synch_buf,
  850. PTP_OUT_SYNCHRONIZE_TIMESET, i),
  851. &ptp->timeset[i]);
  852. wait = ktime_to_timespec64(
  853. ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
  854. window = ptp->timeset[i].window;
  855. corrected = window - wait.tv_nsec;
  856. /* We expect the uncorrected synchronization window to be at
  857. * least as large as the interval between host start and end
  858. * times. If it is smaller than this then this is mostly likely
  859. * to be a consequence of the host's time being adjusted.
  860. * Check that the corrected sync window is in a reasonable
  861. * range. If it is out of range it is likely to be because an
  862. * interrupt or other delay occurred between reading the system
  863. * time and writing it to MC memory.
  864. */
  865. if (window < SYNCHRONISATION_GRANULARITY_NS) {
  866. ++ptp->invalid_sync_windows;
  867. } else if (corrected >= MAX_SYNCHRONISATION_NS) {
  868. ++ptp->oversize_sync_windows;
  869. } else if (corrected < ptp->min_synchronisation_ns) {
  870. ++ptp->undersize_sync_windows;
  871. } else {
  872. ngood++;
  873. last_good = i;
  874. }
  875. }
  876. if (ngood == 0) {
  877. netif_warn(efx, drv, efx->net_dev,
  878. "PTP no suitable synchronisations\n");
  879. return -EAGAIN;
  880. }
  881. /* Calculate delay from last good sync (host time) to last_time.
  882. * It is possible that the seconds rolled over between taking
  883. * the start reading and the last value written by the host. The
  884. * timescales are such that a gap of more than one second is never
  885. * expected. delta is *not* normalised.
  886. */
  887. start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
  888. last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
  889. if (start_sec != last_sec &&
  890. ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
  891. netif_warn(efx, hw, efx->net_dev,
  892. "PTP bad synchronisation seconds\n");
  893. return -EAGAIN;
  894. }
  895. delta.tv_sec = (last_sec - start_sec) & 1;
  896. delta.tv_nsec =
  897. last_time->ts_real.tv_nsec -
  898. (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
  899. /* Convert the NIC time at last good sync into kernel time.
  900. * No correction is required - this time is the output of a
  901. * firmware process.
  902. */
  903. mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
  904. ptp->timeset[last_good].minor, 0);
  905. /* Calculate delay from NIC top of second to last_time */
  906. delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec;
  907. /* Set PPS timestamp to match NIC top of second */
  908. ptp->host_time_pps = *last_time;
  909. pps_sub_ts(&ptp->host_time_pps, delta);
  910. return 0;
  911. }
  912. /* Synchronize times between the host and the MC */
  913. static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
  914. {
  915. struct efx_ptp_data *ptp = efx->ptp_data;
  916. MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
  917. size_t response_length;
  918. int rc;
  919. unsigned long timeout;
  920. struct pps_event_time last_time = {};
  921. unsigned int loops = 0;
  922. int *start = ptp->start.addr;
  923. MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
  924. MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
  925. MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
  926. num_readings);
  927. MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
  928. ptp->start.dma_addr);
  929. /* Clear flag that signals MC ready */
  930. WRITE_ONCE(*start, 0);
  931. rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
  932. MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
  933. EFX_WARN_ON_ONCE_PARANOID(rc);
  934. /* Wait for start from MCDI (or timeout) */
  935. timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
  936. while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) {
  937. udelay(20); /* Usually start MCDI execution quickly */
  938. loops++;
  939. }
  940. if (loops <= 1)
  941. ++ptp->fast_syncs;
  942. if (!time_before(jiffies, timeout))
  943. ++ptp->sync_timeouts;
  944. if (READ_ONCE(*start))
  945. efx_ptp_send_times(efx, &last_time);
  946. /* Collect results */
  947. rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
  948. MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
  949. synch_buf, sizeof(synch_buf),
  950. &response_length);
  951. if (rc == 0) {
  952. rc = efx_ptp_process_times(efx, synch_buf, response_length,
  953. &last_time);
  954. if (rc == 0)
  955. ++ptp->good_syncs;
  956. else
  957. ++ptp->no_time_syncs;
  958. }
  959. /* Increment the bad syncs counter if the synchronize fails, whatever
  960. * the reason.
  961. */
  962. if (rc != 0)
  963. ++ptp->bad_syncs;
  964. return rc;
  965. }
  966. /* Transmit a PTP packet via the dedicated hardware timestamped queue. */
  967. static void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb)
  968. {
  969. struct efx_ptp_data *ptp_data = efx->ptp_data;
  970. struct efx_tx_queue *tx_queue;
  971. u8 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
  972. tx_queue = &ptp_data->channel->tx_queue[type];
  973. if (tx_queue && tx_queue->timestamping) {
  974. efx_enqueue_skb(tx_queue, skb);
  975. } else {
  976. WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
  977. dev_kfree_skb_any(skb);
  978. }
  979. }
  980. /* Transmit a PTP packet, via the MCDI interface, to the wire. */
  981. static void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb)
  982. {
  983. struct efx_ptp_data *ptp_data = efx->ptp_data;
  984. struct skb_shared_hwtstamps timestamps;
  985. int rc = -EIO;
  986. MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
  987. size_t len;
  988. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
  989. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
  990. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
  991. if (skb_shinfo(skb)->nr_frags != 0) {
  992. rc = skb_linearize(skb);
  993. if (rc != 0)
  994. goto fail;
  995. }
  996. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  997. rc = skb_checksum_help(skb);
  998. if (rc != 0)
  999. goto fail;
  1000. }
  1001. skb_copy_from_linear_data(skb,
  1002. MCDI_PTR(ptp_data->txbuf,
  1003. PTP_IN_TRANSMIT_PACKET),
  1004. skb->len);
  1005. rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
  1006. ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
  1007. txtime, sizeof(txtime), &len);
  1008. if (rc != 0)
  1009. goto fail;
  1010. memset(&timestamps, 0, sizeof(timestamps));
  1011. timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
  1012. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
  1013. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
  1014. ptp_data->ts_corrections.ptp_tx);
  1015. skb_tstamp_tx(skb, &timestamps);
  1016. rc = 0;
  1017. fail:
  1018. dev_kfree_skb_any(skb);
  1019. return;
  1020. }
  1021. static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
  1022. {
  1023. struct efx_ptp_data *ptp = efx->ptp_data;
  1024. struct list_head *cursor;
  1025. struct list_head *next;
  1026. if (ptp->rx_ts_inline)
  1027. return;
  1028. /* Drop time-expired events */
  1029. spin_lock_bh(&ptp->evt_lock);
  1030. if (!list_empty(&ptp->evt_list)) {
  1031. list_for_each_safe(cursor, next, &ptp->evt_list) {
  1032. struct efx_ptp_event_rx *evt;
  1033. evt = list_entry(cursor, struct efx_ptp_event_rx,
  1034. link);
  1035. if (time_after(jiffies, evt->expiry)) {
  1036. list_move(&evt->link, &ptp->evt_free_list);
  1037. netif_warn(efx, hw, efx->net_dev,
  1038. "PTP rx event dropped\n");
  1039. }
  1040. }
  1041. }
  1042. spin_unlock_bh(&ptp->evt_lock);
  1043. }
  1044. static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
  1045. struct sk_buff *skb)
  1046. {
  1047. struct efx_ptp_data *ptp = efx->ptp_data;
  1048. bool evts_waiting;
  1049. struct list_head *cursor;
  1050. struct list_head *next;
  1051. struct efx_ptp_match *match;
  1052. enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
  1053. WARN_ON_ONCE(ptp->rx_ts_inline);
  1054. spin_lock_bh(&ptp->evt_lock);
  1055. evts_waiting = !list_empty(&ptp->evt_list);
  1056. spin_unlock_bh(&ptp->evt_lock);
  1057. if (!evts_waiting)
  1058. return PTP_PACKET_STATE_UNMATCHED;
  1059. match = (struct efx_ptp_match *)skb->cb;
  1060. /* Look for a matching timestamp in the event queue */
  1061. spin_lock_bh(&ptp->evt_lock);
  1062. list_for_each_safe(cursor, next, &ptp->evt_list) {
  1063. struct efx_ptp_event_rx *evt;
  1064. evt = list_entry(cursor, struct efx_ptp_event_rx, link);
  1065. if ((evt->seq0 == match->words[0]) &&
  1066. (evt->seq1 == match->words[1])) {
  1067. struct skb_shared_hwtstamps *timestamps;
  1068. /* Match - add in hardware timestamp */
  1069. timestamps = skb_hwtstamps(skb);
  1070. timestamps->hwtstamp = evt->hwtimestamp;
  1071. match->state = PTP_PACKET_STATE_MATCHED;
  1072. rc = PTP_PACKET_STATE_MATCHED;
  1073. list_move(&evt->link, &ptp->evt_free_list);
  1074. break;
  1075. }
  1076. }
  1077. spin_unlock_bh(&ptp->evt_lock);
  1078. return rc;
  1079. }
  1080. /* Process any queued receive events and corresponding packets
  1081. *
  1082. * q is returned with all the packets that are ready for delivery.
  1083. */
  1084. static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
  1085. {
  1086. struct efx_ptp_data *ptp = efx->ptp_data;
  1087. struct sk_buff *skb;
  1088. while ((skb = skb_dequeue(&ptp->rxq))) {
  1089. struct efx_ptp_match *match;
  1090. match = (struct efx_ptp_match *)skb->cb;
  1091. if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
  1092. __skb_queue_tail(q, skb);
  1093. } else if (efx_ptp_match_rx(efx, skb) ==
  1094. PTP_PACKET_STATE_MATCHED) {
  1095. __skb_queue_tail(q, skb);
  1096. } else if (time_after(jiffies, match->expiry)) {
  1097. match->state = PTP_PACKET_STATE_TIMED_OUT;
  1098. ++ptp->rx_no_timestamp;
  1099. __skb_queue_tail(q, skb);
  1100. } else {
  1101. /* Replace unprocessed entry and stop */
  1102. skb_queue_head(&ptp->rxq, skb);
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. /* Complete processing of a received packet */
  1108. static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
  1109. {
  1110. local_bh_disable();
  1111. netif_receive_skb(skb);
  1112. local_bh_enable();
  1113. }
  1114. static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
  1115. {
  1116. struct efx_ptp_data *ptp = efx->ptp_data;
  1117. if (ptp->rxfilter_installed) {
  1118. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  1119. ptp->rxfilter_general);
  1120. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  1121. ptp->rxfilter_event);
  1122. ptp->rxfilter_installed = false;
  1123. }
  1124. }
  1125. static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
  1126. {
  1127. struct efx_ptp_data *ptp = efx->ptp_data;
  1128. struct efx_filter_spec rxfilter;
  1129. int rc;
  1130. if (!ptp->channel || ptp->rxfilter_installed)
  1131. return 0;
  1132. /* Must filter on both event and general ports to ensure
  1133. * that there is no packet re-ordering.
  1134. */
  1135. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  1136. efx_rx_queue_index(
  1137. efx_channel_get_rx_queue(ptp->channel)));
  1138. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  1139. htonl(PTP_ADDRESS),
  1140. htons(PTP_EVENT_PORT));
  1141. if (rc != 0)
  1142. return rc;
  1143. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  1144. if (rc < 0)
  1145. return rc;
  1146. ptp->rxfilter_event = rc;
  1147. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  1148. efx_rx_queue_index(
  1149. efx_channel_get_rx_queue(ptp->channel)));
  1150. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  1151. htonl(PTP_ADDRESS),
  1152. htons(PTP_GENERAL_PORT));
  1153. if (rc != 0)
  1154. goto fail;
  1155. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  1156. if (rc < 0)
  1157. goto fail;
  1158. ptp->rxfilter_general = rc;
  1159. ptp->rxfilter_installed = true;
  1160. return 0;
  1161. fail:
  1162. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  1163. ptp->rxfilter_event);
  1164. return rc;
  1165. }
  1166. static int efx_ptp_start(struct efx_nic *efx)
  1167. {
  1168. struct efx_ptp_data *ptp = efx->ptp_data;
  1169. int rc;
  1170. ptp->reset_required = false;
  1171. rc = efx_ptp_insert_multicast_filters(efx);
  1172. if (rc)
  1173. return rc;
  1174. rc = efx_ptp_enable(efx);
  1175. if (rc != 0)
  1176. goto fail;
  1177. ptp->evt_frag_idx = 0;
  1178. ptp->current_adjfreq = 0;
  1179. return 0;
  1180. fail:
  1181. efx_ptp_remove_multicast_filters(efx);
  1182. return rc;
  1183. }
  1184. static int efx_ptp_stop(struct efx_nic *efx)
  1185. {
  1186. struct efx_ptp_data *ptp = efx->ptp_data;
  1187. struct list_head *cursor;
  1188. struct list_head *next;
  1189. int rc;
  1190. if (ptp == NULL)
  1191. return 0;
  1192. rc = efx_ptp_disable(efx);
  1193. efx_ptp_remove_multicast_filters(efx);
  1194. /* Make sure RX packets are really delivered */
  1195. efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
  1196. skb_queue_purge(&efx->ptp_data->txq);
  1197. /* Drop any pending receive events */
  1198. spin_lock_bh(&efx->ptp_data->evt_lock);
  1199. list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
  1200. list_move(cursor, &efx->ptp_data->evt_free_list);
  1201. }
  1202. spin_unlock_bh(&efx->ptp_data->evt_lock);
  1203. return rc;
  1204. }
  1205. static int efx_ptp_restart(struct efx_nic *efx)
  1206. {
  1207. if (efx->ptp_data && efx->ptp_data->enabled)
  1208. return efx_ptp_start(efx);
  1209. return 0;
  1210. }
  1211. static void efx_ptp_pps_worker(struct work_struct *work)
  1212. {
  1213. struct efx_ptp_data *ptp =
  1214. container_of(work, struct efx_ptp_data, pps_work);
  1215. struct efx_nic *efx = ptp->efx;
  1216. struct ptp_clock_event ptp_evt;
  1217. if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
  1218. return;
  1219. ptp_evt.type = PTP_CLOCK_PPSUSR;
  1220. ptp_evt.pps_times = ptp->host_time_pps;
  1221. ptp_clock_event(ptp->phc_clock, &ptp_evt);
  1222. }
  1223. static void efx_ptp_worker(struct work_struct *work)
  1224. {
  1225. struct efx_ptp_data *ptp_data =
  1226. container_of(work, struct efx_ptp_data, work);
  1227. struct efx_nic *efx = ptp_data->efx;
  1228. struct sk_buff *skb;
  1229. struct sk_buff_head tempq;
  1230. if (ptp_data->reset_required) {
  1231. efx_ptp_stop(efx);
  1232. efx_ptp_start(efx);
  1233. return;
  1234. }
  1235. efx_ptp_drop_time_expired_events(efx);
  1236. __skb_queue_head_init(&tempq);
  1237. efx_ptp_process_events(efx, &tempq);
  1238. while ((skb = skb_dequeue(&ptp_data->txq)))
  1239. ptp_data->xmit_skb(efx, skb);
  1240. while ((skb = __skb_dequeue(&tempq)))
  1241. efx_ptp_process_rx(efx, skb);
  1242. }
  1243. static const struct ptp_clock_info efx_phc_clock_info = {
  1244. .owner = THIS_MODULE,
  1245. .name = "sfc",
  1246. .max_adj = MAX_PPB,
  1247. .n_alarm = 0,
  1248. .n_ext_ts = 0,
  1249. .n_per_out = 0,
  1250. .n_pins = 0,
  1251. .pps = 1,
  1252. .adjfreq = efx_phc_adjfreq,
  1253. .adjtime = efx_phc_adjtime,
  1254. .gettime64 = efx_phc_gettime,
  1255. .settime64 = efx_phc_settime,
  1256. .enable = efx_phc_enable,
  1257. };
  1258. /* Initialise PTP state. */
  1259. int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
  1260. {
  1261. struct efx_ptp_data *ptp;
  1262. int rc = 0;
  1263. unsigned int pos;
  1264. ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
  1265. efx->ptp_data = ptp;
  1266. if (!efx->ptp_data)
  1267. return -ENOMEM;
  1268. ptp->efx = efx;
  1269. ptp->channel = channel;
  1270. ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
  1271. rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
  1272. if (rc != 0)
  1273. goto fail1;
  1274. skb_queue_head_init(&ptp->rxq);
  1275. skb_queue_head_init(&ptp->txq);
  1276. ptp->workwq = create_singlethread_workqueue("sfc_ptp");
  1277. if (!ptp->workwq) {
  1278. rc = -ENOMEM;
  1279. goto fail2;
  1280. }
  1281. if (efx_ptp_use_mac_tx_timestamps(efx)) {
  1282. ptp->xmit_skb = efx_ptp_xmit_skb_queue;
  1283. /* Request sync events on this channel. */
  1284. channel->sync_events_state = SYNC_EVENTS_QUIESCENT;
  1285. } else {
  1286. ptp->xmit_skb = efx_ptp_xmit_skb_mc;
  1287. }
  1288. INIT_WORK(&ptp->work, efx_ptp_worker);
  1289. ptp->config.flags = 0;
  1290. ptp->config.tx_type = HWTSTAMP_TX_OFF;
  1291. ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
  1292. INIT_LIST_HEAD(&ptp->evt_list);
  1293. INIT_LIST_HEAD(&ptp->evt_free_list);
  1294. spin_lock_init(&ptp->evt_lock);
  1295. for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
  1296. list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
  1297. /* Get the NIC PTP attributes and set up time conversions */
  1298. rc = efx_ptp_get_attributes(efx);
  1299. if (rc < 0)
  1300. goto fail3;
  1301. /* Get the timestamp corrections */
  1302. rc = efx_ptp_get_timestamp_corrections(efx);
  1303. if (rc < 0)
  1304. goto fail3;
  1305. if (efx->mcdi->fn_flags &
  1306. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
  1307. ptp->phc_clock_info = efx_phc_clock_info;
  1308. ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
  1309. &efx->pci_dev->dev);
  1310. if (IS_ERR(ptp->phc_clock)) {
  1311. rc = PTR_ERR(ptp->phc_clock);
  1312. goto fail3;
  1313. } else if (ptp->phc_clock) {
  1314. INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
  1315. ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
  1316. if (!ptp->pps_workwq) {
  1317. rc = -ENOMEM;
  1318. goto fail4;
  1319. }
  1320. }
  1321. }
  1322. ptp->nic_ts_enabled = false;
  1323. return 0;
  1324. fail4:
  1325. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1326. fail3:
  1327. destroy_workqueue(efx->ptp_data->workwq);
  1328. fail2:
  1329. efx_nic_free_buffer(efx, &ptp->start);
  1330. fail1:
  1331. kfree(efx->ptp_data);
  1332. efx->ptp_data = NULL;
  1333. return rc;
  1334. }
  1335. /* Initialise PTP channel.
  1336. *
  1337. * Setting core_index to zero causes the queue to be initialised and doesn't
  1338. * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
  1339. */
  1340. static int efx_ptp_probe_channel(struct efx_channel *channel)
  1341. {
  1342. struct efx_nic *efx = channel->efx;
  1343. int rc;
  1344. channel->irq_moderation_us = 0;
  1345. channel->rx_queue.core_index = 0;
  1346. rc = efx_ptp_probe(efx, channel);
  1347. /* Failure to probe PTP is not fatal; this channel will just not be
  1348. * used for anything.
  1349. * In the case of EPERM, efx_ptp_probe will print its own message (in
  1350. * efx_ptp_get_attributes()), so we don't need to.
  1351. */
  1352. if (rc && rc != -EPERM)
  1353. netif_warn(efx, drv, efx->net_dev,
  1354. "Failed to probe PTP, rc=%d\n", rc);
  1355. return 0;
  1356. }
  1357. void efx_ptp_remove(struct efx_nic *efx)
  1358. {
  1359. if (!efx->ptp_data)
  1360. return;
  1361. (void)efx_ptp_disable(efx);
  1362. cancel_work_sync(&efx->ptp_data->work);
  1363. if (efx->ptp_data->pps_workwq)
  1364. cancel_work_sync(&efx->ptp_data->pps_work);
  1365. skb_queue_purge(&efx->ptp_data->rxq);
  1366. skb_queue_purge(&efx->ptp_data->txq);
  1367. if (efx->ptp_data->phc_clock) {
  1368. destroy_workqueue(efx->ptp_data->pps_workwq);
  1369. ptp_clock_unregister(efx->ptp_data->phc_clock);
  1370. }
  1371. destroy_workqueue(efx->ptp_data->workwq);
  1372. efx_nic_free_buffer(efx, &efx->ptp_data->start);
  1373. kfree(efx->ptp_data);
  1374. efx->ptp_data = NULL;
  1375. }
  1376. static void efx_ptp_remove_channel(struct efx_channel *channel)
  1377. {
  1378. efx_ptp_remove(channel->efx);
  1379. }
  1380. static void efx_ptp_get_channel_name(struct efx_channel *channel,
  1381. char *buf, size_t len)
  1382. {
  1383. snprintf(buf, len, "%s-ptp", channel->efx->name);
  1384. }
  1385. /* Determine whether this packet should be processed by the PTP module
  1386. * or transmitted conventionally.
  1387. */
  1388. bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1389. {
  1390. return efx->ptp_data &&
  1391. efx->ptp_data->enabled &&
  1392. skb->len >= PTP_MIN_LENGTH &&
  1393. skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
  1394. likely(skb->protocol == htons(ETH_P_IP)) &&
  1395. skb_transport_header_was_set(skb) &&
  1396. skb_network_header_len(skb) >= sizeof(struct iphdr) &&
  1397. ip_hdr(skb)->protocol == IPPROTO_UDP &&
  1398. skb_headlen(skb) >=
  1399. skb_transport_offset(skb) + sizeof(struct udphdr) &&
  1400. udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
  1401. }
  1402. /* Receive a PTP packet. Packets are queued until the arrival of
  1403. * the receive timestamp from the MC - this will probably occur after the
  1404. * packet arrival because of the processing in the MC.
  1405. */
  1406. static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
  1407. {
  1408. struct efx_nic *efx = channel->efx;
  1409. struct efx_ptp_data *ptp = efx->ptp_data;
  1410. struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
  1411. u8 *match_data_012, *match_data_345;
  1412. unsigned int version;
  1413. u8 *data;
  1414. match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1415. /* Correct version? */
  1416. if (ptp->mode == MC_CMD_PTP_MODE_V1) {
  1417. if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
  1418. return false;
  1419. }
  1420. data = skb->data;
  1421. version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
  1422. if (version != PTP_VERSION_V1) {
  1423. return false;
  1424. }
  1425. /* PTP V1 uses all six bytes of the UUID to match the packet
  1426. * to the timestamp
  1427. */
  1428. match_data_012 = data + PTP_V1_UUID_OFFSET;
  1429. match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
  1430. } else {
  1431. if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
  1432. return false;
  1433. }
  1434. data = skb->data;
  1435. version = data[PTP_V2_VERSION_OFFSET];
  1436. if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
  1437. return false;
  1438. }
  1439. /* The original V2 implementation uses bytes 2-7 of
  1440. * the UUID to match the packet to the timestamp. This
  1441. * discards two of the bytes of the MAC address used
  1442. * to create the UUID (SF bug 33070). The PTP V2
  1443. * enhanced mode fixes this issue and uses bytes 0-2
  1444. * and byte 5-7 of the UUID.
  1445. */
  1446. match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
  1447. if (ptp->mode == MC_CMD_PTP_MODE_V2) {
  1448. match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
  1449. } else {
  1450. match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
  1451. BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
  1452. }
  1453. }
  1454. /* Does this packet require timestamping? */
  1455. if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
  1456. match->state = PTP_PACKET_STATE_UNMATCHED;
  1457. /* We expect the sequence number to be in the same position in
  1458. * the packet for PTP V1 and V2
  1459. */
  1460. BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
  1461. BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
  1462. /* Extract UUID/Sequence information */
  1463. match->words[0] = (match_data_012[0] |
  1464. (match_data_012[1] << 8) |
  1465. (match_data_012[2] << 16) |
  1466. (match_data_345[0] << 24));
  1467. match->words[1] = (match_data_345[1] |
  1468. (match_data_345[2] << 8) |
  1469. (data[PTP_V1_SEQUENCE_OFFSET +
  1470. PTP_V1_SEQUENCE_LENGTH - 1] <<
  1471. 16));
  1472. } else {
  1473. match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
  1474. }
  1475. skb_queue_tail(&ptp->rxq, skb);
  1476. queue_work(ptp->workwq, &ptp->work);
  1477. return true;
  1478. }
  1479. /* Transmit a PTP packet. This has to be transmitted by the MC
  1480. * itself, through an MCDI call. MCDI calls aren't permitted
  1481. * in the transmit path so defer the actual transmission to a suitable worker.
  1482. */
  1483. int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  1484. {
  1485. struct efx_ptp_data *ptp = efx->ptp_data;
  1486. skb_queue_tail(&ptp->txq, skb);
  1487. if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
  1488. (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
  1489. efx_xmit_hwtstamp_pending(skb);
  1490. queue_work(ptp->workwq, &ptp->work);
  1491. return NETDEV_TX_OK;
  1492. }
  1493. int efx_ptp_get_mode(struct efx_nic *efx)
  1494. {
  1495. return efx->ptp_data->mode;
  1496. }
  1497. int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
  1498. unsigned int new_mode)
  1499. {
  1500. if ((enable_wanted != efx->ptp_data->enabled) ||
  1501. (enable_wanted && (efx->ptp_data->mode != new_mode))) {
  1502. int rc = 0;
  1503. if (enable_wanted) {
  1504. /* Change of mode requires disable */
  1505. if (efx->ptp_data->enabled &&
  1506. (efx->ptp_data->mode != new_mode)) {
  1507. efx->ptp_data->enabled = false;
  1508. rc = efx_ptp_stop(efx);
  1509. if (rc != 0)
  1510. return rc;
  1511. }
  1512. /* Set new operating mode and establish
  1513. * baseline synchronisation, which must
  1514. * succeed.
  1515. */
  1516. efx->ptp_data->mode = new_mode;
  1517. if (netif_running(efx->net_dev))
  1518. rc = efx_ptp_start(efx);
  1519. if (rc == 0) {
  1520. rc = efx_ptp_synchronize(efx,
  1521. PTP_SYNC_ATTEMPTS * 2);
  1522. if (rc != 0)
  1523. efx_ptp_stop(efx);
  1524. }
  1525. } else {
  1526. rc = efx_ptp_stop(efx);
  1527. }
  1528. if (rc != 0)
  1529. return rc;
  1530. efx->ptp_data->enabled = enable_wanted;
  1531. }
  1532. return 0;
  1533. }
  1534. static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
  1535. {
  1536. int rc;
  1537. if (init->flags)
  1538. return -EINVAL;
  1539. if ((init->tx_type != HWTSTAMP_TX_OFF) &&
  1540. (init->tx_type != HWTSTAMP_TX_ON))
  1541. return -ERANGE;
  1542. rc = efx->type->ptp_set_ts_config(efx, init);
  1543. if (rc)
  1544. return rc;
  1545. efx->ptp_data->config = *init;
  1546. return 0;
  1547. }
  1548. void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
  1549. {
  1550. struct efx_ptp_data *ptp = efx->ptp_data;
  1551. struct efx_nic *primary = efx->primary;
  1552. ASSERT_RTNL();
  1553. if (!ptp)
  1554. return;
  1555. ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
  1556. SOF_TIMESTAMPING_RX_HARDWARE |
  1557. SOF_TIMESTAMPING_RAW_HARDWARE);
  1558. /* Check licensed features. If we don't have the license for TX
  1559. * timestamps, the NIC will not support them.
  1560. */
  1561. if (efx_ptp_use_mac_tx_timestamps(efx)) {
  1562. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1563. if (!(nic_data->licensed_features &
  1564. (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN)))
  1565. ts_info->so_timestamping &=
  1566. ~SOF_TIMESTAMPING_TX_HARDWARE;
  1567. }
  1568. if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
  1569. ts_info->phc_index =
  1570. ptp_clock_index(primary->ptp_data->phc_clock);
  1571. ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
  1572. ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
  1573. }
  1574. int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1575. {
  1576. struct hwtstamp_config config;
  1577. int rc;
  1578. /* Not a PTP enabled port */
  1579. if (!efx->ptp_data)
  1580. return -EOPNOTSUPP;
  1581. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1582. return -EFAULT;
  1583. rc = efx_ptp_ts_init(efx, &config);
  1584. if (rc != 0)
  1585. return rc;
  1586. return copy_to_user(ifr->ifr_data, &config, sizeof(config))
  1587. ? -EFAULT : 0;
  1588. }
  1589. int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
  1590. {
  1591. if (!efx->ptp_data)
  1592. return -EOPNOTSUPP;
  1593. return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
  1594. sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
  1595. }
  1596. static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
  1597. {
  1598. struct efx_ptp_data *ptp = efx->ptp_data;
  1599. netif_err(efx, hw, efx->net_dev,
  1600. "PTP unexpected event length: got %d expected %d\n",
  1601. ptp->evt_frag_idx, expected_frag_len);
  1602. ptp->reset_required = true;
  1603. queue_work(ptp->workwq, &ptp->work);
  1604. }
  1605. /* Process a completed receive event. Put it on the event queue and
  1606. * start worker thread. This is required because event and their
  1607. * correspoding packets may come in either order.
  1608. */
  1609. static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1610. {
  1611. struct efx_ptp_event_rx *evt = NULL;
  1612. if (WARN_ON_ONCE(ptp->rx_ts_inline))
  1613. return;
  1614. if (ptp->evt_frag_idx != 3) {
  1615. ptp_event_failure(efx, 3);
  1616. return;
  1617. }
  1618. spin_lock_bh(&ptp->evt_lock);
  1619. if (!list_empty(&ptp->evt_free_list)) {
  1620. evt = list_first_entry(&ptp->evt_free_list,
  1621. struct efx_ptp_event_rx, link);
  1622. list_del(&evt->link);
  1623. evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
  1624. evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
  1625. MCDI_EVENT_SRC) |
  1626. (EFX_QWORD_FIELD(ptp->evt_frags[1],
  1627. MCDI_EVENT_SRC) << 8) |
  1628. (EFX_QWORD_FIELD(ptp->evt_frags[0],
  1629. MCDI_EVENT_SRC) << 16));
  1630. evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
  1631. EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
  1632. EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
  1633. ptp->ts_corrections.ptp_rx);
  1634. evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1635. list_add_tail(&evt->link, &ptp->evt_list);
  1636. queue_work(ptp->workwq, &ptp->work);
  1637. } else if (net_ratelimit()) {
  1638. /* Log a rate-limited warning message. */
  1639. netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
  1640. }
  1641. spin_unlock_bh(&ptp->evt_lock);
  1642. }
  1643. static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1644. {
  1645. int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
  1646. if (ptp->evt_frag_idx != 1) {
  1647. ptp_event_failure(efx, 1);
  1648. return;
  1649. }
  1650. netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
  1651. }
  1652. static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1653. {
  1654. if (ptp->nic_ts_enabled)
  1655. queue_work(ptp->pps_workwq, &ptp->pps_work);
  1656. }
  1657. void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
  1658. {
  1659. struct efx_ptp_data *ptp = efx->ptp_data;
  1660. int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
  1661. if (!ptp) {
  1662. if (!efx->ptp_warned) {
  1663. netif_warn(efx, drv, efx->net_dev,
  1664. "Received PTP event but PTP not set up\n");
  1665. efx->ptp_warned = true;
  1666. }
  1667. return;
  1668. }
  1669. if (!ptp->enabled)
  1670. return;
  1671. if (ptp->evt_frag_idx == 0) {
  1672. ptp->evt_code = code;
  1673. } else if (ptp->evt_code != code) {
  1674. netif_err(efx, hw, efx->net_dev,
  1675. "PTP out of sequence event %d\n", code);
  1676. ptp->evt_frag_idx = 0;
  1677. }
  1678. ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
  1679. if (!MCDI_EVENT_FIELD(*ev, CONT)) {
  1680. /* Process resulting event */
  1681. switch (code) {
  1682. case MCDI_EVENT_CODE_PTP_RX:
  1683. ptp_event_rx(efx, ptp);
  1684. break;
  1685. case MCDI_EVENT_CODE_PTP_FAULT:
  1686. ptp_event_fault(efx, ptp);
  1687. break;
  1688. case MCDI_EVENT_CODE_PTP_PPS:
  1689. ptp_event_pps(efx, ptp);
  1690. break;
  1691. default:
  1692. netif_err(efx, hw, efx->net_dev,
  1693. "PTP unknown event %d\n", code);
  1694. break;
  1695. }
  1696. ptp->evt_frag_idx = 0;
  1697. } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
  1698. netif_err(efx, hw, efx->net_dev,
  1699. "PTP too many event fragments\n");
  1700. ptp->evt_frag_idx = 0;
  1701. }
  1702. }
  1703. void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
  1704. {
  1705. struct efx_nic *efx = channel->efx;
  1706. struct efx_ptp_data *ptp = efx->ptp_data;
  1707. /* When extracting the sync timestamp minor value, we should discard
  1708. * the least significant two bits. These are not required in order
  1709. * to reconstruct full-range timestamps and they are optionally used
  1710. * to report status depending on the options supplied when subscribing
  1711. * for sync events.
  1712. */
  1713. channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
  1714. channel->sync_timestamp_minor =
  1715. (MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC)
  1716. << ptp->nic_time.sync_event_minor_shift;
  1717. /* if sync events have been disabled then we want to silently ignore
  1718. * this event, so throw away result.
  1719. */
  1720. (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
  1721. SYNC_EVENTS_VALID);
  1722. }
  1723. static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
  1724. {
  1725. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  1726. return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
  1727. #else
  1728. const u8 *data = eh + efx->rx_packet_ts_offset;
  1729. return (u32)data[0] |
  1730. (u32)data[1] << 8 |
  1731. (u32)data[2] << 16 |
  1732. (u32)data[3] << 24;
  1733. #endif
  1734. }
  1735. void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
  1736. struct sk_buff *skb)
  1737. {
  1738. struct efx_nic *efx = channel->efx;
  1739. struct efx_ptp_data *ptp = efx->ptp_data;
  1740. u32 pkt_timestamp_major, pkt_timestamp_minor;
  1741. u32 diff, carry;
  1742. struct skb_shared_hwtstamps *timestamps;
  1743. if (channel->sync_events_state != SYNC_EVENTS_VALID)
  1744. return;
  1745. pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb));
  1746. /* get the difference between the packet and sync timestamps,
  1747. * modulo one second
  1748. */
  1749. diff = pkt_timestamp_minor - channel->sync_timestamp_minor;
  1750. if (pkt_timestamp_minor < channel->sync_timestamp_minor)
  1751. diff += ptp->nic_time.minor_max;
  1752. /* do we roll over a second boundary and need to carry the one? */
  1753. carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ?
  1754. 1 : 0;
  1755. if (diff <= ptp->nic_time.sync_event_diff_max) {
  1756. /* packet is ahead of the sync event by a quarter of a second or
  1757. * less (allowing for fuzz)
  1758. */
  1759. pkt_timestamp_major = channel->sync_timestamp_major + carry;
  1760. } else if (diff >= ptp->nic_time.sync_event_diff_min) {
  1761. /* packet is behind the sync event but within the fuzz factor.
  1762. * This means the RX packet and sync event crossed as they were
  1763. * placed on the event queue, which can sometimes happen.
  1764. */
  1765. pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
  1766. } else {
  1767. /* it's outside tolerance in both directions. this might be
  1768. * indicative of us missing sync events for some reason, so
  1769. * we'll call it an error rather than risk giving a bogus
  1770. * timestamp.
  1771. */
  1772. netif_vdbg(efx, drv, efx->net_dev,
  1773. "packet timestamp %x too far from sync event %x:%x\n",
  1774. pkt_timestamp_minor, channel->sync_timestamp_major,
  1775. channel->sync_timestamp_minor);
  1776. return;
  1777. }
  1778. /* attach the timestamps to the skb */
  1779. timestamps = skb_hwtstamps(skb);
  1780. timestamps->hwtstamp =
  1781. ptp->nic_to_kernel_time(pkt_timestamp_major,
  1782. pkt_timestamp_minor,
  1783. ptp->ts_corrections.general_rx);
  1784. }
  1785. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  1786. {
  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(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
  1792. s64 adjustment_ns;
  1793. int rc;
  1794. if (delta > MAX_PPB)
  1795. delta = MAX_PPB;
  1796. else if (delta < -MAX_PPB)
  1797. delta = -MAX_PPB;
  1798. /* Convert ppb to fixed point ns taking care to round correctly. */
  1799. adjustment_ns = ((s64)delta * PPB_SCALE_WORD +
  1800. (1 << (ptp_data->adjfreq_ppb_shift - 1))) >>
  1801. ptp_data->adjfreq_ppb_shift;
  1802. MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1803. MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
  1804. MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
  1805. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
  1806. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
  1807. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
  1808. NULL, 0, NULL);
  1809. if (rc != 0)
  1810. return rc;
  1811. ptp_data->current_adjfreq = adjustment_ns;
  1812. return 0;
  1813. }
  1814. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1815. {
  1816. u32 nic_major, nic_minor;
  1817. struct efx_ptp_data *ptp_data = container_of(ptp,
  1818. struct efx_ptp_data,
  1819. phc_clock_info);
  1820. struct efx_nic *efx = ptp_data->efx;
  1821. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
  1822. efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
  1823. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1824. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1825. MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
  1826. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
  1827. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
  1828. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1829. NULL, 0, NULL);
  1830. }
  1831. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  1832. {
  1833. struct efx_ptp_data *ptp_data = container_of(ptp,
  1834. struct efx_ptp_data,
  1835. phc_clock_info);
  1836. struct efx_nic *efx = ptp_data->efx;
  1837. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
  1838. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
  1839. int rc;
  1840. ktime_t kt;
  1841. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
  1842. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1843. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1844. outbuf, sizeof(outbuf), NULL);
  1845. if (rc != 0)
  1846. return rc;
  1847. kt = ptp_data->nic_to_kernel_time(
  1848. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
  1849. MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
  1850. *ts = ktime_to_timespec64(kt);
  1851. return 0;
  1852. }
  1853. static int efx_phc_settime(struct ptp_clock_info *ptp,
  1854. const struct timespec64 *e_ts)
  1855. {
  1856. /* Get the current NIC time, efx_phc_gettime.
  1857. * Subtract from the desired time to get the offset
  1858. * call efx_phc_adjtime with the offset
  1859. */
  1860. int rc;
  1861. struct timespec64 time_now;
  1862. struct timespec64 delta;
  1863. rc = efx_phc_gettime(ptp, &time_now);
  1864. if (rc != 0)
  1865. return rc;
  1866. delta = timespec64_sub(*e_ts, time_now);
  1867. rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
  1868. if (rc != 0)
  1869. return rc;
  1870. return 0;
  1871. }
  1872. static int efx_phc_enable(struct ptp_clock_info *ptp,
  1873. struct ptp_clock_request *request,
  1874. int enable)
  1875. {
  1876. struct efx_ptp_data *ptp_data = container_of(ptp,
  1877. struct efx_ptp_data,
  1878. phc_clock_info);
  1879. if (request->type != PTP_CLK_REQ_PPS)
  1880. return -EOPNOTSUPP;
  1881. ptp_data->nic_ts_enabled = !!enable;
  1882. return 0;
  1883. }
  1884. static const struct efx_channel_type efx_ptp_channel_type = {
  1885. .handle_no_channel = efx_ptp_handle_no_channel,
  1886. .pre_probe = efx_ptp_probe_channel,
  1887. .post_remove = efx_ptp_remove_channel,
  1888. .get_name = efx_ptp_get_channel_name,
  1889. /* no copy operation; there is no need to reallocate this channel */
  1890. .receive_skb = efx_ptp_rx,
  1891. .want_txqs = efx_ptp_want_txqs,
  1892. .keep_eventq = false,
  1893. };
  1894. void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
  1895. {
  1896. /* Check whether PTP is implemented on this NIC. The DISABLE
  1897. * operation will succeed if and only if it is implemented.
  1898. */
  1899. if (efx_ptp_disable(efx) == 0)
  1900. efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
  1901. &efx_ptp_channel_type;
  1902. }
  1903. void efx_ptp_start_datapath(struct efx_nic *efx)
  1904. {
  1905. if (efx_ptp_restart(efx))
  1906. netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
  1907. /* re-enable timestamping if it was previously enabled */
  1908. if (efx->type->ptp_set_ts_sync_events)
  1909. efx->type->ptp_set_ts_sync_events(efx, true, true);
  1910. }
  1911. void efx_ptp_stop_datapath(struct efx_nic *efx)
  1912. {
  1913. /* temporarily disable timestamping */
  1914. if (efx->type->ptp_set_ts_sync_events)
  1915. efx->type->ptp_set_ts_sync_events(efx, false, true);
  1916. efx_ptp_stop(efx);
  1917. }