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"
46 #include "nic.h" /* indirectly includes ptp.h */
48 /* Maximum number of events expected to make up a PTP event */
49 #define MAX_EVENT_FRAGS 3
51 /* Maximum delay, ms, to begin synchronisation */
52 #define MAX_SYNCHRONISE_WAIT_MS 2
54 /* How long, at most, to spend synchronising */
55 #define SYNCHRONISE_PERIOD_NS 250000
57 /* How often to update the shared memory time */
58 #define SYNCHRONISATION_GRANULARITY_NS 200
60 /* Minimum permitted length of a (corrected) synchronisation time */
61 #define DEFAULT_MIN_SYNCHRONISATION_NS 120
63 /* Maximum permitted length of a (corrected) synchronisation time */
64 #define MAX_SYNCHRONISATION_NS 1000
66 /* How many (MC) receive events that can be queued */
67 #define MAX_RECEIVE_EVENTS 8
69 /* Length of (modified) moving average. */
70 #define AVERAGE_LENGTH 16
72 /* How long an unmatched event or packet can be held */
73 #define PKT_EVENT_LIFETIME_MS 10
75 /* Offsets into PTP packet for identification. These offsets are from the
76 * start of the IP header, not the MAC header. Note that neither PTP V1 nor
77 * PTP V2 permit the use of IPV4 options.
79 #define PTP_DPORT_OFFSET 22
81 #define PTP_V1_VERSION_LENGTH 2
82 #define PTP_V1_VERSION_OFFSET 28
84 #define PTP_V1_UUID_LENGTH 6
85 #define PTP_V1_UUID_OFFSET 50
87 #define PTP_V1_SEQUENCE_LENGTH 2
88 #define PTP_V1_SEQUENCE_OFFSET 58
90 /* The minimum length of a PTP V1 packet for offsets, etc. to be valid:
93 #define PTP_V1_MIN_LENGTH 64
95 #define PTP_V2_VERSION_LENGTH 1
96 #define PTP_V2_VERSION_OFFSET 29
98 #define PTP_V2_UUID_LENGTH 8
99 #define PTP_V2_UUID_OFFSET 48
101 /* Although PTP V2 UUIDs are comprised a ClockIdentity (8) and PortNumber (2),
102 * the MC only captures the last six bytes of the clock identity. These values
103 * reflect those, not the ones used in the standard. The standard permits
104 * mapping of V1 UUIDs to V2 UUIDs with these same values.
106 #define PTP_V2_MC_UUID_LENGTH 6
107 #define PTP_V2_MC_UUID_OFFSET 50
109 #define PTP_V2_SEQUENCE_LENGTH 2
110 #define PTP_V2_SEQUENCE_OFFSET 58
112 /* The minimum length of a PTP V2 packet for offsets, etc. to be valid:
113 * includes IP header.
115 #define PTP_V2_MIN_LENGTH 63
117 #define PTP_MIN_LENGTH 63
119 #define PTP_ADDRESS 0xe0000181 /* 224.0.1.129 */
120 #define PTP_EVENT_PORT 319
121 #define PTP_GENERAL_PORT 320
123 /* Annoyingly the format of the version numbers are different between
124 * versions 1 and 2 so it isn't possible to simply look for 1 or 2.
126 #define PTP_VERSION_V1 1
128 #define PTP_VERSION_V2 2
129 #define PTP_VERSION_V2_MASK 0x0f
131 enum ptp_packet_state {
132 PTP_PACKET_STATE_UNMATCHED = 0,
133 PTP_PACKET_STATE_MATCHED,
134 PTP_PACKET_STATE_TIMED_OUT,
135 PTP_PACKET_STATE_MATCH_UNWANTED
138 /* NIC synchronised with single word of time only comprising
139 * partial seconds and full nanoseconds: 10^9 ~ 2^30 so 2 bits for seconds.
141 #define MC_NANOSECOND_BITS 30
142 #define MC_NANOSECOND_MASK ((1 << MC_NANOSECOND_BITS) - 1)
143 #define MC_SECOND_MASK ((1 << (32 - MC_NANOSECOND_BITS)) - 1)
145 /* Maximum parts-per-billion adjustment that is acceptable */
146 #define MAX_PPB 1000000
148 /* Precalculate scale word to avoid long long division at runtime */
149 /* This is equivalent to 2^66 / 10^9. */
150 #define PPB_SCALE_WORD ((1LL << (57)) / 1953125LL)
152 /* How much to shift down after scaling to convert to FP40 */
153 #define PPB_SHIFT_FP40 26
155 #define PPB_SHIFT_FP44 22
157 #define PTP_SYNC_ATTEMPTS 4
160 * struct efx_ptp_match - Matching structure, stored in sk_buff's cb area.
161 * @words: UUID and (partial) sequence number
162 * @expiry: Time after which the packet should be delivered irrespective of
164 * @state: The state of the packet - whether it is ready for processing or
165 * whether that is of no interest.
167 struct efx_ptp_match {
168 u32 words[DIV_ROUND_UP(PTP_V1_UUID_LENGTH, 4)];
169 unsigned long expiry;
170 enum ptp_packet_state state;
174 * struct efx_ptp_event_rx - A PTP receive event (from MC)
175 * @seq0: First part of (PTP) UUID
176 * @seq1: Second part of (PTP) UUID and sequence number
177 * @hwtimestamp: Event timestamp
179 struct efx_ptp_event_rx {
180 struct list_head link;
184 unsigned long expiry;
188 * struct efx_ptp_timeset - Synchronisation between host and MC
189 * @host_start: Host time immediately before hardware timestamp taken
190 * @major: Hardware timestamp, major
191 * @minor: Hardware timestamp, minor
192 * @host_end: Host time immediately after hardware timestamp taken
193 * @wait: Number of NIC clock ticks between hardware timestamp being read and
194 * host end time being seen
195 * @window: Difference of host_end and host_start
196 * @valid: Whether this timeset is valid
198 struct efx_ptp_timeset {
204 u32 window; /* Derived: end - start, allowing for wrap */
208 * struct efx_ptp_data - Precision Time Protocol (PTP) state
209 * @efx: The NIC context
210 * @channel: The PTP channel (Siena only)
211 * @rx_ts_inline: Flag for whether RX timestamps are inline (else they are
213 * @rxq: Receive SKB queue (awaiting timestamps)
214 * @txq: Transmit SKB queue
215 * @evt_list: List of MC receive events awaiting packets
216 * @evt_free_list: List of free events
217 * @evt_lock: Lock for manipulating evt_list and evt_free_list
218 * @rx_evts: Instantiated events (on evt_list and evt_free_list)
219 * @workwq: Work queue for processing pending PTP operations
221 * @reset_required: A serious error has occurred and the PTP task needs to be
222 * reset (disable, enable).
223 * @rxfilter_event: Receive filter when operating
224 * @rxfilter_general: Receive filter when operating
225 * @config: Current timestamp configuration
226 * @enabled: PTP operation enabled
227 * @mode: Mode in which PTP operating (PTP version)
228 * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time
229 * @nic_to_kernel_time: Function to convert from NIC to kernel time
230 * @nic_time.minor_max: Wrap point for NIC minor times
231 * @nic_time.sync_event_diff_min: Minimum acceptable difference between time
232 * in packet prefix and last MCDI time sync event i.e. how much earlier than
233 * the last sync event time a packet timestamp can be.
234 * @nic_time.sync_event_diff_max: Maximum acceptable difference between time
235 * in packet prefix and last MCDI time sync event i.e. how much later than
236 * the last sync event time a packet timestamp can be.
237 * @nic_time.sync_event_minor_shift: Shift required to make minor time from
238 * field in MCDI time sync event.
239 * @min_synchronisation_ns: Minimum acceptable corrected sync window
240 * @capabilities: Capabilities flags from the NIC
241 * @ts_corrections.ptp_tx: Required driver correction of PTP packet transmit
243 * @ts_corrections.ptp_rx: Required driver correction of PTP packet receive
245 * @ts_corrections.pps_out: PPS output error (information only)
246 * @ts_corrections.pps_in: Required driver correction of PPS input timestamps
247 * @ts_corrections.general_tx: Required driver correction of general packet
248 * transmit timestamps
249 * @ts_corrections.general_rx: Required driver correction of general packet
251 * @evt_frags: Partly assembled PTP events
252 * @evt_frag_idx: Current fragment number
253 * @evt_code: Last event code
254 * @start: Address at which MC indicates ready for synchronisation
255 * @host_time_pps: Host time at last PPS
256 * @adjfreq_ppb_shift: Shift required to convert scaled parts-per-billion
257 * frequency adjustment into a fixed point fractional nanosecond format.
258 * @current_adjfreq: Current ppb adjustment.
259 * @phc_clock: Pointer to registered phc device (if primary function)
260 * @phc_clock_info: Registration structure for phc device
261 * @pps_work: pps work task for handling pps events
262 * @pps_workwq: pps work queue
263 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
264 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
265 * allocations in main data path).
266 * @good_syncs: Number of successful synchronisations.
267 * @fast_syncs: Number of synchronisations requiring short delay
268 * @bad_syncs: Number of failed synchronisations.
269 * @sync_timeouts: Number of synchronisation timeouts
270 * @no_time_syncs: Number of synchronisations with no good times.
271 * @invalid_sync_windows: Number of sync windows with bad durations.
272 * @undersize_sync_windows: Number of corrected sync windows that are too small
273 * @oversize_sync_windows: Number of corrected sync windows that are too large
274 * @rx_no_timestamp: Number of packets received without a timestamp.
275 * @timeset: Last set of synchronisation statistics.
276 * @xmit_skb: Transmit SKB function.
278 struct efx_ptp_data {
280 struct efx_channel *channel;
282 struct sk_buff_head rxq;
283 struct sk_buff_head txq;
284 struct list_head evt_list;
285 struct list_head evt_free_list;
287 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
288 struct workqueue_struct *workwq;
289 struct work_struct work;
292 u32 rxfilter_general;
293 bool rxfilter_installed;
294 struct hwtstamp_config config;
297 void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
298 ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
302 u32 sync_event_diff_min;
303 u32 sync_event_diff_max;
304 unsigned int sync_event_minor_shift;
306 unsigned int min_synchronisation_ns;
307 unsigned int capabilities;
316 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
319 struct efx_buffer start;
320 struct pps_event_time host_time_pps;
321 unsigned int adjfreq_ppb_shift;
323 struct ptp_clock *phc_clock;
324 struct ptp_clock_info phc_clock_info;
325 struct work_struct pps_work;
326 struct workqueue_struct *pps_workwq;
328 _MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
330 unsigned int good_syncs;
331 unsigned int fast_syncs;
332 unsigned int bad_syncs;
333 unsigned int sync_timeouts;
334 unsigned int no_time_syncs;
335 unsigned int invalid_sync_windows;
336 unsigned int undersize_sync_windows;
337 unsigned int oversize_sync_windows;
338 unsigned int rx_no_timestamp;
339 struct efx_ptp_timeset
340 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
341 void (*xmit_skb)(struct efx_nic *efx, struct sk_buff *skb);
344 static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
345 static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
346 static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
347 static int efx_phc_settime(struct ptp_clock_info *ptp,
348 const struct timespec64 *e_ts);
349 static int efx_phc_enable(struct ptp_clock_info *ptp,
350 struct ptp_clock_request *request, int on);
352 bool efx_ptp_use_mac_tx_timestamps(struct efx_nic *efx)
354 return efx_has_cap(efx, TX_MAC_TIMESTAMPING);
357 /* PTP 'extra' channel is still a traffic channel, but we only create TX queues
358 * if PTP uses MAC TX timestamps, not if PTP uses the MC directly to transmit.
360 static bool efx_ptp_want_txqs(struct efx_channel *channel)
362 return efx_ptp_use_mac_tx_timestamps(channel->efx);
365 #define PTP_SW_STAT(ext_name, field_name) \
366 { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
367 #define PTP_MC_STAT(ext_name, mcdi_name) \
368 { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
369 static const struct efx_hw_stat_desc efx_ptp_stat_desc[] = {
370 PTP_SW_STAT(ptp_good_syncs, good_syncs),
371 PTP_SW_STAT(ptp_fast_syncs, fast_syncs),
372 PTP_SW_STAT(ptp_bad_syncs, bad_syncs),
373 PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts),
374 PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs),
375 PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows),
376 PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows),
377 PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows),
378 PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp),
379 PTP_MC_STAT(ptp_tx_timestamp_packets, TX),
380 PTP_MC_STAT(ptp_rx_timestamp_packets, RX),
381 PTP_MC_STAT(ptp_timestamp_packets, TS),
382 PTP_MC_STAT(ptp_filter_matches, FM),
383 PTP_MC_STAT(ptp_non_filter_matches, NFM),
385 #define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
386 static const unsigned long efx_ptp_stat_mask[] = {
387 [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
390 size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
395 return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
396 efx_ptp_stat_mask, strings);
399 size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
401 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
402 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
409 /* Copy software statistics */
410 for (i = 0; i < PTP_STAT_COUNT; i++) {
411 if (efx_ptp_stat_desc[i].dma_width)
413 stats[i] = *(unsigned int *)((char *)efx->ptp_data +
414 efx_ptp_stat_desc[i].offset);
417 /* Fetch MC statistics. We *must* fill in all statistics or
418 * risk leaking kernel memory to userland, so if the MCDI
419 * request fails we pretend we got zeroes.
421 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS);
422 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
423 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
424 outbuf, sizeof(outbuf), NULL);
426 memset(outbuf, 0, sizeof(outbuf));
427 efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
429 stats, _MCDI_PTR(outbuf, 0), false);
431 return PTP_STAT_COUNT;
434 /* For Siena platforms NIC time is s and ns */
435 static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
437 struct timespec64 ts = ns_to_timespec64(ns);
438 *nic_major = (u32)ts.tv_sec;
439 *nic_minor = ts.tv_nsec;
442 static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
445 ktime_t kt = ktime_set(nic_major, nic_minor);
447 kt = ktime_add_ns(kt, (u64)correction);
449 kt = ktime_sub_ns(kt, (u64)-correction);
453 /* To convert from s27 format to ns we multiply then divide by a power of 2.
454 * For the conversion from ns to s27, the operation is also converted to a
455 * multiply and shift.
457 #define S27_TO_NS_SHIFT (27)
458 #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
459 #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
460 #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
462 /* For Huntington platforms NIC time is in seconds and fractions of a second
463 * where the minor register only uses 27 bits in units of 2^-27s.
465 static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
467 struct timespec64 ts = ns_to_timespec64(ns);
468 u32 maj = (u32)ts.tv_sec;
469 u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
470 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
472 /* The conversion can result in the minor value exceeding the maximum.
473 * In this case, round up to the next second.
475 if (min >= S27_MINOR_MAX) {
476 min -= S27_MINOR_MAX;
484 static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
486 u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
487 (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
488 return ktime_set(nic_major, ns);
491 static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
494 /* Apply the correction and deal with carry */
495 nic_minor += correction;
496 if ((s32)nic_minor < 0) {
497 nic_minor += S27_MINOR_MAX;
499 } else if (nic_minor >= S27_MINOR_MAX) {
500 nic_minor -= S27_MINOR_MAX;
504 return efx_ptp_s27_to_ktime(nic_major, nic_minor);
507 /* For Medford2 platforms the time is in seconds and quarter nanoseconds. */
508 static void efx_ptp_ns_to_s_qns(s64 ns, u32 *nic_major, u32 *nic_minor)
510 struct timespec64 ts = ns_to_timespec64(ns);
512 *nic_major = (u32)ts.tv_sec;
513 *nic_minor = ts.tv_nsec * 4;
516 static ktime_t efx_ptp_s_qns_to_ktime_correction(u32 nic_major, u32 nic_minor,
521 nic_minor = DIV_ROUND_CLOSEST(nic_minor, 4);
522 correction = DIV_ROUND_CLOSEST(correction, 4);
524 kt = ktime_set(nic_major, nic_minor);
527 kt = ktime_add_ns(kt, (u64)correction);
529 kt = ktime_sub_ns(kt, (u64)-correction);
533 struct efx_channel *efx_ptp_channel(struct efx_nic *efx)
535 return efx->ptp_data ? efx->ptp_data->channel : NULL;
538 static u32 last_sync_timestamp_major(struct efx_nic *efx)
540 struct efx_channel *channel = efx_ptp_channel(efx);
544 major = channel->sync_timestamp_major;
548 /* The 8000 series and later can provide the time from the MAC, which is only
549 * 48 bits long and provides meta-information in the top 2 bits.
552 efx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx,
553 struct efx_ptp_data *ptp,
554 u32 nic_major, u32 nic_minor,
561 if (!(nic_major & 0x80000000)) {
562 WARN_ON_ONCE(nic_major >> 16);
564 /* Medford provides 48 bits of timestamp, so we must get the top
565 * 16 bits from the timesync event state.
567 * We only have the lower 16 bits of the time now, but we do
568 * have a full resolution timestamp at some point in past. As
569 * long as the difference between the (real) now and the sync
570 * is less than 2^15, then we can reconstruct the difference
571 * between those two numbers using only the lower 16 bits of
576 * a - b = ((a mod k) - b) mod k
578 * when -k/2 < (a-b) < k/2. In our case k is 2^16. We know
579 * (a mod k) and b, so can calculate the delta, a - b.
582 sync_timestamp = last_sync_timestamp_major(efx);
584 /* Because delta is s16 this does an implicit mask down to
585 * 16 bits which is what we need, assuming
586 * MEDFORD_TX_SECS_EVENT_BITS is 16. delta is signed so that
587 * we can deal with the (unlikely) case of sync timestamps
588 * arriving from the future.
590 delta = nic_major - sync_timestamp;
592 /* Recover the fully specified time now, by applying the offset
593 * to the (fully specified) sync time.
595 nic_major = sync_timestamp + delta;
597 kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
603 ktime_t efx_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue)
605 struct efx_nic *efx = tx_queue->efx;
606 struct efx_ptp_data *ptp = efx->ptp_data;
609 if (efx_ptp_use_mac_tx_timestamps(efx))
610 kt = efx_ptp_mac_nic_to_ktime_correction(efx, ptp,
611 tx_queue->completed_timestamp_major,
612 tx_queue->completed_timestamp_minor,
613 ptp->ts_corrections.general_tx);
615 kt = ptp->nic_to_kernel_time(
616 tx_queue->completed_timestamp_major,
617 tx_queue->completed_timestamp_minor,
618 ptp->ts_corrections.general_tx);
622 /* Get PTP attributes and set up time conversions */
623 static int efx_ptp_get_attributes(struct efx_nic *efx)
625 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
626 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
627 struct efx_ptp_data *ptp = efx->ptp_data;
632 /* Get the PTP attributes. If the NIC doesn't support the operation we
633 * use the default format for compatibility with older NICs i.e.
634 * seconds and nanoseconds.
636 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
637 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
638 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
639 outbuf, sizeof(outbuf), &out_len);
641 fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
642 } else if (rc == -EINVAL) {
643 fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
644 } else if (rc == -EPERM) {
645 netif_info(efx, probe, efx->net_dev, "no PTP support\n");
648 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf),
649 outbuf, sizeof(outbuf), rc);
654 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION:
655 ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
656 ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
657 ptp->nic_time.minor_max = 1 << 27;
658 ptp->nic_time.sync_event_minor_shift = 19;
660 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS:
661 ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
662 ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
663 ptp->nic_time.minor_max = 1000000000;
664 ptp->nic_time.sync_event_minor_shift = 22;
666 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS:
667 ptp->ns_to_nic_time = efx_ptp_ns_to_s_qns;
668 ptp->nic_to_kernel_time = efx_ptp_s_qns_to_ktime_correction;
669 ptp->nic_time.minor_max = 4000000000UL;
670 ptp->nic_time.sync_event_minor_shift = 24;
676 /* Precalculate acceptable difference between the minor time in the
677 * packet prefix and the last MCDI time sync event. We expect the
678 * packet prefix timestamp to be after of sync event by up to one
679 * sync event interval (0.25s) but we allow it to exceed this by a
680 * fuzz factor of (0.1s)
682 ptp->nic_time.sync_event_diff_min = ptp->nic_time.minor_max
683 - (ptp->nic_time.minor_max / 10);
684 ptp->nic_time.sync_event_diff_max = (ptp->nic_time.minor_max / 4)
685 + (ptp->nic_time.minor_max / 10);
687 /* MC_CMD_PTP_OP_GET_ATTRIBUTES has been extended twice from an older
688 * operation MC_CMD_PTP_OP_GET_TIME_FORMAT. The function now may return
689 * a value to use for the minimum acceptable corrected synchronization
690 * window and may return further capabilities.
691 * If we have the extra information store it. For older firmware that
692 * does not implement the extended command use the default value.
695 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST)
696 ptp->min_synchronisation_ns =
698 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
700 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
703 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
704 ptp->capabilities = MCDI_DWORD(outbuf,
705 PTP_OUT_GET_ATTRIBUTES_CAPABILITIES);
707 ptp->capabilities = 0;
709 /* Set up the shift for conversion between frequency
710 * adjustments in parts-per-billion and the fixed-point
711 * fractional ns format that the adapter uses.
713 if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN))
714 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44;
716 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40;
721 /* Get PTP timestamp corrections */
722 static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
724 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
725 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN);
729 /* Get the timestamp corrections from the NIC. If this operation is
730 * not supported (older NICs) then no correction is required.
732 MCDI_SET_DWORD(inbuf, PTP_IN_OP,
733 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
734 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
736 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
737 outbuf, sizeof(outbuf), &out_len);
739 efx->ptp_data->ts_corrections.ptp_tx = MCDI_DWORD(outbuf,
740 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
741 efx->ptp_data->ts_corrections.ptp_rx = MCDI_DWORD(outbuf,
742 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
743 efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
744 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
745 efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
746 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
748 if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) {
749 efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD(
751 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX);
752 efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD(
754 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX);
756 efx->ptp_data->ts_corrections.general_tx =
757 efx->ptp_data->ts_corrections.ptp_tx;
758 efx->ptp_data->ts_corrections.general_rx =
759 efx->ptp_data->ts_corrections.ptp_rx;
761 } else if (rc == -EINVAL) {
762 efx->ptp_data->ts_corrections.ptp_tx = 0;
763 efx->ptp_data->ts_corrections.ptp_rx = 0;
764 efx->ptp_data->ts_corrections.pps_out = 0;
765 efx->ptp_data->ts_corrections.pps_in = 0;
766 efx->ptp_data->ts_corrections.general_tx = 0;
767 efx->ptp_data->ts_corrections.general_rx = 0;
769 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), outbuf,
777 /* Enable MCDI PTP support. */
778 static int efx_ptp_enable(struct efx_nic *efx)
780 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
781 MCDI_DECLARE_BUF_ERR(outbuf);
784 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
785 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
786 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
787 efx->ptp_data->channel ?
788 efx->ptp_data->channel->channel : 0);
789 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
791 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
792 outbuf, sizeof(outbuf), NULL);
793 rc = (rc == -EALREADY) ? 0 : rc;
795 efx_mcdi_display_error(efx, MC_CMD_PTP,
796 MC_CMD_PTP_IN_ENABLE_LEN,
797 outbuf, sizeof(outbuf), rc);
801 /* Disable MCDI PTP support.
803 * Note that this function should never rely on the presence of ptp_data -
804 * may be called before that exists.
806 static int efx_ptp_disable(struct efx_nic *efx)
808 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
809 MCDI_DECLARE_BUF_ERR(outbuf);
812 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
813 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
814 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
815 outbuf, sizeof(outbuf), NULL);
816 rc = (rc == -EALREADY) ? 0 : rc;
817 /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
818 * should only have been called during probe.
820 if (rc == -ENOSYS || rc == -EPERM)
821 netif_info(efx, probe, efx->net_dev, "no PTP support\n");
823 efx_mcdi_display_error(efx, MC_CMD_PTP,
824 MC_CMD_PTP_IN_DISABLE_LEN,
825 outbuf, sizeof(outbuf), rc);
829 static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
833 while ((skb = skb_dequeue(q))) {
835 netif_receive_skb(skb);
840 static void efx_ptp_handle_no_channel(struct efx_nic *efx)
842 netif_err(efx, drv, efx->net_dev,
843 "ERROR: PTP requires MSI-X and 1 additional interrupt"
844 "vector. PTP disabled\n");
847 /* Repeatedly send the host time to the MC which will capture the hardware
850 static void efx_ptp_send_times(struct efx_nic *efx,
851 struct pps_event_time *last_time)
853 struct pps_event_time now;
854 struct timespec64 limit;
855 struct efx_ptp_data *ptp = efx->ptp_data;
856 int *mc_running = ptp->start.addr;
860 timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
862 /* Write host time for specified period or until MC is done */
863 while ((timespec64_compare(&now.ts_real, &limit) < 0) &&
864 READ_ONCE(*mc_running)) {
865 struct timespec64 update_time;
866 unsigned int host_time;
868 /* Don't update continuously to avoid saturating the PCIe bus */
869 update_time = now.ts_real;
870 timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
873 } while ((timespec64_compare(&now.ts_real, &update_time) < 0) &&
874 READ_ONCE(*mc_running));
876 /* Synchronise NIC with single word of time only */
877 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
878 now.ts_real.tv_nsec);
879 /* Update host time in NIC memory */
880 efx->type->ptp_write_host_time(efx, host_time);
885 /* Read a timeset from the MC's results and partial process. */
886 static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
887 struct efx_ptp_timeset *timeset)
889 unsigned start_ns, end_ns;
891 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
892 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
893 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
894 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
895 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
898 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
899 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
900 /* Allow for rollover */
901 if (end_ns < start_ns)
902 end_ns += NSEC_PER_SEC;
903 /* Determine duration of operation */
904 timeset->window = end_ns - start_ns;
907 /* Process times received from MC.
909 * Extract times from returned results, and establish the minimum value
910 * seen. The minimum value represents the "best" possible time and events
911 * too much greater than this are rejected - the machine is, perhaps, too
912 * busy. A number of readings are taken so that, hopefully, at least one good
913 * synchronisation will be seen in the results.
916 efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
917 size_t response_length,
918 const struct pps_event_time *last_time)
920 unsigned number_readings =
921 MCDI_VAR_ARRAY_LEN(response_length,
922 PTP_OUT_SYNCHRONIZE_TIMESET);
925 unsigned last_good = 0;
926 struct efx_ptp_data *ptp = efx->ptp_data;
929 struct timespec64 delta;
932 if (number_readings == 0)
935 /* Read the set of results and find the last good host-MC
936 * synchronization result. The MC times when it finishes reading the
937 * host time so the corrected window time should be fairly constant
938 * for a given platform. Increment stats for any results that appear
941 for (i = 0; i < number_readings; i++) {
942 s32 window, corrected;
943 struct timespec64 wait;
945 efx_ptp_read_timeset(
946 MCDI_ARRAY_STRUCT_PTR(synch_buf,
947 PTP_OUT_SYNCHRONIZE_TIMESET, i),
950 wait = ktime_to_timespec64(
951 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
952 window = ptp->timeset[i].window;
953 corrected = window - wait.tv_nsec;
955 /* We expect the uncorrected synchronization window to be at
956 * least as large as the interval between host start and end
957 * times. If it is smaller than this then this is mostly likely
958 * to be a consequence of the host's time being adjusted.
959 * Check that the corrected sync window is in a reasonable
960 * range. If it is out of range it is likely to be because an
961 * interrupt or other delay occurred between reading the system
962 * time and writing it to MC memory.
964 if (window < SYNCHRONISATION_GRANULARITY_NS) {
965 ++ptp->invalid_sync_windows;
966 } else if (corrected >= MAX_SYNCHRONISATION_NS) {
967 ++ptp->oversize_sync_windows;
968 } else if (corrected < ptp->min_synchronisation_ns) {
969 ++ptp->undersize_sync_windows;
977 netif_warn(efx, drv, efx->net_dev,
978 "PTP no suitable synchronisations\n");
982 /* Calculate delay from last good sync (host time) to last_time.
983 * It is possible that the seconds rolled over between taking
984 * the start reading and the last value written by the host. The
985 * timescales are such that a gap of more than one second is never
986 * expected. delta is *not* normalised.
988 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
989 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
990 if (start_sec != last_sec &&
991 ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
992 netif_warn(efx, hw, efx->net_dev,
993 "PTP bad synchronisation seconds\n");
996 delta.tv_sec = (last_sec - start_sec) & 1;
998 last_time->ts_real.tv_nsec -
999 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
1001 /* Convert the NIC time at last good sync into kernel time.
1002 * No correction is required - this time is the output of a
1005 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
1006 ptp->timeset[last_good].minor, 0);
1008 /* Calculate delay from NIC top of second to last_time */
1009 delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec;
1011 /* Set PPS timestamp to match NIC top of second */
1012 ptp->host_time_pps = *last_time;
1013 pps_sub_ts(&ptp->host_time_pps, delta);
1018 /* Synchronize times between the host and the MC */
1019 static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
1021 struct efx_ptp_data *ptp = efx->ptp_data;
1022 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
1023 size_t response_length;
1025 unsigned long timeout;
1026 struct pps_event_time last_time = {};
1027 unsigned int loops = 0;
1028 int *start = ptp->start.addr;
1030 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
1031 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
1032 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
1034 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
1035 ptp->start.dma_addr);
1037 /* Clear flag that signals MC ready */
1038 WRITE_ONCE(*start, 0);
1039 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
1040 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
1041 EFX_WARN_ON_ONCE_PARANOID(rc);
1043 /* Wait for start from MCDI (or timeout) */
1044 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
1045 while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) {
1046 udelay(20); /* Usually start MCDI execution quickly */
1052 if (!time_before(jiffies, timeout))
1053 ++ptp->sync_timeouts;
1055 if (READ_ONCE(*start))
1056 efx_ptp_send_times(efx, &last_time);
1058 /* Collect results */
1059 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
1060 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
1061 synch_buf, sizeof(synch_buf),
1064 rc = efx_ptp_process_times(efx, synch_buf, response_length,
1069 ++ptp->no_time_syncs;
1072 /* Increment the bad syncs counter if the synchronize fails, whatever
1081 /* Transmit a PTP packet via the dedicated hardware timestamped queue. */
1082 static void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb)
1084 struct efx_ptp_data *ptp_data = efx->ptp_data;
1085 struct efx_tx_queue *tx_queue;
1086 u8 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
1088 tx_queue = &ptp_data->channel->tx_queue[type];
1089 if (tx_queue && tx_queue->timestamping) {
1090 efx_enqueue_skb(tx_queue, skb);
1092 WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
1093 dev_kfree_skb_any(skb);
1097 /* Transmit a PTP packet, via the MCDI interface, to the wire. */
1098 static void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb)
1100 struct efx_ptp_data *ptp_data = efx->ptp_data;
1101 struct skb_shared_hwtstamps timestamps;
1103 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
1106 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
1107 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
1108 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
1109 if (skb_shinfo(skb)->nr_frags != 0) {
1110 rc = skb_linearize(skb);
1115 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1116 rc = skb_checksum_help(skb);
1120 skb_copy_from_linear_data(skb,
1121 MCDI_PTR(ptp_data->txbuf,
1122 PTP_IN_TRANSMIT_PACKET),
1124 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
1125 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
1126 txtime, sizeof(txtime), &len);
1130 memset(×tamps, 0, sizeof(timestamps));
1131 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
1132 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
1133 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
1134 ptp_data->ts_corrections.ptp_tx);
1136 skb_tstamp_tx(skb, ×tamps);
1141 dev_kfree_skb_any(skb);
1146 static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
1148 struct efx_ptp_data *ptp = efx->ptp_data;
1149 struct list_head *cursor;
1150 struct list_head *next;
1152 if (ptp->rx_ts_inline)
1155 /* Drop time-expired events */
1156 spin_lock_bh(&ptp->evt_lock);
1157 if (!list_empty(&ptp->evt_list)) {
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");
1170 spin_unlock_bh(&ptp->evt_lock);
1173 static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
1174 struct sk_buff *skb)
1176 struct efx_ptp_data *ptp = efx->ptp_data;
1178 struct list_head *cursor;
1179 struct list_head *next;
1180 struct efx_ptp_match *match;
1181 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
1183 WARN_ON_ONCE(ptp->rx_ts_inline);
1185 spin_lock_bh(&ptp->evt_lock);
1186 evts_waiting = !list_empty(&ptp->evt_list);
1187 spin_unlock_bh(&ptp->evt_lock);
1190 return PTP_PACKET_STATE_UNMATCHED;
1192 match = (struct efx_ptp_match *)skb->cb;
1193 /* Look for a matching timestamp in the event queue */
1194 spin_lock_bh(&ptp->evt_lock);
1195 list_for_each_safe(cursor, next, &ptp->evt_list) {
1196 struct efx_ptp_event_rx *evt;
1198 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
1199 if ((evt->seq0 == match->words[0]) &&
1200 (evt->seq1 == match->words[1])) {
1201 struct skb_shared_hwtstamps *timestamps;
1203 /* Match - add in hardware timestamp */
1204 timestamps = skb_hwtstamps(skb);
1205 timestamps->hwtstamp = evt->hwtimestamp;
1207 match->state = PTP_PACKET_STATE_MATCHED;
1208 rc = PTP_PACKET_STATE_MATCHED;
1209 list_move(&evt->link, &ptp->evt_free_list);
1213 spin_unlock_bh(&ptp->evt_lock);
1218 /* Process any queued receive events and corresponding packets
1220 * q is returned with all the packets that are ready for delivery.
1222 static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
1224 struct efx_ptp_data *ptp = efx->ptp_data;
1225 struct sk_buff *skb;
1227 while ((skb = skb_dequeue(&ptp->rxq))) {
1228 struct efx_ptp_match *match;
1230 match = (struct efx_ptp_match *)skb->cb;
1231 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
1232 __skb_queue_tail(q, skb);
1233 } else if (efx_ptp_match_rx(efx, skb) ==
1234 PTP_PACKET_STATE_MATCHED) {
1235 __skb_queue_tail(q, skb);
1236 } else if (time_after(jiffies, match->expiry)) {
1237 match->state = PTP_PACKET_STATE_TIMED_OUT;
1238 ++ptp->rx_no_timestamp;
1239 __skb_queue_tail(q, skb);
1241 /* Replace unprocessed entry and stop */
1242 skb_queue_head(&ptp->rxq, skb);
1248 /* Complete processing of a received packet */
1249 static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
1252 netif_receive_skb(skb);
1256 static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
1258 struct efx_ptp_data *ptp = efx->ptp_data;
1260 if (ptp->rxfilter_installed) {
1261 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1262 ptp->rxfilter_general);
1263 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1264 ptp->rxfilter_event);
1265 ptp->rxfilter_installed = false;
1269 static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
1271 struct efx_ptp_data *ptp = efx->ptp_data;
1272 struct efx_filter_spec rxfilter;
1275 if (!ptp->channel || ptp->rxfilter_installed)
1278 /* Must filter on both event and general ports to ensure
1279 * that there is no packet re-ordering.
1281 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1283 efx_channel_get_rx_queue(ptp->channel)));
1284 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1286 htons(PTP_EVENT_PORT));
1290 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1293 ptp->rxfilter_event = rc;
1295 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1297 efx_channel_get_rx_queue(ptp->channel)));
1298 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1300 htons(PTP_GENERAL_PORT));
1304 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1307 ptp->rxfilter_general = rc;
1309 ptp->rxfilter_installed = true;
1313 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1314 ptp->rxfilter_event);
1318 static int efx_ptp_start(struct efx_nic *efx)
1320 struct efx_ptp_data *ptp = efx->ptp_data;
1323 ptp->reset_required = false;
1325 rc = efx_ptp_insert_multicast_filters(efx);
1329 rc = efx_ptp_enable(efx);
1333 ptp->evt_frag_idx = 0;
1334 ptp->current_adjfreq = 0;
1339 efx_ptp_remove_multicast_filters(efx);
1343 static int efx_ptp_stop(struct efx_nic *efx)
1345 struct efx_ptp_data *ptp = efx->ptp_data;
1346 struct list_head *cursor;
1347 struct list_head *next;
1353 rc = efx_ptp_disable(efx);
1355 efx_ptp_remove_multicast_filters(efx);
1357 /* Make sure RX packets are really delivered */
1358 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1359 skb_queue_purge(&efx->ptp_data->txq);
1361 /* Drop any pending receive events */
1362 spin_lock_bh(&efx->ptp_data->evt_lock);
1363 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
1364 list_move(cursor, &efx->ptp_data->evt_free_list);
1366 spin_unlock_bh(&efx->ptp_data->evt_lock);
1371 static int efx_ptp_restart(struct efx_nic *efx)
1373 if (efx->ptp_data && efx->ptp_data->enabled)
1374 return efx_ptp_start(efx);
1378 static void efx_ptp_pps_worker(struct work_struct *work)
1380 struct efx_ptp_data *ptp =
1381 container_of(work, struct efx_ptp_data, pps_work);
1382 struct efx_nic *efx = ptp->efx;
1383 struct ptp_clock_event ptp_evt;
1385 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1388 ptp_evt.type = PTP_CLOCK_PPSUSR;
1389 ptp_evt.pps_times = ptp->host_time_pps;
1390 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1393 static void efx_ptp_worker(struct work_struct *work)
1395 struct efx_ptp_data *ptp_data =
1396 container_of(work, struct efx_ptp_data, work);
1397 struct efx_nic *efx = ptp_data->efx;
1398 struct sk_buff *skb;
1399 struct sk_buff_head tempq;
1401 if (ptp_data->reset_required) {
1407 efx_ptp_drop_time_expired_events(efx);
1409 __skb_queue_head_init(&tempq);
1410 efx_ptp_process_events(efx, &tempq);
1412 while ((skb = skb_dequeue(&ptp_data->txq)))
1413 ptp_data->xmit_skb(efx, skb);
1415 while ((skb = __skb_dequeue(&tempq)))
1416 efx_ptp_process_rx(efx, skb);
1419 static const struct ptp_clock_info efx_phc_clock_info = {
1420 .owner = THIS_MODULE,
1428 .adjfreq = efx_phc_adjfreq,
1429 .adjtime = efx_phc_adjtime,
1430 .gettime64 = efx_phc_gettime,
1431 .settime64 = efx_phc_settime,
1432 .enable = efx_phc_enable,
1435 /* Initialise PTP state. */
1436 int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
1438 struct efx_ptp_data *ptp;
1442 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1443 efx->ptp_data = ptp;
1448 ptp->channel = channel;
1449 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
1451 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
1455 skb_queue_head_init(&ptp->rxq);
1456 skb_queue_head_init(&ptp->txq);
1457 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1463 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1464 ptp->xmit_skb = efx_ptp_xmit_skb_queue;
1465 /* Request sync events on this channel. */
1466 channel->sync_events_state = SYNC_EVENTS_QUIESCENT;
1468 ptp->xmit_skb = efx_ptp_xmit_skb_mc;
1471 INIT_WORK(&ptp->work, efx_ptp_worker);
1472 ptp->config.flags = 0;
1473 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1474 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1475 INIT_LIST_HEAD(&ptp->evt_list);
1476 INIT_LIST_HEAD(&ptp->evt_free_list);
1477 spin_lock_init(&ptp->evt_lock);
1478 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1479 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1481 /* Get the NIC PTP attributes and set up time conversions */
1482 rc = efx_ptp_get_attributes(efx);
1486 /* Get the timestamp corrections */
1487 rc = efx_ptp_get_timestamp_corrections(efx);
1491 if (efx->mcdi->fn_flags &
1492 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1493 ptp->phc_clock_info = efx_phc_clock_info;
1494 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1495 &efx->pci_dev->dev);
1496 if (IS_ERR(ptp->phc_clock)) {
1497 rc = PTR_ERR(ptp->phc_clock);
1499 } else if (ptp->phc_clock) {
1500 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1501 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1502 if (!ptp->pps_workwq) {
1508 ptp->nic_ts_enabled = false;
1512 ptp_clock_unregister(efx->ptp_data->phc_clock);
1515 destroy_workqueue(efx->ptp_data->workwq);
1518 efx_nic_free_buffer(efx, &ptp->start);
1521 kfree(efx->ptp_data);
1522 efx->ptp_data = NULL;
1527 /* Initialise PTP channel.
1529 * Setting core_index to zero causes the queue to be initialised and doesn't
1530 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
1532 static int efx_ptp_probe_channel(struct efx_channel *channel)
1534 struct efx_nic *efx = channel->efx;
1537 channel->irq_moderation_us = 0;
1538 channel->rx_queue.core_index = 0;
1540 rc = efx_ptp_probe(efx, channel);
1541 /* Failure to probe PTP is not fatal; this channel will just not be
1542 * used for anything.
1543 * In the case of EPERM, efx_ptp_probe will print its own message (in
1544 * efx_ptp_get_attributes()), so we don't need to.
1546 if (rc && rc != -EPERM)
1547 netif_warn(efx, drv, efx->net_dev,
1548 "Failed to probe PTP, rc=%d\n", rc);
1552 void efx_ptp_remove(struct efx_nic *efx)
1557 (void)efx_ptp_disable(efx);
1559 cancel_work_sync(&efx->ptp_data->work);
1560 if (efx->ptp_data->pps_workwq)
1561 cancel_work_sync(&efx->ptp_data->pps_work);
1563 skb_queue_purge(&efx->ptp_data->rxq);
1564 skb_queue_purge(&efx->ptp_data->txq);
1566 if (efx->ptp_data->phc_clock) {
1567 destroy_workqueue(efx->ptp_data->pps_workwq);
1568 ptp_clock_unregister(efx->ptp_data->phc_clock);
1571 destroy_workqueue(efx->ptp_data->workwq);
1573 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1574 kfree(efx->ptp_data);
1575 efx->ptp_data = NULL;
1578 static void efx_ptp_remove_channel(struct efx_channel *channel)
1580 efx_ptp_remove(channel->efx);
1583 static void efx_ptp_get_channel_name(struct efx_channel *channel,
1584 char *buf, size_t len)
1586 snprintf(buf, len, "%s-ptp", channel->efx->name);
1589 /* Determine whether this packet should be processed by the PTP module
1590 * or transmitted conventionally.
1592 bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1594 return efx->ptp_data &&
1595 efx->ptp_data->enabled &&
1596 skb->len >= PTP_MIN_LENGTH &&
1597 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1598 likely(skb->protocol == htons(ETH_P_IP)) &&
1599 skb_transport_header_was_set(skb) &&
1600 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
1601 ip_hdr(skb)->protocol == IPPROTO_UDP &&
1603 skb_transport_offset(skb) + sizeof(struct udphdr) &&
1604 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1607 /* Receive a PTP packet. Packets are queued until the arrival of
1608 * the receive timestamp from the MC - this will probably occur after the
1609 * packet arrival because of the processing in the MC.
1611 static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
1613 struct efx_nic *efx = channel->efx;
1614 struct efx_ptp_data *ptp = efx->ptp_data;
1615 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
1616 u8 *match_data_012, *match_data_345;
1617 unsigned int version;
1620 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1622 /* Correct version? */
1623 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
1624 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
1628 version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
1629 if (version != PTP_VERSION_V1) {
1633 /* PTP V1 uses all six bytes of the UUID to match the packet
1636 match_data_012 = data + PTP_V1_UUID_OFFSET;
1637 match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
1639 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
1643 version = data[PTP_V2_VERSION_OFFSET];
1644 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
1648 /* The original V2 implementation uses bytes 2-7 of
1649 * the UUID to match the packet to the timestamp. This
1650 * discards two of the bytes of the MAC address used
1651 * to create the UUID (SF bug 33070). The PTP V2
1652 * enhanced mode fixes this issue and uses bytes 0-2
1653 * and byte 5-7 of the UUID.
1655 match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
1656 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1657 match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
1659 match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
1660 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1664 /* Does this packet require timestamping? */
1665 if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1666 match->state = PTP_PACKET_STATE_UNMATCHED;
1668 /* We expect the sequence number to be in the same position in
1669 * the packet for PTP V1 and V2
1671 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1672 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1674 /* Extract UUID/Sequence information */
1675 match->words[0] = (match_data_012[0] |
1676 (match_data_012[1] << 8) |
1677 (match_data_012[2] << 16) |
1678 (match_data_345[0] << 24));
1679 match->words[1] = (match_data_345[1] |
1680 (match_data_345[2] << 8) |
1681 (data[PTP_V1_SEQUENCE_OFFSET +
1682 PTP_V1_SEQUENCE_LENGTH - 1] <<
1685 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1688 skb_queue_tail(&ptp->rxq, skb);
1689 queue_work(ptp->workwq, &ptp->work);
1694 /* Transmit a PTP packet. This has to be transmitted by the MC
1695 * itself, through an MCDI call. MCDI calls aren't permitted
1696 * in the transmit path so defer the actual transmission to a suitable worker.
1698 int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1700 struct efx_ptp_data *ptp = efx->ptp_data;
1702 skb_queue_tail(&ptp->txq, skb);
1704 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1705 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1706 efx_xmit_hwtstamp_pending(skb);
1707 queue_work(ptp->workwq, &ptp->work);
1709 return NETDEV_TX_OK;
1712 int efx_ptp_get_mode(struct efx_nic *efx)
1714 return efx->ptp_data->mode;
1717 int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1718 unsigned int new_mode)
1720 if ((enable_wanted != efx->ptp_data->enabled) ||
1721 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1724 if (enable_wanted) {
1725 /* Change of mode requires disable */
1726 if (efx->ptp_data->enabled &&
1727 (efx->ptp_data->mode != new_mode)) {
1728 efx->ptp_data->enabled = false;
1729 rc = efx_ptp_stop(efx);
1734 /* Set new operating mode and establish
1735 * baseline synchronisation, which must
1738 efx->ptp_data->mode = new_mode;
1739 if (netif_running(efx->net_dev))
1740 rc = efx_ptp_start(efx);
1742 rc = efx_ptp_synchronize(efx,
1743 PTP_SYNC_ATTEMPTS * 2);
1748 rc = efx_ptp_stop(efx);
1754 efx->ptp_data->enabled = enable_wanted;
1760 static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1767 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1768 (init->tx_type != HWTSTAMP_TX_ON))
1771 rc = efx->type->ptp_set_ts_config(efx, init);
1775 efx->ptp_data->config = *init;
1779 void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
1781 struct efx_ptp_data *ptp = efx->ptp_data;
1782 struct efx_nic *primary = efx->primary;
1789 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1790 SOF_TIMESTAMPING_RX_HARDWARE |
1791 SOF_TIMESTAMPING_RAW_HARDWARE);
1792 /* Check licensed features. If we don't have the license for TX
1793 * timestamps, the NIC will not support them.
1795 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1796 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1798 if (!(nic_data->licensed_features &
1799 (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN)))
1800 ts_info->so_timestamping &=
1801 ~SOF_TIMESTAMPING_TX_HARDWARE;
1803 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1804 ts_info->phc_index =
1805 ptp_clock_index(primary->ptp_data->phc_clock);
1806 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1807 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
1810 int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1812 struct hwtstamp_config config;
1815 /* Not a PTP enabled port */
1819 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1822 rc = efx_ptp_ts_init(efx, &config);
1826 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1830 int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1835 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1836 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1839 static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1841 struct efx_ptp_data *ptp = efx->ptp_data;
1843 netif_err(efx, hw, efx->net_dev,
1844 "PTP unexpected event length: got %d expected %d\n",
1845 ptp->evt_frag_idx, expected_frag_len);
1846 ptp->reset_required = true;
1847 queue_work(ptp->workwq, &ptp->work);
1850 /* Process a completed receive event. Put it on the event queue and
1851 * start worker thread. This is required because event and their
1852 * correspoding packets may come in either order.
1854 static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1856 struct efx_ptp_event_rx *evt = NULL;
1858 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1861 if (ptp->evt_frag_idx != 3) {
1862 ptp_event_failure(efx, 3);
1866 spin_lock_bh(&ptp->evt_lock);
1867 if (!list_empty(&ptp->evt_free_list)) {
1868 evt = list_first_entry(&ptp->evt_free_list,
1869 struct efx_ptp_event_rx, link);
1870 list_del(&evt->link);
1872 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1873 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1875 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1876 MCDI_EVENT_SRC) << 8) |
1877 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1878 MCDI_EVENT_SRC) << 16));
1879 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
1880 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1881 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1882 ptp->ts_corrections.ptp_rx);
1883 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1884 list_add_tail(&evt->link, &ptp->evt_list);
1886 queue_work(ptp->workwq, &ptp->work);
1887 } else if (net_ratelimit()) {
1888 /* Log a rate-limited warning message. */
1889 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1891 spin_unlock_bh(&ptp->evt_lock);
1894 static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1896 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1897 if (ptp->evt_frag_idx != 1) {
1898 ptp_event_failure(efx, 1);
1902 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1905 static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1907 if (ptp->nic_ts_enabled)
1908 queue_work(ptp->pps_workwq, &ptp->pps_work);
1911 void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1913 struct efx_ptp_data *ptp = efx->ptp_data;
1914 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1917 if (!efx->ptp_warned) {
1918 netif_warn(efx, drv, efx->net_dev,
1919 "Received PTP event but PTP not set up\n");
1920 efx->ptp_warned = true;
1928 if (ptp->evt_frag_idx == 0) {
1929 ptp->evt_code = code;
1930 } else if (ptp->evt_code != code) {
1931 netif_err(efx, hw, efx->net_dev,
1932 "PTP out of sequence event %d\n", code);
1933 ptp->evt_frag_idx = 0;
1936 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1937 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1938 /* Process resulting event */
1940 case MCDI_EVENT_CODE_PTP_RX:
1941 ptp_event_rx(efx, ptp);
1943 case MCDI_EVENT_CODE_PTP_FAULT:
1944 ptp_event_fault(efx, ptp);
1946 case MCDI_EVENT_CODE_PTP_PPS:
1947 ptp_event_pps(efx, ptp);
1950 netif_err(efx, hw, efx->net_dev,
1951 "PTP unknown event %d\n", code);
1954 ptp->evt_frag_idx = 0;
1955 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1956 netif_err(efx, hw, efx->net_dev,
1957 "PTP too many event fragments\n");
1958 ptp->evt_frag_idx = 0;
1962 void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1964 struct efx_nic *efx = channel->efx;
1965 struct efx_ptp_data *ptp = efx->ptp_data;
1967 /* When extracting the sync timestamp minor value, we should discard
1968 * the least significant two bits. These are not required in order
1969 * to reconstruct full-range timestamps and they are optionally used
1970 * to report status depending on the options supplied when subscribing
1973 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
1974 channel->sync_timestamp_minor =
1975 (MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC)
1976 << ptp->nic_time.sync_event_minor_shift;
1978 /* if sync events have been disabled then we want to silently ignore
1979 * this event, so throw away result.
1981 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
1985 static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
1987 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1988 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
1990 const u8 *data = eh + efx->rx_packet_ts_offset;
1991 return (u32)data[0] |
1993 (u32)data[2] << 16 |
1998 void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
1999 struct sk_buff *skb)
2001 struct efx_nic *efx = channel->efx;
2002 struct efx_ptp_data *ptp = efx->ptp_data;
2003 u32 pkt_timestamp_major, pkt_timestamp_minor;
2005 struct skb_shared_hwtstamps *timestamps;
2007 if (channel->sync_events_state != SYNC_EVENTS_VALID)
2010 pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb));
2012 /* get the difference between the packet and sync timestamps,
2015 diff = pkt_timestamp_minor - channel->sync_timestamp_minor;
2016 if (pkt_timestamp_minor < channel->sync_timestamp_minor)
2017 diff += ptp->nic_time.minor_max;
2019 /* do we roll over a second boundary and need to carry the one? */
2020 carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ?
2023 if (diff <= ptp->nic_time.sync_event_diff_max) {
2024 /* packet is ahead of the sync event by a quarter of a second or
2025 * less (allowing for fuzz)
2027 pkt_timestamp_major = channel->sync_timestamp_major + carry;
2028 } else if (diff >= ptp->nic_time.sync_event_diff_min) {
2029 /* packet is behind the sync event but within the fuzz factor.
2030 * This means the RX packet and sync event crossed as they were
2031 * placed on the event queue, which can sometimes happen.
2033 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
2035 /* it's outside tolerance in both directions. this might be
2036 * indicative of us missing sync events for some reason, so
2037 * we'll call it an error rather than risk giving a bogus
2040 netif_vdbg(efx, drv, efx->net_dev,
2041 "packet timestamp %x too far from sync event %x:%x\n",
2042 pkt_timestamp_minor, channel->sync_timestamp_major,
2043 channel->sync_timestamp_minor);
2047 /* attach the timestamps to the skb */
2048 timestamps = skb_hwtstamps(skb);
2049 timestamps->hwtstamp =
2050 ptp->nic_to_kernel_time(pkt_timestamp_major,
2051 pkt_timestamp_minor,
2052 ptp->ts_corrections.general_rx);
2055 static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
2057 struct efx_ptp_data *ptp_data = container_of(ptp,
2058 struct efx_ptp_data,
2060 struct efx_nic *efx = ptp_data->efx;
2061 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
2065 if (delta > MAX_PPB)
2067 else if (delta < -MAX_PPB)
2070 /* Convert ppb to fixed point ns taking care to round correctly. */
2071 adjustment_ns = ((s64)delta * PPB_SCALE_WORD +
2072 (1 << (ptp_data->adjfreq_ppb_shift - 1))) >>
2073 ptp_data->adjfreq_ppb_shift;
2075 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2076 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
2077 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
2078 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
2079 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
2080 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
2085 ptp_data->current_adjfreq = adjustment_ns;
2089 static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
2091 u32 nic_major, nic_minor;
2092 struct efx_ptp_data *ptp_data = container_of(ptp,
2093 struct efx_ptp_data,
2095 struct efx_nic *efx = ptp_data->efx;
2096 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
2098 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
2100 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2101 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2102 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
2103 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
2104 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
2105 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2109 static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
2111 struct efx_ptp_data *ptp_data = container_of(ptp,
2112 struct efx_ptp_data,
2114 struct efx_nic *efx = ptp_data->efx;
2115 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
2116 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
2120 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
2121 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2123 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2124 outbuf, sizeof(outbuf), NULL);
2128 kt = ptp_data->nic_to_kernel_time(
2129 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
2130 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
2131 *ts = ktime_to_timespec64(kt);
2135 static int efx_phc_settime(struct ptp_clock_info *ptp,
2136 const struct timespec64 *e_ts)
2138 /* Get the current NIC time, efx_phc_gettime.
2139 * Subtract from the desired time to get the offset
2140 * call efx_phc_adjtime with the offset
2143 struct timespec64 time_now;
2144 struct timespec64 delta;
2146 rc = efx_phc_gettime(ptp, &time_now);
2150 delta = timespec64_sub(*e_ts, time_now);
2152 rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
2159 static int efx_phc_enable(struct ptp_clock_info *ptp,
2160 struct ptp_clock_request *request,
2163 struct efx_ptp_data *ptp_data = container_of(ptp,
2164 struct efx_ptp_data,
2166 if (request->type != PTP_CLK_REQ_PPS)
2169 ptp_data->nic_ts_enabled = !!enable;
2173 static const struct efx_channel_type efx_ptp_channel_type = {
2174 .handle_no_channel = efx_ptp_handle_no_channel,
2175 .pre_probe = efx_ptp_probe_channel,
2176 .post_remove = efx_ptp_remove_channel,
2177 .get_name = efx_ptp_get_channel_name,
2178 /* no copy operation; there is no need to reallocate this channel */
2179 .receive_skb = efx_ptp_rx,
2180 .want_txqs = efx_ptp_want_txqs,
2181 .keep_eventq = false,
2184 void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
2186 /* Check whether PTP is implemented on this NIC. The DISABLE
2187 * operation will succeed if and only if it is implemented.
2189 if (efx_ptp_disable(efx) == 0)
2190 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
2191 &efx_ptp_channel_type;
2194 void efx_ptp_start_datapath(struct efx_nic *efx)
2196 if (efx_ptp_restart(efx))
2197 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
2198 /* re-enable timestamping if it was previously enabled */
2199 if (efx->type->ptp_set_ts_sync_events)
2200 efx->type->ptp_set_ts_sync_events(efx, true, true);
2203 void efx_ptp_stop_datapath(struct efx_nic *efx)
2205 /* temporarily disable timestamping */
2206 if (efx->type->ptp_set_ts_sync_events)
2207 efx->type->ptp_set_ts_sync_events(efx, false, true);