ptp.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2011 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 MIN_SYNCHRONISATION_NS 120
  60. /* Maximum permitted length of a (corrected) synchronisation time */
  61. #define MAX_SYNCHRONISATION_NS 1000
  62. /* How many (MC) receive events that can be queued */
  63. #define MAX_RECEIVE_EVENTS 8
  64. /* Length of (modified) moving average. */
  65. #define AVERAGE_LENGTH 16
  66. /* How long an unmatched event or packet can be held */
  67. #define PKT_EVENT_LIFETIME_MS 10
  68. /* Offsets into PTP packet for identification. These offsets are from the
  69. * start of the IP header, not the MAC header. Note that neither PTP V1 nor
  70. * PTP V2 permit the use of IPV4 options.
  71. */
  72. #define PTP_DPORT_OFFSET 22
  73. #define PTP_V1_VERSION_LENGTH 2
  74. #define PTP_V1_VERSION_OFFSET 28
  75. #define PTP_V1_UUID_LENGTH 6
  76. #define PTP_V1_UUID_OFFSET 50
  77. #define PTP_V1_SEQUENCE_LENGTH 2
  78. #define PTP_V1_SEQUENCE_OFFSET 58
  79. /* The minimum length of a PTP V1 packet for offsets, etc. to be valid:
  80. * includes IP header.
  81. */
  82. #define PTP_V1_MIN_LENGTH 64
  83. #define PTP_V2_VERSION_LENGTH 1
  84. #define PTP_V2_VERSION_OFFSET 29
  85. #define PTP_V2_UUID_LENGTH 8
  86. #define PTP_V2_UUID_OFFSET 48
  87. /* Although PTP V2 UUIDs are comprised a ClockIdentity (8) and PortNumber (2),
  88. * the MC only captures the last six bytes of the clock identity. These values
  89. * reflect those, not the ones used in the standard. The standard permits
  90. * mapping of V1 UUIDs to V2 UUIDs with these same values.
  91. */
  92. #define PTP_V2_MC_UUID_LENGTH 6
  93. #define PTP_V2_MC_UUID_OFFSET 50
  94. #define PTP_V2_SEQUENCE_LENGTH 2
  95. #define PTP_V2_SEQUENCE_OFFSET 58
  96. /* The minimum length of a PTP V2 packet for offsets, etc. to be valid:
  97. * includes IP header.
  98. */
  99. #define PTP_V2_MIN_LENGTH 63
  100. #define PTP_MIN_LENGTH 63
  101. #define PTP_ADDRESS 0xe0000181 /* 224.0.1.129 */
  102. #define PTP_EVENT_PORT 319
  103. #define PTP_GENERAL_PORT 320
  104. /* Annoyingly the format of the version numbers are different between
  105. * versions 1 and 2 so it isn't possible to simply look for 1 or 2.
  106. */
  107. #define PTP_VERSION_V1 1
  108. #define PTP_VERSION_V2 2
  109. #define PTP_VERSION_V2_MASK 0x0f
  110. enum ptp_packet_state {
  111. PTP_PACKET_STATE_UNMATCHED = 0,
  112. PTP_PACKET_STATE_MATCHED,
  113. PTP_PACKET_STATE_TIMED_OUT,
  114. PTP_PACKET_STATE_MATCH_UNWANTED
  115. };
  116. /* NIC synchronised with single word of time only comprising
  117. * partial seconds and full nanoseconds: 10^9 ~ 2^30 so 2 bits for seconds.
  118. */
  119. #define MC_NANOSECOND_BITS 30
  120. #define MC_NANOSECOND_MASK ((1 << MC_NANOSECOND_BITS) - 1)
  121. #define MC_SECOND_MASK ((1 << (32 - MC_NANOSECOND_BITS)) - 1)
  122. /* Maximum parts-per-billion adjustment that is acceptable */
  123. #define MAX_PPB 1000000
  124. /* Number of bits required to hold the above */
  125. #define MAX_PPB_BITS 20
  126. /* Number of extra bits allowed when calculating fractional ns.
  127. * EXTRA_BITS + MC_CMD_PTP_IN_ADJUST_BITS + MAX_PPB_BITS should
  128. * be less than 63.
  129. */
  130. #define PPB_EXTRA_BITS 2
  131. /* Precalculate scale word to avoid long long division at runtime */
  132. #define PPB_SCALE_WORD ((1LL << (PPB_EXTRA_BITS + MC_CMD_PTP_IN_ADJUST_BITS +\
  133. MAX_PPB_BITS)) / 1000000000LL)
  134. #define PTP_SYNC_ATTEMPTS 4
  135. /**
  136. * struct efx_ptp_match - Matching structure, stored in sk_buff's cb area.
  137. * @words: UUID and (partial) sequence number
  138. * @expiry: Time after which the packet should be delivered irrespective of
  139. * event arrival.
  140. * @state: The state of the packet - whether it is ready for processing or
  141. * whether that is of no interest.
  142. */
  143. struct efx_ptp_match {
  144. u32 words[DIV_ROUND_UP(PTP_V1_UUID_LENGTH, 4)];
  145. unsigned long expiry;
  146. enum ptp_packet_state state;
  147. };
  148. /**
  149. * struct efx_ptp_event_rx - A PTP receive event (from MC)
  150. * @seq0: First part of (PTP) UUID
  151. * @seq1: Second part of (PTP) UUID and sequence number
  152. * @hwtimestamp: Event timestamp
  153. */
  154. struct efx_ptp_event_rx {
  155. struct list_head link;
  156. u32 seq0;
  157. u32 seq1;
  158. ktime_t hwtimestamp;
  159. unsigned long expiry;
  160. };
  161. /**
  162. * struct efx_ptp_timeset - Synchronisation between host and MC
  163. * @host_start: Host time immediately before hardware timestamp taken
  164. * @seconds: Hardware timestamp, seconds
  165. * @nanoseconds: Hardware timestamp, nanoseconds
  166. * @host_end: Host time immediately after hardware timestamp taken
  167. * @waitns: Number of nanoseconds between hardware timestamp being read and
  168. * host end time being seen
  169. * @window: Difference of host_end and host_start
  170. * @valid: Whether this timeset is valid
  171. */
  172. struct efx_ptp_timeset {
  173. u32 host_start;
  174. u32 seconds;
  175. u32 nanoseconds;
  176. u32 host_end;
  177. u32 waitns;
  178. u32 window; /* Derived: end - start, allowing for wrap */
  179. };
  180. /**
  181. * struct efx_ptp_data - Precision Time Protocol (PTP) state
  182. * @channel: The PTP channel
  183. * @rxq: Receive queue (awaiting timestamps)
  184. * @txq: Transmit 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. * @evt_frags: Partly assembled PTP events
  199. * @evt_frag_idx: Current fragment number
  200. * @evt_code: Last event code
  201. * @start: Address at which MC indicates ready for synchronisation
  202. * @host_time_pps: Host time at last PPS
  203. * @last_sync_ns: Last number of nanoseconds between readings when synchronising
  204. * @base_sync_ns: Number of nanoseconds for last synchronisation.
  205. * @base_sync_valid: Whether base_sync_time is valid.
  206. * @current_adjfreq: Current ppb adjustment.
  207. * @phc_clock: Pointer to registered phc device
  208. * @phc_clock_info: Registration structure for phc device
  209. * @pps_work: pps work task for handling pps events
  210. * @pps_workwq: pps work queue
  211. * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
  212. * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
  213. * allocations in main data path).
  214. * @debug_ptp_dir: PTP debugfs directory
  215. * @missed_rx_sync: Number of packets received without syncrhonisation.
  216. * @good_syncs: Number of successful synchronisations.
  217. * @no_time_syncs: Number of synchronisations with no good times.
  218. * @bad_sync_durations: Number of synchronisations with bad durations.
  219. * @bad_syncs: Number of failed synchronisations.
  220. * @last_sync_time: Number of nanoseconds for last synchronisation.
  221. * @sync_timeouts: Number of synchronisation timeouts
  222. * @fast_syncs: Number of synchronisations requiring short delay
  223. * @min_sync_delta: Minimum time between event and synchronisation
  224. * @max_sync_delta: Maximum time between event and synchronisation
  225. * @average_sync_delta: Average time between event and synchronisation.
  226. * Modified moving average.
  227. * @last_sync_delta: Last time between event and synchronisation
  228. * @mc_stats: Context value for MC statistics
  229. * @timeset: Last set of synchronisation statistics.
  230. */
  231. struct efx_ptp_data {
  232. struct efx_channel *channel;
  233. struct sk_buff_head rxq;
  234. struct sk_buff_head txq;
  235. struct list_head evt_list;
  236. struct list_head evt_free_list;
  237. spinlock_t evt_lock;
  238. struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
  239. struct workqueue_struct *workwq;
  240. struct work_struct work;
  241. bool reset_required;
  242. u32 rxfilter_event;
  243. u32 rxfilter_general;
  244. bool rxfilter_installed;
  245. struct hwtstamp_config config;
  246. bool enabled;
  247. unsigned int mode;
  248. efx_qword_t evt_frags[MAX_EVENT_FRAGS];
  249. int evt_frag_idx;
  250. int evt_code;
  251. struct efx_buffer start;
  252. struct pps_event_time host_time_pps;
  253. unsigned last_sync_ns;
  254. unsigned base_sync_ns;
  255. bool base_sync_valid;
  256. s64 current_adjfreq;
  257. struct ptp_clock *phc_clock;
  258. struct ptp_clock_info phc_clock_info;
  259. struct work_struct pps_work;
  260. struct workqueue_struct *pps_workwq;
  261. bool nic_ts_enabled;
  262. MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
  263. struct efx_ptp_timeset
  264. timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
  265. };
  266. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
  267. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
  268. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts);
  269. static int efx_phc_settime(struct ptp_clock_info *ptp,
  270. const struct timespec *e_ts);
  271. static int efx_phc_enable(struct ptp_clock_info *ptp,
  272. struct ptp_clock_request *request, int on);
  273. /* Enable MCDI PTP support. */
  274. static int efx_ptp_enable(struct efx_nic *efx)
  275. {
  276. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
  277. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
  278. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  279. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
  280. efx->ptp_data->channel->channel);
  281. MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
  282. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  283. NULL, 0, NULL);
  284. }
  285. /* Disable MCDI PTP support.
  286. *
  287. * Note that this function should never rely on the presence of ptp_data -
  288. * may be called before that exists.
  289. */
  290. static int efx_ptp_disable(struct efx_nic *efx)
  291. {
  292. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
  293. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
  294. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  295. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  296. NULL, 0, NULL);
  297. }
  298. static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
  299. {
  300. struct sk_buff *skb;
  301. while ((skb = skb_dequeue(q))) {
  302. local_bh_disable();
  303. netif_receive_skb(skb);
  304. local_bh_enable();
  305. }
  306. }
  307. static void efx_ptp_handle_no_channel(struct efx_nic *efx)
  308. {
  309. netif_err(efx, drv, efx->net_dev,
  310. "ERROR: PTP requires MSI-X and 1 additional interrupt"
  311. "vector. PTP disabled\n");
  312. }
  313. /* Repeatedly send the host time to the MC which will capture the hardware
  314. * time.
  315. */
  316. static void efx_ptp_send_times(struct efx_nic *efx,
  317. struct pps_event_time *last_time)
  318. {
  319. struct pps_event_time now;
  320. struct timespec limit;
  321. struct efx_ptp_data *ptp = efx->ptp_data;
  322. struct timespec start;
  323. int *mc_running = ptp->start.addr;
  324. pps_get_ts(&now);
  325. start = now.ts_real;
  326. limit = now.ts_real;
  327. timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
  328. /* Write host time for specified period or until MC is done */
  329. while ((timespec_compare(&now.ts_real, &limit) < 0) &&
  330. ACCESS_ONCE(*mc_running)) {
  331. struct timespec update_time;
  332. unsigned int host_time;
  333. /* Don't update continuously to avoid saturating the PCIe bus */
  334. update_time = now.ts_real;
  335. timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
  336. do {
  337. pps_get_ts(&now);
  338. } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
  339. ACCESS_ONCE(*mc_running));
  340. /* Synchronise NIC with single word of time only */
  341. host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
  342. now.ts_real.tv_nsec);
  343. /* Update host time in NIC memory */
  344. _efx_writed(efx, cpu_to_le32(host_time),
  345. FR_CZ_MC_TREG_SMEM + MC_SMEM_P0_PTP_TIME_OFST);
  346. }
  347. *last_time = now;
  348. }
  349. /* Read a timeset from the MC's results and partial process. */
  350. static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
  351. struct efx_ptp_timeset *timeset)
  352. {
  353. unsigned start_ns, end_ns;
  354. timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
  355. timeset->seconds = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_SECONDS);
  356. timeset->nanoseconds = MCDI_DWORD(data,
  357. PTP_OUT_SYNCHRONIZE_NANOSECONDS);
  358. timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
  359. timeset->waitns = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
  360. /* Ignore seconds */
  361. start_ns = timeset->host_start & MC_NANOSECOND_MASK;
  362. end_ns = timeset->host_end & MC_NANOSECOND_MASK;
  363. /* Allow for rollover */
  364. if (end_ns < start_ns)
  365. end_ns += NSEC_PER_SEC;
  366. /* Determine duration of operation */
  367. timeset->window = end_ns - start_ns;
  368. }
  369. /* Process times received from MC.
  370. *
  371. * Extract times from returned results, and establish the minimum value
  372. * seen. The minimum value represents the "best" possible time and events
  373. * too much greater than this are rejected - the machine is, perhaps, too
  374. * busy. A number of readings are taken so that, hopefully, at least one good
  375. * synchronisation will be seen in the results.
  376. */
  377. static int
  378. efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
  379. size_t response_length,
  380. const struct pps_event_time *last_time)
  381. {
  382. unsigned number_readings =
  383. MCDI_VAR_ARRAY_LEN(response_length,
  384. PTP_OUT_SYNCHRONIZE_TIMESET);
  385. unsigned i;
  386. unsigned total;
  387. unsigned ngood = 0;
  388. unsigned last_good = 0;
  389. struct efx_ptp_data *ptp = efx->ptp_data;
  390. u32 last_sec;
  391. u32 start_sec;
  392. struct timespec delta;
  393. if (number_readings == 0)
  394. return -EAGAIN;
  395. /* Read the set of results and increment stats for any results that
  396. * appera to be erroneous.
  397. */
  398. for (i = 0; i < number_readings; i++) {
  399. efx_ptp_read_timeset(
  400. MCDI_ARRAY_STRUCT_PTR(synch_buf,
  401. PTP_OUT_SYNCHRONIZE_TIMESET, i),
  402. &ptp->timeset[i]);
  403. }
  404. /* Find the last good host-MC synchronization result. The MC times
  405. * when it finishes reading the host time so the corrected window time
  406. * should be fairly constant for a given platform.
  407. */
  408. total = 0;
  409. for (i = 0; i < number_readings; i++)
  410. if (ptp->timeset[i].window > ptp->timeset[i].waitns) {
  411. unsigned win;
  412. win = ptp->timeset[i].window - ptp->timeset[i].waitns;
  413. if (win >= MIN_SYNCHRONISATION_NS &&
  414. win < MAX_SYNCHRONISATION_NS) {
  415. total += ptp->timeset[i].window;
  416. ngood++;
  417. last_good = i;
  418. }
  419. }
  420. if (ngood == 0) {
  421. netif_warn(efx, drv, efx->net_dev,
  422. "PTP no suitable synchronisations %dns\n",
  423. ptp->base_sync_ns);
  424. return -EAGAIN;
  425. }
  426. /* Average minimum this synchronisation */
  427. ptp->last_sync_ns = DIV_ROUND_UP(total, ngood);
  428. if (!ptp->base_sync_valid || (ptp->last_sync_ns < ptp->base_sync_ns)) {
  429. ptp->base_sync_valid = true;
  430. ptp->base_sync_ns = ptp->last_sync_ns;
  431. }
  432. /* Calculate delay from actual PPS to last_time */
  433. delta.tv_nsec =
  434. ptp->timeset[last_good].nanoseconds +
  435. last_time->ts_real.tv_nsec -
  436. (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
  437. /* It is possible that the seconds rolled over between taking
  438. * the start reading and the last value written by the host. The
  439. * timescales are such that a gap of more than one second is never
  440. * expected.
  441. */
  442. start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
  443. last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
  444. if (start_sec != last_sec) {
  445. if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
  446. netif_warn(efx, hw, efx->net_dev,
  447. "PTP bad synchronisation seconds\n");
  448. return -EAGAIN;
  449. } else {
  450. delta.tv_sec = 1;
  451. }
  452. } else {
  453. delta.tv_sec = 0;
  454. }
  455. ptp->host_time_pps = *last_time;
  456. pps_sub_ts(&ptp->host_time_pps, delta);
  457. return 0;
  458. }
  459. /* Synchronize times between the host and the MC */
  460. static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
  461. {
  462. struct efx_ptp_data *ptp = efx->ptp_data;
  463. MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
  464. size_t response_length;
  465. int rc;
  466. unsigned long timeout;
  467. struct pps_event_time last_time = {};
  468. unsigned int loops = 0;
  469. int *start = ptp->start.addr;
  470. MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
  471. MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
  472. MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
  473. num_readings);
  474. MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
  475. ptp->start.dma_addr);
  476. /* Clear flag that signals MC ready */
  477. ACCESS_ONCE(*start) = 0;
  478. rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
  479. MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
  480. EFX_BUG_ON_PARANOID(rc);
  481. /* Wait for start from MCDI (or timeout) */
  482. timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
  483. while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
  484. udelay(20); /* Usually start MCDI execution quickly */
  485. loops++;
  486. }
  487. if (ACCESS_ONCE(*start))
  488. efx_ptp_send_times(efx, &last_time);
  489. /* Collect results */
  490. rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
  491. MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
  492. synch_buf, sizeof(synch_buf),
  493. &response_length);
  494. if (rc == 0)
  495. rc = efx_ptp_process_times(efx, synch_buf, response_length,
  496. &last_time);
  497. return rc;
  498. }
  499. /* Transmit a PTP packet, via the MCDI interface, to the wire. */
  500. static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
  501. {
  502. struct efx_ptp_data *ptp_data = efx->ptp_data;
  503. struct skb_shared_hwtstamps timestamps;
  504. int rc = -EIO;
  505. MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
  506. size_t len;
  507. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
  508. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
  509. MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
  510. if (skb_shinfo(skb)->nr_frags != 0) {
  511. rc = skb_linearize(skb);
  512. if (rc != 0)
  513. goto fail;
  514. }
  515. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  516. rc = skb_checksum_help(skb);
  517. if (rc != 0)
  518. goto fail;
  519. }
  520. skb_copy_from_linear_data(skb,
  521. MCDI_PTR(ptp_data->txbuf,
  522. PTP_IN_TRANSMIT_PACKET),
  523. skb->len);
  524. rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
  525. ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
  526. txtime, sizeof(txtime), &len);
  527. if (rc != 0)
  528. goto fail;
  529. memset(&timestamps, 0, sizeof(timestamps));
  530. timestamps.hwtstamp = ktime_set(
  531. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_SECONDS),
  532. MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_NANOSECONDS));
  533. skb_tstamp_tx(skb, &timestamps);
  534. rc = 0;
  535. fail:
  536. dev_kfree_skb(skb);
  537. return rc;
  538. }
  539. static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
  540. {
  541. struct efx_ptp_data *ptp = efx->ptp_data;
  542. struct list_head *cursor;
  543. struct list_head *next;
  544. /* Drop time-expired events */
  545. spin_lock_bh(&ptp->evt_lock);
  546. if (!list_empty(&ptp->evt_list)) {
  547. list_for_each_safe(cursor, next, &ptp->evt_list) {
  548. struct efx_ptp_event_rx *evt;
  549. evt = list_entry(cursor, struct efx_ptp_event_rx,
  550. link);
  551. if (time_after(jiffies, evt->expiry)) {
  552. list_move(&evt->link, &ptp->evt_free_list);
  553. netif_warn(efx, hw, efx->net_dev,
  554. "PTP rx event dropped\n");
  555. }
  556. }
  557. }
  558. spin_unlock_bh(&ptp->evt_lock);
  559. }
  560. static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
  561. struct sk_buff *skb)
  562. {
  563. struct efx_ptp_data *ptp = efx->ptp_data;
  564. bool evts_waiting;
  565. struct list_head *cursor;
  566. struct list_head *next;
  567. struct efx_ptp_match *match;
  568. enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
  569. spin_lock_bh(&ptp->evt_lock);
  570. evts_waiting = !list_empty(&ptp->evt_list);
  571. spin_unlock_bh(&ptp->evt_lock);
  572. if (!evts_waiting)
  573. return PTP_PACKET_STATE_UNMATCHED;
  574. match = (struct efx_ptp_match *)skb->cb;
  575. /* Look for a matching timestamp in the event queue */
  576. spin_lock_bh(&ptp->evt_lock);
  577. list_for_each_safe(cursor, next, &ptp->evt_list) {
  578. struct efx_ptp_event_rx *evt;
  579. evt = list_entry(cursor, struct efx_ptp_event_rx, link);
  580. if ((evt->seq0 == match->words[0]) &&
  581. (evt->seq1 == match->words[1])) {
  582. struct skb_shared_hwtstamps *timestamps;
  583. /* Match - add in hardware timestamp */
  584. timestamps = skb_hwtstamps(skb);
  585. timestamps->hwtstamp = evt->hwtimestamp;
  586. match->state = PTP_PACKET_STATE_MATCHED;
  587. rc = PTP_PACKET_STATE_MATCHED;
  588. list_move(&evt->link, &ptp->evt_free_list);
  589. break;
  590. }
  591. }
  592. spin_unlock_bh(&ptp->evt_lock);
  593. return rc;
  594. }
  595. /* Process any queued receive events and corresponding packets
  596. *
  597. * q is returned with all the packets that are ready for delivery.
  598. * true is returned if at least one of those packets requires
  599. * synchronisation.
  600. */
  601. static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
  602. {
  603. struct efx_ptp_data *ptp = efx->ptp_data;
  604. bool rc = false;
  605. struct sk_buff *skb;
  606. while ((skb = skb_dequeue(&ptp->rxq))) {
  607. struct efx_ptp_match *match;
  608. match = (struct efx_ptp_match *)skb->cb;
  609. if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
  610. __skb_queue_tail(q, skb);
  611. } else if (efx_ptp_match_rx(efx, skb) ==
  612. PTP_PACKET_STATE_MATCHED) {
  613. rc = true;
  614. __skb_queue_tail(q, skb);
  615. } else if (time_after(jiffies, match->expiry)) {
  616. match->state = PTP_PACKET_STATE_TIMED_OUT;
  617. netif_warn(efx, rx_err, efx->net_dev,
  618. "PTP packet - no timestamp seen\n");
  619. __skb_queue_tail(q, skb);
  620. } else {
  621. /* Replace unprocessed entry and stop */
  622. skb_queue_head(&ptp->rxq, skb);
  623. break;
  624. }
  625. }
  626. return rc;
  627. }
  628. /* Complete processing of a received packet */
  629. static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
  630. {
  631. local_bh_disable();
  632. netif_receive_skb(skb);
  633. local_bh_enable();
  634. }
  635. static int efx_ptp_start(struct efx_nic *efx)
  636. {
  637. struct efx_ptp_data *ptp = efx->ptp_data;
  638. struct efx_filter_spec rxfilter;
  639. int rc;
  640. ptp->reset_required = false;
  641. /* Must filter on both event and general ports to ensure
  642. * that there is no packet re-ordering.
  643. */
  644. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  645. efx_rx_queue_index(
  646. efx_channel_get_rx_queue(ptp->channel)));
  647. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  648. htonl(PTP_ADDRESS),
  649. htons(PTP_EVENT_PORT));
  650. if (rc != 0)
  651. return rc;
  652. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  653. if (rc < 0)
  654. return rc;
  655. ptp->rxfilter_event = rc;
  656. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  657. efx_rx_queue_index(
  658. efx_channel_get_rx_queue(ptp->channel)));
  659. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  660. htonl(PTP_ADDRESS),
  661. htons(PTP_GENERAL_PORT));
  662. if (rc != 0)
  663. goto fail;
  664. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  665. if (rc < 0)
  666. goto fail;
  667. ptp->rxfilter_general = rc;
  668. rc = efx_ptp_enable(efx);
  669. if (rc != 0)
  670. goto fail2;
  671. ptp->evt_frag_idx = 0;
  672. ptp->current_adjfreq = 0;
  673. ptp->rxfilter_installed = true;
  674. return 0;
  675. fail2:
  676. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  677. ptp->rxfilter_general);
  678. fail:
  679. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  680. ptp->rxfilter_event);
  681. return rc;
  682. }
  683. static int efx_ptp_stop(struct efx_nic *efx)
  684. {
  685. struct efx_ptp_data *ptp = efx->ptp_data;
  686. int rc = efx_ptp_disable(efx);
  687. struct list_head *cursor;
  688. struct list_head *next;
  689. if (ptp->rxfilter_installed) {
  690. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  691. ptp->rxfilter_general);
  692. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  693. ptp->rxfilter_event);
  694. ptp->rxfilter_installed = false;
  695. }
  696. /* Make sure RX packets are really delivered */
  697. efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
  698. skb_queue_purge(&efx->ptp_data->txq);
  699. /* Drop any pending receive events */
  700. spin_lock_bh(&efx->ptp_data->evt_lock);
  701. list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
  702. list_move(cursor, &efx->ptp_data->evt_free_list);
  703. }
  704. spin_unlock_bh(&efx->ptp_data->evt_lock);
  705. return rc;
  706. }
  707. static void efx_ptp_pps_worker(struct work_struct *work)
  708. {
  709. struct efx_ptp_data *ptp =
  710. container_of(work, struct efx_ptp_data, pps_work);
  711. struct efx_nic *efx = ptp->channel->efx;
  712. struct ptp_clock_event ptp_evt;
  713. if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
  714. return;
  715. ptp_evt.type = PTP_CLOCK_PPSUSR;
  716. ptp_evt.pps_times = ptp->host_time_pps;
  717. ptp_clock_event(ptp->phc_clock, &ptp_evt);
  718. }
  719. /* Process any pending transmissions and timestamp any received packets.
  720. */
  721. static void efx_ptp_worker(struct work_struct *work)
  722. {
  723. struct efx_ptp_data *ptp_data =
  724. container_of(work, struct efx_ptp_data, work);
  725. struct efx_nic *efx = ptp_data->channel->efx;
  726. struct sk_buff *skb;
  727. struct sk_buff_head tempq;
  728. if (ptp_data->reset_required) {
  729. efx_ptp_stop(efx);
  730. efx_ptp_start(efx);
  731. return;
  732. }
  733. efx_ptp_drop_time_expired_events(efx);
  734. __skb_queue_head_init(&tempq);
  735. if (efx_ptp_process_events(efx, &tempq) ||
  736. !skb_queue_empty(&ptp_data->txq)) {
  737. while ((skb = skb_dequeue(&ptp_data->txq)))
  738. efx_ptp_xmit_skb(efx, skb);
  739. }
  740. while ((skb = __skb_dequeue(&tempq)))
  741. efx_ptp_process_rx(efx, skb);
  742. }
  743. /* Initialise PTP channel and state.
  744. *
  745. * Setting core_index to zero causes the queue to be initialised and doesn't
  746. * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
  747. */
  748. static int efx_ptp_probe_channel(struct efx_channel *channel)
  749. {
  750. struct efx_nic *efx = channel->efx;
  751. struct efx_ptp_data *ptp;
  752. int rc = 0;
  753. unsigned int pos;
  754. channel->irq_moderation = 0;
  755. channel->rx_queue.core_index = 0;
  756. ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
  757. efx->ptp_data = ptp;
  758. if (!efx->ptp_data)
  759. return -ENOMEM;
  760. rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
  761. if (rc != 0)
  762. goto fail1;
  763. ptp->channel = channel;
  764. skb_queue_head_init(&ptp->rxq);
  765. skb_queue_head_init(&ptp->txq);
  766. ptp->workwq = create_singlethread_workqueue("sfc_ptp");
  767. if (!ptp->workwq) {
  768. rc = -ENOMEM;
  769. goto fail2;
  770. }
  771. INIT_WORK(&ptp->work, efx_ptp_worker);
  772. ptp->config.flags = 0;
  773. ptp->config.tx_type = HWTSTAMP_TX_OFF;
  774. ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
  775. INIT_LIST_HEAD(&ptp->evt_list);
  776. INIT_LIST_HEAD(&ptp->evt_free_list);
  777. spin_lock_init(&ptp->evt_lock);
  778. for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
  779. list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
  780. ptp->phc_clock_info.owner = THIS_MODULE;
  781. snprintf(ptp->phc_clock_info.name,
  782. sizeof(ptp->phc_clock_info.name),
  783. "%pm", efx->net_dev->perm_addr);
  784. ptp->phc_clock_info.max_adj = MAX_PPB;
  785. ptp->phc_clock_info.n_alarm = 0;
  786. ptp->phc_clock_info.n_ext_ts = 0;
  787. ptp->phc_clock_info.n_per_out = 0;
  788. ptp->phc_clock_info.pps = 1;
  789. ptp->phc_clock_info.adjfreq = efx_phc_adjfreq;
  790. ptp->phc_clock_info.adjtime = efx_phc_adjtime;
  791. ptp->phc_clock_info.gettime = efx_phc_gettime;
  792. ptp->phc_clock_info.settime = efx_phc_settime;
  793. ptp->phc_clock_info.enable = efx_phc_enable;
  794. ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
  795. &efx->pci_dev->dev);
  796. if (IS_ERR(ptp->phc_clock)) {
  797. rc = PTR_ERR(ptp->phc_clock);
  798. goto fail3;
  799. }
  800. INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
  801. ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
  802. if (!ptp->pps_workwq) {
  803. rc = -ENOMEM;
  804. goto fail4;
  805. }
  806. ptp->nic_ts_enabled = false;
  807. return 0;
  808. fail4:
  809. ptp_clock_unregister(efx->ptp_data->phc_clock);
  810. fail3:
  811. destroy_workqueue(efx->ptp_data->workwq);
  812. fail2:
  813. efx_nic_free_buffer(efx, &ptp->start);
  814. fail1:
  815. kfree(efx->ptp_data);
  816. efx->ptp_data = NULL;
  817. return rc;
  818. }
  819. static void efx_ptp_remove_channel(struct efx_channel *channel)
  820. {
  821. struct efx_nic *efx = channel->efx;
  822. if (!efx->ptp_data)
  823. return;
  824. (void)efx_ptp_disable(channel->efx);
  825. cancel_work_sync(&efx->ptp_data->work);
  826. cancel_work_sync(&efx->ptp_data->pps_work);
  827. skb_queue_purge(&efx->ptp_data->rxq);
  828. skb_queue_purge(&efx->ptp_data->txq);
  829. ptp_clock_unregister(efx->ptp_data->phc_clock);
  830. destroy_workqueue(efx->ptp_data->workwq);
  831. destroy_workqueue(efx->ptp_data->pps_workwq);
  832. efx_nic_free_buffer(efx, &efx->ptp_data->start);
  833. kfree(efx->ptp_data);
  834. }
  835. static void efx_ptp_get_channel_name(struct efx_channel *channel,
  836. char *buf, size_t len)
  837. {
  838. snprintf(buf, len, "%s-ptp", channel->efx->name);
  839. }
  840. /* Determine whether this packet should be processed by the PTP module
  841. * or transmitted conventionally.
  842. */
  843. bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  844. {
  845. return efx->ptp_data &&
  846. efx->ptp_data->enabled &&
  847. skb->len >= PTP_MIN_LENGTH &&
  848. skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
  849. likely(skb->protocol == htons(ETH_P_IP)) &&
  850. ip_hdr(skb)->protocol == IPPROTO_UDP &&
  851. udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
  852. }
  853. /* Receive a PTP packet. Packets are queued until the arrival of
  854. * the receive timestamp from the MC - this will probably occur after the
  855. * packet arrival because of the processing in the MC.
  856. */
  857. static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
  858. {
  859. struct efx_nic *efx = channel->efx;
  860. struct efx_ptp_data *ptp = efx->ptp_data;
  861. struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
  862. u8 *match_data_012, *match_data_345;
  863. unsigned int version;
  864. match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  865. /* Correct version? */
  866. if (ptp->mode == MC_CMD_PTP_MODE_V1) {
  867. if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
  868. return false;
  869. }
  870. version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
  871. if (version != PTP_VERSION_V1) {
  872. return false;
  873. }
  874. /* PTP V1 uses all six bytes of the UUID to match the packet
  875. * to the timestamp
  876. */
  877. match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
  878. match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
  879. } else {
  880. if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
  881. return false;
  882. }
  883. version = skb->data[PTP_V2_VERSION_OFFSET];
  884. if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
  885. return false;
  886. }
  887. /* The original V2 implementation uses bytes 2-7 of
  888. * the UUID to match the packet to the timestamp. This
  889. * discards two of the bytes of the MAC address used
  890. * to create the UUID (SF bug 33070). The PTP V2
  891. * enhanced mode fixes this issue and uses bytes 0-2
  892. * and byte 5-7 of the UUID.
  893. */
  894. match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
  895. if (ptp->mode == MC_CMD_PTP_MODE_V2) {
  896. match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
  897. } else {
  898. match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
  899. BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
  900. }
  901. }
  902. /* Does this packet require timestamping? */
  903. if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
  904. struct skb_shared_hwtstamps *timestamps;
  905. match->state = PTP_PACKET_STATE_UNMATCHED;
  906. /* Clear all timestamps held: filled in later */
  907. timestamps = skb_hwtstamps(skb);
  908. memset(timestamps, 0, sizeof(*timestamps));
  909. /* We expect the sequence number to be in the same position in
  910. * the packet for PTP V1 and V2
  911. */
  912. BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
  913. BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
  914. /* Extract UUID/Sequence information */
  915. match->words[0] = (match_data_012[0] |
  916. (match_data_012[1] << 8) |
  917. (match_data_012[2] << 16) |
  918. (match_data_345[0] << 24));
  919. match->words[1] = (match_data_345[1] |
  920. (match_data_345[2] << 8) |
  921. (skb->data[PTP_V1_SEQUENCE_OFFSET +
  922. PTP_V1_SEQUENCE_LENGTH - 1] <<
  923. 16));
  924. } else {
  925. match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
  926. }
  927. skb_queue_tail(&ptp->rxq, skb);
  928. queue_work(ptp->workwq, &ptp->work);
  929. return true;
  930. }
  931. /* Transmit a PTP packet. This has to be transmitted by the MC
  932. * itself, through an MCDI call. MCDI calls aren't permitted
  933. * in the transmit path so defer the actual transmission to a suitable worker.
  934. */
  935. int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  936. {
  937. struct efx_ptp_data *ptp = efx->ptp_data;
  938. skb_queue_tail(&ptp->txq, skb);
  939. if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
  940. (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
  941. efx_xmit_hwtstamp_pending(skb);
  942. queue_work(ptp->workwq, &ptp->work);
  943. return NETDEV_TX_OK;
  944. }
  945. static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
  946. unsigned int new_mode)
  947. {
  948. if ((enable_wanted != efx->ptp_data->enabled) ||
  949. (enable_wanted && (efx->ptp_data->mode != new_mode))) {
  950. int rc;
  951. if (enable_wanted) {
  952. /* Change of mode requires disable */
  953. if (efx->ptp_data->enabled &&
  954. (efx->ptp_data->mode != new_mode)) {
  955. efx->ptp_data->enabled = false;
  956. rc = efx_ptp_stop(efx);
  957. if (rc != 0)
  958. return rc;
  959. }
  960. /* Set new operating mode and establish
  961. * baseline synchronisation, which must
  962. * succeed.
  963. */
  964. efx->ptp_data->mode = new_mode;
  965. rc = efx_ptp_start(efx);
  966. if (rc == 0) {
  967. rc = efx_ptp_synchronize(efx,
  968. PTP_SYNC_ATTEMPTS * 2);
  969. if (rc != 0)
  970. efx_ptp_stop(efx);
  971. }
  972. } else {
  973. rc = efx_ptp_stop(efx);
  974. }
  975. if (rc != 0)
  976. return rc;
  977. efx->ptp_data->enabled = enable_wanted;
  978. }
  979. return 0;
  980. }
  981. static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
  982. {
  983. bool enable_wanted = false;
  984. unsigned int new_mode;
  985. int rc;
  986. if (init->flags)
  987. return -EINVAL;
  988. if ((init->tx_type != HWTSTAMP_TX_OFF) &&
  989. (init->tx_type != HWTSTAMP_TX_ON))
  990. return -ERANGE;
  991. new_mode = efx->ptp_data->mode;
  992. /* Determine whether any PTP HW operations are required */
  993. switch (init->rx_filter) {
  994. case HWTSTAMP_FILTER_NONE:
  995. break;
  996. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  997. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  998. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  999. init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  1000. new_mode = MC_CMD_PTP_MODE_V1;
  1001. enable_wanted = true;
  1002. break;
  1003. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1004. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1005. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1006. /* Although these three are accepted only IPV4 packets will be
  1007. * timestamped
  1008. */
  1009. init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  1010. new_mode = MC_CMD_PTP_MODE_V2_ENHANCED;
  1011. enable_wanted = true;
  1012. break;
  1013. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1014. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1015. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1016. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1017. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1018. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1019. /* Non-IP + IPv6 timestamping not supported */
  1020. return -ERANGE;
  1021. break;
  1022. default:
  1023. return -ERANGE;
  1024. }
  1025. if (init->tx_type != HWTSTAMP_TX_OFF)
  1026. enable_wanted = true;
  1027. /* Old versions of the firmware do not support the improved
  1028. * UUID filtering option (SF bug 33070). If the firmware does
  1029. * not accept the enhanced mode, fall back to the standard PTP
  1030. * v2 UUID filtering.
  1031. */
  1032. rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
  1033. if ((rc != 0) && (new_mode == MC_CMD_PTP_MODE_V2_ENHANCED))
  1034. rc = efx_ptp_change_mode(efx, enable_wanted, MC_CMD_PTP_MODE_V2);
  1035. if (rc != 0)
  1036. return rc;
  1037. efx->ptp_data->config = *init;
  1038. return 0;
  1039. }
  1040. void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
  1041. {
  1042. struct efx_ptp_data *ptp = efx->ptp_data;
  1043. if (!ptp)
  1044. return;
  1045. ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
  1046. SOF_TIMESTAMPING_RX_HARDWARE |
  1047. SOF_TIMESTAMPING_RAW_HARDWARE);
  1048. ts_info->phc_index = ptp_clock_index(ptp->phc_clock);
  1049. ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
  1050. ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE |
  1051. 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT |
  1052. 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC |
  1053. 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ |
  1054. 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT |
  1055. 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC |
  1056. 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
  1057. }
  1058. int efx_ptp_ioctl(struct efx_nic *efx, struct ifreq *ifr, int cmd)
  1059. {
  1060. struct hwtstamp_config config;
  1061. int rc;
  1062. /* Not a PTP enabled port */
  1063. if (!efx->ptp_data)
  1064. return -EOPNOTSUPP;
  1065. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1066. return -EFAULT;
  1067. rc = efx_ptp_ts_init(efx, &config);
  1068. if (rc != 0)
  1069. return rc;
  1070. return copy_to_user(ifr->ifr_data, &config, sizeof(config))
  1071. ? -EFAULT : 0;
  1072. }
  1073. static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
  1074. {
  1075. struct efx_ptp_data *ptp = efx->ptp_data;
  1076. netif_err(efx, hw, efx->net_dev,
  1077. "PTP unexpected event length: got %d expected %d\n",
  1078. ptp->evt_frag_idx, expected_frag_len);
  1079. ptp->reset_required = true;
  1080. queue_work(ptp->workwq, &ptp->work);
  1081. }
  1082. /* Process a completed receive event. Put it on the event queue and
  1083. * start worker thread. This is required because event and their
  1084. * correspoding packets may come in either order.
  1085. */
  1086. static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1087. {
  1088. struct efx_ptp_event_rx *evt = NULL;
  1089. if (ptp->evt_frag_idx != 3) {
  1090. ptp_event_failure(efx, 3);
  1091. return;
  1092. }
  1093. spin_lock_bh(&ptp->evt_lock);
  1094. if (!list_empty(&ptp->evt_free_list)) {
  1095. evt = list_first_entry(&ptp->evt_free_list,
  1096. struct efx_ptp_event_rx, link);
  1097. list_del(&evt->link);
  1098. evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
  1099. evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
  1100. MCDI_EVENT_SRC) |
  1101. (EFX_QWORD_FIELD(ptp->evt_frags[1],
  1102. MCDI_EVENT_SRC) << 8) |
  1103. (EFX_QWORD_FIELD(ptp->evt_frags[0],
  1104. MCDI_EVENT_SRC) << 16));
  1105. evt->hwtimestamp = ktime_set(
  1106. EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
  1107. EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA));
  1108. evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1109. list_add_tail(&evt->link, &ptp->evt_list);
  1110. queue_work(ptp->workwq, &ptp->work);
  1111. } else {
  1112. netif_err(efx, rx_err, efx->net_dev, "No free PTP event");
  1113. }
  1114. spin_unlock_bh(&ptp->evt_lock);
  1115. }
  1116. static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1117. {
  1118. int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
  1119. if (ptp->evt_frag_idx != 1) {
  1120. ptp_event_failure(efx, 1);
  1121. return;
  1122. }
  1123. netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
  1124. }
  1125. static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1126. {
  1127. if (ptp->nic_ts_enabled)
  1128. queue_work(ptp->pps_workwq, &ptp->pps_work);
  1129. }
  1130. void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
  1131. {
  1132. struct efx_ptp_data *ptp = efx->ptp_data;
  1133. int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
  1134. if (!ptp->enabled)
  1135. return;
  1136. if (ptp->evt_frag_idx == 0) {
  1137. ptp->evt_code = code;
  1138. } else if (ptp->evt_code != code) {
  1139. netif_err(efx, hw, efx->net_dev,
  1140. "PTP out of sequence event %d\n", code);
  1141. ptp->evt_frag_idx = 0;
  1142. }
  1143. ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
  1144. if (!MCDI_EVENT_FIELD(*ev, CONT)) {
  1145. /* Process resulting event */
  1146. switch (code) {
  1147. case MCDI_EVENT_CODE_PTP_RX:
  1148. ptp_event_rx(efx, ptp);
  1149. break;
  1150. case MCDI_EVENT_CODE_PTP_FAULT:
  1151. ptp_event_fault(efx, ptp);
  1152. break;
  1153. case MCDI_EVENT_CODE_PTP_PPS:
  1154. ptp_event_pps(efx, ptp);
  1155. break;
  1156. default:
  1157. netif_err(efx, hw, efx->net_dev,
  1158. "PTP unknown event %d\n", code);
  1159. break;
  1160. }
  1161. ptp->evt_frag_idx = 0;
  1162. } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
  1163. netif_err(efx, hw, efx->net_dev,
  1164. "PTP too many event fragments\n");
  1165. ptp->evt_frag_idx = 0;
  1166. }
  1167. }
  1168. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  1169. {
  1170. struct efx_ptp_data *ptp_data = container_of(ptp,
  1171. struct efx_ptp_data,
  1172. phc_clock_info);
  1173. struct efx_nic *efx = ptp_data->channel->efx;
  1174. MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
  1175. s64 adjustment_ns;
  1176. int rc;
  1177. if (delta > MAX_PPB)
  1178. delta = MAX_PPB;
  1179. else if (delta < -MAX_PPB)
  1180. delta = -MAX_PPB;
  1181. /* Convert ppb to fixed point ns. */
  1182. adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
  1183. (PPB_EXTRA_BITS + MAX_PPB_BITS));
  1184. MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1185. MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
  1186. MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
  1187. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
  1188. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
  1189. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
  1190. NULL, 0, NULL);
  1191. if (rc != 0)
  1192. return rc;
  1193. ptp_data->current_adjfreq = delta;
  1194. return 0;
  1195. }
  1196. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1197. {
  1198. struct efx_ptp_data *ptp_data = container_of(ptp,
  1199. struct efx_ptp_data,
  1200. phc_clock_info);
  1201. struct efx_nic *efx = ptp_data->channel->efx;
  1202. struct timespec delta_ts = ns_to_timespec(delta);
  1203. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
  1204. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1205. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1206. MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, 0);
  1207. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_SECONDS, (u32)delta_ts.tv_sec);
  1208. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_NANOSECONDS, (u32)delta_ts.tv_nsec);
  1209. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1210. NULL, 0, NULL);
  1211. }
  1212. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  1213. {
  1214. struct efx_ptp_data *ptp_data = container_of(ptp,
  1215. struct efx_ptp_data,
  1216. phc_clock_info);
  1217. struct efx_nic *efx = ptp_data->channel->efx;
  1218. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
  1219. MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
  1220. int rc;
  1221. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
  1222. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  1223. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1224. outbuf, sizeof(outbuf), NULL);
  1225. if (rc != 0)
  1226. return rc;
  1227. ts->tv_sec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_SECONDS);
  1228. ts->tv_nsec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_NANOSECONDS);
  1229. return 0;
  1230. }
  1231. static int efx_phc_settime(struct ptp_clock_info *ptp,
  1232. const struct timespec *e_ts)
  1233. {
  1234. /* Get the current NIC time, efx_phc_gettime.
  1235. * Subtract from the desired time to get the offset
  1236. * call efx_phc_adjtime with the offset
  1237. */
  1238. int rc;
  1239. struct timespec time_now;
  1240. struct timespec delta;
  1241. rc = efx_phc_gettime(ptp, &time_now);
  1242. if (rc != 0)
  1243. return rc;
  1244. delta = timespec_sub(*e_ts, time_now);
  1245. rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
  1246. if (rc != 0)
  1247. return rc;
  1248. return 0;
  1249. }
  1250. static int efx_phc_enable(struct ptp_clock_info *ptp,
  1251. struct ptp_clock_request *request,
  1252. int enable)
  1253. {
  1254. struct efx_ptp_data *ptp_data = container_of(ptp,
  1255. struct efx_ptp_data,
  1256. phc_clock_info);
  1257. if (request->type != PTP_CLK_REQ_PPS)
  1258. return -EOPNOTSUPP;
  1259. ptp_data->nic_ts_enabled = !!enable;
  1260. return 0;
  1261. }
  1262. static const struct efx_channel_type efx_ptp_channel_type = {
  1263. .handle_no_channel = efx_ptp_handle_no_channel,
  1264. .pre_probe = efx_ptp_probe_channel,
  1265. .post_remove = efx_ptp_remove_channel,
  1266. .get_name = efx_ptp_get_channel_name,
  1267. /* no copy operation; there is no need to reallocate this channel */
  1268. .receive_skb = efx_ptp_rx,
  1269. .keep_eventq = false,
  1270. };
  1271. void efx_ptp_probe(struct efx_nic *efx)
  1272. {
  1273. /* Check whether PTP is implemented on this NIC. The DISABLE
  1274. * operation will succeed if and only if it is implemented.
  1275. */
  1276. if (efx_ptp_disable(efx) == 0)
  1277. efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
  1278. &efx_ptp_channel_type;
  1279. }