ptp.c 43 KB

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