ptp.c 68 KB

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