1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/ip.h>
34#include <linux/udp.h>
35#include <linux/time.h>
36#include <linux/ktime.h>
37#include <linux/module.h>
38#include <linux/net_tstamp.h>
39#include <linux/pps_kernel.h>
40#include <linux/ptp_clock_kernel.h>
41#include "net_driver.h"
42#include "efx.h"
43#include "mcdi.h"
44#include "mcdi_pcol.h"
45#include "io.h"
46#include "farch_regs.h"
47#include "nic.h"
48
49
50#define MAX_EVENT_FRAGS 3
51
52
53#define MAX_SYNCHRONISE_WAIT_MS 2
54
55
56#define SYNCHRONISE_PERIOD_NS 250000
57
58
59#define SYNCHRONISATION_GRANULARITY_NS 200
60
61
62#define DEFAULT_MIN_SYNCHRONISATION_NS 120
63
64
65#define MAX_SYNCHRONISATION_NS 1000
66
67
68#define MAX_RECEIVE_EVENTS 8
69
70
71#define AVERAGE_LENGTH 16
72
73
74#define PKT_EVENT_LIFETIME_MS 10
75
76
77
78
79
80#define PTP_DPORT_OFFSET 22
81
82#define PTP_V1_VERSION_LENGTH 2
83#define PTP_V1_VERSION_OFFSET 28
84
85#define PTP_V1_UUID_LENGTH 6
86#define PTP_V1_UUID_OFFSET 50
87
88#define PTP_V1_SEQUENCE_LENGTH 2
89#define PTP_V1_SEQUENCE_OFFSET 58
90
91
92
93
94#define PTP_V1_MIN_LENGTH 64
95
96#define PTP_V2_VERSION_LENGTH 1
97#define PTP_V2_VERSION_OFFSET 29
98
99#define PTP_V2_UUID_LENGTH 8
100#define PTP_V2_UUID_OFFSET 48
101
102
103
104
105
106
107#define PTP_V2_MC_UUID_LENGTH 6
108#define PTP_V2_MC_UUID_OFFSET 50
109
110#define PTP_V2_SEQUENCE_LENGTH 2
111#define PTP_V2_SEQUENCE_OFFSET 58
112
113
114
115
116#define PTP_V2_MIN_LENGTH 63
117
118#define PTP_MIN_LENGTH 63
119
120#define PTP_ADDRESS 0xe0000181
121#define PTP_EVENT_PORT 319
122#define PTP_GENERAL_PORT 320
123
124
125
126
127#define PTP_VERSION_V1 1
128
129#define PTP_VERSION_V2 2
130#define PTP_VERSION_V2_MASK 0x0f
131
132enum 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
137};
138
139
140
141
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)
145
146
147#define MAX_PPB 1000000
148
149
150
151#define PPB_SCALE_WORD ((1LL << (57)) / 1953125LL)
152
153
154#define PPB_SHIFT_FP40 26
155
156#define PPB_SHIFT_FP44 22
157
158#define PTP_SYNC_ATTEMPTS 4
159
160
161
162
163
164
165
166
167
168struct efx_ptp_match {
169 u32 words[DIV_ROUND_UP(PTP_V1_UUID_LENGTH, 4)];
170 unsigned long expiry;
171 enum ptp_packet_state state;
172};
173
174
175
176
177
178
179
180struct efx_ptp_event_rx {
181 struct list_head link;
182 u32 seq0;
183 u32 seq1;
184 ktime_t hwtimestamp;
185 unsigned long expiry;
186};
187
188
189
190
191
192
193
194
195
196
197
198
199struct efx_ptp_timeset {
200 u32 host_start;
201 u32 major;
202 u32 minor;
203 u32 host_end;
204 u32 wait;
205 u32 window;
206};
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279struct efx_ptp_data {
280 struct efx_nic *efx;
281 struct efx_channel *channel;
282 bool rx_ts_inline;
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;
287 spinlock_t evt_lock;
288 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
289 struct workqueue_struct *workwq;
290 struct work_struct work;
291 bool reset_required;
292 u32 rxfilter_event;
293 u32 rxfilter_general;
294 bool rxfilter_installed;
295 struct hwtstamp_config config;
296 bool enabled;
297 unsigned int mode;
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,
300 s32 correction);
301 struct {
302 u32 minor_max;
303 u32 sync_event_diff_min;
304 u32 sync_event_diff_max;
305 unsigned int sync_event_minor_shift;
306 } nic_time;
307 unsigned int min_synchronisation_ns;
308 unsigned int capabilities;
309 struct {
310 s32 ptp_tx;
311 s32 ptp_rx;
312 s32 pps_out;
313 s32 pps_in;
314 s32 general_tx;
315 s32 general_rx;
316 } ts_corrections;
317 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
318 int evt_frag_idx;
319 int evt_code;
320 struct efx_buffer start;
321 struct pps_event_time host_time_pps;
322 unsigned int adjfreq_ppb_shift;
323 s64 current_adjfreq;
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;
328 bool nic_ts_enabled;
329 _MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
330
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);
343};
344
345static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
346static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
347static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
348static int efx_phc_settime(struct ptp_clock_info *ptp,
349 const struct timespec64 *e_ts);
350static int efx_phc_enable(struct ptp_clock_info *ptp,
351 struct ptp_clock_request *request, int on);
352
353bool efx_ptp_use_mac_tx_timestamps(struct efx_nic *efx)
354{
355 return efx_has_cap(efx, TX_MAC_TIMESTAMPING, FLAGS2);
356}
357
358
359
360
361static bool efx_ptp_want_txqs(struct efx_channel *channel)
362{
363 return efx_ptp_use_mac_tx_timestamps(channel->efx);
364}
365
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 }
370static 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),
385};
386#define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
387static const unsigned long efx_ptp_stat_mask[] = {
388 [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
389};
390
391size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
392{
393 if (!efx->ptp_data)
394 return 0;
395
396 return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
397 efx_ptp_stat_mask, strings);
398}
399
400size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
401{
402 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
403 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
404 size_t i;
405 int rc;
406
407 if (!efx->ptp_data)
408 return 0;
409
410
411 for (i = 0; i < PTP_STAT_COUNT; i++) {
412 if (efx_ptp_stat_desc[i].dma_width)
413 continue;
414 stats[i] = *(unsigned int *)((char *)efx->ptp_data +
415 efx_ptp_stat_desc[i].offset);
416 }
417
418
419
420
421
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);
426 if (rc)
427 memset(outbuf, 0, sizeof(outbuf));
428 efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
429 efx_ptp_stat_mask,
430 stats, _MCDI_PTR(outbuf, 0), false);
431
432 return PTP_STAT_COUNT;
433}
434
435
436static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
437{
438 struct timespec64 ts = ns_to_timespec64(ns);
439 *nic_major = (u32)ts.tv_sec;
440 *nic_minor = ts.tv_nsec;
441}
442
443static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
444 s32 correction)
445{
446 ktime_t kt = ktime_set(nic_major, nic_minor);
447 if (correction >= 0)
448 kt = ktime_add_ns(kt, (u64)correction);
449 else
450 kt = ktime_sub_ns(kt, (u64)-correction);
451 return kt;
452}
453
454
455
456
457
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)
462
463
464
465
466static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
467{
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);
472
473
474
475
476 if (min >= S27_MINOR_MAX) {
477 min -= S27_MINOR_MAX;
478 maj++;
479 }
480
481 *nic_major = maj;
482 *nic_minor = min;
483}
484
485static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
486{
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);
490}
491
492static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
493 s32 correction)
494{
495
496 nic_minor += correction;
497 if ((s32)nic_minor < 0) {
498 nic_minor += S27_MINOR_MAX;
499 nic_major--;
500 } else if (nic_minor >= S27_MINOR_MAX) {
501 nic_minor -= S27_MINOR_MAX;
502 nic_major++;
503 }
504
505 return efx_ptp_s27_to_ktime(nic_major, nic_minor);
506}
507
508
509static void efx_ptp_ns_to_s_qns(s64 ns, u32 *nic_major, u32 *nic_minor)
510{
511 struct timespec64 ts = ns_to_timespec64(ns);
512
513 *nic_major = (u32)ts.tv_sec;
514 *nic_minor = ts.tv_nsec * 4;
515}
516
517static ktime_t efx_ptp_s_qns_to_ktime_correction(u32 nic_major, u32 nic_minor,
518 s32 correction)
519{
520 ktime_t kt;
521
522 nic_minor = DIV_ROUND_CLOSEST(nic_minor, 4);
523 correction = DIV_ROUND_CLOSEST(correction, 4);
524
525 kt = ktime_set(nic_major, nic_minor);
526
527 if (correction >= 0)
528 kt = ktime_add_ns(kt, (u64)correction);
529 else
530 kt = ktime_sub_ns(kt, (u64)-correction);
531 return kt;
532}
533
534struct efx_channel *efx_ptp_channel(struct efx_nic *efx)
535{
536 return efx->ptp_data ? efx->ptp_data->channel : NULL;
537}
538
539static u32 last_sync_timestamp_major(struct efx_nic *efx)
540{
541 struct efx_channel *channel = efx_ptp_channel(efx);
542 u32 major = 0;
543
544 if (channel)
545 major = channel->sync_timestamp_major;
546 return major;
547}
548
549
550
551
552static ktime_t
553efx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx,
554 struct efx_ptp_data *ptp,
555 u32 nic_major, u32 nic_minor,
556 s32 correction)
557{
558 u32 sync_timestamp;
559 ktime_t kt = { 0 };
560 s16 delta;
561
562 if (!(nic_major & 0x80000000)) {
563 WARN_ON_ONCE(nic_major >> 16);
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583 sync_timestamp = last_sync_timestamp_major(efx);
584
585
586
587
588
589
590
591 delta = nic_major - sync_timestamp;
592
593
594
595
596 nic_major = sync_timestamp + delta;
597
598 kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
599 correction);
600 }
601 return kt;
602}
603
604ktime_t efx_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue)
605{
606 struct efx_nic *efx = tx_queue->efx;
607 struct efx_ptp_data *ptp = efx->ptp_data;
608 ktime_t kt;
609
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);
615 else
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);
620 return kt;
621}
622
623
624static int efx_ptp_get_attributes(struct efx_nic *efx)
625{
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;
629 int rc;
630 u32 fmt;
631 size_t out_len;
632
633
634
635
636
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);
641 if (rc == 0) {
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");
647 return rc;
648 } else {
649 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf),
650 outbuf, sizeof(outbuf), rc);
651 return rc;
652 }
653
654 switch (fmt) {
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;
660 break;
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;
666 break;
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;
672 break;
673 default:
674 return -ERANGE;
675 }
676
677
678
679
680
681
682
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);
687
688
689
690
691
692
693
694
695 if (rc == 0 &&
696 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST)
697 ptp->min_synchronisation_ns =
698 MCDI_DWORD(outbuf,
699 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
700 else
701 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
702
703 if (rc == 0 &&
704 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
705 ptp->capabilities = MCDI_DWORD(outbuf,
706 PTP_OUT_GET_ATTRIBUTES_CAPABILITIES);
707 else
708 ptp->capabilities = 0;
709
710
711
712
713
714 if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN))
715 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44;
716 else
717 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40;
718
719 return 0;
720}
721
722
723static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
724{
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);
727 int rc;
728 size_t out_len;
729
730
731
732
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);
736
737 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
738 outbuf, sizeof(outbuf), &out_len);
739 if (rc == 0) {
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);
748
749 if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) {
750 efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD(
751 outbuf,
752 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX);
753 efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD(
754 outbuf,
755 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX);
756 } else {
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;
761 }
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;
769 } else {
770 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), outbuf,
771 sizeof(outbuf), rc);
772 return rc;
773 }
774
775 return 0;
776}
777
778
779static int efx_ptp_enable(struct efx_nic *efx)
780{
781 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
782 MCDI_DECLARE_BUF_ERR(outbuf);
783 int rc;
784
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);
791
792 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
793 outbuf, sizeof(outbuf), NULL);
794 rc = (rc == -EALREADY) ? 0 : rc;
795 if (rc)
796 efx_mcdi_display_error(efx, MC_CMD_PTP,
797 MC_CMD_PTP_IN_ENABLE_LEN,
798 outbuf, sizeof(outbuf), rc);
799 return rc;
800}
801
802
803
804
805
806
807static int efx_ptp_disable(struct efx_nic *efx)
808{
809 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
810 MCDI_DECLARE_BUF_ERR(outbuf);
811 int rc;
812
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
819
820
821 if (rc == -ENOSYS || rc == -EPERM)
822 netif_info(efx, probe, efx->net_dev, "no PTP support\n");
823 else if (rc)
824 efx_mcdi_display_error(efx, MC_CMD_PTP,
825 MC_CMD_PTP_IN_DISABLE_LEN,
826 outbuf, sizeof(outbuf), rc);
827 return rc;
828}
829
830static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
831{
832 struct sk_buff *skb;
833
834 while ((skb = skb_dequeue(q))) {
835 local_bh_disable();
836 netif_receive_skb(skb);
837 local_bh_enable();
838 }
839}
840
841static void efx_ptp_handle_no_channel(struct efx_nic *efx)
842{
843 netif_err(efx, drv, efx->net_dev,
844 "ERROR: PTP requires MSI-X and 1 additional interrupt"
845 "vector. PTP disabled\n");
846}
847
848
849
850
851static void efx_ptp_send_times(struct efx_nic *efx,
852 struct pps_event_time *last_time)
853{
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;
858
859 pps_get_ts(&now);
860 limit = now.ts_real;
861 timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
862
863
864 while ((timespec64_compare(&now.ts_real, &limit) < 0) &&
865 READ_ONCE(*mc_running)) {
866 struct timespec64 update_time;
867 unsigned int host_time;
868
869
870 update_time = now.ts_real;
871 timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
872 do {
873 pps_get_ts(&now);
874 } while ((timespec64_compare(&now.ts_real, &update_time) < 0) &&
875 READ_ONCE(*mc_running));
876
877
878 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
879 now.ts_real.tv_nsec);
880
881 efx->type->ptp_write_host_time(efx, host_time);
882 }
883 *last_time = now;
884}
885
886
887static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
888 struct efx_ptp_timeset *timeset)
889{
890 unsigned start_ns, end_ns;
891
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);
897
898
899 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
900 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
901
902 if (end_ns < start_ns)
903 end_ns += NSEC_PER_SEC;
904
905 timeset->window = end_ns - start_ns;
906}
907
908
909
910
911
912
913
914
915
916static int
917efx_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)
920{
921 unsigned number_readings =
922 MCDI_VAR_ARRAY_LEN(response_length,
923 PTP_OUT_SYNCHRONIZE_TIMESET);
924 unsigned i;
925 unsigned ngood = 0;
926 unsigned last_good = 0;
927 struct efx_ptp_data *ptp = efx->ptp_data;
928 u32 last_sec;
929 u32 start_sec;
930 struct timespec64 delta;
931 ktime_t mc_time;
932
933 if (number_readings == 0)
934 return -EAGAIN;
935
936
937
938
939
940
941
942 for (i = 0; i < number_readings; i++) {
943 s32 window, corrected;
944 struct timespec64 wait;
945
946 efx_ptp_read_timeset(
947 MCDI_ARRAY_STRUCT_PTR(synch_buf,
948 PTP_OUT_SYNCHRONIZE_TIMESET, i),
949 &ptp->timeset[i]);
950
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;
955
956
957
958
959
960
961
962
963
964
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;
971 } else {
972 ngood++;
973 last_good = i;
974 }
975 }
976
977 if (ngood == 0) {
978 netif_warn(efx, drv, efx->net_dev,
979 "PTP no suitable synchronisations\n");
980 return -EAGAIN;
981 }
982
983
984
985
986
987
988
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");
995 return -EAGAIN;
996 }
997 delta.tv_sec = (last_sec - start_sec) & 1;
998 delta.tv_nsec =
999 last_time->ts_real.tv_nsec -
1000 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
1001
1002
1003
1004
1005
1006 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
1007 ptp->timeset[last_good].minor, 0);
1008
1009
1010 delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec;
1011
1012
1013 ptp->host_time_pps = *last_time;
1014 pps_sub_ts(&ptp->host_time_pps, delta);
1015
1016 return 0;
1017}
1018
1019
1020static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
1021{
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;
1025 int rc;
1026 unsigned long timeout;
1027 struct pps_event_time last_time = {};
1028 unsigned int loops = 0;
1029 int *start = ptp->start.addr;
1030
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,
1034 num_readings);
1035 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
1036 ptp->start.dma_addr);
1037
1038
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);
1043
1044
1045 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
1046 while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) {
1047 udelay(20);
1048 loops++;
1049 }
1050
1051 if (loops <= 1)
1052 ++ptp->fast_syncs;
1053 if (!time_before(jiffies, timeout))
1054 ++ptp->sync_timeouts;
1055
1056 if (READ_ONCE(*start))
1057 efx_ptp_send_times(efx, &last_time);
1058
1059
1060 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
1061 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
1062 synch_buf, sizeof(synch_buf),
1063 &response_length);
1064 if (rc == 0) {
1065 rc = efx_ptp_process_times(efx, synch_buf, response_length,
1066 &last_time);
1067 if (rc == 0)
1068 ++ptp->good_syncs;
1069 else
1070 ++ptp->no_time_syncs;
1071 }
1072
1073
1074
1075
1076 if (rc != 0)
1077 ++ptp->bad_syncs;
1078
1079 return rc;
1080}
1081
1082
1083static void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb)
1084{
1085 struct efx_ptp_data *ptp_data = efx->ptp_data;
1086 struct efx_tx_queue *tx_queue;
1087 u8 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
1088
1089 tx_queue = &ptp_data->channel->tx_queue[type];
1090 if (tx_queue && tx_queue->timestamping) {
1091 efx_enqueue_skb(tx_queue, skb);
1092 } else {
1093 WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
1094 dev_kfree_skb_any(skb);
1095 }
1096}
1097
1098
1099static void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb)
1100{
1101 struct efx_ptp_data *ptp_data = efx->ptp_data;
1102 struct skb_shared_hwtstamps timestamps;
1103 int rc = -EIO;
1104 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
1105 size_t len;
1106
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);
1112 if (rc != 0)
1113 goto fail;
1114 }
1115
1116 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1117 rc = skb_checksum_help(skb);
1118 if (rc != 0)
1119 goto fail;
1120 }
1121 skb_copy_from_linear_data(skb,
1122 MCDI_PTR(ptp_data->txbuf,
1123 PTP_IN_TRANSMIT_PACKET),
1124 skb->len);
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);
1128 if (rc != 0)
1129 goto fail;
1130
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);
1136
1137 skb_tstamp_tx(skb, ×tamps);
1138
1139 rc = 0;
1140
1141fail:
1142 dev_kfree_skb_any(skb);
1143
1144 return;
1145}
1146
1147static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
1148{
1149 struct efx_ptp_data *ptp = efx->ptp_data;
1150 struct list_head *cursor;
1151 struct list_head *next;
1152
1153 if (ptp->rx_ts_inline)
1154 return;
1155
1156
1157 spin_lock_bh(&ptp->evt_lock);
1158 if (!list_empty(&ptp->evt_list)) {
1159 list_for_each_safe(cursor, next, &ptp->evt_list) {
1160 struct efx_ptp_event_rx *evt;
1161
1162 evt = list_entry(cursor, struct efx_ptp_event_rx,
1163 link);
1164 if (time_after(jiffies, evt->expiry)) {
1165 list_move(&evt->link, &ptp->evt_free_list);
1166 netif_warn(efx, hw, efx->net_dev,
1167 "PTP rx event dropped\n");
1168 }
1169 }
1170 }
1171 spin_unlock_bh(&ptp->evt_lock);
1172}
1173
1174static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
1175 struct sk_buff *skb)
1176{
1177 struct efx_ptp_data *ptp = efx->ptp_data;
1178 bool evts_waiting;
1179 struct list_head *cursor;
1180 struct list_head *next;
1181 struct efx_ptp_match *match;
1182 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
1183
1184 WARN_ON_ONCE(ptp->rx_ts_inline);
1185
1186 spin_lock_bh(&ptp->evt_lock);
1187 evts_waiting = !list_empty(&ptp->evt_list);
1188 spin_unlock_bh(&ptp->evt_lock);
1189
1190 if (!evts_waiting)
1191 return PTP_PACKET_STATE_UNMATCHED;
1192
1193 match = (struct efx_ptp_match *)skb->cb;
1194
1195 spin_lock_bh(&ptp->evt_lock);
1196 list_for_each_safe(cursor, next, &ptp->evt_list) {
1197 struct efx_ptp_event_rx *evt;
1198
1199 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
1200 if ((evt->seq0 == match->words[0]) &&
1201 (evt->seq1 == match->words[1])) {
1202 struct skb_shared_hwtstamps *timestamps;
1203
1204
1205 timestamps = skb_hwtstamps(skb);
1206 timestamps->hwtstamp = evt->hwtimestamp;
1207
1208 match->state = PTP_PACKET_STATE_MATCHED;
1209 rc = PTP_PACKET_STATE_MATCHED;
1210 list_move(&evt->link, &ptp->evt_free_list);
1211 break;
1212 }
1213 }
1214 spin_unlock_bh(&ptp->evt_lock);
1215
1216 return rc;
1217}
1218
1219
1220
1221
1222
1223static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
1224{
1225 struct efx_ptp_data *ptp = efx->ptp_data;
1226 struct sk_buff *skb;
1227
1228 while ((skb = skb_dequeue(&ptp->rxq))) {
1229 struct efx_ptp_match *match;
1230
1231 match = (struct efx_ptp_match *)skb->cb;
1232 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
1233 __skb_queue_tail(q, skb);
1234 } else if (efx_ptp_match_rx(efx, skb) ==
1235 PTP_PACKET_STATE_MATCHED) {
1236 __skb_queue_tail(q, skb);
1237 } else if (time_after(jiffies, match->expiry)) {
1238 match->state = PTP_PACKET_STATE_TIMED_OUT;
1239 ++ptp->rx_no_timestamp;
1240 __skb_queue_tail(q, skb);
1241 } else {
1242
1243 skb_queue_head(&ptp->rxq, skb);
1244 break;
1245 }
1246 }
1247}
1248
1249
1250static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
1251{
1252 local_bh_disable();
1253 netif_receive_skb(skb);
1254 local_bh_enable();
1255}
1256
1257static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
1258{
1259 struct efx_ptp_data *ptp = efx->ptp_data;
1260
1261 if (ptp->rxfilter_installed) {
1262 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1263 ptp->rxfilter_general);
1264 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1265 ptp->rxfilter_event);
1266 ptp->rxfilter_installed = false;
1267 }
1268}
1269
1270static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
1271{
1272 struct efx_ptp_data *ptp = efx->ptp_data;
1273 struct efx_filter_spec rxfilter;
1274 int rc;
1275
1276 if (!ptp->channel || ptp->rxfilter_installed)
1277 return 0;
1278
1279
1280
1281
1282 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1283 efx_rx_queue_index(
1284 efx_channel_get_rx_queue(ptp->channel)));
1285 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1286 htonl(PTP_ADDRESS),
1287 htons(PTP_EVENT_PORT));
1288 if (rc != 0)
1289 return rc;
1290
1291 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1292 if (rc < 0)
1293 return rc;
1294 ptp->rxfilter_event = rc;
1295
1296 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1297 efx_rx_queue_index(
1298 efx_channel_get_rx_queue(ptp->channel)));
1299 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1300 htonl(PTP_ADDRESS),
1301 htons(PTP_GENERAL_PORT));
1302 if (rc != 0)
1303 goto fail;
1304
1305 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1306 if (rc < 0)
1307 goto fail;
1308 ptp->rxfilter_general = rc;
1309
1310 ptp->rxfilter_installed = true;
1311 return 0;
1312
1313fail:
1314 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1315 ptp->rxfilter_event);
1316 return rc;
1317}
1318
1319static int efx_ptp_start(struct efx_nic *efx)
1320{
1321 struct efx_ptp_data *ptp = efx->ptp_data;
1322 int rc;
1323
1324 ptp->reset_required = false;
1325
1326 rc = efx_ptp_insert_multicast_filters(efx);
1327 if (rc)
1328 return rc;
1329
1330 rc = efx_ptp_enable(efx);
1331 if (rc != 0)
1332 goto fail;
1333
1334 ptp->evt_frag_idx = 0;
1335 ptp->current_adjfreq = 0;
1336
1337 return 0;
1338
1339fail:
1340 efx_ptp_remove_multicast_filters(efx);
1341 return rc;
1342}
1343
1344static int efx_ptp_stop(struct efx_nic *efx)
1345{
1346 struct efx_ptp_data *ptp = efx->ptp_data;
1347 struct list_head *cursor;
1348 struct list_head *next;
1349 int rc;
1350
1351 if (ptp == NULL)
1352 return 0;
1353
1354 rc = efx_ptp_disable(efx);
1355
1356 efx_ptp_remove_multicast_filters(efx);
1357
1358
1359 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1360 skb_queue_purge(&efx->ptp_data->txq);
1361
1362
1363 spin_lock_bh(&efx->ptp_data->evt_lock);
1364 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
1365 list_move(cursor, &efx->ptp_data->evt_free_list);
1366 }
1367 spin_unlock_bh(&efx->ptp_data->evt_lock);
1368
1369 return rc;
1370}
1371
1372static int efx_ptp_restart(struct efx_nic *efx)
1373{
1374 if (efx->ptp_data && efx->ptp_data->enabled)
1375 return efx_ptp_start(efx);
1376 return 0;
1377}
1378
1379static void efx_ptp_pps_worker(struct work_struct *work)
1380{
1381 struct efx_ptp_data *ptp =
1382 container_of(work, struct efx_ptp_data, pps_work);
1383 struct efx_nic *efx = ptp->efx;
1384 struct ptp_clock_event ptp_evt;
1385
1386 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1387 return;
1388
1389 ptp_evt.type = PTP_CLOCK_PPSUSR;
1390 ptp_evt.pps_times = ptp->host_time_pps;
1391 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1392}
1393
1394static void efx_ptp_worker(struct work_struct *work)
1395{
1396 struct efx_ptp_data *ptp_data =
1397 container_of(work, struct efx_ptp_data, work);
1398 struct efx_nic *efx = ptp_data->efx;
1399 struct sk_buff *skb;
1400 struct sk_buff_head tempq;
1401
1402 if (ptp_data->reset_required) {
1403 efx_ptp_stop(efx);
1404 efx_ptp_start(efx);
1405 return;
1406 }
1407
1408 efx_ptp_drop_time_expired_events(efx);
1409
1410 __skb_queue_head_init(&tempq);
1411 efx_ptp_process_events(efx, &tempq);
1412
1413 while ((skb = skb_dequeue(&ptp_data->txq)))
1414 ptp_data->xmit_skb(efx, skb);
1415
1416 while ((skb = __skb_dequeue(&tempq)))
1417 efx_ptp_process_rx(efx, skb);
1418}
1419
1420static const struct ptp_clock_info efx_phc_clock_info = {
1421 .owner = THIS_MODULE,
1422 .name = "sfc",
1423 .max_adj = MAX_PPB,
1424 .n_alarm = 0,
1425 .n_ext_ts = 0,
1426 .n_per_out = 0,
1427 .n_pins = 0,
1428 .pps = 1,
1429 .adjfreq = efx_phc_adjfreq,
1430 .adjtime = efx_phc_adjtime,
1431 .gettime64 = efx_phc_gettime,
1432 .settime64 = efx_phc_settime,
1433 .enable = efx_phc_enable,
1434};
1435
1436
1437int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
1438{
1439 struct efx_ptp_data *ptp;
1440 int rc = 0;
1441 unsigned int pos;
1442
1443 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1444 efx->ptp_data = ptp;
1445 if (!efx->ptp_data)
1446 return -ENOMEM;
1447
1448 ptp->efx = efx;
1449 ptp->channel = channel;
1450 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
1451
1452 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
1453 if (rc != 0)
1454 goto fail1;
1455
1456 skb_queue_head_init(&ptp->rxq);
1457 skb_queue_head_init(&ptp->txq);
1458 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1459 if (!ptp->workwq) {
1460 rc = -ENOMEM;
1461 goto fail2;
1462 }
1463
1464 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1465 ptp->xmit_skb = efx_ptp_xmit_skb_queue;
1466
1467 channel->sync_events_state = SYNC_EVENTS_QUIESCENT;
1468 } else {
1469 ptp->xmit_skb = efx_ptp_xmit_skb_mc;
1470 }
1471
1472 INIT_WORK(&ptp->work, efx_ptp_worker);
1473 ptp->config.flags = 0;
1474 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1475 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1476 INIT_LIST_HEAD(&ptp->evt_list);
1477 INIT_LIST_HEAD(&ptp->evt_free_list);
1478 spin_lock_init(&ptp->evt_lock);
1479 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1480 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1481
1482
1483 rc = efx_ptp_get_attributes(efx);
1484 if (rc < 0)
1485 goto fail3;
1486
1487
1488 rc = efx_ptp_get_timestamp_corrections(efx);
1489 if (rc < 0)
1490 goto fail3;
1491
1492 if (efx->mcdi->fn_flags &
1493 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1494 ptp->phc_clock_info = efx_phc_clock_info;
1495 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1496 &efx->pci_dev->dev);
1497 if (IS_ERR(ptp->phc_clock)) {
1498 rc = PTR_ERR(ptp->phc_clock);
1499 goto fail3;
1500 } else if (ptp->phc_clock) {
1501 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1502 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1503 if (!ptp->pps_workwq) {
1504 rc = -ENOMEM;
1505 goto fail4;
1506 }
1507 }
1508 }
1509 ptp->nic_ts_enabled = false;
1510
1511 return 0;
1512fail4:
1513 ptp_clock_unregister(efx->ptp_data->phc_clock);
1514
1515fail3:
1516 destroy_workqueue(efx->ptp_data->workwq);
1517
1518fail2:
1519 efx_nic_free_buffer(efx, &ptp->start);
1520
1521fail1:
1522 kfree(efx->ptp_data);
1523 efx->ptp_data = NULL;
1524
1525 return rc;
1526}
1527
1528
1529
1530
1531
1532
1533static int efx_ptp_probe_channel(struct efx_channel *channel)
1534{
1535 struct efx_nic *efx = channel->efx;
1536 int rc;
1537
1538 channel->irq_moderation_us = 0;
1539 channel->rx_queue.core_index = 0;
1540
1541 rc = efx_ptp_probe(efx, channel);
1542
1543
1544
1545
1546
1547 if (rc && rc != -EPERM)
1548 netif_warn(efx, drv, efx->net_dev,
1549 "Failed to probe PTP, rc=%d\n", rc);
1550 return 0;
1551}
1552
1553void efx_ptp_remove(struct efx_nic *efx)
1554{
1555 if (!efx->ptp_data)
1556 return;
1557
1558 (void)efx_ptp_disable(efx);
1559
1560 cancel_work_sync(&efx->ptp_data->work);
1561 if (efx->ptp_data->pps_workwq)
1562 cancel_work_sync(&efx->ptp_data->pps_work);
1563
1564 skb_queue_purge(&efx->ptp_data->rxq);
1565 skb_queue_purge(&efx->ptp_data->txq);
1566
1567 if (efx->ptp_data->phc_clock) {
1568 destroy_workqueue(efx->ptp_data->pps_workwq);
1569 ptp_clock_unregister(efx->ptp_data->phc_clock);
1570 }
1571
1572 destroy_workqueue(efx->ptp_data->workwq);
1573
1574 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1575 kfree(efx->ptp_data);
1576 efx->ptp_data = NULL;
1577}
1578
1579static void efx_ptp_remove_channel(struct efx_channel *channel)
1580{
1581 efx_ptp_remove(channel->efx);
1582}
1583
1584static void efx_ptp_get_channel_name(struct efx_channel *channel,
1585 char *buf, size_t len)
1586{
1587 snprintf(buf, len, "%s-ptp", channel->efx->name);
1588}
1589
1590
1591
1592
1593bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1594{
1595 return efx->ptp_data &&
1596 efx->ptp_data->enabled &&
1597 skb->len >= PTP_MIN_LENGTH &&
1598 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1599 likely(skb->protocol == htons(ETH_P_IP)) &&
1600 skb_transport_header_was_set(skb) &&
1601 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
1602 ip_hdr(skb)->protocol == IPPROTO_UDP &&
1603 skb_headlen(skb) >=
1604 skb_transport_offset(skb) + sizeof(struct udphdr) &&
1605 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1606}
1607
1608
1609
1610
1611
1612static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
1613{
1614 struct efx_nic *efx = channel->efx;
1615 struct efx_ptp_data *ptp = efx->ptp_data;
1616 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
1617 u8 *match_data_012, *match_data_345;
1618 unsigned int version;
1619 u8 *data;
1620
1621 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1622
1623
1624 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
1625 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
1626 return false;
1627 }
1628 data = skb->data;
1629 version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
1630 if (version != PTP_VERSION_V1) {
1631 return false;
1632 }
1633
1634
1635
1636
1637 match_data_012 = data + PTP_V1_UUID_OFFSET;
1638 match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
1639 } else {
1640 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
1641 return false;
1642 }
1643 data = skb->data;
1644 version = data[PTP_V2_VERSION_OFFSET];
1645 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
1646 return false;
1647 }
1648
1649
1650
1651
1652
1653
1654
1655
1656 match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
1657 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1658 match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
1659 } else {
1660 match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
1661 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1662 }
1663 }
1664
1665
1666 if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1667 match->state = PTP_PACKET_STATE_UNMATCHED;
1668
1669
1670
1671
1672 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1673 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1674
1675
1676 match->words[0] = (match_data_012[0] |
1677 (match_data_012[1] << 8) |
1678 (match_data_012[2] << 16) |
1679 (match_data_345[0] << 24));
1680 match->words[1] = (match_data_345[1] |
1681 (match_data_345[2] << 8) |
1682 (data[PTP_V1_SEQUENCE_OFFSET +
1683 PTP_V1_SEQUENCE_LENGTH - 1] <<
1684 16));
1685 } else {
1686 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1687 }
1688
1689 skb_queue_tail(&ptp->rxq, skb);
1690 queue_work(ptp->workwq, &ptp->work);
1691
1692 return true;
1693}
1694
1695
1696
1697
1698
1699int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1700{
1701 struct efx_ptp_data *ptp = efx->ptp_data;
1702
1703 skb_queue_tail(&ptp->txq, skb);
1704
1705 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1706 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1707 efx_xmit_hwtstamp_pending(skb);
1708 queue_work(ptp->workwq, &ptp->work);
1709
1710 return NETDEV_TX_OK;
1711}
1712
1713int efx_ptp_get_mode(struct efx_nic *efx)
1714{
1715 return efx->ptp_data->mode;
1716}
1717
1718int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1719 unsigned int new_mode)
1720{
1721 if ((enable_wanted != efx->ptp_data->enabled) ||
1722 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1723 int rc = 0;
1724
1725 if (enable_wanted) {
1726
1727 if (efx->ptp_data->enabled &&
1728 (efx->ptp_data->mode != new_mode)) {
1729 efx->ptp_data->enabled = false;
1730 rc = efx_ptp_stop(efx);
1731 if (rc != 0)
1732 return rc;
1733 }
1734
1735
1736
1737
1738
1739 efx->ptp_data->mode = new_mode;
1740 if (netif_running(efx->net_dev))
1741 rc = efx_ptp_start(efx);
1742 if (rc == 0) {
1743 rc = efx_ptp_synchronize(efx,
1744 PTP_SYNC_ATTEMPTS * 2);
1745 if (rc != 0)
1746 efx_ptp_stop(efx);
1747 }
1748 } else {
1749 rc = efx_ptp_stop(efx);
1750 }
1751
1752 if (rc != 0)
1753 return rc;
1754
1755 efx->ptp_data->enabled = enable_wanted;
1756 }
1757
1758 return 0;
1759}
1760
1761static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1762{
1763 int rc;
1764
1765 if (init->flags)
1766 return -EINVAL;
1767
1768 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1769 (init->tx_type != HWTSTAMP_TX_ON))
1770 return -ERANGE;
1771
1772 rc = efx->type->ptp_set_ts_config(efx, init);
1773 if (rc)
1774 return rc;
1775
1776 efx->ptp_data->config = *init;
1777 return 0;
1778}
1779
1780void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
1781{
1782 struct efx_ptp_data *ptp = efx->ptp_data;
1783 struct efx_nic *primary = efx->primary;
1784
1785 ASSERT_RTNL();
1786
1787 if (!ptp)
1788 return;
1789
1790 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1791 SOF_TIMESTAMPING_RX_HARDWARE |
1792 SOF_TIMESTAMPING_RAW_HARDWARE);
1793
1794
1795
1796 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1797 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1798
1799 if (!(nic_data->licensed_features &
1800 (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN)))
1801 ts_info->so_timestamping &=
1802 ~SOF_TIMESTAMPING_TX_HARDWARE;
1803 }
1804 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1805 ts_info->phc_index =
1806 ptp_clock_index(primary->ptp_data->phc_clock);
1807 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1808 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
1809}
1810
1811int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1812{
1813 struct hwtstamp_config config;
1814 int rc;
1815
1816
1817 if (!efx->ptp_data)
1818 return -EOPNOTSUPP;
1819
1820 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1821 return -EFAULT;
1822
1823 rc = efx_ptp_ts_init(efx, &config);
1824 if (rc != 0)
1825 return rc;
1826
1827 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1828 ? -EFAULT : 0;
1829}
1830
1831int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1832{
1833 if (!efx->ptp_data)
1834 return -EOPNOTSUPP;
1835
1836 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1837 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1838}
1839
1840static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1841{
1842 struct efx_ptp_data *ptp = efx->ptp_data;
1843
1844 netif_err(efx, hw, efx->net_dev,
1845 "PTP unexpected event length: got %d expected %d\n",
1846 ptp->evt_frag_idx, expected_frag_len);
1847 ptp->reset_required = true;
1848 queue_work(ptp->workwq, &ptp->work);
1849}
1850
1851
1852
1853
1854
1855static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1856{
1857 struct efx_ptp_event_rx *evt = NULL;
1858
1859 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1860 return;
1861
1862 if (ptp->evt_frag_idx != 3) {
1863 ptp_event_failure(efx, 3);
1864 return;
1865 }
1866
1867 spin_lock_bh(&ptp->evt_lock);
1868 if (!list_empty(&ptp->evt_free_list)) {
1869 evt = list_first_entry(&ptp->evt_free_list,
1870 struct efx_ptp_event_rx, link);
1871 list_del(&evt->link);
1872
1873 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1874 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1875 MCDI_EVENT_SRC) |
1876 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1877 MCDI_EVENT_SRC) << 8) |
1878 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1879 MCDI_EVENT_SRC) << 16));
1880 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
1881 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1882 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1883 ptp->ts_corrections.ptp_rx);
1884 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1885 list_add_tail(&evt->link, &ptp->evt_list);
1886
1887 queue_work(ptp->workwq, &ptp->work);
1888 } else if (net_ratelimit()) {
1889
1890 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1891 }
1892 spin_unlock_bh(&ptp->evt_lock);
1893}
1894
1895static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1896{
1897 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1898 if (ptp->evt_frag_idx != 1) {
1899 ptp_event_failure(efx, 1);
1900 return;
1901 }
1902
1903 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1904}
1905
1906static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1907{
1908 if (ptp->nic_ts_enabled)
1909 queue_work(ptp->pps_workwq, &ptp->pps_work);
1910}
1911
1912void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1913{
1914 struct efx_ptp_data *ptp = efx->ptp_data;
1915 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1916
1917 if (!ptp) {
1918 if (!efx->ptp_warned) {
1919 netif_warn(efx, drv, efx->net_dev,
1920 "Received PTP event but PTP not set up\n");
1921 efx->ptp_warned = true;
1922 }
1923 return;
1924 }
1925
1926 if (!ptp->enabled)
1927 return;
1928
1929 if (ptp->evt_frag_idx == 0) {
1930 ptp->evt_code = code;
1931 } else if (ptp->evt_code != code) {
1932 netif_err(efx, hw, efx->net_dev,
1933 "PTP out of sequence event %d\n", code);
1934 ptp->evt_frag_idx = 0;
1935 }
1936
1937 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1938 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1939
1940 switch (code) {
1941 case MCDI_EVENT_CODE_PTP_RX:
1942 ptp_event_rx(efx, ptp);
1943 break;
1944 case MCDI_EVENT_CODE_PTP_FAULT:
1945 ptp_event_fault(efx, ptp);
1946 break;
1947 case MCDI_EVENT_CODE_PTP_PPS:
1948 ptp_event_pps(efx, ptp);
1949 break;
1950 default:
1951 netif_err(efx, hw, efx->net_dev,
1952 "PTP unknown event %d\n", code);
1953 break;
1954 }
1955 ptp->evt_frag_idx = 0;
1956 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1957 netif_err(efx, hw, efx->net_dev,
1958 "PTP too many event fragments\n");
1959 ptp->evt_frag_idx = 0;
1960 }
1961}
1962
1963void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1964{
1965 struct efx_nic *efx = channel->efx;
1966 struct efx_ptp_data *ptp = efx->ptp_data;
1967
1968
1969
1970
1971
1972
1973
1974 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
1975 channel->sync_timestamp_minor =
1976 (MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC)
1977 << ptp->nic_time.sync_event_minor_shift;
1978
1979
1980
1981
1982 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
1983 SYNC_EVENTS_VALID);
1984}
1985
1986static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
1987{
1988#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1989 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
1990#else
1991 const u8 *data = eh + efx->rx_packet_ts_offset;
1992 return (u32)data[0] |
1993 (u32)data[1] << 8 |
1994 (u32)data[2] << 16 |
1995 (u32)data[3] << 24;
1996#endif
1997}
1998
1999void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
2000 struct sk_buff *skb)
2001{
2002 struct efx_nic *efx = channel->efx;
2003 struct efx_ptp_data *ptp = efx->ptp_data;
2004 u32 pkt_timestamp_major, pkt_timestamp_minor;
2005 u32 diff, carry;
2006 struct skb_shared_hwtstamps *timestamps;
2007
2008 if (channel->sync_events_state != SYNC_EVENTS_VALID)
2009 return;
2010
2011 pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb));
2012
2013
2014
2015
2016 diff = pkt_timestamp_minor - channel->sync_timestamp_minor;
2017 if (pkt_timestamp_minor < channel->sync_timestamp_minor)
2018 diff += ptp->nic_time.minor_max;
2019
2020
2021 carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ?
2022 1 : 0;
2023
2024 if (diff <= ptp->nic_time.sync_event_diff_max) {
2025
2026
2027
2028 pkt_timestamp_major = channel->sync_timestamp_major + carry;
2029 } else if (diff >= ptp->nic_time.sync_event_diff_min) {
2030
2031
2032
2033
2034 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
2035 } else {
2036
2037
2038
2039
2040
2041 netif_vdbg(efx, drv, efx->net_dev,
2042 "packet timestamp %x too far from sync event %x:%x\n",
2043 pkt_timestamp_minor, channel->sync_timestamp_major,
2044 channel->sync_timestamp_minor);
2045 return;
2046 }
2047
2048
2049 timestamps = skb_hwtstamps(skb);
2050 timestamps->hwtstamp =
2051 ptp->nic_to_kernel_time(pkt_timestamp_major,
2052 pkt_timestamp_minor,
2053 ptp->ts_corrections.general_rx);
2054}
2055
2056static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
2057{
2058 struct efx_ptp_data *ptp_data = container_of(ptp,
2059 struct efx_ptp_data,
2060 phc_clock_info);
2061 struct efx_nic *efx = ptp_data->efx;
2062 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
2063 s64 adjustment_ns;
2064 int rc;
2065
2066 if (delta > MAX_PPB)
2067 delta = MAX_PPB;
2068 else if (delta < -MAX_PPB)
2069 delta = -MAX_PPB;
2070
2071
2072 adjustment_ns = ((s64)delta * PPB_SCALE_WORD +
2073 (1 << (ptp_data->adjfreq_ppb_shift - 1))) >>
2074 ptp_data->adjfreq_ppb_shift;
2075
2076 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2077 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
2078 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
2079 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
2080 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
2081 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
2082 NULL, 0, NULL);
2083 if (rc != 0)
2084 return rc;
2085
2086 ptp_data->current_adjfreq = adjustment_ns;
2087 return 0;
2088}
2089
2090static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
2091{
2092 u32 nic_major, nic_minor;
2093 struct efx_ptp_data *ptp_data = container_of(ptp,
2094 struct efx_ptp_data,
2095 phc_clock_info);
2096 struct efx_nic *efx = ptp_data->efx;
2097 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
2098
2099 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
2100
2101 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2102 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2103 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
2104 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
2105 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
2106 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2107 NULL, 0, NULL);
2108}
2109
2110static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
2111{
2112 struct efx_ptp_data *ptp_data = container_of(ptp,
2113 struct efx_ptp_data,
2114 phc_clock_info);
2115 struct efx_nic *efx = ptp_data->efx;
2116 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
2117 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
2118 int rc;
2119 ktime_t kt;
2120
2121 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
2122 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2123
2124 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2125 outbuf, sizeof(outbuf), NULL);
2126 if (rc != 0)
2127 return rc;
2128
2129 kt = ptp_data->nic_to_kernel_time(
2130 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
2131 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
2132 *ts = ktime_to_timespec64(kt);
2133 return 0;
2134}
2135
2136static int efx_phc_settime(struct ptp_clock_info *ptp,
2137 const struct timespec64 *e_ts)
2138{
2139
2140
2141
2142
2143 int rc;
2144 struct timespec64 time_now;
2145 struct timespec64 delta;
2146
2147 rc = efx_phc_gettime(ptp, &time_now);
2148 if (rc != 0)
2149 return rc;
2150
2151 delta = timespec64_sub(*e_ts, time_now);
2152
2153 rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
2154 if (rc != 0)
2155 return rc;
2156
2157 return 0;
2158}
2159
2160static int efx_phc_enable(struct ptp_clock_info *ptp,
2161 struct ptp_clock_request *request,
2162 int enable)
2163{
2164 struct efx_ptp_data *ptp_data = container_of(ptp,
2165 struct efx_ptp_data,
2166 phc_clock_info);
2167 if (request->type != PTP_CLK_REQ_PPS)
2168 return -EOPNOTSUPP;
2169
2170 ptp_data->nic_ts_enabled = !!enable;
2171 return 0;
2172}
2173
2174static const struct efx_channel_type efx_ptp_channel_type = {
2175 .handle_no_channel = efx_ptp_handle_no_channel,
2176 .pre_probe = efx_ptp_probe_channel,
2177 .post_remove = efx_ptp_remove_channel,
2178 .get_name = efx_ptp_get_channel_name,
2179
2180 .receive_skb = efx_ptp_rx,
2181 .want_txqs = efx_ptp_want_txqs,
2182 .keep_eventq = false,
2183};
2184
2185void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
2186{
2187
2188
2189
2190 if (efx_ptp_disable(efx) == 0)
2191 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
2192 &efx_ptp_channel_type;
2193}
2194
2195void efx_ptp_start_datapath(struct efx_nic *efx)
2196{
2197 if (efx_ptp_restart(efx))
2198 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
2199
2200 if (efx->type->ptp_set_ts_sync_events)
2201 efx->type->ptp_set_ts_sync_events(efx, true, true);
2202}
2203
2204void efx_ptp_stop_datapath(struct efx_nic *efx)
2205{
2206
2207 if (efx->type->ptp_set_ts_sync_events)
2208 efx->type->ptp_set_ts_sync_events(efx, false, true);
2209 efx_ptp_stop(efx);
2210}
2211