ptp.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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_del(&evt->link);
  563. list_add(&evt->link, &ptp->evt_free_list);
  564. netif_warn(efx, hw, efx->net_dev,
  565. "PTP rx event dropped\n");
  566. }
  567. }
  568. }
  569. spin_unlock_bh(&ptp->evt_lock);
  570. }
  571. static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
  572. struct sk_buff *skb)
  573. {
  574. struct efx_ptp_data *ptp = efx->ptp_data;
  575. bool evts_waiting;
  576. struct list_head *cursor;
  577. struct list_head *next;
  578. struct efx_ptp_match *match;
  579. enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
  580. spin_lock_bh(&ptp->evt_lock);
  581. evts_waiting = !list_empty(&ptp->evt_list);
  582. spin_unlock_bh(&ptp->evt_lock);
  583. if (!evts_waiting)
  584. return PTP_PACKET_STATE_UNMATCHED;
  585. match = (struct efx_ptp_match *)skb->cb;
  586. /* Look for a matching timestamp in the event queue */
  587. spin_lock_bh(&ptp->evt_lock);
  588. list_for_each_safe(cursor, next, &ptp->evt_list) {
  589. struct efx_ptp_event_rx *evt;
  590. evt = list_entry(cursor, struct efx_ptp_event_rx, link);
  591. if ((evt->seq0 == match->words[0]) &&
  592. (evt->seq1 == match->words[1])) {
  593. struct skb_shared_hwtstamps *timestamps;
  594. /* Match - add in hardware timestamp */
  595. timestamps = skb_hwtstamps(skb);
  596. timestamps->hwtstamp = evt->hwtimestamp;
  597. match->state = PTP_PACKET_STATE_MATCHED;
  598. rc = PTP_PACKET_STATE_MATCHED;
  599. list_del(&evt->link);
  600. list_add(&evt->link, &ptp->evt_free_list);
  601. break;
  602. }
  603. }
  604. spin_unlock_bh(&ptp->evt_lock);
  605. return rc;
  606. }
  607. /* Process any queued receive events and corresponding packets
  608. *
  609. * q is returned with all the packets that are ready for delivery.
  610. * true is returned if at least one of those packets requires
  611. * synchronisation.
  612. */
  613. static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
  614. {
  615. struct efx_ptp_data *ptp = efx->ptp_data;
  616. bool rc = false;
  617. struct sk_buff *skb;
  618. while ((skb = skb_dequeue(&ptp->rxq))) {
  619. struct efx_ptp_match *match;
  620. match = (struct efx_ptp_match *)skb->cb;
  621. if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
  622. __skb_queue_tail(q, skb);
  623. } else if (efx_ptp_match_rx(efx, skb) ==
  624. PTP_PACKET_STATE_MATCHED) {
  625. rc = true;
  626. __skb_queue_tail(q, skb);
  627. } else if (time_after(jiffies, match->expiry)) {
  628. match->state = PTP_PACKET_STATE_TIMED_OUT;
  629. netif_warn(efx, rx_err, efx->net_dev,
  630. "PTP packet - no timestamp seen\n");
  631. __skb_queue_tail(q, skb);
  632. } else {
  633. /* Replace unprocessed entry and stop */
  634. skb_queue_head(&ptp->rxq, skb);
  635. break;
  636. }
  637. }
  638. return rc;
  639. }
  640. /* Complete processing of a received packet */
  641. static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
  642. {
  643. local_bh_disable();
  644. netif_receive_skb(skb);
  645. local_bh_enable();
  646. }
  647. static int efx_ptp_start(struct efx_nic *efx)
  648. {
  649. struct efx_ptp_data *ptp = efx->ptp_data;
  650. struct efx_filter_spec rxfilter;
  651. int rc;
  652. ptp->reset_required = false;
  653. /* Must filter on both event and general ports to ensure
  654. * that there is no packet re-ordering.
  655. */
  656. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  657. efx_rx_queue_index(
  658. efx_channel_get_rx_queue(ptp->channel)));
  659. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  660. htonl(PTP_ADDRESS),
  661. htons(PTP_EVENT_PORT));
  662. if (rc != 0)
  663. return rc;
  664. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  665. if (rc < 0)
  666. return rc;
  667. ptp->rxfilter_event = rc;
  668. efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
  669. efx_rx_queue_index(
  670. efx_channel_get_rx_queue(ptp->channel)));
  671. rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
  672. htonl(PTP_ADDRESS),
  673. htons(PTP_GENERAL_PORT));
  674. if (rc != 0)
  675. goto fail;
  676. rc = efx_filter_insert_filter(efx, &rxfilter, true);
  677. if (rc < 0)
  678. goto fail;
  679. ptp->rxfilter_general = rc;
  680. rc = efx_ptp_enable(efx);
  681. if (rc != 0)
  682. goto fail2;
  683. ptp->evt_frag_idx = 0;
  684. ptp->current_adjfreq = 0;
  685. ptp->rxfilter_installed = true;
  686. return 0;
  687. fail2:
  688. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  689. ptp->rxfilter_general);
  690. fail:
  691. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  692. ptp->rxfilter_event);
  693. return rc;
  694. }
  695. static int efx_ptp_stop(struct efx_nic *efx)
  696. {
  697. struct efx_ptp_data *ptp = efx->ptp_data;
  698. int rc = efx_ptp_disable(efx);
  699. struct list_head *cursor;
  700. struct list_head *next;
  701. if (ptp->rxfilter_installed) {
  702. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  703. ptp->rxfilter_general);
  704. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
  705. ptp->rxfilter_event);
  706. ptp->rxfilter_installed = false;
  707. }
  708. /* Make sure RX packets are really delivered */
  709. efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
  710. skb_queue_purge(&efx->ptp_data->txq);
  711. /* Drop any pending receive events */
  712. spin_lock_bh(&efx->ptp_data->evt_lock);
  713. list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
  714. list_del(cursor);
  715. list_add(cursor, &efx->ptp_data->evt_free_list);
  716. }
  717. spin_unlock_bh(&efx->ptp_data->evt_lock);
  718. return rc;
  719. }
  720. static void efx_ptp_pps_worker(struct work_struct *work)
  721. {
  722. struct efx_ptp_data *ptp =
  723. container_of(work, struct efx_ptp_data, pps_work);
  724. struct efx_nic *efx = ptp->channel->efx;
  725. struct ptp_clock_event ptp_evt;
  726. if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
  727. return;
  728. ptp_evt.type = PTP_CLOCK_PPSUSR;
  729. ptp_evt.pps_times = ptp->host_time_pps;
  730. ptp_clock_event(ptp->phc_clock, &ptp_evt);
  731. }
  732. /* Process any pending transmissions and timestamp any received packets.
  733. */
  734. static void efx_ptp_worker(struct work_struct *work)
  735. {
  736. struct efx_ptp_data *ptp_data =
  737. container_of(work, struct efx_ptp_data, work);
  738. struct efx_nic *efx = ptp_data->channel->efx;
  739. struct sk_buff *skb;
  740. struct sk_buff_head tempq;
  741. if (ptp_data->reset_required) {
  742. efx_ptp_stop(efx);
  743. efx_ptp_start(efx);
  744. return;
  745. }
  746. efx_ptp_drop_time_expired_events(efx);
  747. __skb_queue_head_init(&tempq);
  748. if (efx_ptp_process_events(efx, &tempq) ||
  749. !skb_queue_empty(&ptp_data->txq)) {
  750. while ((skb = skb_dequeue(&ptp_data->txq)))
  751. efx_ptp_xmit_skb(efx, skb);
  752. }
  753. while ((skb = __skb_dequeue(&tempq)))
  754. efx_ptp_process_rx(efx, skb);
  755. }
  756. /* Initialise PTP channel and state.
  757. *
  758. * Setting core_index to zero causes the queue to be initialised and doesn't
  759. * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
  760. */
  761. static int efx_ptp_probe_channel(struct efx_channel *channel)
  762. {
  763. struct efx_nic *efx = channel->efx;
  764. struct efx_ptp_data *ptp;
  765. int rc = 0;
  766. unsigned int pos;
  767. channel->irq_moderation = 0;
  768. channel->rx_queue.core_index = 0;
  769. ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
  770. efx->ptp_data = ptp;
  771. if (!efx->ptp_data)
  772. return -ENOMEM;
  773. rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int));
  774. if (rc != 0)
  775. goto fail1;
  776. ptp->channel = channel;
  777. skb_queue_head_init(&ptp->rxq);
  778. skb_queue_head_init(&ptp->txq);
  779. ptp->workwq = create_singlethread_workqueue("sfc_ptp");
  780. if (!ptp->workwq) {
  781. rc = -ENOMEM;
  782. goto fail2;
  783. }
  784. INIT_WORK(&ptp->work, efx_ptp_worker);
  785. ptp->config.flags = 0;
  786. ptp->config.tx_type = HWTSTAMP_TX_OFF;
  787. ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
  788. INIT_LIST_HEAD(&ptp->evt_list);
  789. INIT_LIST_HEAD(&ptp->evt_free_list);
  790. spin_lock_init(&ptp->evt_lock);
  791. for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
  792. list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
  793. ptp->phc_clock_info.owner = THIS_MODULE;
  794. snprintf(ptp->phc_clock_info.name,
  795. sizeof(ptp->phc_clock_info.name),
  796. "%pm", efx->net_dev->perm_addr);
  797. ptp->phc_clock_info.max_adj = MAX_PPB;
  798. ptp->phc_clock_info.n_alarm = 0;
  799. ptp->phc_clock_info.n_ext_ts = 0;
  800. ptp->phc_clock_info.n_per_out = 0;
  801. ptp->phc_clock_info.pps = 1;
  802. ptp->phc_clock_info.adjfreq = efx_phc_adjfreq;
  803. ptp->phc_clock_info.adjtime = efx_phc_adjtime;
  804. ptp->phc_clock_info.gettime = efx_phc_gettime;
  805. ptp->phc_clock_info.settime = efx_phc_settime;
  806. ptp->phc_clock_info.enable = efx_phc_enable;
  807. ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
  808. &efx->pci_dev->dev);
  809. if (!ptp->phc_clock)
  810. goto fail3;
  811. INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
  812. ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
  813. if (!ptp->pps_workwq) {
  814. rc = -ENOMEM;
  815. goto fail4;
  816. }
  817. ptp->nic_ts_enabled = false;
  818. return 0;
  819. fail4:
  820. ptp_clock_unregister(efx->ptp_data->phc_clock);
  821. fail3:
  822. destroy_workqueue(efx->ptp_data->workwq);
  823. fail2:
  824. efx_nic_free_buffer(efx, &ptp->start);
  825. fail1:
  826. kfree(efx->ptp_data);
  827. efx->ptp_data = NULL;
  828. return rc;
  829. }
  830. static void efx_ptp_remove_channel(struct efx_channel *channel)
  831. {
  832. struct efx_nic *efx = channel->efx;
  833. if (!efx->ptp_data)
  834. return;
  835. (void)efx_ptp_disable(channel->efx);
  836. cancel_work_sync(&efx->ptp_data->work);
  837. cancel_work_sync(&efx->ptp_data->pps_work);
  838. skb_queue_purge(&efx->ptp_data->rxq);
  839. skb_queue_purge(&efx->ptp_data->txq);
  840. ptp_clock_unregister(efx->ptp_data->phc_clock);
  841. destroy_workqueue(efx->ptp_data->workwq);
  842. destroy_workqueue(efx->ptp_data->pps_workwq);
  843. efx_nic_free_buffer(efx, &efx->ptp_data->start);
  844. kfree(efx->ptp_data);
  845. }
  846. static void efx_ptp_get_channel_name(struct efx_channel *channel,
  847. char *buf, size_t len)
  848. {
  849. snprintf(buf, len, "%s-ptp", channel->efx->name);
  850. }
  851. /* Determine whether this packet should be processed by the PTP module
  852. * or transmitted conventionally.
  853. */
  854. bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  855. {
  856. return efx->ptp_data &&
  857. efx->ptp_data->enabled &&
  858. skb->len >= PTP_MIN_LENGTH &&
  859. skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
  860. likely(skb->protocol == htons(ETH_P_IP)) &&
  861. ip_hdr(skb)->protocol == IPPROTO_UDP &&
  862. udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
  863. }
  864. /* Receive a PTP packet. Packets are queued until the arrival of
  865. * the receive timestamp from the MC - this will probably occur after the
  866. * packet arrival because of the processing in the MC.
  867. */
  868. static void efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
  869. {
  870. struct efx_nic *efx = channel->efx;
  871. struct efx_ptp_data *ptp = efx->ptp_data;
  872. struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
  873. u8 *data;
  874. unsigned int version;
  875. match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  876. /* Correct version? */
  877. if (ptp->mode == MC_CMD_PTP_MODE_V1) {
  878. if (skb->len < PTP_V1_MIN_LENGTH) {
  879. netif_receive_skb(skb);
  880. return;
  881. }
  882. version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
  883. if (version != PTP_VERSION_V1) {
  884. netif_receive_skb(skb);
  885. return;
  886. }
  887. } else {
  888. if (skb->len < PTP_V2_MIN_LENGTH) {
  889. netif_receive_skb(skb);
  890. return;
  891. }
  892. version = skb->data[PTP_V2_VERSION_OFFSET];
  893. BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2);
  894. BUILD_BUG_ON(PTP_V1_UUID_OFFSET != PTP_V2_MC_UUID_OFFSET);
  895. BUILD_BUG_ON(PTP_V1_UUID_LENGTH != PTP_V2_MC_UUID_LENGTH);
  896. BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
  897. BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
  898. if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
  899. netif_receive_skb(skb);
  900. return;
  901. }
  902. }
  903. /* Does this packet require timestamping? */
  904. if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
  905. struct skb_shared_hwtstamps *timestamps;
  906. match->state = PTP_PACKET_STATE_UNMATCHED;
  907. /* Clear all timestamps held: filled in later */
  908. timestamps = skb_hwtstamps(skb);
  909. memset(timestamps, 0, sizeof(*timestamps));
  910. /* Extract UUID/Sequence information */
  911. data = skb->data + PTP_V1_UUID_OFFSET;
  912. match->words[0] = (data[0] |
  913. (data[1] << 8) |
  914. (data[2] << 16) |
  915. (data[3] << 24));
  916. match->words[1] = (data[4] |
  917. (data[5] << 8) |
  918. (skb->data[PTP_V1_SEQUENCE_OFFSET +
  919. PTP_V1_SEQUENCE_LENGTH - 1] <<
  920. 16));
  921. } else {
  922. match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
  923. }
  924. skb_queue_tail(&ptp->rxq, skb);
  925. queue_work(ptp->workwq, &ptp->work);
  926. }
  927. /* Transmit a PTP packet. This has to be transmitted by the MC
  928. * itself, through an MCDI call. MCDI calls aren't permitted
  929. * in the transmit path so defer the actual transmission to a suitable worker.
  930. */
  931. int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
  932. {
  933. struct efx_ptp_data *ptp = efx->ptp_data;
  934. skb_queue_tail(&ptp->txq, skb);
  935. if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
  936. (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
  937. efx_xmit_hwtstamp_pending(skb);
  938. queue_work(ptp->workwq, &ptp->work);
  939. return NETDEV_TX_OK;
  940. }
  941. static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
  942. unsigned int new_mode)
  943. {
  944. if ((enable_wanted != efx->ptp_data->enabled) ||
  945. (enable_wanted && (efx->ptp_data->mode != new_mode))) {
  946. int rc;
  947. if (enable_wanted) {
  948. /* Change of mode requires disable */
  949. if (efx->ptp_data->enabled &&
  950. (efx->ptp_data->mode != new_mode)) {
  951. efx->ptp_data->enabled = false;
  952. rc = efx_ptp_stop(efx);
  953. if (rc != 0)
  954. return rc;
  955. }
  956. /* Set new operating mode and establish
  957. * baseline synchronisation, which must
  958. * succeed.
  959. */
  960. efx->ptp_data->mode = new_mode;
  961. rc = efx_ptp_start(efx);
  962. if (rc == 0) {
  963. rc = efx_ptp_synchronize(efx,
  964. PTP_SYNC_ATTEMPTS * 2);
  965. if (rc != 0)
  966. efx_ptp_stop(efx);
  967. }
  968. } else {
  969. rc = efx_ptp_stop(efx);
  970. }
  971. if (rc != 0)
  972. return rc;
  973. efx->ptp_data->enabled = enable_wanted;
  974. }
  975. return 0;
  976. }
  977. static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
  978. {
  979. bool enable_wanted = false;
  980. unsigned int new_mode;
  981. int rc;
  982. if (init->flags)
  983. return -EINVAL;
  984. if ((init->tx_type != HWTSTAMP_TX_OFF) &&
  985. (init->tx_type != HWTSTAMP_TX_ON))
  986. return -ERANGE;
  987. new_mode = efx->ptp_data->mode;
  988. /* Determine whether any PTP HW operations are required */
  989. switch (init->rx_filter) {
  990. case HWTSTAMP_FILTER_NONE:
  991. break;
  992. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  993. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  994. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  995. init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  996. new_mode = MC_CMD_PTP_MODE_V1;
  997. enable_wanted = true;
  998. break;
  999. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1000. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1001. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1002. /* Although these three are accepted only IPV4 packets will be
  1003. * timestamped
  1004. */
  1005. init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  1006. new_mode = MC_CMD_PTP_MODE_V2;
  1007. enable_wanted = true;
  1008. break;
  1009. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1010. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1011. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1012. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1013. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1014. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1015. /* Non-IP + IPv6 timestamping not supported */
  1016. return -ERANGE;
  1017. break;
  1018. default:
  1019. return -ERANGE;
  1020. }
  1021. if (init->tx_type != HWTSTAMP_TX_OFF)
  1022. enable_wanted = true;
  1023. rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
  1024. if (rc != 0)
  1025. return rc;
  1026. efx->ptp_data->config = *init;
  1027. return 0;
  1028. }
  1029. int
  1030. efx_ptp_get_ts_info(struct net_device *net_dev, struct ethtool_ts_info *ts_info)
  1031. {
  1032. struct efx_nic *efx = netdev_priv(net_dev);
  1033. struct efx_ptp_data *ptp = efx->ptp_data;
  1034. if (!ptp)
  1035. return -EOPNOTSUPP;
  1036. ts_info->so_timestamping = (SOF_TIMESTAMPING_TX_HARDWARE |
  1037. SOF_TIMESTAMPING_RX_HARDWARE |
  1038. SOF_TIMESTAMPING_RAW_HARDWARE);
  1039. ts_info->phc_index = ptp_clock_index(ptp->phc_clock);
  1040. ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
  1041. ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE |
  1042. 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT |
  1043. 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC |
  1044. 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ |
  1045. 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT |
  1046. 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC |
  1047. 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
  1048. return 0;
  1049. }
  1050. int efx_ptp_ioctl(struct efx_nic *efx, struct ifreq *ifr, int cmd)
  1051. {
  1052. struct hwtstamp_config config;
  1053. int rc;
  1054. /* Not a PTP enabled port */
  1055. if (!efx->ptp_data)
  1056. return -EOPNOTSUPP;
  1057. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1058. return -EFAULT;
  1059. rc = efx_ptp_ts_init(efx, &config);
  1060. if (rc != 0)
  1061. return rc;
  1062. return copy_to_user(ifr->ifr_data, &config, sizeof(config))
  1063. ? -EFAULT : 0;
  1064. }
  1065. static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
  1066. {
  1067. struct efx_ptp_data *ptp = efx->ptp_data;
  1068. netif_err(efx, hw, efx->net_dev,
  1069. "PTP unexpected event length: got %d expected %d\n",
  1070. ptp->evt_frag_idx, expected_frag_len);
  1071. ptp->reset_required = true;
  1072. queue_work(ptp->workwq, &ptp->work);
  1073. }
  1074. /* Process a completed receive event. Put it on the event queue and
  1075. * start worker thread. This is required because event and their
  1076. * correspoding packets may come in either order.
  1077. */
  1078. static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1079. {
  1080. struct efx_ptp_event_rx *evt = NULL;
  1081. if (ptp->evt_frag_idx != 3) {
  1082. ptp_event_failure(efx, 3);
  1083. return;
  1084. }
  1085. spin_lock_bh(&ptp->evt_lock);
  1086. if (!list_empty(&ptp->evt_free_list)) {
  1087. evt = list_first_entry(&ptp->evt_free_list,
  1088. struct efx_ptp_event_rx, link);
  1089. list_del(&evt->link);
  1090. evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
  1091. evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
  1092. MCDI_EVENT_SRC) |
  1093. (EFX_QWORD_FIELD(ptp->evt_frags[1],
  1094. MCDI_EVENT_SRC) << 8) |
  1095. (EFX_QWORD_FIELD(ptp->evt_frags[0],
  1096. MCDI_EVENT_SRC) << 16));
  1097. evt->hwtimestamp = ktime_set(
  1098. EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
  1099. EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA));
  1100. evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
  1101. list_add_tail(&evt->link, &ptp->evt_list);
  1102. queue_work(ptp->workwq, &ptp->work);
  1103. } else {
  1104. netif_err(efx, rx_err, efx->net_dev, "No free PTP event");
  1105. }
  1106. spin_unlock_bh(&ptp->evt_lock);
  1107. }
  1108. static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1109. {
  1110. int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
  1111. if (ptp->evt_frag_idx != 1) {
  1112. ptp_event_failure(efx, 1);
  1113. return;
  1114. }
  1115. netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
  1116. }
  1117. static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
  1118. {
  1119. if (ptp->nic_ts_enabled)
  1120. queue_work(ptp->pps_workwq, &ptp->pps_work);
  1121. }
  1122. void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
  1123. {
  1124. struct efx_ptp_data *ptp = efx->ptp_data;
  1125. int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
  1126. if (!ptp->enabled)
  1127. return;
  1128. if (ptp->evt_frag_idx == 0) {
  1129. ptp->evt_code = code;
  1130. } else if (ptp->evt_code != code) {
  1131. netif_err(efx, hw, efx->net_dev,
  1132. "PTP out of sequence event %d\n", code);
  1133. ptp->evt_frag_idx = 0;
  1134. }
  1135. ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
  1136. if (!MCDI_EVENT_FIELD(*ev, CONT)) {
  1137. /* Process resulting event */
  1138. switch (code) {
  1139. case MCDI_EVENT_CODE_PTP_RX:
  1140. ptp_event_rx(efx, ptp);
  1141. break;
  1142. case MCDI_EVENT_CODE_PTP_FAULT:
  1143. ptp_event_fault(efx, ptp);
  1144. break;
  1145. case MCDI_EVENT_CODE_PTP_PPS:
  1146. ptp_event_pps(efx, ptp);
  1147. break;
  1148. default:
  1149. netif_err(efx, hw, efx->net_dev,
  1150. "PTP unknown event %d\n", code);
  1151. break;
  1152. }
  1153. ptp->evt_frag_idx = 0;
  1154. } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
  1155. netif_err(efx, hw, efx->net_dev,
  1156. "PTP too many event fragments\n");
  1157. ptp->evt_frag_idx = 0;
  1158. }
  1159. }
  1160. static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  1161. {
  1162. struct efx_ptp_data *ptp_data = container_of(ptp,
  1163. struct efx_ptp_data,
  1164. phc_clock_info);
  1165. struct efx_nic *efx = ptp_data->channel->efx;
  1166. u8 inadj[MC_CMD_PTP_IN_ADJUST_LEN];
  1167. s64 adjustment_ns;
  1168. int rc;
  1169. if (delta > MAX_PPB)
  1170. delta = MAX_PPB;
  1171. else if (delta < -MAX_PPB)
  1172. delta = -MAX_PPB;
  1173. /* Convert ppb to fixed point ns. */
  1174. adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
  1175. (PPB_EXTRA_BITS + MAX_PPB_BITS));
  1176. MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1177. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_FREQ_LO, (u32)adjustment_ns);
  1178. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_FREQ_HI,
  1179. (u32)(adjustment_ns >> 32));
  1180. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
  1181. MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
  1182. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
  1183. NULL, 0, NULL);
  1184. if (rc != 0)
  1185. return rc;
  1186. ptp_data->current_adjfreq = delta;
  1187. return 0;
  1188. }
  1189. static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1190. {
  1191. struct efx_ptp_data *ptp_data = container_of(ptp,
  1192. struct efx_ptp_data,
  1193. phc_clock_info);
  1194. struct efx_nic *efx = ptp_data->channel->efx;
  1195. struct timespec delta_ts = ns_to_timespec(delta);
  1196. u8 inbuf[MC_CMD_PTP_IN_ADJUST_LEN];
  1197. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
  1198. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_FREQ_LO, 0);
  1199. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_FREQ_HI, 0);
  1200. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_SECONDS, (u32)delta_ts.tv_sec);
  1201. MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_NANOSECONDS, (u32)delta_ts.tv_nsec);
  1202. return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1203. NULL, 0, NULL);
  1204. }
  1205. static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  1206. {
  1207. struct efx_ptp_data *ptp_data = container_of(ptp,
  1208. struct efx_ptp_data,
  1209. phc_clock_info);
  1210. struct efx_nic *efx = ptp_data->channel->efx;
  1211. u8 inbuf[MC_CMD_PTP_IN_READ_NIC_TIME_LEN];
  1212. u8 outbuf[MC_CMD_PTP_OUT_READ_NIC_TIME_LEN];
  1213. int rc;
  1214. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
  1215. rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
  1216. outbuf, sizeof(outbuf), NULL);
  1217. if (rc != 0)
  1218. return rc;
  1219. ts->tv_sec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_SECONDS);
  1220. ts->tv_nsec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_NANOSECONDS);
  1221. return 0;
  1222. }
  1223. static int efx_phc_settime(struct ptp_clock_info *ptp,
  1224. const struct timespec *e_ts)
  1225. {
  1226. /* Get the current NIC time, efx_phc_gettime.
  1227. * Subtract from the desired time to get the offset
  1228. * call efx_phc_adjtime with the offset
  1229. */
  1230. int rc;
  1231. struct timespec time_now;
  1232. struct timespec delta;
  1233. rc = efx_phc_gettime(ptp, &time_now);
  1234. if (rc != 0)
  1235. return rc;
  1236. delta = timespec_sub(*e_ts, time_now);
  1237. efx_phc_adjtime(ptp, timespec_to_ns(&delta));
  1238. if (rc != 0)
  1239. return rc;
  1240. return 0;
  1241. }
  1242. static int efx_phc_enable(struct ptp_clock_info *ptp,
  1243. struct ptp_clock_request *request,
  1244. int enable)
  1245. {
  1246. struct efx_ptp_data *ptp_data = container_of(ptp,
  1247. struct efx_ptp_data,
  1248. phc_clock_info);
  1249. if (request->type != PTP_CLK_REQ_PPS)
  1250. return -EOPNOTSUPP;
  1251. ptp_data->nic_ts_enabled = !!enable;
  1252. return 0;
  1253. }
  1254. static const struct efx_channel_type efx_ptp_channel_type = {
  1255. .handle_no_channel = efx_ptp_handle_no_channel,
  1256. .pre_probe = efx_ptp_probe_channel,
  1257. .post_remove = efx_ptp_remove_channel,
  1258. .get_name = efx_ptp_get_channel_name,
  1259. /* no copy operation; there is no need to reallocate this channel */
  1260. .receive_skb = efx_ptp_rx,
  1261. .keep_eventq = false,
  1262. };
  1263. void efx_ptp_probe(struct efx_nic *efx)
  1264. {
  1265. /* Check whether PTP is implemented on this NIC. The DISABLE
  1266. * operation will succeed if and only if it is implemented.
  1267. */
  1268. if (efx_ptp_disable(efx) == 0)
  1269. efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
  1270. &efx_ptp_channel_type;
  1271. }