1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2011-2013 Solarflare Communications Inc.
7 /* Theory of operation:
9 * PTP support is assisted by firmware running on the MC, which provides
10 * the hardware timestamping capabilities. Both transmitted and received
11 * PTP event packets are queued onto internal queues for subsequent processing;
12 * this is because the MC operations are relatively long and would block
13 * block NAPI/interrupt operation.
15 * Receive event processing:
16 * The event contains the packet's UUID and sequence number, together
17 * with the hardware timestamp. The PTP receive packet queue is searched
18 * for this UUID/sequence number and, if found, put on a pending queue.
19 * Packets not matching are delivered without timestamps (MCDI events will
20 * always arrive after the actual packet).
21 * It is important for the operation of the PTP protocol that the ordering
22 * of packets between the event and general port is maintained.
24 * Work queue processing:
25 * If work waiting, synchronise host/hardware time
27 * Transmit: send packet through MC, which returns the transmission time
28 * that is converted to an appropriate timestamp.
30 * Receive: the packet's reception time is converted to an appropriate
34 #include <linux/udp.h>
35 #include <linux/time.h>
36 #include <linux/ktime.h>
37 #include <linux/module.h>
38 #include <linux/net_tstamp.h>
39 #include <linux/pps_kernel.h>
40 #include <linux/ptp_clock_kernel.h>
41 #include "net_driver.h"
44 #include "mcdi_pcol.h"
46 #include "farch_regs.h"
49 /* Maximum number of events expected to make up a PTP event */
50 #define MAX_EVENT_FRAGS 3
52 /* Maximum delay, ms, to begin synchronisation */
53 #define MAX_SYNCHRONISE_WAIT_MS 2
55 /* How long, at most, to spend synchronising */
56 #define SYNCHRONISE_PERIOD_NS 250000
58 /* How often to update the shared memory time */
59 #define SYNCHRONISATION_GRANULARITY_NS 200
61 /* Minimum permitted length of a (corrected) synchronisation time */
62 #define DEFAULT_MIN_SYNCHRONISATION_NS 120
64 /* Maximum permitted length of a (corrected) synchronisation time */
65 #define MAX_SYNCHRONISATION_NS 1000
67 /* How many (MC) receive events that can be queued */
68 #define MAX_RECEIVE_EVENTS 8
70 /* Length of (modified) moving average. */
71 #define AVERAGE_LENGTH 16
73 /* How long an unmatched event or packet can be held */
74 #define PKT_EVENT_LIFETIME_MS 10
76 /* Offsets into PTP packet for identification. These offsets are from the
77 * start of the IP header, not the MAC header. Note that neither PTP V1 nor
78 * PTP V2 permit the use of IPV4 options.
80 #define PTP_DPORT_OFFSET 22
82 #define PTP_V1_VERSION_LENGTH 2
83 #define PTP_V1_VERSION_OFFSET 28
85 #define PTP_V1_UUID_LENGTH 6
86 #define PTP_V1_UUID_OFFSET 50
88 #define PTP_V1_SEQUENCE_LENGTH 2
89 #define PTP_V1_SEQUENCE_OFFSET 58
91 /* The minimum length of a PTP V1 packet for offsets, etc. to be valid:
94 #define PTP_V1_MIN_LENGTH 64
96 #define PTP_V2_VERSION_LENGTH 1
97 #define PTP_V2_VERSION_OFFSET 29
99 #define PTP_V2_UUID_LENGTH 8
100 #define PTP_V2_UUID_OFFSET 48
102 /* Although PTP V2 UUIDs are comprised a ClockIdentity (8) and PortNumber (2),
103 * the MC only captures the last six bytes of the clock identity. These values
104 * reflect those, not the ones used in the standard. The standard permits
105 * mapping of V1 UUIDs to V2 UUIDs with these same values.
107 #define PTP_V2_MC_UUID_LENGTH 6
108 #define PTP_V2_MC_UUID_OFFSET 50
110 #define PTP_V2_SEQUENCE_LENGTH 2
111 #define PTP_V2_SEQUENCE_OFFSET 58
113 /* The minimum length of a PTP V2 packet for offsets, etc. to be valid:
114 * includes IP header.
116 #define PTP_V2_MIN_LENGTH 63
118 #define PTP_MIN_LENGTH 63
120 #define PTP_ADDRESS 0xe0000181 /* 224.0.1.129 */
121 #define PTP_EVENT_PORT 319
122 #define PTP_GENERAL_PORT 320
124 /* Annoyingly the format of the version numbers are different between
125 * versions 1 and 2 so it isn't possible to simply look for 1 or 2.
127 #define PTP_VERSION_V1 1
129 #define PTP_VERSION_V2 2
130 #define PTP_VERSION_V2_MASK 0x0f
132 enum ptp_packet_state
{
133 PTP_PACKET_STATE_UNMATCHED
= 0,
134 PTP_PACKET_STATE_MATCHED
,
135 PTP_PACKET_STATE_TIMED_OUT
,
136 PTP_PACKET_STATE_MATCH_UNWANTED
139 /* NIC synchronised with single word of time only comprising
140 * partial seconds and full nanoseconds: 10^9 ~ 2^30 so 2 bits for seconds.
142 #define MC_NANOSECOND_BITS 30
143 #define MC_NANOSECOND_MASK ((1 << MC_NANOSECOND_BITS) - 1)
144 #define MC_SECOND_MASK ((1 << (32 - MC_NANOSECOND_BITS)) - 1)
146 /* Maximum parts-per-billion adjustment that is acceptable */
147 #define MAX_PPB 1000000
149 /* Precalculate scale word to avoid long long division at runtime */
150 /* This is equivalent to 2^66 / 10^9. */
151 #define PPB_SCALE_WORD ((1LL << (57)) / 1953125LL)
153 /* How much to shift down after scaling to convert to FP40 */
154 #define PPB_SHIFT_FP40 26
156 #define PPB_SHIFT_FP44 22
158 #define PTP_SYNC_ATTEMPTS 4
161 * struct efx_ptp_match - Matching structure, stored in sk_buff's cb area.
162 * @words: UUID and (partial) sequence number
163 * @expiry: Time after which the packet should be delivered irrespective of
165 * @state: The state of the packet - whether it is ready for processing or
166 * whether that is of no interest.
168 struct efx_ptp_match
{
169 u32 words
[DIV_ROUND_UP(PTP_V1_UUID_LENGTH
, 4)];
170 unsigned long expiry
;
171 enum ptp_packet_state state
;
175 * struct efx_ptp_event_rx - A PTP receive event (from MC)
176 * @seq0: First part of (PTP) UUID
177 * @seq1: Second part of (PTP) UUID and sequence number
178 * @hwtimestamp: Event timestamp
180 struct efx_ptp_event_rx
{
181 struct list_head link
;
185 unsigned long expiry
;
189 * struct efx_ptp_timeset - Synchronisation between host and MC
190 * @host_start: Host time immediately before hardware timestamp taken
191 * @major: Hardware timestamp, major
192 * @minor: Hardware timestamp, minor
193 * @host_end: Host time immediately after hardware timestamp taken
194 * @wait: Number of NIC clock ticks between hardware timestamp being read and
195 * host end time being seen
196 * @window: Difference of host_end and host_start
197 * @valid: Whether this timeset is valid
199 struct efx_ptp_timeset
{
205 u32 window
; /* Derived: end - start, allowing for wrap */
209 * struct efx_ptp_data - Precision Time Protocol (PTP) state
210 * @efx: The NIC context
211 * @channel: The PTP channel (Siena only)
212 * @rx_ts_inline: Flag for whether RX timestamps are inline (else they are
214 * @rxq: Receive SKB queue (awaiting timestamps)
215 * @txq: Transmit SKB queue
216 * @evt_list: List of MC receive events awaiting packets
217 * @evt_free_list: List of free events
218 * @evt_lock: Lock for manipulating evt_list and evt_free_list
219 * @rx_evts: Instantiated events (on evt_list and evt_free_list)
220 * @workwq: Work queue for processing pending PTP operations
222 * @reset_required: A serious error has occurred and the PTP task needs to be
223 * reset (disable, enable).
224 * @rxfilter_event: Receive filter when operating
225 * @rxfilter_general: Receive filter when operating
226 * @config: Current timestamp configuration
227 * @enabled: PTP operation enabled
228 * @mode: Mode in which PTP operating (PTP version)
229 * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time
230 * @nic_to_kernel_time: Function to convert from NIC to kernel time
231 * @nic_time.minor_max: Wrap point for NIC minor times
232 * @nic_time.sync_event_diff_min: Minimum acceptable difference between time
233 * in packet prefix and last MCDI time sync event i.e. how much earlier than
234 * the last sync event time a packet timestamp can be.
235 * @nic_time.sync_event_diff_max: Maximum acceptable difference between time
236 * in packet prefix and last MCDI time sync event i.e. how much later than
237 * the last sync event time a packet timestamp can be.
238 * @nic_time.sync_event_minor_shift: Shift required to make minor time from
239 * field in MCDI time sync event.
240 * @min_synchronisation_ns: Minimum acceptable corrected sync window
241 * @capabilities: Capabilities flags from the NIC
242 * @ts_corrections.ptp_tx: Required driver correction of PTP packet transmit
244 * @ts_corrections.ptp_rx: Required driver correction of PTP packet receive
246 * @ts_corrections.pps_out: PPS output error (information only)
247 * @ts_corrections.pps_in: Required driver correction of PPS input timestamps
248 * @ts_corrections.general_tx: Required driver correction of general packet
249 * transmit timestamps
250 * @ts_corrections.general_rx: Required driver correction of general packet
252 * @evt_frags: Partly assembled PTP events
253 * @evt_frag_idx: Current fragment number
254 * @evt_code: Last event code
255 * @start: Address at which MC indicates ready for synchronisation
256 * @host_time_pps: Host time at last PPS
257 * @adjfreq_ppb_shift: Shift required to convert scaled parts-per-billion
258 * frequency adjustment into a fixed point fractional nanosecond format.
259 * @current_adjfreq: Current ppb adjustment.
260 * @phc_clock: Pointer to registered phc device (if primary function)
261 * @phc_clock_info: Registration structure for phc device
262 * @pps_work: pps work task for handling pps events
263 * @pps_workwq: pps work queue
264 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
265 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
266 * allocations in main data path).
267 * @good_syncs: Number of successful synchronisations.
268 * @fast_syncs: Number of synchronisations requiring short delay
269 * @bad_syncs: Number of failed synchronisations.
270 * @sync_timeouts: Number of synchronisation timeouts
271 * @no_time_syncs: Number of synchronisations with no good times.
272 * @invalid_sync_windows: Number of sync windows with bad durations.
273 * @undersize_sync_windows: Number of corrected sync windows that are too small
274 * @oversize_sync_windows: Number of corrected sync windows that are too large
275 * @rx_no_timestamp: Number of packets received without a timestamp.
276 * @timeset: Last set of synchronisation statistics.
277 * @xmit_skb: Transmit SKB function.
279 struct efx_ptp_data
{
281 struct efx_channel
*channel
;
283 struct sk_buff_head rxq
;
284 struct sk_buff_head txq
;
285 struct list_head evt_list
;
286 struct list_head evt_free_list
;
288 struct efx_ptp_event_rx rx_evts
[MAX_RECEIVE_EVENTS
];
289 struct workqueue_struct
*workwq
;
290 struct work_struct work
;
293 u32 rxfilter_general
;
294 bool rxfilter_installed
;
295 struct hwtstamp_config config
;
298 void (*ns_to_nic_time
)(s64 ns
, u32
*nic_major
, u32
*nic_minor
);
299 ktime_t (*nic_to_kernel_time
)(u32 nic_major
, u32 nic_minor
,
303 u32 sync_event_diff_min
;
304 u32 sync_event_diff_max
;
305 unsigned int sync_event_minor_shift
;
307 unsigned int min_synchronisation_ns
;
308 unsigned int capabilities
;
317 efx_qword_t evt_frags
[MAX_EVENT_FRAGS
];
320 struct efx_buffer start
;
321 struct pps_event_time host_time_pps
;
322 unsigned int adjfreq_ppb_shift
;
324 struct ptp_clock
*phc_clock
;
325 struct ptp_clock_info phc_clock_info
;
326 struct work_struct pps_work
;
327 struct workqueue_struct
*pps_workwq
;
329 _MCDI_DECLARE_BUF(txbuf
, MC_CMD_PTP_IN_TRANSMIT_LENMAX
);
331 unsigned int good_syncs
;
332 unsigned int fast_syncs
;
333 unsigned int bad_syncs
;
334 unsigned int sync_timeouts
;
335 unsigned int no_time_syncs
;
336 unsigned int invalid_sync_windows
;
337 unsigned int undersize_sync_windows
;
338 unsigned int oversize_sync_windows
;
339 unsigned int rx_no_timestamp
;
340 struct efx_ptp_timeset
341 timeset
[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM
];
342 void (*xmit_skb
)(struct efx_nic
*efx
, struct sk_buff
*skb
);
345 static int efx_phc_adjfreq(struct ptp_clock_info
*ptp
, s32 delta
);
346 static int efx_phc_adjtime(struct ptp_clock_info
*ptp
, s64 delta
);
347 static int efx_phc_gettime(struct ptp_clock_info
*ptp
, struct timespec64
*ts
);
348 static int efx_phc_settime(struct ptp_clock_info
*ptp
,
349 const struct timespec64
*e_ts
);
350 static int efx_phc_enable(struct ptp_clock_info
*ptp
,
351 struct ptp_clock_request
*request
, int on
);
353 bool efx_ptp_use_mac_tx_timestamps(struct efx_nic
*efx
)
355 struct efx_ef10_nic_data
*nic_data
= efx
->nic_data
;
357 return ((efx_nic_rev(efx
) >= EFX_REV_HUNT_A0
) &&
358 (nic_data
->datapath_caps2
&
359 (1 << MC_CMD_GET_CAPABILITIES_V2_OUT_TX_MAC_TIMESTAMPING_LBN
)
363 /* PTP 'extra' channel is still a traffic channel, but we only create TX queues
364 * if PTP uses MAC TX timestamps, not if PTP uses the MC directly to transmit.
366 static bool efx_ptp_want_txqs(struct efx_channel
*channel
)
368 return efx_ptp_use_mac_tx_timestamps(channel
->efx
);
371 #define PTP_SW_STAT(ext_name, field_name) \
372 { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
373 #define PTP_MC_STAT(ext_name, mcdi_name) \
374 { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
375 static const struct efx_hw_stat_desc efx_ptp_stat_desc
[] = {
376 PTP_SW_STAT(ptp_good_syncs
, good_syncs
),
377 PTP_SW_STAT(ptp_fast_syncs
, fast_syncs
),
378 PTP_SW_STAT(ptp_bad_syncs
, bad_syncs
),
379 PTP_SW_STAT(ptp_sync_timeouts
, sync_timeouts
),
380 PTP_SW_STAT(ptp_no_time_syncs
, no_time_syncs
),
381 PTP_SW_STAT(ptp_invalid_sync_windows
, invalid_sync_windows
),
382 PTP_SW_STAT(ptp_undersize_sync_windows
, undersize_sync_windows
),
383 PTP_SW_STAT(ptp_oversize_sync_windows
, oversize_sync_windows
),
384 PTP_SW_STAT(ptp_rx_no_timestamp
, rx_no_timestamp
),
385 PTP_MC_STAT(ptp_tx_timestamp_packets
, TX
),
386 PTP_MC_STAT(ptp_rx_timestamp_packets
, RX
),
387 PTP_MC_STAT(ptp_timestamp_packets
, TS
),
388 PTP_MC_STAT(ptp_filter_matches
, FM
),
389 PTP_MC_STAT(ptp_non_filter_matches
, NFM
),
391 #define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
392 static const unsigned long efx_ptp_stat_mask
[] = {
393 [0 ... BITS_TO_LONGS(PTP_STAT_COUNT
) - 1] = ~0UL,
396 size_t efx_ptp_describe_stats(struct efx_nic
*efx
, u8
*strings
)
401 return efx_nic_describe_stats(efx_ptp_stat_desc
, PTP_STAT_COUNT
,
402 efx_ptp_stat_mask
, strings
);
405 size_t efx_ptp_update_stats(struct efx_nic
*efx
, u64
*stats
)
407 MCDI_DECLARE_BUF(inbuf
, MC_CMD_PTP_IN_STATUS_LEN
);
408 MCDI_DECLARE_BUF(outbuf
, MC_CMD_PTP_OUT_STATUS_LEN
);
415 /* Copy software statistics */
416 for (i
= 0; i
< PTP_STAT_COUNT
; i
++) {
417 if (efx_ptp_stat_desc
[i
].dma_width
)
419 stats
[i
] = *(unsigned int *)((char *)efx
->ptp_data
+
420 efx_ptp_stat_desc
[i
].offset
);
423 /* Fetch MC statistics. We *must* fill in all statistics or
424 * risk leaking kernel memory to userland, so if the MCDI
425 * request fails we pretend we got zeroes.
427 MCDI_SET_DWORD(inbuf
, PTP_IN_OP
, MC_CMD_PTP_OP_STATUS
);
428 MCDI_SET_DWORD(inbuf
, PTP_IN_PERIPH_ID
, 0);
429 rc
= efx_mcdi_rpc(efx
, MC_CMD_PTP
, inbuf
, sizeof(inbuf
),
430 outbuf
, sizeof(outbuf
), NULL
);
432 memset(outbuf
, 0, sizeof(outbuf
));
433 efx_nic_update_stats(efx_ptp_stat_desc
, PTP_STAT_COUNT
,
435 stats
, _MCDI_PTR(outbuf
, 0), false);
437 return PTP_STAT_COUNT
;
440 /* For Siena platforms NIC time is s and ns */
441 static void efx_ptp_ns_to_s_ns(s64 ns
, u32
*nic_major
, u32
*nic_minor
)
443 struct timespec64 ts
= ns_to_timespec64(ns
);
444 *nic_major
= (u32
)ts
.tv_sec
;
445 *nic_minor
= ts
.tv_nsec
;
448 static ktime_t
efx_ptp_s_ns_to_ktime_correction(u32 nic_major
, u32 nic_minor
,
451 ktime_t kt
= ktime_set(nic_major
, nic_minor
);
453 kt
= ktime_add_ns(kt
, (u64
)correction
);
455 kt
= ktime_sub_ns(kt
, (u64
)-correction
);
459 /* To convert from s27 format to ns we multiply then divide by a power of 2.
460 * For the conversion from ns to s27, the operation is also converted to a
461 * multiply and shift.
463 #define S27_TO_NS_SHIFT (27)
464 #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
465 #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
466 #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
468 /* For Huntington platforms NIC time is in seconds and fractions of a second
469 * where the minor register only uses 27 bits in units of 2^-27s.
471 static void efx_ptp_ns_to_s27(s64 ns
, u32
*nic_major
, u32
*nic_minor
)
473 struct timespec64 ts
= ns_to_timespec64(ns
);
474 u32 maj
= (u32
)ts
.tv_sec
;
475 u32 min
= (u32
)(((u64
)ts
.tv_nsec
* NS_TO_S27_MULT
+
476 (1ULL << (NS_TO_S27_SHIFT
- 1))) >> NS_TO_S27_SHIFT
);
478 /* The conversion can result in the minor value exceeding the maximum.
479 * In this case, round up to the next second.
481 if (min
>= S27_MINOR_MAX
) {
482 min
-= S27_MINOR_MAX
;
490 static inline ktime_t
efx_ptp_s27_to_ktime(u32 nic_major
, u32 nic_minor
)
492 u32 ns
= (u32
)(((u64
)nic_minor
* NSEC_PER_SEC
+
493 (1ULL << (S27_TO_NS_SHIFT
- 1))) >> S27_TO_NS_SHIFT
);
494 return ktime_set(nic_major
, ns
);
497 static ktime_t
efx_ptp_s27_to_ktime_correction(u32 nic_major
, u32 nic_minor
,
500 /* Apply the correction and deal with carry */
501 nic_minor
+= correction
;
502 if ((s32
)nic_minor
< 0) {
503 nic_minor
+= S27_MINOR_MAX
;
505 } else if (nic_minor
>= S27_MINOR_MAX
) {
506 nic_minor
-= S27_MINOR_MAX
;
510 return efx_ptp_s27_to_ktime(nic_major
, nic_minor
);
513 /* For Medford2 platforms the time is in seconds and quarter nanoseconds. */
514 static void efx_ptp_ns_to_s_qns(s64 ns
, u32
*nic_major
, u32
*nic_minor
)
516 struct timespec64 ts
= ns_to_timespec64(ns
);
518 *nic_major
= (u32
)ts
.tv_sec
;
519 *nic_minor
= ts
.tv_nsec
* 4;
522 static ktime_t
efx_ptp_s_qns_to_ktime_correction(u32 nic_major
, u32 nic_minor
,
527 nic_minor
= DIV_ROUND_CLOSEST(nic_minor
, 4);
528 correction
= DIV_ROUND_CLOSEST(correction
, 4);
530 kt
= ktime_set(nic_major
, nic_minor
);
533 kt
= ktime_add_ns(kt
, (u64
)correction
);
535 kt
= ktime_sub_ns(kt
, (u64
)-correction
);
539 struct efx_channel
*efx_ptp_channel(struct efx_nic
*efx
)
541 return efx
->ptp_data
? efx
->ptp_data
->channel
: NULL
;
544 static u32
last_sync_timestamp_major(struct efx_nic
*efx
)
546 struct efx_channel
*channel
= efx_ptp_channel(efx
);
550 major
= channel
->sync_timestamp_major
;
554 /* The 8000 series and later can provide the time from the MAC, which is only
555 * 48 bits long and provides meta-information in the top 2 bits.
558 efx_ptp_mac_nic_to_ktime_correction(struct efx_nic
*efx
,
559 struct efx_ptp_data
*ptp
,
560 u32 nic_major
, u32 nic_minor
,
565 if (!(nic_major
& 0x80000000)) {
566 WARN_ON_ONCE(nic_major
>> 16);
567 /* Use the top bits from the latest sync event. */
569 nic_major
|= (last_sync_timestamp_major(efx
) & 0xffff0000);
571 kt
= ptp
->nic_to_kernel_time(nic_major
, nic_minor
,
577 ktime_t
efx_ptp_nic_to_kernel_time(struct efx_tx_queue
*tx_queue
)
579 struct efx_nic
*efx
= tx_queue
->efx
;
580 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
583 if (efx_ptp_use_mac_tx_timestamps(efx
))
584 kt
= efx_ptp_mac_nic_to_ktime_correction(efx
, ptp
,
585 tx_queue
->completed_timestamp_major
,
586 tx_queue
->completed_timestamp_minor
,
587 ptp
->ts_corrections
.general_tx
);
589 kt
= ptp
->nic_to_kernel_time(
590 tx_queue
->completed_timestamp_major
,
591 tx_queue
->completed_timestamp_minor
,
592 ptp
->ts_corrections
.general_tx
);
596 /* Get PTP attributes and set up time conversions */
597 static int efx_ptp_get_attributes(struct efx_nic
*efx
)
599 MCDI_DECLARE_BUF(inbuf
, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN
);
600 MCDI_DECLARE_BUF(outbuf
, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN
);
601 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
606 /* Get the PTP attributes. If the NIC doesn't support the operation we
607 * use the default format for compatibility with older NICs i.e.
608 * seconds and nanoseconds.
610 MCDI_SET_DWORD(inbuf
, PTP_IN_OP
, MC_CMD_PTP_OP_GET_ATTRIBUTES
);
611 MCDI_SET_DWORD(inbuf
, PTP_IN_PERIPH_ID
, 0);
612 rc
= efx_mcdi_rpc_quiet(efx
, MC_CMD_PTP
, inbuf
, sizeof(inbuf
),
613 outbuf
, sizeof(outbuf
), &out_len
);
615 fmt
= MCDI_DWORD(outbuf
, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT
);
616 } else if (rc
== -EINVAL
) {
617 fmt
= MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS
;
618 } else if (rc
== -EPERM
) {
619 netif_info(efx
, probe
, efx
->net_dev
, "no PTP support\n");
622 efx_mcdi_display_error(efx
, MC_CMD_PTP
, sizeof(inbuf
),
623 outbuf
, sizeof(outbuf
), rc
);
628 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION
:
629 ptp
->ns_to_nic_time
= efx_ptp_ns_to_s27
;
630 ptp
->nic_to_kernel_time
= efx_ptp_s27_to_ktime_correction
;
631 ptp
->nic_time
.minor_max
= 1 << 27;
632 ptp
->nic_time
.sync_event_minor_shift
= 19;
634 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS
:
635 ptp
->ns_to_nic_time
= efx_ptp_ns_to_s_ns
;
636 ptp
->nic_to_kernel_time
= efx_ptp_s_ns_to_ktime_correction
;
637 ptp
->nic_time
.minor_max
= 1000000000;
638 ptp
->nic_time
.sync_event_minor_shift
= 22;
640 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS
:
641 ptp
->ns_to_nic_time
= efx_ptp_ns_to_s_qns
;
642 ptp
->nic_to_kernel_time
= efx_ptp_s_qns_to_ktime_correction
;
643 ptp
->nic_time
.minor_max
= 4000000000UL;
644 ptp
->nic_time
.sync_event_minor_shift
= 24;
650 /* Precalculate acceptable difference between the minor time in the
651 * packet prefix and the last MCDI time sync event. We expect the
652 * packet prefix timestamp to be after of sync event by up to one
653 * sync event interval (0.25s) but we allow it to exceed this by a
654 * fuzz factor of (0.1s)
656 ptp
->nic_time
.sync_event_diff_min
= ptp
->nic_time
.minor_max
657 - (ptp
->nic_time
.minor_max
/ 10);
658 ptp
->nic_time
.sync_event_diff_max
= (ptp
->nic_time
.minor_max
/ 4)
659 + (ptp
->nic_time
.minor_max
/ 10);
661 /* MC_CMD_PTP_OP_GET_ATTRIBUTES has been extended twice from an older
662 * operation MC_CMD_PTP_OP_GET_TIME_FORMAT. The function now may return
663 * a value to use for the minimum acceptable corrected synchronization
664 * window and may return further capabilities.
665 * If we have the extra information store it. For older firmware that
666 * does not implement the extended command use the default value.
669 out_len
>= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST
)
670 ptp
->min_synchronisation_ns
=
672 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN
);
674 ptp
->min_synchronisation_ns
= DEFAULT_MIN_SYNCHRONISATION_NS
;
677 out_len
>= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN
)
678 ptp
->capabilities
= MCDI_DWORD(outbuf
,
679 PTP_OUT_GET_ATTRIBUTES_CAPABILITIES
);
681 ptp
->capabilities
= 0;
683 /* Set up the shift for conversion between frequency
684 * adjustments in parts-per-billion and the fixed-point
685 * fractional ns format that the adapter uses.
687 if (ptp
->capabilities
& (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN
))
688 ptp
->adjfreq_ppb_shift
= PPB_SHIFT_FP44
;
690 ptp
->adjfreq_ppb_shift
= PPB_SHIFT_FP40
;
695 /* Get PTP timestamp corrections */
696 static int efx_ptp_get_timestamp_corrections(struct efx_nic
*efx
)
698 MCDI_DECLARE_BUF(inbuf
, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN
);
699 MCDI_DECLARE_BUF(outbuf
, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN
);
703 /* Get the timestamp corrections from the NIC. If this operation is
704 * not supported (older NICs) then no correction is required.
706 MCDI_SET_DWORD(inbuf
, PTP_IN_OP
,
707 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS
);
708 MCDI_SET_DWORD(inbuf
, PTP_IN_PERIPH_ID
, 0);
710 rc
= efx_mcdi_rpc_quiet(efx
, MC_CMD_PTP
, inbuf
, sizeof(inbuf
),
711 outbuf
, sizeof(outbuf
), &out_len
);
713 efx
->ptp_data
->ts_corrections
.ptp_tx
= MCDI_DWORD(outbuf
,
714 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT
);
715 efx
->ptp_data
->ts_corrections
.ptp_rx
= MCDI_DWORD(outbuf
,
716 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE
);
717 efx
->ptp_data
->ts_corrections
.pps_out
= MCDI_DWORD(outbuf
,
718 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT
);
719 efx
->ptp_data
->ts_corrections
.pps_in
= MCDI_DWORD(outbuf
,
720 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN
);
722 if (out_len
>= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN
) {
723 efx
->ptp_data
->ts_corrections
.general_tx
= MCDI_DWORD(
725 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX
);
726 efx
->ptp_data
->ts_corrections
.general_rx
= MCDI_DWORD(
728 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX
);
730 efx
->ptp_data
->ts_corrections
.general_tx
=
731 efx
->ptp_data
->ts_corrections
.ptp_tx
;
732 efx
->ptp_data
->ts_corrections
.general_rx
=
733 efx
->ptp_data
->ts_corrections
.ptp_rx
;
735 } else if (rc
== -EINVAL
) {
736 efx
->ptp_data
->ts_corrections
.ptp_tx
= 0;
737 efx
->ptp_data
->ts_corrections
.ptp_rx
= 0;
738 efx
->ptp_data
->ts_corrections
.pps_out
= 0;
739 efx
->ptp_data
->ts_corrections
.pps_in
= 0;
740 efx
->ptp_data
->ts_corrections
.general_tx
= 0;
741 efx
->ptp_data
->ts_corrections
.general_rx
= 0;
743 efx_mcdi_display_error(efx
, MC_CMD_PTP
, sizeof(inbuf
), outbuf
,
751 /* Enable MCDI PTP support. */
752 static int efx_ptp_enable(struct efx_nic
*efx
)
754 MCDI_DECLARE_BUF(inbuf
, MC_CMD_PTP_IN_ENABLE_LEN
);
755 MCDI_DECLARE_BUF_ERR(outbuf
);
758 MCDI_SET_DWORD(inbuf
, PTP_IN_OP
, MC_CMD_PTP_OP_ENABLE
);
759 MCDI_SET_DWORD(inbuf
, PTP_IN_PERIPH_ID
, 0);
760 MCDI_SET_DWORD(inbuf
, PTP_IN_ENABLE_QUEUE
,
761 efx
->ptp_data
->channel
?
762 efx
->ptp_data
->channel
->channel
: 0);
763 MCDI_SET_DWORD(inbuf
, PTP_IN_ENABLE_MODE
, efx
->ptp_data
->mode
);
765 rc
= efx_mcdi_rpc_quiet(efx
, MC_CMD_PTP
, inbuf
, sizeof(inbuf
),
766 outbuf
, sizeof(outbuf
), NULL
);
767 rc
= (rc
== -EALREADY
) ? 0 : rc
;
769 efx_mcdi_display_error(efx
, MC_CMD_PTP
,
770 MC_CMD_PTP_IN_ENABLE_LEN
,
771 outbuf
, sizeof(outbuf
), rc
);
775 /* Disable MCDI PTP support.
777 * Note that this function should never rely on the presence of ptp_data -
778 * may be called before that exists.
780 static int efx_ptp_disable(struct efx_nic
*efx
)
782 MCDI_DECLARE_BUF(inbuf
, MC_CMD_PTP_IN_DISABLE_LEN
);
783 MCDI_DECLARE_BUF_ERR(outbuf
);
786 MCDI_SET_DWORD(inbuf
, PTP_IN_OP
, MC_CMD_PTP_OP_DISABLE
);
787 MCDI_SET_DWORD(inbuf
, PTP_IN_PERIPH_ID
, 0);
788 rc
= efx_mcdi_rpc_quiet(efx
, MC_CMD_PTP
, inbuf
, sizeof(inbuf
),
789 outbuf
, sizeof(outbuf
), NULL
);
790 rc
= (rc
== -EALREADY
) ? 0 : rc
;
791 /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
792 * should only have been called during probe.
794 if (rc
== -ENOSYS
|| rc
== -EPERM
)
795 netif_info(efx
, probe
, efx
->net_dev
, "no PTP support\n");
797 efx_mcdi_display_error(efx
, MC_CMD_PTP
,
798 MC_CMD_PTP_IN_DISABLE_LEN
,
799 outbuf
, sizeof(outbuf
), rc
);
803 static void efx_ptp_deliver_rx_queue(struct sk_buff_head
*q
)
807 while ((skb
= skb_dequeue(q
))) {
809 netif_receive_skb(skb
);
814 static void efx_ptp_handle_no_channel(struct efx_nic
*efx
)
816 netif_err(efx
, drv
, efx
->net_dev
,
817 "ERROR: PTP requires MSI-X and 1 additional interrupt"
818 "vector. PTP disabled\n");
821 /* Repeatedly send the host time to the MC which will capture the hardware
824 static void efx_ptp_send_times(struct efx_nic
*efx
,
825 struct pps_event_time
*last_time
)
827 struct pps_event_time now
;
828 struct timespec64 limit
;
829 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
830 int *mc_running
= ptp
->start
.addr
;
834 timespec64_add_ns(&limit
, SYNCHRONISE_PERIOD_NS
);
836 /* Write host time for specified period or until MC is done */
837 while ((timespec64_compare(&now
.ts_real
, &limit
) < 0) &&
838 READ_ONCE(*mc_running
)) {
839 struct timespec64 update_time
;
840 unsigned int host_time
;
842 /* Don't update continuously to avoid saturating the PCIe bus */
843 update_time
= now
.ts_real
;
844 timespec64_add_ns(&update_time
, SYNCHRONISATION_GRANULARITY_NS
);
847 } while ((timespec64_compare(&now
.ts_real
, &update_time
) < 0) &&
848 READ_ONCE(*mc_running
));
850 /* Synchronise NIC with single word of time only */
851 host_time
= (now
.ts_real
.tv_sec
<< MC_NANOSECOND_BITS
|
852 now
.ts_real
.tv_nsec
);
853 /* Update host time in NIC memory */
854 efx
->type
->ptp_write_host_time(efx
, host_time
);
859 /* Read a timeset from the MC's results and partial process. */
860 static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data
),
861 struct efx_ptp_timeset
*timeset
)
863 unsigned start_ns
, end_ns
;
865 timeset
->host_start
= MCDI_DWORD(data
, PTP_OUT_SYNCHRONIZE_HOSTSTART
);
866 timeset
->major
= MCDI_DWORD(data
, PTP_OUT_SYNCHRONIZE_MAJOR
);
867 timeset
->minor
= MCDI_DWORD(data
, PTP_OUT_SYNCHRONIZE_MINOR
);
868 timeset
->host_end
= MCDI_DWORD(data
, PTP_OUT_SYNCHRONIZE_HOSTEND
),
869 timeset
->wait
= MCDI_DWORD(data
, PTP_OUT_SYNCHRONIZE_WAITNS
);
872 start_ns
= timeset
->host_start
& MC_NANOSECOND_MASK
;
873 end_ns
= timeset
->host_end
& MC_NANOSECOND_MASK
;
874 /* Allow for rollover */
875 if (end_ns
< start_ns
)
876 end_ns
+= NSEC_PER_SEC
;
877 /* Determine duration of operation */
878 timeset
->window
= end_ns
- start_ns
;
881 /* Process times received from MC.
883 * Extract times from returned results, and establish the minimum value
884 * seen. The minimum value represents the "best" possible time and events
885 * too much greater than this are rejected - the machine is, perhaps, too
886 * busy. A number of readings are taken so that, hopefully, at least one good
887 * synchronisation will be seen in the results.
890 efx_ptp_process_times(struct efx_nic
*efx
, MCDI_DECLARE_STRUCT_PTR(synch_buf
),
891 size_t response_length
,
892 const struct pps_event_time
*last_time
)
894 unsigned number_readings
=
895 MCDI_VAR_ARRAY_LEN(response_length
,
896 PTP_OUT_SYNCHRONIZE_TIMESET
);
899 unsigned last_good
= 0;
900 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
903 struct timespec64 delta
;
906 if (number_readings
== 0)
909 /* Read the set of results and find the last good host-MC
910 * synchronization result. The MC times when it finishes reading the
911 * host time so the corrected window time should be fairly constant
912 * for a given platform. Increment stats for any results that appear
915 for (i
= 0; i
< number_readings
; i
++) {
916 s32 window
, corrected
;
917 struct timespec64 wait
;
919 efx_ptp_read_timeset(
920 MCDI_ARRAY_STRUCT_PTR(synch_buf
,
921 PTP_OUT_SYNCHRONIZE_TIMESET
, i
),
924 wait
= ktime_to_timespec64(
925 ptp
->nic_to_kernel_time(0, ptp
->timeset
[i
].wait
, 0));
926 window
= ptp
->timeset
[i
].window
;
927 corrected
= window
- wait
.tv_nsec
;
929 /* We expect the uncorrected synchronization window to be at
930 * least as large as the interval between host start and end
931 * times. If it is smaller than this then this is mostly likely
932 * to be a consequence of the host's time being adjusted.
933 * Check that the corrected sync window is in a reasonable
934 * range. If it is out of range it is likely to be because an
935 * interrupt or other delay occurred between reading the system
936 * time and writing it to MC memory.
938 if (window
< SYNCHRONISATION_GRANULARITY_NS
) {
939 ++ptp
->invalid_sync_windows
;
940 } else if (corrected
>= MAX_SYNCHRONISATION_NS
) {
941 ++ptp
->oversize_sync_windows
;
942 } else if (corrected
< ptp
->min_synchronisation_ns
) {
943 ++ptp
->undersize_sync_windows
;
951 netif_warn(efx
, drv
, efx
->net_dev
,
952 "PTP no suitable synchronisations\n");
956 /* Calculate delay from last good sync (host time) to last_time.
957 * It is possible that the seconds rolled over between taking
958 * the start reading and the last value written by the host. The
959 * timescales are such that a gap of more than one second is never
960 * expected. delta is *not* normalised.
962 start_sec
= ptp
->timeset
[last_good
].host_start
>> MC_NANOSECOND_BITS
;
963 last_sec
= last_time
->ts_real
.tv_sec
& MC_SECOND_MASK
;
964 if (start_sec
!= last_sec
&&
965 ((start_sec
+ 1) & MC_SECOND_MASK
) != last_sec
) {
966 netif_warn(efx
, hw
, efx
->net_dev
,
967 "PTP bad synchronisation seconds\n");
970 delta
.tv_sec
= (last_sec
- start_sec
) & 1;
972 last_time
->ts_real
.tv_nsec
-
973 (ptp
->timeset
[last_good
].host_start
& MC_NANOSECOND_MASK
);
975 /* Convert the NIC time at last good sync into kernel time.
976 * No correction is required - this time is the output of a
979 mc_time
= ptp
->nic_to_kernel_time(ptp
->timeset
[last_good
].major
,
980 ptp
->timeset
[last_good
].minor
, 0);
982 /* Calculate delay from NIC top of second to last_time */
983 delta
.tv_nsec
+= ktime_to_timespec64(mc_time
).tv_nsec
;
985 /* Set PPS timestamp to match NIC top of second */
986 ptp
->host_time_pps
= *last_time
;
987 pps_sub_ts(&ptp
->host_time_pps
, delta
);
992 /* Synchronize times between the host and the MC */
993 static int efx_ptp_synchronize(struct efx_nic
*efx
, unsigned int num_readings
)
995 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
996 MCDI_DECLARE_BUF(synch_buf
, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX
);
997 size_t response_length
;
999 unsigned long timeout
;
1000 struct pps_event_time last_time
= {};
1001 unsigned int loops
= 0;
1002 int *start
= ptp
->start
.addr
;
1004 MCDI_SET_DWORD(synch_buf
, PTP_IN_OP
, MC_CMD_PTP_OP_SYNCHRONIZE
);
1005 MCDI_SET_DWORD(synch_buf
, PTP_IN_PERIPH_ID
, 0);
1006 MCDI_SET_DWORD(synch_buf
, PTP_IN_SYNCHRONIZE_NUMTIMESETS
,
1008 MCDI_SET_QWORD(synch_buf
, PTP_IN_SYNCHRONIZE_START_ADDR
,
1009 ptp
->start
.dma_addr
);
1011 /* Clear flag that signals MC ready */
1012 WRITE_ONCE(*start
, 0);
1013 rc
= efx_mcdi_rpc_start(efx
, MC_CMD_PTP
, synch_buf
,
1014 MC_CMD_PTP_IN_SYNCHRONIZE_LEN
);
1015 EFX_WARN_ON_ONCE_PARANOID(rc
);
1017 /* Wait for start from MCDI (or timeout) */
1018 timeout
= jiffies
+ msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS
);
1019 while (!READ_ONCE(*start
) && (time_before(jiffies
, timeout
))) {
1020 udelay(20); /* Usually start MCDI execution quickly */
1026 if (!time_before(jiffies
, timeout
))
1027 ++ptp
->sync_timeouts
;
1029 if (READ_ONCE(*start
))
1030 efx_ptp_send_times(efx
, &last_time
);
1032 /* Collect results */
1033 rc
= efx_mcdi_rpc_finish(efx
, MC_CMD_PTP
,
1034 MC_CMD_PTP_IN_SYNCHRONIZE_LEN
,
1035 synch_buf
, sizeof(synch_buf
),
1038 rc
= efx_ptp_process_times(efx
, synch_buf
, response_length
,
1043 ++ptp
->no_time_syncs
;
1046 /* Increment the bad syncs counter if the synchronize fails, whatever
1055 /* Transmit a PTP packet via the dedicated hardware timestamped queue. */
1056 static void efx_ptp_xmit_skb_queue(struct efx_nic
*efx
, struct sk_buff
*skb
)
1058 struct efx_ptp_data
*ptp_data
= efx
->ptp_data
;
1059 struct efx_tx_queue
*tx_queue
;
1060 u8 type
= skb
->ip_summed
== CHECKSUM_PARTIAL
? EFX_TXQ_TYPE_OFFLOAD
: 0;
1062 tx_queue
= &ptp_data
->channel
->tx_queue
[type
];
1063 if (tx_queue
&& tx_queue
->timestamping
) {
1064 efx_enqueue_skb(tx_queue
, skb
);
1066 WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
1067 dev_kfree_skb_any(skb
);
1071 /* Transmit a PTP packet, via the MCDI interface, to the wire. */
1072 static void efx_ptp_xmit_skb_mc(struct efx_nic
*efx
, struct sk_buff
*skb
)
1074 struct efx_ptp_data
*ptp_data
= efx
->ptp_data
;
1075 struct skb_shared_hwtstamps timestamps
;
1077 MCDI_DECLARE_BUF(txtime
, MC_CMD_PTP_OUT_TRANSMIT_LEN
);
1080 MCDI_SET_DWORD(ptp_data
->txbuf
, PTP_IN_OP
, MC_CMD_PTP_OP_TRANSMIT
);
1081 MCDI_SET_DWORD(ptp_data
->txbuf
, PTP_IN_PERIPH_ID
, 0);
1082 MCDI_SET_DWORD(ptp_data
->txbuf
, PTP_IN_TRANSMIT_LENGTH
, skb
->len
);
1083 if (skb_shinfo(skb
)->nr_frags
!= 0) {
1084 rc
= skb_linearize(skb
);
1089 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1090 rc
= skb_checksum_help(skb
);
1094 skb_copy_from_linear_data(skb
,
1095 MCDI_PTR(ptp_data
->txbuf
,
1096 PTP_IN_TRANSMIT_PACKET
),
1098 rc
= efx_mcdi_rpc(efx
, MC_CMD_PTP
,
1099 ptp_data
->txbuf
, MC_CMD_PTP_IN_TRANSMIT_LEN(skb
->len
),
1100 txtime
, sizeof(txtime
), &len
);
1104 memset(×tamps
, 0, sizeof(timestamps
));
1105 timestamps
.hwtstamp
= ptp_data
->nic_to_kernel_time(
1106 MCDI_DWORD(txtime
, PTP_OUT_TRANSMIT_MAJOR
),
1107 MCDI_DWORD(txtime
, PTP_OUT_TRANSMIT_MINOR
),
1108 ptp_data
->ts_corrections
.ptp_tx
);
1110 skb_tstamp_tx(skb
, ×tamps
);
1115 dev_kfree_skb_any(skb
);
1120 static void efx_ptp_drop_time_expired_events(struct efx_nic
*efx
)
1122 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1123 struct list_head
*cursor
;
1124 struct list_head
*next
;
1126 if (ptp
->rx_ts_inline
)
1129 /* Drop time-expired events */
1130 spin_lock_bh(&ptp
->evt_lock
);
1131 if (!list_empty(&ptp
->evt_list
)) {
1132 list_for_each_safe(cursor
, next
, &ptp
->evt_list
) {
1133 struct efx_ptp_event_rx
*evt
;
1135 evt
= list_entry(cursor
, struct efx_ptp_event_rx
,
1137 if (time_after(jiffies
, evt
->expiry
)) {
1138 list_move(&evt
->link
, &ptp
->evt_free_list
);
1139 netif_warn(efx
, hw
, efx
->net_dev
,
1140 "PTP rx event dropped\n");
1144 spin_unlock_bh(&ptp
->evt_lock
);
1147 static enum ptp_packet_state
efx_ptp_match_rx(struct efx_nic
*efx
,
1148 struct sk_buff
*skb
)
1150 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1152 struct list_head
*cursor
;
1153 struct list_head
*next
;
1154 struct efx_ptp_match
*match
;
1155 enum ptp_packet_state rc
= PTP_PACKET_STATE_UNMATCHED
;
1157 WARN_ON_ONCE(ptp
->rx_ts_inline
);
1159 spin_lock_bh(&ptp
->evt_lock
);
1160 evts_waiting
= !list_empty(&ptp
->evt_list
);
1161 spin_unlock_bh(&ptp
->evt_lock
);
1164 return PTP_PACKET_STATE_UNMATCHED
;
1166 match
= (struct efx_ptp_match
*)skb
->cb
;
1167 /* Look for a matching timestamp in the event queue */
1168 spin_lock_bh(&ptp
->evt_lock
);
1169 list_for_each_safe(cursor
, next
, &ptp
->evt_list
) {
1170 struct efx_ptp_event_rx
*evt
;
1172 evt
= list_entry(cursor
, struct efx_ptp_event_rx
, link
);
1173 if ((evt
->seq0
== match
->words
[0]) &&
1174 (evt
->seq1
== match
->words
[1])) {
1175 struct skb_shared_hwtstamps
*timestamps
;
1177 /* Match - add in hardware timestamp */
1178 timestamps
= skb_hwtstamps(skb
);
1179 timestamps
->hwtstamp
= evt
->hwtimestamp
;
1181 match
->state
= PTP_PACKET_STATE_MATCHED
;
1182 rc
= PTP_PACKET_STATE_MATCHED
;
1183 list_move(&evt
->link
, &ptp
->evt_free_list
);
1187 spin_unlock_bh(&ptp
->evt_lock
);
1192 /* Process any queued receive events and corresponding packets
1194 * q is returned with all the packets that are ready for delivery.
1196 static void efx_ptp_process_events(struct efx_nic
*efx
, struct sk_buff_head
*q
)
1198 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1199 struct sk_buff
*skb
;
1201 while ((skb
= skb_dequeue(&ptp
->rxq
))) {
1202 struct efx_ptp_match
*match
;
1204 match
= (struct efx_ptp_match
*)skb
->cb
;
1205 if (match
->state
== PTP_PACKET_STATE_MATCH_UNWANTED
) {
1206 __skb_queue_tail(q
, skb
);
1207 } else if (efx_ptp_match_rx(efx
, skb
) ==
1208 PTP_PACKET_STATE_MATCHED
) {
1209 __skb_queue_tail(q
, skb
);
1210 } else if (time_after(jiffies
, match
->expiry
)) {
1211 match
->state
= PTP_PACKET_STATE_TIMED_OUT
;
1212 ++ptp
->rx_no_timestamp
;
1213 __skb_queue_tail(q
, skb
);
1215 /* Replace unprocessed entry and stop */
1216 skb_queue_head(&ptp
->rxq
, skb
);
1222 /* Complete processing of a received packet */
1223 static inline void efx_ptp_process_rx(struct efx_nic
*efx
, struct sk_buff
*skb
)
1226 netif_receive_skb(skb
);
1230 static void efx_ptp_remove_multicast_filters(struct efx_nic
*efx
)
1232 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1234 if (ptp
->rxfilter_installed
) {
1235 efx_filter_remove_id_safe(efx
, EFX_FILTER_PRI_REQUIRED
,
1236 ptp
->rxfilter_general
);
1237 efx_filter_remove_id_safe(efx
, EFX_FILTER_PRI_REQUIRED
,
1238 ptp
->rxfilter_event
);
1239 ptp
->rxfilter_installed
= false;
1243 static int efx_ptp_insert_multicast_filters(struct efx_nic
*efx
)
1245 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1246 struct efx_filter_spec rxfilter
;
1249 if (!ptp
->channel
|| ptp
->rxfilter_installed
)
1252 /* Must filter on both event and general ports to ensure
1253 * that there is no packet re-ordering.
1255 efx_filter_init_rx(&rxfilter
, EFX_FILTER_PRI_REQUIRED
, 0,
1257 efx_channel_get_rx_queue(ptp
->channel
)));
1258 rc
= efx_filter_set_ipv4_local(&rxfilter
, IPPROTO_UDP
,
1260 htons(PTP_EVENT_PORT
));
1264 rc
= efx_filter_insert_filter(efx
, &rxfilter
, true);
1267 ptp
->rxfilter_event
= rc
;
1269 efx_filter_init_rx(&rxfilter
, EFX_FILTER_PRI_REQUIRED
, 0,
1271 efx_channel_get_rx_queue(ptp
->channel
)));
1272 rc
= efx_filter_set_ipv4_local(&rxfilter
, IPPROTO_UDP
,
1274 htons(PTP_GENERAL_PORT
));
1278 rc
= efx_filter_insert_filter(efx
, &rxfilter
, true);
1281 ptp
->rxfilter_general
= rc
;
1283 ptp
->rxfilter_installed
= true;
1287 efx_filter_remove_id_safe(efx
, EFX_FILTER_PRI_REQUIRED
,
1288 ptp
->rxfilter_event
);
1292 static int efx_ptp_start(struct efx_nic
*efx
)
1294 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1297 ptp
->reset_required
= false;
1299 rc
= efx_ptp_insert_multicast_filters(efx
);
1303 rc
= efx_ptp_enable(efx
);
1307 ptp
->evt_frag_idx
= 0;
1308 ptp
->current_adjfreq
= 0;
1313 efx_ptp_remove_multicast_filters(efx
);
1317 static int efx_ptp_stop(struct efx_nic
*efx
)
1319 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1320 struct list_head
*cursor
;
1321 struct list_head
*next
;
1327 rc
= efx_ptp_disable(efx
);
1329 efx_ptp_remove_multicast_filters(efx
);
1331 /* Make sure RX packets are really delivered */
1332 efx_ptp_deliver_rx_queue(&efx
->ptp_data
->rxq
);
1333 skb_queue_purge(&efx
->ptp_data
->txq
);
1335 /* Drop any pending receive events */
1336 spin_lock_bh(&efx
->ptp_data
->evt_lock
);
1337 list_for_each_safe(cursor
, next
, &efx
->ptp_data
->evt_list
) {
1338 list_move(cursor
, &efx
->ptp_data
->evt_free_list
);
1340 spin_unlock_bh(&efx
->ptp_data
->evt_lock
);
1345 static int efx_ptp_restart(struct efx_nic
*efx
)
1347 if (efx
->ptp_data
&& efx
->ptp_data
->enabled
)
1348 return efx_ptp_start(efx
);
1352 static void efx_ptp_pps_worker(struct work_struct
*work
)
1354 struct efx_ptp_data
*ptp
=
1355 container_of(work
, struct efx_ptp_data
, pps_work
);
1356 struct efx_nic
*efx
= ptp
->efx
;
1357 struct ptp_clock_event ptp_evt
;
1359 if (efx_ptp_synchronize(efx
, PTP_SYNC_ATTEMPTS
))
1362 ptp_evt
.type
= PTP_CLOCK_PPSUSR
;
1363 ptp_evt
.pps_times
= ptp
->host_time_pps
;
1364 ptp_clock_event(ptp
->phc_clock
, &ptp_evt
);
1367 static void efx_ptp_worker(struct work_struct
*work
)
1369 struct efx_ptp_data
*ptp_data
=
1370 container_of(work
, struct efx_ptp_data
, work
);
1371 struct efx_nic
*efx
= ptp_data
->efx
;
1372 struct sk_buff
*skb
;
1373 struct sk_buff_head tempq
;
1375 if (ptp_data
->reset_required
) {
1381 efx_ptp_drop_time_expired_events(efx
);
1383 __skb_queue_head_init(&tempq
);
1384 efx_ptp_process_events(efx
, &tempq
);
1386 while ((skb
= skb_dequeue(&ptp_data
->txq
)))
1387 ptp_data
->xmit_skb(efx
, skb
);
1389 while ((skb
= __skb_dequeue(&tempq
)))
1390 efx_ptp_process_rx(efx
, skb
);
1393 static const struct ptp_clock_info efx_phc_clock_info
= {
1394 .owner
= THIS_MODULE
,
1402 .adjfreq
= efx_phc_adjfreq
,
1403 .adjtime
= efx_phc_adjtime
,
1404 .gettime64
= efx_phc_gettime
,
1405 .settime64
= efx_phc_settime
,
1406 .enable
= efx_phc_enable
,
1409 /* Initialise PTP state. */
1410 int efx_ptp_probe(struct efx_nic
*efx
, struct efx_channel
*channel
)
1412 struct efx_ptp_data
*ptp
;
1416 ptp
= kzalloc(sizeof(struct efx_ptp_data
), GFP_KERNEL
);
1417 efx
->ptp_data
= ptp
;
1422 ptp
->channel
= channel
;
1423 ptp
->rx_ts_inline
= efx_nic_rev(efx
) >= EFX_REV_HUNT_A0
;
1425 rc
= efx_nic_alloc_buffer(efx
, &ptp
->start
, sizeof(int), GFP_KERNEL
);
1429 skb_queue_head_init(&ptp
->rxq
);
1430 skb_queue_head_init(&ptp
->txq
);
1431 ptp
->workwq
= create_singlethread_workqueue("sfc_ptp");
1437 if (efx_ptp_use_mac_tx_timestamps(efx
)) {
1438 ptp
->xmit_skb
= efx_ptp_xmit_skb_queue
;
1439 /* Request sync events on this channel. */
1440 channel
->sync_events_state
= SYNC_EVENTS_QUIESCENT
;
1442 ptp
->xmit_skb
= efx_ptp_xmit_skb_mc
;
1445 INIT_WORK(&ptp
->work
, efx_ptp_worker
);
1446 ptp
->config
.flags
= 0;
1447 ptp
->config
.tx_type
= HWTSTAMP_TX_OFF
;
1448 ptp
->config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
1449 INIT_LIST_HEAD(&ptp
->evt_list
);
1450 INIT_LIST_HEAD(&ptp
->evt_free_list
);
1451 spin_lock_init(&ptp
->evt_lock
);
1452 for (pos
= 0; pos
< MAX_RECEIVE_EVENTS
; pos
++)
1453 list_add(&ptp
->rx_evts
[pos
].link
, &ptp
->evt_free_list
);
1455 /* Get the NIC PTP attributes and set up time conversions */
1456 rc
= efx_ptp_get_attributes(efx
);
1460 /* Get the timestamp corrections */
1461 rc
= efx_ptp_get_timestamp_corrections(efx
);
1465 if (efx
->mcdi
->fn_flags
&
1466 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY
)) {
1467 ptp
->phc_clock_info
= efx_phc_clock_info
;
1468 ptp
->phc_clock
= ptp_clock_register(&ptp
->phc_clock_info
,
1469 &efx
->pci_dev
->dev
);
1470 if (IS_ERR(ptp
->phc_clock
)) {
1471 rc
= PTR_ERR(ptp
->phc_clock
);
1473 } else if (ptp
->phc_clock
) {
1474 INIT_WORK(&ptp
->pps_work
, efx_ptp_pps_worker
);
1475 ptp
->pps_workwq
= create_singlethread_workqueue("sfc_pps");
1476 if (!ptp
->pps_workwq
) {
1482 ptp
->nic_ts_enabled
= false;
1486 ptp_clock_unregister(efx
->ptp_data
->phc_clock
);
1489 destroy_workqueue(efx
->ptp_data
->workwq
);
1492 efx_nic_free_buffer(efx
, &ptp
->start
);
1495 kfree(efx
->ptp_data
);
1496 efx
->ptp_data
= NULL
;
1501 /* Initialise PTP channel.
1503 * Setting core_index to zero causes the queue to be initialised and doesn't
1504 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
1506 static int efx_ptp_probe_channel(struct efx_channel
*channel
)
1508 struct efx_nic
*efx
= channel
->efx
;
1511 channel
->irq_moderation_us
= 0;
1512 channel
->rx_queue
.core_index
= 0;
1514 rc
= efx_ptp_probe(efx
, channel
);
1515 /* Failure to probe PTP is not fatal; this channel will just not be
1516 * used for anything.
1517 * In the case of EPERM, efx_ptp_probe will print its own message (in
1518 * efx_ptp_get_attributes()), so we don't need to.
1520 if (rc
&& rc
!= -EPERM
)
1521 netif_warn(efx
, drv
, efx
->net_dev
,
1522 "Failed to probe PTP, rc=%d\n", rc
);
1526 void efx_ptp_remove(struct efx_nic
*efx
)
1531 (void)efx_ptp_disable(efx
);
1533 cancel_work_sync(&efx
->ptp_data
->work
);
1534 if (efx
->ptp_data
->pps_workwq
)
1535 cancel_work_sync(&efx
->ptp_data
->pps_work
);
1537 skb_queue_purge(&efx
->ptp_data
->rxq
);
1538 skb_queue_purge(&efx
->ptp_data
->txq
);
1540 if (efx
->ptp_data
->phc_clock
) {
1541 destroy_workqueue(efx
->ptp_data
->pps_workwq
);
1542 ptp_clock_unregister(efx
->ptp_data
->phc_clock
);
1545 destroy_workqueue(efx
->ptp_data
->workwq
);
1547 efx_nic_free_buffer(efx
, &efx
->ptp_data
->start
);
1548 kfree(efx
->ptp_data
);
1549 efx
->ptp_data
= NULL
;
1552 static void efx_ptp_remove_channel(struct efx_channel
*channel
)
1554 efx_ptp_remove(channel
->efx
);
1557 static void efx_ptp_get_channel_name(struct efx_channel
*channel
,
1558 char *buf
, size_t len
)
1560 snprintf(buf
, len
, "%s-ptp", channel
->efx
->name
);
1563 /* Determine whether this packet should be processed by the PTP module
1564 * or transmitted conventionally.
1566 bool efx_ptp_is_ptp_tx(struct efx_nic
*efx
, struct sk_buff
*skb
)
1568 return efx
->ptp_data
&&
1569 efx
->ptp_data
->enabled
&&
1570 skb
->len
>= PTP_MIN_LENGTH
&&
1571 skb
->len
<= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM
&&
1572 likely(skb
->protocol
== htons(ETH_P_IP
)) &&
1573 skb_transport_header_was_set(skb
) &&
1574 skb_network_header_len(skb
) >= sizeof(struct iphdr
) &&
1575 ip_hdr(skb
)->protocol
== IPPROTO_UDP
&&
1577 skb_transport_offset(skb
) + sizeof(struct udphdr
) &&
1578 udp_hdr(skb
)->dest
== htons(PTP_EVENT_PORT
);
1581 /* Receive a PTP packet. Packets are queued until the arrival of
1582 * the receive timestamp from the MC - this will probably occur after the
1583 * packet arrival because of the processing in the MC.
1585 static bool efx_ptp_rx(struct efx_channel
*channel
, struct sk_buff
*skb
)
1587 struct efx_nic
*efx
= channel
->efx
;
1588 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1589 struct efx_ptp_match
*match
= (struct efx_ptp_match
*)skb
->cb
;
1590 u8
*match_data_012
, *match_data_345
;
1591 unsigned int version
;
1594 match
->expiry
= jiffies
+ msecs_to_jiffies(PKT_EVENT_LIFETIME_MS
);
1596 /* Correct version? */
1597 if (ptp
->mode
== MC_CMD_PTP_MODE_V1
) {
1598 if (!pskb_may_pull(skb
, PTP_V1_MIN_LENGTH
)) {
1602 version
= ntohs(*(__be16
*)&data
[PTP_V1_VERSION_OFFSET
]);
1603 if (version
!= PTP_VERSION_V1
) {
1607 /* PTP V1 uses all six bytes of the UUID to match the packet
1610 match_data_012
= data
+ PTP_V1_UUID_OFFSET
;
1611 match_data_345
= data
+ PTP_V1_UUID_OFFSET
+ 3;
1613 if (!pskb_may_pull(skb
, PTP_V2_MIN_LENGTH
)) {
1617 version
= data
[PTP_V2_VERSION_OFFSET
];
1618 if ((version
& PTP_VERSION_V2_MASK
) != PTP_VERSION_V2
) {
1622 /* The original V2 implementation uses bytes 2-7 of
1623 * the UUID to match the packet to the timestamp. This
1624 * discards two of the bytes of the MAC address used
1625 * to create the UUID (SF bug 33070). The PTP V2
1626 * enhanced mode fixes this issue and uses bytes 0-2
1627 * and byte 5-7 of the UUID.
1629 match_data_345
= data
+ PTP_V2_UUID_OFFSET
+ 5;
1630 if (ptp
->mode
== MC_CMD_PTP_MODE_V2
) {
1631 match_data_012
= data
+ PTP_V2_UUID_OFFSET
+ 2;
1633 match_data_012
= data
+ PTP_V2_UUID_OFFSET
+ 0;
1634 BUG_ON(ptp
->mode
!= MC_CMD_PTP_MODE_V2_ENHANCED
);
1638 /* Does this packet require timestamping? */
1639 if (ntohs(*(__be16
*)&data
[PTP_DPORT_OFFSET
]) == PTP_EVENT_PORT
) {
1640 match
->state
= PTP_PACKET_STATE_UNMATCHED
;
1642 /* We expect the sequence number to be in the same position in
1643 * the packet for PTP V1 and V2
1645 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET
!= PTP_V2_SEQUENCE_OFFSET
);
1646 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH
!= PTP_V2_SEQUENCE_LENGTH
);
1648 /* Extract UUID/Sequence information */
1649 match
->words
[0] = (match_data_012
[0] |
1650 (match_data_012
[1] << 8) |
1651 (match_data_012
[2] << 16) |
1652 (match_data_345
[0] << 24));
1653 match
->words
[1] = (match_data_345
[1] |
1654 (match_data_345
[2] << 8) |
1655 (data
[PTP_V1_SEQUENCE_OFFSET
+
1656 PTP_V1_SEQUENCE_LENGTH
- 1] <<
1659 match
->state
= PTP_PACKET_STATE_MATCH_UNWANTED
;
1662 skb_queue_tail(&ptp
->rxq
, skb
);
1663 queue_work(ptp
->workwq
, &ptp
->work
);
1668 /* Transmit a PTP packet. This has to be transmitted by the MC
1669 * itself, through an MCDI call. MCDI calls aren't permitted
1670 * in the transmit path so defer the actual transmission to a suitable worker.
1672 int efx_ptp_tx(struct efx_nic
*efx
, struct sk_buff
*skb
)
1674 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1676 skb_queue_tail(&ptp
->txq
, skb
);
1678 if ((udp_hdr(skb
)->dest
== htons(PTP_EVENT_PORT
)) &&
1679 (skb
->len
<= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM
))
1680 efx_xmit_hwtstamp_pending(skb
);
1681 queue_work(ptp
->workwq
, &ptp
->work
);
1683 return NETDEV_TX_OK
;
1686 int efx_ptp_get_mode(struct efx_nic
*efx
)
1688 return efx
->ptp_data
->mode
;
1691 int efx_ptp_change_mode(struct efx_nic
*efx
, bool enable_wanted
,
1692 unsigned int new_mode
)
1694 if ((enable_wanted
!= efx
->ptp_data
->enabled
) ||
1695 (enable_wanted
&& (efx
->ptp_data
->mode
!= new_mode
))) {
1698 if (enable_wanted
) {
1699 /* Change of mode requires disable */
1700 if (efx
->ptp_data
->enabled
&&
1701 (efx
->ptp_data
->mode
!= new_mode
)) {
1702 efx
->ptp_data
->enabled
= false;
1703 rc
= efx_ptp_stop(efx
);
1708 /* Set new operating mode and establish
1709 * baseline synchronisation, which must
1712 efx
->ptp_data
->mode
= new_mode
;
1713 if (netif_running(efx
->net_dev
))
1714 rc
= efx_ptp_start(efx
);
1716 rc
= efx_ptp_synchronize(efx
,
1717 PTP_SYNC_ATTEMPTS
* 2);
1722 rc
= efx_ptp_stop(efx
);
1728 efx
->ptp_data
->enabled
= enable_wanted
;
1734 static int efx_ptp_ts_init(struct efx_nic
*efx
, struct hwtstamp_config
*init
)
1741 if ((init
->tx_type
!= HWTSTAMP_TX_OFF
) &&
1742 (init
->tx_type
!= HWTSTAMP_TX_ON
))
1745 rc
= efx
->type
->ptp_set_ts_config(efx
, init
);
1749 efx
->ptp_data
->config
= *init
;
1753 void efx_ptp_get_ts_info(struct efx_nic
*efx
, struct ethtool_ts_info
*ts_info
)
1755 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1756 struct efx_nic
*primary
= efx
->primary
;
1763 ts_info
->so_timestamping
|= (SOF_TIMESTAMPING_TX_HARDWARE
|
1764 SOF_TIMESTAMPING_RX_HARDWARE
|
1765 SOF_TIMESTAMPING_RAW_HARDWARE
);
1766 /* Check licensed features. If we don't have the license for TX
1767 * timestamps, the NIC will not support them.
1769 if (efx_ptp_use_mac_tx_timestamps(efx
)) {
1770 struct efx_ef10_nic_data
*nic_data
= efx
->nic_data
;
1772 if (!(nic_data
->licensed_features
&
1773 (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN
)))
1774 ts_info
->so_timestamping
&=
1775 ~SOF_TIMESTAMPING_TX_HARDWARE
;
1777 if (primary
&& primary
->ptp_data
&& primary
->ptp_data
->phc_clock
)
1778 ts_info
->phc_index
=
1779 ptp_clock_index(primary
->ptp_data
->phc_clock
);
1780 ts_info
->tx_types
= 1 << HWTSTAMP_TX_OFF
| 1 << HWTSTAMP_TX_ON
;
1781 ts_info
->rx_filters
= ptp
->efx
->type
->hwtstamp_filters
;
1784 int efx_ptp_set_ts_config(struct efx_nic
*efx
, struct ifreq
*ifr
)
1786 struct hwtstamp_config config
;
1789 /* Not a PTP enabled port */
1793 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
1796 rc
= efx_ptp_ts_init(efx
, &config
);
1800 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
))
1804 int efx_ptp_get_ts_config(struct efx_nic
*efx
, struct ifreq
*ifr
)
1809 return copy_to_user(ifr
->ifr_data
, &efx
->ptp_data
->config
,
1810 sizeof(efx
->ptp_data
->config
)) ? -EFAULT
: 0;
1813 static void ptp_event_failure(struct efx_nic
*efx
, int expected_frag_len
)
1815 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1817 netif_err(efx
, hw
, efx
->net_dev
,
1818 "PTP unexpected event length: got %d expected %d\n",
1819 ptp
->evt_frag_idx
, expected_frag_len
);
1820 ptp
->reset_required
= true;
1821 queue_work(ptp
->workwq
, &ptp
->work
);
1824 /* Process a completed receive event. Put it on the event queue and
1825 * start worker thread. This is required because event and their
1826 * correspoding packets may come in either order.
1828 static void ptp_event_rx(struct efx_nic
*efx
, struct efx_ptp_data
*ptp
)
1830 struct efx_ptp_event_rx
*evt
= NULL
;
1832 if (WARN_ON_ONCE(ptp
->rx_ts_inline
))
1835 if (ptp
->evt_frag_idx
!= 3) {
1836 ptp_event_failure(efx
, 3);
1840 spin_lock_bh(&ptp
->evt_lock
);
1841 if (!list_empty(&ptp
->evt_free_list
)) {
1842 evt
= list_first_entry(&ptp
->evt_free_list
,
1843 struct efx_ptp_event_rx
, link
);
1844 list_del(&evt
->link
);
1846 evt
->seq0
= EFX_QWORD_FIELD(ptp
->evt_frags
[2], MCDI_EVENT_DATA
);
1847 evt
->seq1
= (EFX_QWORD_FIELD(ptp
->evt_frags
[2],
1849 (EFX_QWORD_FIELD(ptp
->evt_frags
[1],
1850 MCDI_EVENT_SRC
) << 8) |
1851 (EFX_QWORD_FIELD(ptp
->evt_frags
[0],
1852 MCDI_EVENT_SRC
) << 16));
1853 evt
->hwtimestamp
= efx
->ptp_data
->nic_to_kernel_time(
1854 EFX_QWORD_FIELD(ptp
->evt_frags
[0], MCDI_EVENT_DATA
),
1855 EFX_QWORD_FIELD(ptp
->evt_frags
[1], MCDI_EVENT_DATA
),
1856 ptp
->ts_corrections
.ptp_rx
);
1857 evt
->expiry
= jiffies
+ msecs_to_jiffies(PKT_EVENT_LIFETIME_MS
);
1858 list_add_tail(&evt
->link
, &ptp
->evt_list
);
1860 queue_work(ptp
->workwq
, &ptp
->work
);
1861 } else if (net_ratelimit()) {
1862 /* Log a rate-limited warning message. */
1863 netif_err(efx
, rx_err
, efx
->net_dev
, "PTP event queue overflow\n");
1865 spin_unlock_bh(&ptp
->evt_lock
);
1868 static void ptp_event_fault(struct efx_nic
*efx
, struct efx_ptp_data
*ptp
)
1870 int code
= EFX_QWORD_FIELD(ptp
->evt_frags
[0], MCDI_EVENT_DATA
);
1871 if (ptp
->evt_frag_idx
!= 1) {
1872 ptp_event_failure(efx
, 1);
1876 netif_err(efx
, hw
, efx
->net_dev
, "PTP error %d\n", code
);
1879 static void ptp_event_pps(struct efx_nic
*efx
, struct efx_ptp_data
*ptp
)
1881 if (ptp
->nic_ts_enabled
)
1882 queue_work(ptp
->pps_workwq
, &ptp
->pps_work
);
1885 void efx_ptp_event(struct efx_nic
*efx
, efx_qword_t
*ev
)
1887 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1888 int code
= EFX_QWORD_FIELD(*ev
, MCDI_EVENT_CODE
);
1891 if (!efx
->ptp_warned
) {
1892 netif_warn(efx
, drv
, efx
->net_dev
,
1893 "Received PTP event but PTP not set up\n");
1894 efx
->ptp_warned
= true;
1902 if (ptp
->evt_frag_idx
== 0) {
1903 ptp
->evt_code
= code
;
1904 } else if (ptp
->evt_code
!= code
) {
1905 netif_err(efx
, hw
, efx
->net_dev
,
1906 "PTP out of sequence event %d\n", code
);
1907 ptp
->evt_frag_idx
= 0;
1910 ptp
->evt_frags
[ptp
->evt_frag_idx
++] = *ev
;
1911 if (!MCDI_EVENT_FIELD(*ev
, CONT
)) {
1912 /* Process resulting event */
1914 case MCDI_EVENT_CODE_PTP_RX
:
1915 ptp_event_rx(efx
, ptp
);
1917 case MCDI_EVENT_CODE_PTP_FAULT
:
1918 ptp_event_fault(efx
, ptp
);
1920 case MCDI_EVENT_CODE_PTP_PPS
:
1921 ptp_event_pps(efx
, ptp
);
1924 netif_err(efx
, hw
, efx
->net_dev
,
1925 "PTP unknown event %d\n", code
);
1928 ptp
->evt_frag_idx
= 0;
1929 } else if (MAX_EVENT_FRAGS
== ptp
->evt_frag_idx
) {
1930 netif_err(efx
, hw
, efx
->net_dev
,
1931 "PTP too many event fragments\n");
1932 ptp
->evt_frag_idx
= 0;
1936 void efx_time_sync_event(struct efx_channel
*channel
, efx_qword_t
*ev
)
1938 struct efx_nic
*efx
= channel
->efx
;
1939 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1941 /* When extracting the sync timestamp minor value, we should discard
1942 * the least significant two bits. These are not required in order
1943 * to reconstruct full-range timestamps and they are optionally used
1944 * to report status depending on the options supplied when subscribing
1947 channel
->sync_timestamp_major
= MCDI_EVENT_FIELD(*ev
, PTP_TIME_MAJOR
);
1948 channel
->sync_timestamp_minor
=
1949 (MCDI_EVENT_FIELD(*ev
, PTP_TIME_MINOR_MS_8BITS
) & 0xFC)
1950 << ptp
->nic_time
.sync_event_minor_shift
;
1952 /* if sync events have been disabled then we want to silently ignore
1953 * this event, so throw away result.
1955 (void) cmpxchg(&channel
->sync_events_state
, SYNC_EVENTS_REQUESTED
,
1959 static inline u32
efx_rx_buf_timestamp_minor(struct efx_nic
*efx
, const u8
*eh
)
1961 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1962 return __le32_to_cpup((const __le32
*)(eh
+ efx
->rx_packet_ts_offset
));
1964 const u8
*data
= eh
+ efx
->rx_packet_ts_offset
;
1965 return (u32
)data
[0] |
1967 (u32
)data
[2] << 16 |
1972 void __efx_rx_skb_attach_timestamp(struct efx_channel
*channel
,
1973 struct sk_buff
*skb
)
1975 struct efx_nic
*efx
= channel
->efx
;
1976 struct efx_ptp_data
*ptp
= efx
->ptp_data
;
1977 u32 pkt_timestamp_major
, pkt_timestamp_minor
;
1979 struct skb_shared_hwtstamps
*timestamps
;
1981 if (channel
->sync_events_state
!= SYNC_EVENTS_VALID
)
1984 pkt_timestamp_minor
= efx_rx_buf_timestamp_minor(efx
, skb_mac_header(skb
));
1986 /* get the difference between the packet and sync timestamps,
1989 diff
= pkt_timestamp_minor
- channel
->sync_timestamp_minor
;
1990 if (pkt_timestamp_minor
< channel
->sync_timestamp_minor
)
1991 diff
+= ptp
->nic_time
.minor_max
;
1993 /* do we roll over a second boundary and need to carry the one? */
1994 carry
= (channel
->sync_timestamp_minor
>= ptp
->nic_time
.minor_max
- diff
) ?
1997 if (diff
<= ptp
->nic_time
.sync_event_diff_max
) {
1998 /* packet is ahead of the sync event by a quarter of a second or
1999 * less (allowing for fuzz)
2001 pkt_timestamp_major
= channel
->sync_timestamp_major
+ carry
;
2002 } else if (diff
>= ptp
->nic_time
.sync_event_diff_min
) {
2003 /* packet is behind the sync event but within the fuzz factor.
2004 * This means the RX packet and sync event crossed as they were
2005 * placed on the event queue, which can sometimes happen.
2007 pkt_timestamp_major
= channel
->sync_timestamp_major
- 1 + carry
;
2009 /* it's outside tolerance in both directions. this might be
2010 * indicative of us missing sync events for some reason, so
2011 * we'll call it an error rather than risk giving a bogus
2014 netif_vdbg(efx
, drv
, efx
->net_dev
,
2015 "packet timestamp %x too far from sync event %x:%x\n",
2016 pkt_timestamp_minor
, channel
->sync_timestamp_major
,
2017 channel
->sync_timestamp_minor
);
2021 /* attach the timestamps to the skb */
2022 timestamps
= skb_hwtstamps(skb
);
2023 timestamps
->hwtstamp
=
2024 ptp
->nic_to_kernel_time(pkt_timestamp_major
,
2025 pkt_timestamp_minor
,
2026 ptp
->ts_corrections
.general_rx
);
2029 static int efx_phc_adjfreq(struct ptp_clock_info
*ptp
, s32 delta
)
2031 struct efx_ptp_data
*ptp_data
= container_of(ptp
,
2032 struct efx_ptp_data
,
2034 struct efx_nic
*efx
= ptp_data
->efx
;
2035 MCDI_DECLARE_BUF(inadj
, MC_CMD_PTP_IN_ADJUST_LEN
);
2039 if (delta
> MAX_PPB
)
2041 else if (delta
< -MAX_PPB
)
2044 /* Convert ppb to fixed point ns taking care to round correctly. */
2045 adjustment_ns
= ((s64
)delta
* PPB_SCALE_WORD
+
2046 (1 << (ptp_data
->adjfreq_ppb_shift
- 1))) >>
2047 ptp_data
->adjfreq_ppb_shift
;
2049 MCDI_SET_DWORD(inadj
, PTP_IN_OP
, MC_CMD_PTP_OP_ADJUST
);
2050 MCDI_SET_DWORD(inadj
, PTP_IN_PERIPH_ID
, 0);
2051 MCDI_SET_QWORD(inadj
, PTP_IN_ADJUST_FREQ
, adjustment_ns
);
2052 MCDI_SET_DWORD(inadj
, PTP_IN_ADJUST_SECONDS
, 0);
2053 MCDI_SET_DWORD(inadj
, PTP_IN_ADJUST_NANOSECONDS
, 0);
2054 rc
= efx_mcdi_rpc(efx
, MC_CMD_PTP
, inadj
, sizeof(inadj
),
2059 ptp_data
->current_adjfreq
= adjustment_ns
;
2063 static int efx_phc_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
2065 u32 nic_major
, nic_minor
;
2066 struct efx_ptp_data
*ptp_data
= container_of(ptp
,
2067 struct efx_ptp_data
,
2069 struct efx_nic
*efx
= ptp_data
->efx
;
2070 MCDI_DECLARE_BUF(inbuf
, MC_CMD_PTP_IN_ADJUST_LEN
);
2072 efx
->ptp_data
->ns_to_nic_time(delta
, &nic_major
, &nic_minor
);
2074 MCDI_SET_DWORD(inbuf
, PTP_IN_OP
, MC_CMD_PTP_OP_ADJUST
);
2075 MCDI_SET_DWORD(inbuf
, PTP_IN_PERIPH_ID
, 0);
2076 MCDI_SET_QWORD(inbuf
, PTP_IN_ADJUST_FREQ
, ptp_data
->current_adjfreq
);
2077 MCDI_SET_DWORD(inbuf
, PTP_IN_ADJUST_MAJOR
, nic_major
);
2078 MCDI_SET_DWORD(inbuf
, PTP_IN_ADJUST_MINOR
, nic_minor
);
2079 return efx_mcdi_rpc(efx
, MC_CMD_PTP
, inbuf
, sizeof(inbuf
),
2083 static int efx_phc_gettime(struct ptp_clock_info
*ptp
, struct timespec64
*ts
)
2085 struct efx_ptp_data
*ptp_data
= container_of(ptp
,
2086 struct efx_ptp_data
,
2088 struct efx_nic
*efx
= ptp_data
->efx
;
2089 MCDI_DECLARE_BUF(inbuf
, MC_CMD_PTP_IN_READ_NIC_TIME_LEN
);
2090 MCDI_DECLARE_BUF(outbuf
, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN
);
2094 MCDI_SET_DWORD(inbuf
, PTP_IN_OP
, MC_CMD_PTP_OP_READ_NIC_TIME
);
2095 MCDI_SET_DWORD(inbuf
, PTP_IN_PERIPH_ID
, 0);
2097 rc
= efx_mcdi_rpc(efx
, MC_CMD_PTP
, inbuf
, sizeof(inbuf
),
2098 outbuf
, sizeof(outbuf
), NULL
);
2102 kt
= ptp_data
->nic_to_kernel_time(
2103 MCDI_DWORD(outbuf
, PTP_OUT_READ_NIC_TIME_MAJOR
),
2104 MCDI_DWORD(outbuf
, PTP_OUT_READ_NIC_TIME_MINOR
), 0);
2105 *ts
= ktime_to_timespec64(kt
);
2109 static int efx_phc_settime(struct ptp_clock_info
*ptp
,
2110 const struct timespec64
*e_ts
)
2112 /* Get the current NIC time, efx_phc_gettime.
2113 * Subtract from the desired time to get the offset
2114 * call efx_phc_adjtime with the offset
2117 struct timespec64 time_now
;
2118 struct timespec64 delta
;
2120 rc
= efx_phc_gettime(ptp
, &time_now
);
2124 delta
= timespec64_sub(*e_ts
, time_now
);
2126 rc
= efx_phc_adjtime(ptp
, timespec64_to_ns(&delta
));
2133 static int efx_phc_enable(struct ptp_clock_info
*ptp
,
2134 struct ptp_clock_request
*request
,
2137 struct efx_ptp_data
*ptp_data
= container_of(ptp
,
2138 struct efx_ptp_data
,
2140 if (request
->type
!= PTP_CLK_REQ_PPS
)
2143 ptp_data
->nic_ts_enabled
= !!enable
;
2147 static const struct efx_channel_type efx_ptp_channel_type
= {
2148 .handle_no_channel
= efx_ptp_handle_no_channel
,
2149 .pre_probe
= efx_ptp_probe_channel
,
2150 .post_remove
= efx_ptp_remove_channel
,
2151 .get_name
= efx_ptp_get_channel_name
,
2152 /* no copy operation; there is no need to reallocate this channel */
2153 .receive_skb
= efx_ptp_rx
,
2154 .want_txqs
= efx_ptp_want_txqs
,
2155 .keep_eventq
= false,
2158 void efx_ptp_defer_probe_with_channel(struct efx_nic
*efx
)
2160 /* Check whether PTP is implemented on this NIC. The DISABLE
2161 * operation will succeed if and only if it is implemented.
2163 if (efx_ptp_disable(efx
) == 0)
2164 efx
->extra_channel_type
[EFX_EXTRA_CHANNEL_PTP
] =
2165 &efx_ptp_channel_type
;
2168 void efx_ptp_start_datapath(struct efx_nic
*efx
)
2170 if (efx_ptp_restart(efx
))
2171 netif_err(efx
, drv
, efx
->net_dev
, "Failed to restart PTP.\n");
2172 /* re-enable timestamping if it was previously enabled */
2173 if (efx
->type
->ptp_set_ts_sync_events
)
2174 efx
->type
->ptp_set_ts_sync_events(efx
, true, true);
2177 void efx_ptp_stop_datapath(struct efx_nic
*efx
)
2179 /* temporarily disable timestamping */
2180 if (efx
->type
->ptp_set_ts_sync_events
)
2181 efx
->type
->ptp_set_ts_sync_events(efx
, false, true);