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/pps_kernel.h>
39 #include <linux/ptp_clock_kernel.h>
40 #include "net_driver.h"
43 #include "mcdi_pcol.h"
45 #include "farch_regs.h"
47 #include "nic.h" /* indirectly includes ptp.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 return efx_has_cap(efx, TX_MAC_TIMESTAMPING);
358 /* PTP 'extra' channel is still a traffic channel, but we only create TX queues
359 * if PTP uses MAC TX timestamps, not if PTP uses the MC directly to transmit.
361 static bool efx_ptp_want_txqs(struct efx_channel *channel)
363 return efx_ptp_use_mac_tx_timestamps(channel->efx);
366 #define PTP_SW_STAT(ext_name, field_name) \
367 { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
368 #define PTP_MC_STAT(ext_name, mcdi_name) \
369 { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
370 static const struct efx_hw_stat_desc efx_ptp_stat_desc[] = {
371 PTP_SW_STAT(ptp_good_syncs, good_syncs),
372 PTP_SW_STAT(ptp_fast_syncs, fast_syncs),
373 PTP_SW_STAT(ptp_bad_syncs, bad_syncs),
374 PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts),
375 PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs),
376 PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows),
377 PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows),
378 PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows),
379 PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp),
380 PTP_MC_STAT(ptp_tx_timestamp_packets, TX),
381 PTP_MC_STAT(ptp_rx_timestamp_packets, RX),
382 PTP_MC_STAT(ptp_timestamp_packets, TS),
383 PTP_MC_STAT(ptp_filter_matches, FM),
384 PTP_MC_STAT(ptp_non_filter_matches, NFM),
386 #define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
387 static const unsigned long efx_ptp_stat_mask[] = {
388 [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
391 size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
396 return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
397 efx_ptp_stat_mask, strings);
400 size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
402 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
403 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
410 /* Copy software statistics */
411 for (i = 0; i < PTP_STAT_COUNT; i++) {
412 if (efx_ptp_stat_desc[i].dma_width)
414 stats[i] = *(unsigned int *)((char *)efx->ptp_data +
415 efx_ptp_stat_desc[i].offset);
418 /* Fetch MC statistics. We *must* fill in all statistics or
419 * risk leaking kernel memory to userland, so if the MCDI
420 * request fails we pretend we got zeroes.
422 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS);
423 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
424 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
425 outbuf, sizeof(outbuf), NULL);
427 memset(outbuf, 0, sizeof(outbuf));
428 efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
430 stats, _MCDI_PTR(outbuf, 0), false);
432 return PTP_STAT_COUNT;
435 /* For Siena platforms NIC time is s and ns */
436 static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
438 struct timespec64 ts = ns_to_timespec64(ns);
439 *nic_major = (u32)ts.tv_sec;
440 *nic_minor = ts.tv_nsec;
443 static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
446 ktime_t kt = ktime_set(nic_major, nic_minor);
448 kt = ktime_add_ns(kt, (u64)correction);
450 kt = ktime_sub_ns(kt, (u64)-correction);
454 /* To convert from s27 format to ns we multiply then divide by a power of 2.
455 * For the conversion from ns to s27, the operation is also converted to a
456 * multiply and shift.
458 #define S27_TO_NS_SHIFT (27)
459 #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
460 #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
461 #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
463 /* For Huntington platforms NIC time is in seconds and fractions of a second
464 * where the minor register only uses 27 bits in units of 2^-27s.
466 static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
468 struct timespec64 ts = ns_to_timespec64(ns);
469 u32 maj = (u32)ts.tv_sec;
470 u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
471 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
473 /* The conversion can result in the minor value exceeding the maximum.
474 * In this case, round up to the next second.
476 if (min >= S27_MINOR_MAX) {
477 min -= S27_MINOR_MAX;
485 static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
487 u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
488 (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
489 return ktime_set(nic_major, ns);
492 static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
495 /* Apply the correction and deal with carry */
496 nic_minor += correction;
497 if ((s32)nic_minor < 0) {
498 nic_minor += S27_MINOR_MAX;
500 } else if (nic_minor >= S27_MINOR_MAX) {
501 nic_minor -= S27_MINOR_MAX;
505 return efx_ptp_s27_to_ktime(nic_major, nic_minor);
508 /* For Medford2 platforms the time is in seconds and quarter nanoseconds. */
509 static void efx_ptp_ns_to_s_qns(s64 ns, u32 *nic_major, u32 *nic_minor)
511 struct timespec64 ts = ns_to_timespec64(ns);
513 *nic_major = (u32)ts.tv_sec;
514 *nic_minor = ts.tv_nsec * 4;
517 static ktime_t efx_ptp_s_qns_to_ktime_correction(u32 nic_major, u32 nic_minor,
522 nic_minor = DIV_ROUND_CLOSEST(nic_minor, 4);
523 correction = DIV_ROUND_CLOSEST(correction, 4);
525 kt = ktime_set(nic_major, nic_minor);
528 kt = ktime_add_ns(kt, (u64)correction);
530 kt = ktime_sub_ns(kt, (u64)-correction);
534 struct efx_channel *efx_ptp_channel(struct efx_nic *efx)
536 return efx->ptp_data ? efx->ptp_data->channel : NULL;
539 static u32 last_sync_timestamp_major(struct efx_nic *efx)
541 struct efx_channel *channel = efx_ptp_channel(efx);
545 major = channel->sync_timestamp_major;
549 /* The 8000 series and later can provide the time from the MAC, which is only
550 * 48 bits long and provides meta-information in the top 2 bits.
553 efx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx,
554 struct efx_ptp_data *ptp,
555 u32 nic_major, u32 nic_minor,
562 if (!(nic_major & 0x80000000)) {
563 WARN_ON_ONCE(nic_major >> 16);
565 /* Medford provides 48 bits of timestamp, so we must get the top
566 * 16 bits from the timesync event state.
568 * We only have the lower 16 bits of the time now, but we do
569 * have a full resolution timestamp at some point in past. As
570 * long as the difference between the (real) now and the sync
571 * is less than 2^15, then we can reconstruct the difference
572 * between those two numbers using only the lower 16 bits of
577 * a - b = ((a mod k) - b) mod k
579 * when -k/2 < (a-b) < k/2. In our case k is 2^16. We know
580 * (a mod k) and b, so can calculate the delta, a - b.
583 sync_timestamp = last_sync_timestamp_major(efx);
585 /* Because delta is s16 this does an implicit mask down to
586 * 16 bits which is what we need, assuming
587 * MEDFORD_TX_SECS_EVENT_BITS is 16. delta is signed so that
588 * we can deal with the (unlikely) case of sync timestamps
589 * arriving from the future.
591 delta = nic_major - sync_timestamp;
593 /* Recover the fully specified time now, by applying the offset
594 * to the (fully specified) sync time.
596 nic_major = sync_timestamp + delta;
598 kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
604 ktime_t efx_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue)
606 struct efx_nic *efx = tx_queue->efx;
607 struct efx_ptp_data *ptp = efx->ptp_data;
610 if (efx_ptp_use_mac_tx_timestamps(efx))
611 kt = efx_ptp_mac_nic_to_ktime_correction(efx, ptp,
612 tx_queue->completed_timestamp_major,
613 tx_queue->completed_timestamp_minor,
614 ptp->ts_corrections.general_tx);
616 kt = ptp->nic_to_kernel_time(
617 tx_queue->completed_timestamp_major,
618 tx_queue->completed_timestamp_minor,
619 ptp->ts_corrections.general_tx);
623 /* Get PTP attributes and set up time conversions */
624 static int efx_ptp_get_attributes(struct efx_nic *efx)
626 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
627 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
628 struct efx_ptp_data *ptp = efx->ptp_data;
633 /* Get the PTP attributes. If the NIC doesn't support the operation we
634 * use the default format for compatibility with older NICs i.e.
635 * seconds and nanoseconds.
637 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
638 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
639 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
640 outbuf, sizeof(outbuf), &out_len);
642 fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
643 } else if (rc == -EINVAL) {
644 fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
645 } else if (rc == -EPERM) {
646 netif_info(efx, probe, efx->net_dev, "no PTP support\n");
649 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf),
650 outbuf, sizeof(outbuf), rc);
655 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION:
656 ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
657 ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
658 ptp->nic_time.minor_max = 1 << 27;
659 ptp->nic_time.sync_event_minor_shift = 19;
661 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS:
662 ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
663 ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
664 ptp->nic_time.minor_max = 1000000000;
665 ptp->nic_time.sync_event_minor_shift = 22;
667 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS:
668 ptp->ns_to_nic_time = efx_ptp_ns_to_s_qns;
669 ptp->nic_to_kernel_time = efx_ptp_s_qns_to_ktime_correction;
670 ptp->nic_time.minor_max = 4000000000UL;
671 ptp->nic_time.sync_event_minor_shift = 24;
677 /* Precalculate acceptable difference between the minor time in the
678 * packet prefix and the last MCDI time sync event. We expect the
679 * packet prefix timestamp to be after of sync event by up to one
680 * sync event interval (0.25s) but we allow it to exceed this by a
681 * fuzz factor of (0.1s)
683 ptp->nic_time.sync_event_diff_min = ptp->nic_time.minor_max
684 - (ptp->nic_time.minor_max / 10);
685 ptp->nic_time.sync_event_diff_max = (ptp->nic_time.minor_max / 4)
686 + (ptp->nic_time.minor_max / 10);
688 /* MC_CMD_PTP_OP_GET_ATTRIBUTES has been extended twice from an older
689 * operation MC_CMD_PTP_OP_GET_TIME_FORMAT. The function now may return
690 * a value to use for the minimum acceptable corrected synchronization
691 * window and may return further capabilities.
692 * If we have the extra information store it. For older firmware that
693 * does not implement the extended command use the default value.
696 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST)
697 ptp->min_synchronisation_ns =
699 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
701 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
704 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
705 ptp->capabilities = MCDI_DWORD(outbuf,
706 PTP_OUT_GET_ATTRIBUTES_CAPABILITIES);
708 ptp->capabilities = 0;
710 /* Set up the shift for conversion between frequency
711 * adjustments in parts-per-billion and the fixed-point
712 * fractional ns format that the adapter uses.
714 if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN))
715 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44;
717 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40;
722 /* Get PTP timestamp corrections */
723 static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
725 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
726 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN);
730 /* Get the timestamp corrections from the NIC. If this operation is
731 * not supported (older NICs) then no correction is required.
733 MCDI_SET_DWORD(inbuf, PTP_IN_OP,
734 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
735 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
737 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
738 outbuf, sizeof(outbuf), &out_len);
740 efx->ptp_data->ts_corrections.ptp_tx = MCDI_DWORD(outbuf,
741 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
742 efx->ptp_data->ts_corrections.ptp_rx = MCDI_DWORD(outbuf,
743 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
744 efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
745 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
746 efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
747 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
749 if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) {
750 efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD(
752 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX);
753 efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD(
755 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX);
757 efx->ptp_data->ts_corrections.general_tx =
758 efx->ptp_data->ts_corrections.ptp_tx;
759 efx->ptp_data->ts_corrections.general_rx =
760 efx->ptp_data->ts_corrections.ptp_rx;
762 } else if (rc == -EINVAL) {
763 efx->ptp_data->ts_corrections.ptp_tx = 0;
764 efx->ptp_data->ts_corrections.ptp_rx = 0;
765 efx->ptp_data->ts_corrections.pps_out = 0;
766 efx->ptp_data->ts_corrections.pps_in = 0;
767 efx->ptp_data->ts_corrections.general_tx = 0;
768 efx->ptp_data->ts_corrections.general_rx = 0;
770 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), outbuf,
778 /* Enable MCDI PTP support. */
779 static int efx_ptp_enable(struct efx_nic *efx)
781 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
782 MCDI_DECLARE_BUF_ERR(outbuf);
785 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
786 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
787 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
788 efx->ptp_data->channel ?
789 efx->ptp_data->channel->channel : 0);
790 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
792 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
793 outbuf, sizeof(outbuf), NULL);
794 rc = (rc == -EALREADY) ? 0 : rc;
796 efx_mcdi_display_error(efx, MC_CMD_PTP,
797 MC_CMD_PTP_IN_ENABLE_LEN,
798 outbuf, sizeof(outbuf), rc);
802 /* Disable MCDI PTP support.
804 * Note that this function should never rely on the presence of ptp_data -
805 * may be called before that exists.
807 static int efx_ptp_disable(struct efx_nic *efx)
809 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
810 MCDI_DECLARE_BUF_ERR(outbuf);
813 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
814 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
815 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
816 outbuf, sizeof(outbuf), NULL);
817 rc = (rc == -EALREADY) ? 0 : rc;
818 /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
819 * should only have been called during probe.
821 if (rc == -ENOSYS || rc == -EPERM)
822 netif_info(efx, probe, efx->net_dev, "no PTP support\n");
824 efx_mcdi_display_error(efx, MC_CMD_PTP,
825 MC_CMD_PTP_IN_DISABLE_LEN,
826 outbuf, sizeof(outbuf), rc);
830 static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
834 while ((skb = skb_dequeue(q))) {
836 netif_receive_skb(skb);
841 static void efx_ptp_handle_no_channel(struct efx_nic *efx)
843 netif_err(efx, drv, efx->net_dev,
844 "ERROR: PTP requires MSI-X and 1 additional interrupt"
845 "vector. PTP disabled\n");
848 /* Repeatedly send the host time to the MC which will capture the hardware
851 static void efx_ptp_send_times(struct efx_nic *efx,
852 struct pps_event_time *last_time)
854 struct pps_event_time now;
855 struct timespec64 limit;
856 struct efx_ptp_data *ptp = efx->ptp_data;
857 int *mc_running = ptp->start.addr;
861 timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
863 /* Write host time for specified period or until MC is done */
864 while ((timespec64_compare(&now.ts_real, &limit) < 0) &&
865 READ_ONCE(*mc_running)) {
866 struct timespec64 update_time;
867 unsigned int host_time;
869 /* Don't update continuously to avoid saturating the PCIe bus */
870 update_time = now.ts_real;
871 timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
874 } while ((timespec64_compare(&now.ts_real, &update_time) < 0) &&
875 READ_ONCE(*mc_running));
877 /* Synchronise NIC with single word of time only */
878 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
879 now.ts_real.tv_nsec);
880 /* Update host time in NIC memory */
881 efx->type->ptp_write_host_time(efx, host_time);
886 /* Read a timeset from the MC's results and partial process. */
887 static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
888 struct efx_ptp_timeset *timeset)
890 unsigned start_ns, end_ns;
892 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
893 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
894 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
895 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
896 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
899 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
900 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
901 /* Allow for rollover */
902 if (end_ns < start_ns)
903 end_ns += NSEC_PER_SEC;
904 /* Determine duration of operation */
905 timeset->window = end_ns - start_ns;
908 /* Process times received from MC.
910 * Extract times from returned results, and establish the minimum value
911 * seen. The minimum value represents the "best" possible time and events
912 * too much greater than this are rejected - the machine is, perhaps, too
913 * busy. A number of readings are taken so that, hopefully, at least one good
914 * synchronisation will be seen in the results.
917 efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
918 size_t response_length,
919 const struct pps_event_time *last_time)
921 unsigned number_readings =
922 MCDI_VAR_ARRAY_LEN(response_length,
923 PTP_OUT_SYNCHRONIZE_TIMESET);
926 unsigned last_good = 0;
927 struct efx_ptp_data *ptp = efx->ptp_data;
930 struct timespec64 delta;
933 if (number_readings == 0)
936 /* Read the set of results and find the last good host-MC
937 * synchronization result. The MC times when it finishes reading the
938 * host time so the corrected window time should be fairly constant
939 * for a given platform. Increment stats for any results that appear
942 for (i = 0; i < number_readings; i++) {
943 s32 window, corrected;
944 struct timespec64 wait;
946 efx_ptp_read_timeset(
947 MCDI_ARRAY_STRUCT_PTR(synch_buf,
948 PTP_OUT_SYNCHRONIZE_TIMESET, i),
951 wait = ktime_to_timespec64(
952 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
953 window = ptp->timeset[i].window;
954 corrected = window - wait.tv_nsec;
956 /* We expect the uncorrected synchronization window to be at
957 * least as large as the interval between host start and end
958 * times. If it is smaller than this then this is mostly likely
959 * to be a consequence of the host's time being adjusted.
960 * Check that the corrected sync window is in a reasonable
961 * range. If it is out of range it is likely to be because an
962 * interrupt or other delay occurred between reading the system
963 * time and writing it to MC memory.
965 if (window < SYNCHRONISATION_GRANULARITY_NS) {
966 ++ptp->invalid_sync_windows;
967 } else if (corrected >= MAX_SYNCHRONISATION_NS) {
968 ++ptp->oversize_sync_windows;
969 } else if (corrected < ptp->min_synchronisation_ns) {
970 ++ptp->undersize_sync_windows;
978 netif_warn(efx, drv, efx->net_dev,
979 "PTP no suitable synchronisations\n");
983 /* Calculate delay from last good sync (host time) to last_time.
984 * It is possible that the seconds rolled over between taking
985 * the start reading and the last value written by the host. The
986 * timescales are such that a gap of more than one second is never
987 * expected. delta is *not* normalised.
989 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
990 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
991 if (start_sec != last_sec &&
992 ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
993 netif_warn(efx, hw, efx->net_dev,
994 "PTP bad synchronisation seconds\n");
997 delta.tv_sec = (last_sec - start_sec) & 1;
999 last_time->ts_real.tv_nsec -
1000 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
1002 /* Convert the NIC time at last good sync into kernel time.
1003 * No correction is required - this time is the output of a
1006 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
1007 ptp->timeset[last_good].minor, 0);
1009 /* Calculate delay from NIC top of second to last_time */
1010 delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec;
1012 /* Set PPS timestamp to match NIC top of second */
1013 ptp->host_time_pps = *last_time;
1014 pps_sub_ts(&ptp->host_time_pps, delta);
1019 /* Synchronize times between the host and the MC */
1020 static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
1022 struct efx_ptp_data *ptp = efx->ptp_data;
1023 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
1024 size_t response_length;
1026 unsigned long timeout;
1027 struct pps_event_time last_time = {};
1028 unsigned int loops = 0;
1029 int *start = ptp->start.addr;
1031 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
1032 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
1033 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
1035 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
1036 ptp->start.dma_addr);
1038 /* Clear flag that signals MC ready */
1039 WRITE_ONCE(*start, 0);
1040 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
1041 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
1042 EFX_WARN_ON_ONCE_PARANOID(rc);
1044 /* Wait for start from MCDI (or timeout) */
1045 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
1046 while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) {
1047 udelay(20); /* Usually start MCDI execution quickly */
1053 if (!time_before(jiffies, timeout))
1054 ++ptp->sync_timeouts;
1056 if (READ_ONCE(*start))
1057 efx_ptp_send_times(efx, &last_time);
1059 /* Collect results */
1060 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
1061 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
1062 synch_buf, sizeof(synch_buf),
1065 rc = efx_ptp_process_times(efx, synch_buf, response_length,
1070 ++ptp->no_time_syncs;
1073 /* Increment the bad syncs counter if the synchronize fails, whatever
1082 /* Transmit a PTP packet via the dedicated hardware timestamped queue. */
1083 static void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb)
1085 struct efx_ptp_data *ptp_data = efx->ptp_data;
1086 u8 type = efx_tx_csum_type_skb(skb);
1087 struct efx_tx_queue *tx_queue;
1089 tx_queue = efx_channel_get_tx_queue(ptp_data->channel, type);
1090 if (tx_queue && tx_queue->timestamping) {
1091 efx_enqueue_skb(tx_queue, skb);
1093 WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
1094 dev_kfree_skb_any(skb);
1098 /* Transmit a PTP packet, via the MCDI interface, to the wire. */
1099 static void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb)
1101 struct efx_ptp_data *ptp_data = efx->ptp_data;
1102 struct skb_shared_hwtstamps timestamps;
1104 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
1107 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
1108 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
1109 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
1110 if (skb_shinfo(skb)->nr_frags != 0) {
1111 rc = skb_linearize(skb);
1116 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1117 rc = skb_checksum_help(skb);
1121 skb_copy_from_linear_data(skb,
1122 MCDI_PTR(ptp_data->txbuf,
1123 PTP_IN_TRANSMIT_PACKET),
1125 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
1126 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
1127 txtime, sizeof(txtime), &len);
1131 memset(×tamps, 0, sizeof(timestamps));
1132 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
1133 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
1134 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
1135 ptp_data->ts_corrections.ptp_tx);
1137 skb_tstamp_tx(skb, ×tamps);
1142 dev_kfree_skb_any(skb);
1147 static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
1149 struct efx_ptp_data *ptp = efx->ptp_data;
1150 struct list_head *cursor;
1151 struct list_head *next;
1153 if (ptp->rx_ts_inline)
1156 /* Drop time-expired events */
1157 spin_lock_bh(&ptp->evt_lock);
1158 list_for_each_safe(cursor, next, &ptp->evt_list) {
1159 struct efx_ptp_event_rx *evt;
1161 evt = list_entry(cursor, struct efx_ptp_event_rx,
1163 if (time_after(jiffies, evt->expiry)) {
1164 list_move(&evt->link, &ptp->evt_free_list);
1165 netif_warn(efx, hw, efx->net_dev,
1166 "PTP rx event dropped\n");
1169 spin_unlock_bh(&ptp->evt_lock);
1172 static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
1173 struct sk_buff *skb)
1175 struct efx_ptp_data *ptp = efx->ptp_data;
1177 struct list_head *cursor;
1178 struct list_head *next;
1179 struct efx_ptp_match *match;
1180 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
1182 WARN_ON_ONCE(ptp->rx_ts_inline);
1184 spin_lock_bh(&ptp->evt_lock);
1185 evts_waiting = !list_empty(&ptp->evt_list);
1186 spin_unlock_bh(&ptp->evt_lock);
1189 return PTP_PACKET_STATE_UNMATCHED;
1191 match = (struct efx_ptp_match *)skb->cb;
1192 /* Look for a matching timestamp in the event queue */
1193 spin_lock_bh(&ptp->evt_lock);
1194 list_for_each_safe(cursor, next, &ptp->evt_list) {
1195 struct efx_ptp_event_rx *evt;
1197 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
1198 if ((evt->seq0 == match->words[0]) &&
1199 (evt->seq1 == match->words[1])) {
1200 struct skb_shared_hwtstamps *timestamps;
1202 /* Match - add in hardware timestamp */
1203 timestamps = skb_hwtstamps(skb);
1204 timestamps->hwtstamp = evt->hwtimestamp;
1206 match->state = PTP_PACKET_STATE_MATCHED;
1207 rc = PTP_PACKET_STATE_MATCHED;
1208 list_move(&evt->link, &ptp->evt_free_list);
1212 spin_unlock_bh(&ptp->evt_lock);
1217 /* Process any queued receive events and corresponding packets
1219 * q is returned with all the packets that are ready for delivery.
1221 static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
1223 struct efx_ptp_data *ptp = efx->ptp_data;
1224 struct sk_buff *skb;
1226 while ((skb = skb_dequeue(&ptp->rxq))) {
1227 struct efx_ptp_match *match;
1229 match = (struct efx_ptp_match *)skb->cb;
1230 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
1231 __skb_queue_tail(q, skb);
1232 } else if (efx_ptp_match_rx(efx, skb) ==
1233 PTP_PACKET_STATE_MATCHED) {
1234 __skb_queue_tail(q, skb);
1235 } else if (time_after(jiffies, match->expiry)) {
1236 match->state = PTP_PACKET_STATE_TIMED_OUT;
1237 ++ptp->rx_no_timestamp;
1238 __skb_queue_tail(q, skb);
1240 /* Replace unprocessed entry and stop */
1241 skb_queue_head(&ptp->rxq, skb);
1247 /* Complete processing of a received packet */
1248 static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
1251 netif_receive_skb(skb);
1255 static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
1257 struct efx_ptp_data *ptp = efx->ptp_data;
1259 if (ptp->rxfilter_installed) {
1260 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1261 ptp->rxfilter_general);
1262 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1263 ptp->rxfilter_event);
1264 ptp->rxfilter_installed = false;
1268 static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
1270 struct efx_ptp_data *ptp = efx->ptp_data;
1271 struct efx_filter_spec rxfilter;
1274 if (!ptp->channel || ptp->rxfilter_installed)
1277 /* Must filter on both event and general ports to ensure
1278 * that there is no packet re-ordering.
1280 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1282 efx_channel_get_rx_queue(ptp->channel)));
1283 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1285 htons(PTP_EVENT_PORT));
1289 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1292 ptp->rxfilter_event = rc;
1294 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1296 efx_channel_get_rx_queue(ptp->channel)));
1297 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1299 htons(PTP_GENERAL_PORT));
1303 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1306 ptp->rxfilter_general = rc;
1308 ptp->rxfilter_installed = true;
1312 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1313 ptp->rxfilter_event);
1317 static int efx_ptp_start(struct efx_nic *efx)
1319 struct efx_ptp_data *ptp = efx->ptp_data;
1322 ptp->reset_required = false;
1324 rc = efx_ptp_insert_multicast_filters(efx);
1328 rc = efx_ptp_enable(efx);
1332 ptp->evt_frag_idx = 0;
1333 ptp->current_adjfreq = 0;
1338 efx_ptp_remove_multicast_filters(efx);
1342 static int efx_ptp_stop(struct efx_nic *efx)
1344 struct efx_ptp_data *ptp = efx->ptp_data;
1345 struct list_head *cursor;
1346 struct list_head *next;
1352 rc = efx_ptp_disable(efx);
1354 efx_ptp_remove_multicast_filters(efx);
1356 /* Make sure RX packets are really delivered */
1357 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1358 skb_queue_purge(&efx->ptp_data->txq);
1360 /* Drop any pending receive events */
1361 spin_lock_bh(&efx->ptp_data->evt_lock);
1362 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
1363 list_move(cursor, &efx->ptp_data->evt_free_list);
1365 spin_unlock_bh(&efx->ptp_data->evt_lock);
1370 static int efx_ptp_restart(struct efx_nic *efx)
1372 if (efx->ptp_data && efx->ptp_data->enabled)
1373 return efx_ptp_start(efx);
1377 static void efx_ptp_pps_worker(struct work_struct *work)
1379 struct efx_ptp_data *ptp =
1380 container_of(work, struct efx_ptp_data, pps_work);
1381 struct efx_nic *efx = ptp->efx;
1382 struct ptp_clock_event ptp_evt;
1384 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1387 ptp_evt.type = PTP_CLOCK_PPSUSR;
1388 ptp_evt.pps_times = ptp->host_time_pps;
1389 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1392 static void efx_ptp_worker(struct work_struct *work)
1394 struct efx_ptp_data *ptp_data =
1395 container_of(work, struct efx_ptp_data, work);
1396 struct efx_nic *efx = ptp_data->efx;
1397 struct sk_buff *skb;
1398 struct sk_buff_head tempq;
1400 if (ptp_data->reset_required) {
1406 efx_ptp_drop_time_expired_events(efx);
1408 __skb_queue_head_init(&tempq);
1409 efx_ptp_process_events(efx, &tempq);
1411 while ((skb = skb_dequeue(&ptp_data->txq)))
1412 ptp_data->xmit_skb(efx, skb);
1414 while ((skb = __skb_dequeue(&tempq)))
1415 efx_ptp_process_rx(efx, skb);
1418 static const struct ptp_clock_info efx_phc_clock_info = {
1419 .owner = THIS_MODULE,
1427 .adjfreq = efx_phc_adjfreq,
1428 .adjtime = efx_phc_adjtime,
1429 .gettime64 = efx_phc_gettime,
1430 .settime64 = efx_phc_settime,
1431 .enable = efx_phc_enable,
1434 /* Initialise PTP state. */
1435 int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
1437 struct efx_ptp_data *ptp;
1441 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1442 efx->ptp_data = ptp;
1447 ptp->channel = channel;
1448 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
1450 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
1454 skb_queue_head_init(&ptp->rxq);
1455 skb_queue_head_init(&ptp->txq);
1456 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1462 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1463 ptp->xmit_skb = efx_ptp_xmit_skb_queue;
1464 /* Request sync events on this channel. */
1465 channel->sync_events_state = SYNC_EVENTS_QUIESCENT;
1467 ptp->xmit_skb = efx_ptp_xmit_skb_mc;
1470 INIT_WORK(&ptp->work, efx_ptp_worker);
1471 ptp->config.flags = 0;
1472 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1473 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1474 INIT_LIST_HEAD(&ptp->evt_list);
1475 INIT_LIST_HEAD(&ptp->evt_free_list);
1476 spin_lock_init(&ptp->evt_lock);
1477 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1478 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1480 /* Get the NIC PTP attributes and set up time conversions */
1481 rc = efx_ptp_get_attributes(efx);
1485 /* Get the timestamp corrections */
1486 rc = efx_ptp_get_timestamp_corrections(efx);
1490 if (efx->mcdi->fn_flags &
1491 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1492 ptp->phc_clock_info = efx_phc_clock_info;
1493 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1494 &efx->pci_dev->dev);
1495 if (IS_ERR(ptp->phc_clock)) {
1496 rc = PTR_ERR(ptp->phc_clock);
1498 } else if (ptp->phc_clock) {
1499 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1500 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1501 if (!ptp->pps_workwq) {
1507 ptp->nic_ts_enabled = false;
1511 ptp_clock_unregister(efx->ptp_data->phc_clock);
1514 destroy_workqueue(efx->ptp_data->workwq);
1517 efx_nic_free_buffer(efx, &ptp->start);
1520 kfree(efx->ptp_data);
1521 efx->ptp_data = NULL;
1526 /* Initialise PTP channel.
1528 * Setting core_index to zero causes the queue to be initialised and doesn't
1529 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
1531 static int efx_ptp_probe_channel(struct efx_channel *channel)
1533 struct efx_nic *efx = channel->efx;
1536 channel->irq_moderation_us = 0;
1537 channel->rx_queue.core_index = 0;
1539 rc = efx_ptp_probe(efx, channel);
1540 /* Failure to probe PTP is not fatal; this channel will just not be
1541 * used for anything.
1542 * In the case of EPERM, efx_ptp_probe will print its own message (in
1543 * efx_ptp_get_attributes()), so we don't need to.
1545 if (rc && rc != -EPERM)
1546 netif_warn(efx, drv, efx->net_dev,
1547 "Failed to probe PTP, rc=%d\n", rc);
1551 void efx_ptp_remove(struct efx_nic *efx)
1556 (void)efx_ptp_disable(efx);
1558 cancel_work_sync(&efx->ptp_data->work);
1559 if (efx->ptp_data->pps_workwq)
1560 cancel_work_sync(&efx->ptp_data->pps_work);
1562 skb_queue_purge(&efx->ptp_data->rxq);
1563 skb_queue_purge(&efx->ptp_data->txq);
1565 if (efx->ptp_data->phc_clock) {
1566 destroy_workqueue(efx->ptp_data->pps_workwq);
1567 ptp_clock_unregister(efx->ptp_data->phc_clock);
1570 destroy_workqueue(efx->ptp_data->workwq);
1572 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1573 kfree(efx->ptp_data);
1574 efx->ptp_data = NULL;
1577 static void efx_ptp_remove_channel(struct efx_channel *channel)
1579 efx_ptp_remove(channel->efx);
1582 static void efx_ptp_get_channel_name(struct efx_channel *channel,
1583 char *buf, size_t len)
1585 snprintf(buf, len, "%s-ptp", channel->efx->name);
1588 /* Determine whether this packet should be processed by the PTP module
1589 * or transmitted conventionally.
1591 bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1593 return efx->ptp_data &&
1594 efx->ptp_data->enabled &&
1595 skb->len >= PTP_MIN_LENGTH &&
1596 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1597 likely(skb->protocol == htons(ETH_P_IP)) &&
1598 skb_transport_header_was_set(skb) &&
1599 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
1600 ip_hdr(skb)->protocol == IPPROTO_UDP &&
1602 skb_transport_offset(skb) + sizeof(struct udphdr) &&
1603 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1606 /* Receive a PTP packet. Packets are queued until the arrival of
1607 * the receive timestamp from the MC - this will probably occur after the
1608 * packet arrival because of the processing in the MC.
1610 static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
1612 struct efx_nic *efx = channel->efx;
1613 struct efx_ptp_data *ptp = efx->ptp_data;
1614 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
1615 u8 *match_data_012, *match_data_345;
1616 unsigned int version;
1619 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1621 /* Correct version? */
1622 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
1623 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
1627 version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
1628 if (version != PTP_VERSION_V1) {
1632 /* PTP V1 uses all six bytes of the UUID to match the packet
1635 match_data_012 = data + PTP_V1_UUID_OFFSET;
1636 match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
1638 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
1642 version = data[PTP_V2_VERSION_OFFSET];
1643 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
1647 /* The original V2 implementation uses bytes 2-7 of
1648 * the UUID to match the packet to the timestamp. This
1649 * discards two of the bytes of the MAC address used
1650 * to create the UUID (SF bug 33070). The PTP V2
1651 * enhanced mode fixes this issue and uses bytes 0-2
1652 * and byte 5-7 of the UUID.
1654 match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
1655 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1656 match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
1658 match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
1659 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1663 /* Does this packet require timestamping? */
1664 if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1665 match->state = PTP_PACKET_STATE_UNMATCHED;
1667 /* We expect the sequence number to be in the same position in
1668 * the packet for PTP V1 and V2
1670 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1671 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1673 /* Extract UUID/Sequence information */
1674 match->words[0] = (match_data_012[0] |
1675 (match_data_012[1] << 8) |
1676 (match_data_012[2] << 16) |
1677 (match_data_345[0] << 24));
1678 match->words[1] = (match_data_345[1] |
1679 (match_data_345[2] << 8) |
1680 (data[PTP_V1_SEQUENCE_OFFSET +
1681 PTP_V1_SEQUENCE_LENGTH - 1] <<
1684 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1687 skb_queue_tail(&ptp->rxq, skb);
1688 queue_work(ptp->workwq, &ptp->work);
1693 /* Transmit a PTP packet. This has to be transmitted by the MC
1694 * itself, through an MCDI call. MCDI calls aren't permitted
1695 * in the transmit path so defer the actual transmission to a suitable worker.
1697 int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1699 struct efx_ptp_data *ptp = efx->ptp_data;
1701 skb_queue_tail(&ptp->txq, skb);
1703 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1704 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1705 efx_xmit_hwtstamp_pending(skb);
1706 queue_work(ptp->workwq, &ptp->work);
1708 return NETDEV_TX_OK;
1711 int efx_ptp_get_mode(struct efx_nic *efx)
1713 return efx->ptp_data->mode;
1716 int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1717 unsigned int new_mode)
1719 if ((enable_wanted != efx->ptp_data->enabled) ||
1720 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1723 if (enable_wanted) {
1724 /* Change of mode requires disable */
1725 if (efx->ptp_data->enabled &&
1726 (efx->ptp_data->mode != new_mode)) {
1727 efx->ptp_data->enabled = false;
1728 rc = efx_ptp_stop(efx);
1733 /* Set new operating mode and establish
1734 * baseline synchronisation, which must
1737 efx->ptp_data->mode = new_mode;
1738 if (netif_running(efx->net_dev))
1739 rc = efx_ptp_start(efx);
1741 rc = efx_ptp_synchronize(efx,
1742 PTP_SYNC_ATTEMPTS * 2);
1747 rc = efx_ptp_stop(efx);
1753 efx->ptp_data->enabled = enable_wanted;
1759 static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1766 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1767 (init->tx_type != HWTSTAMP_TX_ON))
1770 rc = efx->type->ptp_set_ts_config(efx, init);
1774 efx->ptp_data->config = *init;
1778 void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
1780 struct efx_ptp_data *ptp = efx->ptp_data;
1781 struct efx_nic *primary = efx->primary;
1788 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1789 SOF_TIMESTAMPING_RX_HARDWARE |
1790 SOF_TIMESTAMPING_RAW_HARDWARE);
1791 /* Check licensed features. If we don't have the license for TX
1792 * timestamps, the NIC will not support them.
1794 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1795 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1797 if (!(nic_data->licensed_features &
1798 (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN)))
1799 ts_info->so_timestamping &=
1800 ~SOF_TIMESTAMPING_TX_HARDWARE;
1802 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1803 ts_info->phc_index =
1804 ptp_clock_index(primary->ptp_data->phc_clock);
1805 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1806 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
1809 int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1811 struct hwtstamp_config config;
1814 /* Not a PTP enabled port */
1818 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1821 rc = efx_ptp_ts_init(efx, &config);
1825 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1829 int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1834 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1835 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1838 static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1840 struct efx_ptp_data *ptp = efx->ptp_data;
1842 netif_err(efx, hw, efx->net_dev,
1843 "PTP unexpected event length: got %d expected %d\n",
1844 ptp->evt_frag_idx, expected_frag_len);
1845 ptp->reset_required = true;
1846 queue_work(ptp->workwq, &ptp->work);
1849 /* Process a completed receive event. Put it on the event queue and
1850 * start worker thread. This is required because event and their
1851 * correspoding packets may come in either order.
1853 static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1855 struct efx_ptp_event_rx *evt = NULL;
1857 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1860 if (ptp->evt_frag_idx != 3) {
1861 ptp_event_failure(efx, 3);
1865 spin_lock_bh(&ptp->evt_lock);
1866 if (!list_empty(&ptp->evt_free_list)) {
1867 evt = list_first_entry(&ptp->evt_free_list,
1868 struct efx_ptp_event_rx, link);
1869 list_del(&evt->link);
1871 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1872 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1874 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1875 MCDI_EVENT_SRC) << 8) |
1876 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1877 MCDI_EVENT_SRC) << 16));
1878 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
1879 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1880 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1881 ptp->ts_corrections.ptp_rx);
1882 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1883 list_add_tail(&evt->link, &ptp->evt_list);
1885 queue_work(ptp->workwq, &ptp->work);
1886 } else if (net_ratelimit()) {
1887 /* Log a rate-limited warning message. */
1888 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1890 spin_unlock_bh(&ptp->evt_lock);
1893 static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1895 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1896 if (ptp->evt_frag_idx != 1) {
1897 ptp_event_failure(efx, 1);
1901 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1904 static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1906 if (ptp->nic_ts_enabled)
1907 queue_work(ptp->pps_workwq, &ptp->pps_work);
1910 void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1912 struct efx_ptp_data *ptp = efx->ptp_data;
1913 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1916 if (!efx->ptp_warned) {
1917 netif_warn(efx, drv, efx->net_dev,
1918 "Received PTP event but PTP not set up\n");
1919 efx->ptp_warned = true;
1927 if (ptp->evt_frag_idx == 0) {
1928 ptp->evt_code = code;
1929 } else if (ptp->evt_code != code) {
1930 netif_err(efx, hw, efx->net_dev,
1931 "PTP out of sequence event %d\n", code);
1932 ptp->evt_frag_idx = 0;
1935 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1936 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1937 /* Process resulting event */
1939 case MCDI_EVENT_CODE_PTP_RX:
1940 ptp_event_rx(efx, ptp);
1942 case MCDI_EVENT_CODE_PTP_FAULT:
1943 ptp_event_fault(efx, ptp);
1945 case MCDI_EVENT_CODE_PTP_PPS:
1946 ptp_event_pps(efx, ptp);
1949 netif_err(efx, hw, efx->net_dev,
1950 "PTP unknown event %d\n", code);
1953 ptp->evt_frag_idx = 0;
1954 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1955 netif_err(efx, hw, efx->net_dev,
1956 "PTP too many event fragments\n");
1957 ptp->evt_frag_idx = 0;
1961 void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1963 struct efx_nic *efx = channel->efx;
1964 struct efx_ptp_data *ptp = efx->ptp_data;
1966 /* When extracting the sync timestamp minor value, we should discard
1967 * the least significant two bits. These are not required in order
1968 * to reconstruct full-range timestamps and they are optionally used
1969 * to report status depending on the options supplied when subscribing
1972 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
1973 channel->sync_timestamp_minor =
1974 (MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC)
1975 << ptp->nic_time.sync_event_minor_shift;
1977 /* if sync events have been disabled then we want to silently ignore
1978 * this event, so throw away result.
1980 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
1984 static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
1986 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1987 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
1989 const u8 *data = eh + efx->rx_packet_ts_offset;
1990 return (u32)data[0] |
1992 (u32)data[2] << 16 |
1997 void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
1998 struct sk_buff *skb)
2000 struct efx_nic *efx = channel->efx;
2001 struct efx_ptp_data *ptp = efx->ptp_data;
2002 u32 pkt_timestamp_major, pkt_timestamp_minor;
2004 struct skb_shared_hwtstamps *timestamps;
2006 if (channel->sync_events_state != SYNC_EVENTS_VALID)
2009 pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb));
2011 /* get the difference between the packet and sync timestamps,
2014 diff = pkt_timestamp_minor - channel->sync_timestamp_minor;
2015 if (pkt_timestamp_minor < channel->sync_timestamp_minor)
2016 diff += ptp->nic_time.minor_max;
2018 /* do we roll over a second boundary and need to carry the one? */
2019 carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ?
2022 if (diff <= ptp->nic_time.sync_event_diff_max) {
2023 /* packet is ahead of the sync event by a quarter of a second or
2024 * less (allowing for fuzz)
2026 pkt_timestamp_major = channel->sync_timestamp_major + carry;
2027 } else if (diff >= ptp->nic_time.sync_event_diff_min) {
2028 /* packet is behind the sync event but within the fuzz factor.
2029 * This means the RX packet and sync event crossed as they were
2030 * placed on the event queue, which can sometimes happen.
2032 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
2034 /* it's outside tolerance in both directions. this might be
2035 * indicative of us missing sync events for some reason, so
2036 * we'll call it an error rather than risk giving a bogus
2039 netif_vdbg(efx, drv, efx->net_dev,
2040 "packet timestamp %x too far from sync event %x:%x\n",
2041 pkt_timestamp_minor, channel->sync_timestamp_major,
2042 channel->sync_timestamp_minor);
2046 /* attach the timestamps to the skb */
2047 timestamps = skb_hwtstamps(skb);
2048 timestamps->hwtstamp =
2049 ptp->nic_to_kernel_time(pkt_timestamp_major,
2050 pkt_timestamp_minor,
2051 ptp->ts_corrections.general_rx);
2054 static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
2056 struct efx_ptp_data *ptp_data = container_of(ptp,
2057 struct efx_ptp_data,
2059 struct efx_nic *efx = ptp_data->efx;
2060 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
2064 if (delta > MAX_PPB)
2066 else if (delta < -MAX_PPB)
2069 /* Convert ppb to fixed point ns taking care to round correctly. */
2070 adjustment_ns = ((s64)delta * PPB_SCALE_WORD +
2071 (1 << (ptp_data->adjfreq_ppb_shift - 1))) >>
2072 ptp_data->adjfreq_ppb_shift;
2074 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2075 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
2076 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
2077 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
2078 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
2079 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
2084 ptp_data->current_adjfreq = adjustment_ns;
2088 static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
2090 u32 nic_major, nic_minor;
2091 struct efx_ptp_data *ptp_data = container_of(ptp,
2092 struct efx_ptp_data,
2094 struct efx_nic *efx = ptp_data->efx;
2095 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
2097 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
2099 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2100 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2101 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
2102 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
2103 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
2104 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2108 static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
2110 struct efx_ptp_data *ptp_data = container_of(ptp,
2111 struct efx_ptp_data,
2113 struct efx_nic *efx = ptp_data->efx;
2114 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
2115 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
2119 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
2120 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2122 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2123 outbuf, sizeof(outbuf), NULL);
2127 kt = ptp_data->nic_to_kernel_time(
2128 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
2129 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
2130 *ts = ktime_to_timespec64(kt);
2134 static int efx_phc_settime(struct ptp_clock_info *ptp,
2135 const struct timespec64 *e_ts)
2137 /* Get the current NIC time, efx_phc_gettime.
2138 * Subtract from the desired time to get the offset
2139 * call efx_phc_adjtime with the offset
2142 struct timespec64 time_now;
2143 struct timespec64 delta;
2145 rc = efx_phc_gettime(ptp, &time_now);
2149 delta = timespec64_sub(*e_ts, time_now);
2151 rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
2158 static int efx_phc_enable(struct ptp_clock_info *ptp,
2159 struct ptp_clock_request *request,
2162 struct efx_ptp_data *ptp_data = container_of(ptp,
2163 struct efx_ptp_data,
2165 if (request->type != PTP_CLK_REQ_PPS)
2168 ptp_data->nic_ts_enabled = !!enable;
2172 static const struct efx_channel_type efx_ptp_channel_type = {
2173 .handle_no_channel = efx_ptp_handle_no_channel,
2174 .pre_probe = efx_ptp_probe_channel,
2175 .post_remove = efx_ptp_remove_channel,
2176 .get_name = efx_ptp_get_channel_name,
2177 /* no copy operation; there is no need to reallocate this channel */
2178 .receive_skb = efx_ptp_rx,
2179 .want_txqs = efx_ptp_want_txqs,
2180 .keep_eventq = false,
2183 void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
2185 /* Check whether PTP is implemented on this NIC. The DISABLE
2186 * operation will succeed if and only if it is implemented.
2188 if (efx_ptp_disable(efx) == 0)
2189 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
2190 &efx_ptp_channel_type;
2193 void efx_ptp_start_datapath(struct efx_nic *efx)
2195 if (efx_ptp_restart(efx))
2196 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
2197 /* re-enable timestamping if it was previously enabled */
2198 if (efx->type->ptp_set_ts_sync_events)
2199 efx->type->ptp_set_ts_sync_events(efx, true, true);
2202 void efx_ptp_stop_datapath(struct efx_nic *efx)
2204 /* temporarily disable timestamping */
2205 if (efx->type->ptp_set_ts_sync_events)
2206 efx->type->ptp_set_ts_sync_events(efx, false, true);