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