ptp.c 42 KB

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