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