1
2
3
4#include "ixgbe.h"
5#include <linux/ptp_classify.h>
6#include <linux/clocksource.h>
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63#define IXGBE_INCVAL_10GB 0x66666666
64#define IXGBE_INCVAL_1GB 0x40000000
65#define IXGBE_INCVAL_100 0x50000000
66
67#define IXGBE_INCVAL_SHIFT_10GB 28
68#define IXGBE_INCVAL_SHIFT_1GB 24
69#define IXGBE_INCVAL_SHIFT_100 21
70
71#define IXGBE_INCVAL_SHIFT_82599 7
72#define IXGBE_INCPER_SHIFT_82599 24
73
74#define IXGBE_OVERFLOW_PERIOD (HZ * 30)
75#define IXGBE_PTP_TX_TIMEOUT (HZ)
76
77
78
79
80#define NS_PER_SEC 1000000000ULL
81#define NS_PER_HALF_SEC 500000000ULL
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138#define IXGBE_X550_BASE_PERIOD 0xC80000000ULL
139#define INCVALUE_MASK 0x7FFFFFFF
140#define ISGN 0x80000000
141#define MAX_TIMADJ 0x7FFFFFFF
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157static void ixgbe_ptp_setup_sdp_X540(struct ixgbe_adapter *adapter)
158{
159 struct cyclecounter *cc = &adapter->hw_cc;
160 struct ixgbe_hw *hw = &adapter->hw;
161 u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem;
162 u64 ns = 0, clock_edge = 0, clock_period;
163 unsigned long flags;
164
165
166 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
167 IXGBE_WRITE_FLUSH(hw);
168
169 if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
170 return;
171
172 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
173
174
175
176
177 esdp |= IXGBE_ESDP_SDP0_DIR |
178 IXGBE_ESDP_SDP0_NATIVE;
179
180
181
182
183 tsauxc = (IXGBE_TSAUXC_EN_CLK |
184 IXGBE_TSAUXC_SYNCLK |
185 IXGBE_TSAUXC_SDP0_INT);
186
187
188
189
190 clock_period = div_u64((NS_PER_HALF_SEC << cc->shift), cc->mult);
191 clktiml = (u32)(clock_period);
192 clktimh = (u32)(clock_period >> 32);
193
194
195 spin_lock_irqsave(&adapter->tmreg_lock, flags);
196 ns = timecounter_read(&adapter->hw_tc);
197 clock_edge = adapter->hw_tc.cycle_last;
198 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
199
200
201 div_u64_rem(ns, NS_PER_SEC, &rem);
202
203
204
205
206 rem = (NS_PER_SEC - rem);
207
208
209 clock_edge += div_u64(((u64)rem << cc->shift), cc->mult);
210 trgttiml = (u32)clock_edge;
211 trgttimh = (u32)(clock_edge >> 32);
212
213 IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml);
214 IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh);
215 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
216 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
217
218 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
219 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
220
221 IXGBE_WRITE_FLUSH(hw);
222}
223
224
225
226
227
228
229
230
231
232
233
234
235
236static void ixgbe_ptp_setup_sdp_X550(struct ixgbe_adapter *adapter)
237{
238 u32 esdp, tsauxc, freqout, trgttiml, trgttimh, rem, tssdp;
239 struct cyclecounter *cc = &adapter->hw_cc;
240 struct ixgbe_hw *hw = &adapter->hw;
241 u64 ns = 0, clock_edge = 0;
242 struct timespec64 ts;
243 unsigned long flags;
244
245
246 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0);
247 IXGBE_WRITE_FLUSH(hw);
248
249 if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED))
250 return;
251
252 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
253
254
255
256
257 esdp |= IXGBE_ESDP_SDP0_DIR |
258 IXGBE_ESDP_SDP0_NATIVE;
259
260
261
262
263#define IXGBE_TSAUXC_DIS_TS_CLEAR 0x40000000
264 tsauxc = (IXGBE_TSAUXC_EN_CLK | IXGBE_TSAUXC_ST0 |
265 IXGBE_TSAUXC_EN_TT0 | IXGBE_TSAUXC_SDP0_INT |
266 IXGBE_TSAUXC_DIS_TS_CLEAR);
267
268 tssdp = (IXGBE_TSSDP_TS_SDP0_EN |
269 IXGBE_TSSDP_TS_SDP0_CLK0);
270
271
272
273
274
275 freqout = div_u64(NS_PER_HALF_SEC << cc->shift, cc->mult);
276
277
278 spin_lock_irqsave(&adapter->tmreg_lock, flags);
279 ns = timecounter_read(&adapter->hw_tc);
280 clock_edge = adapter->hw_tc.cycle_last;
281 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
282
283
284 div_u64_rem(ns, NS_PER_SEC, &rem);
285
286
287
288
289 rem = (NS_PER_SEC - rem);
290
291
292 clock_edge += div_u64(((u64)rem << cc->shift), cc->mult);
293
294
295
296
297
298
299
300
301 ts = ns_to_timespec64(clock_edge);
302 trgttiml = (u32)ts.tv_nsec;
303 trgttimh = (u32)ts.tv_sec;
304
305 IXGBE_WRITE_REG(hw, IXGBE_FREQOUT0, freqout);
306 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml);
307 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh);
308
309 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp);
310 IXGBE_WRITE_REG(hw, IXGBE_TSSDP, tssdp);
311 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
312
313 IXGBE_WRITE_FLUSH(hw);
314}
315
316
317
318
319
320
321
322
323
324
325
326static u64 ixgbe_ptp_read_X550(const struct cyclecounter *cc)
327{
328 struct ixgbe_adapter *adapter =
329 container_of(cc, struct ixgbe_adapter, hw_cc);
330 struct ixgbe_hw *hw = &adapter->hw;
331 struct timespec64 ts;
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
349 ts.tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
350 ts.tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
351
352 return (u64)timespec64_to_ns(&ts);
353}
354
355
356
357
358
359
360
361
362
363static u64 ixgbe_ptp_read_82599(const struct cyclecounter *cc)
364{
365 struct ixgbe_adapter *adapter =
366 container_of(cc, struct ixgbe_adapter, hw_cc);
367 struct ixgbe_hw *hw = &adapter->hw;
368 u64 stamp = 0;
369
370 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
371 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
372
373 return stamp;
374}
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393static void ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter *adapter,
394 struct skb_shared_hwtstamps *hwtstamp,
395 u64 timestamp)
396{
397 unsigned long flags;
398 struct timespec64 systime;
399 u64 ns;
400
401 memset(hwtstamp, 0, sizeof(*hwtstamp));
402
403 switch (adapter->hw.mac.type) {
404
405
406
407
408
409
410
411
412 case ixgbe_mac_X550:
413 case ixgbe_mac_X550EM_x:
414 case ixgbe_mac_x550em_a:
415
416
417
418
419
420 systime.tv_sec = timestamp >> 32;
421 systime.tv_nsec = timestamp & 0xFFFFFFFF;
422
423 timestamp = timespec64_to_ns(&systime);
424 break;
425 default:
426 break;
427 }
428
429 spin_lock_irqsave(&adapter->tmreg_lock, flags);
430 ns = timecounter_cyc2time(&adapter->hw_tc, timestamp);
431 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
432
433 hwtstamp->hwtstamp = ns_to_ktime(ns);
434}
435
436
437
438
439
440
441
442
443
444static int ixgbe_ptp_adjfreq_82599(struct ptp_clock_info *ptp, s32 ppb)
445{
446 struct ixgbe_adapter *adapter =
447 container_of(ptp, struct ixgbe_adapter, ptp_caps);
448 struct ixgbe_hw *hw = &adapter->hw;
449 u64 freq, incval;
450 u32 diff;
451 int neg_adj = 0;
452
453 if (ppb < 0) {
454 neg_adj = 1;
455 ppb = -ppb;
456 }
457
458 smp_mb();
459 incval = READ_ONCE(adapter->base_incval);
460
461 freq = incval;
462 freq *= ppb;
463 diff = div_u64(freq, 1000000000ULL);
464
465 incval = neg_adj ? (incval - diff) : (incval + diff);
466
467 switch (hw->mac.type) {
468 case ixgbe_mac_X540:
469 if (incval > 0xFFFFFFFFULL)
470 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
471 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, (u32)incval);
472 break;
473 case ixgbe_mac_82599EB:
474 if (incval > 0x00FFFFFFULL)
475 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
476 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
477 BIT(IXGBE_INCPER_SHIFT_82599) |
478 ((u32)incval & 0x00FFFFFFUL));
479 break;
480 default:
481 break;
482 }
483
484 return 0;
485}
486
487
488
489
490
491
492
493
494
495static int ixgbe_ptp_adjfreq_X550(struct ptp_clock_info *ptp, s32 ppb)
496{
497 struct ixgbe_adapter *adapter =
498 container_of(ptp, struct ixgbe_adapter, ptp_caps);
499 struct ixgbe_hw *hw = &adapter->hw;
500 int neg_adj = 0;
501 u64 rate = IXGBE_X550_BASE_PERIOD;
502 u32 inca;
503
504 if (ppb < 0) {
505 neg_adj = 1;
506 ppb = -ppb;
507 }
508 rate *= ppb;
509 rate = div_u64(rate, 1000000000ULL);
510
511
512 if (rate >= INCVALUE_MASK)
513 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n");
514
515 inca = rate & INCVALUE_MASK;
516 if (neg_adj)
517 inca |= ISGN;
518
519 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, inca);
520
521 return 0;
522}
523
524
525
526
527
528
529
530
531static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
532{
533 struct ixgbe_adapter *adapter =
534 container_of(ptp, struct ixgbe_adapter, ptp_caps);
535 unsigned long flags;
536
537 spin_lock_irqsave(&adapter->tmreg_lock, flags);
538 timecounter_adjtime(&adapter->hw_tc, delta);
539 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
540
541 if (adapter->ptp_setup_sdp)
542 adapter->ptp_setup_sdp(adapter);
543
544 return 0;
545}
546
547
548
549
550
551
552
553
554
555
556static int ixgbe_ptp_gettimex(struct ptp_clock_info *ptp,
557 struct timespec64 *ts,
558 struct ptp_system_timestamp *sts)
559{
560 struct ixgbe_adapter *adapter =
561 container_of(ptp, struct ixgbe_adapter, ptp_caps);
562 struct ixgbe_hw *hw = &adapter->hw;
563 unsigned long flags;
564 u64 ns, stamp;
565
566 spin_lock_irqsave(&adapter->tmreg_lock, flags);
567
568 switch (adapter->hw.mac.type) {
569 case ixgbe_mac_X550:
570 case ixgbe_mac_X550EM_x:
571 case ixgbe_mac_x550em_a:
572
573
574
575
576
577 ptp_read_system_prets(sts);
578 IXGBE_READ_REG(hw, IXGBE_SYSTIMR);
579 ptp_read_system_postts(sts);
580 ts->tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
581 ts->tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH);
582 stamp = timespec64_to_ns(ts);
583 break;
584 default:
585 ptp_read_system_prets(sts);
586 stamp = IXGBE_READ_REG(hw, IXGBE_SYSTIML);
587 ptp_read_system_postts(sts);
588 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
589 break;
590 }
591
592 ns = timecounter_cyc2time(&adapter->hw_tc, stamp);
593
594 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
595
596 *ts = ns_to_timespec64(ns);
597
598 return 0;
599}
600
601
602
603
604
605
606
607
608
609static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
610 const struct timespec64 *ts)
611{
612 struct ixgbe_adapter *adapter =
613 container_of(ptp, struct ixgbe_adapter, ptp_caps);
614 unsigned long flags;
615 u64 ns = timespec64_to_ns(ts);
616
617
618 spin_lock_irqsave(&adapter->tmreg_lock, flags);
619 timecounter_init(&adapter->hw_tc, &adapter->hw_cc, ns);
620 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
621
622 if (adapter->ptp_setup_sdp)
623 adapter->ptp_setup_sdp(adapter);
624 return 0;
625}
626
627
628
629
630
631
632
633
634
635
636static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp,
637 struct ptp_clock_request *rq, int on)
638{
639 struct ixgbe_adapter *adapter =
640 container_of(ptp, struct ixgbe_adapter, ptp_caps);
641
642
643
644
645
646
647
648 if (rq->type != PTP_CLK_REQ_PPS || !adapter->ptp_setup_sdp)
649 return -ENOTSUPP;
650
651 if (on)
652 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED;
653 else
654 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
655
656 adapter->ptp_setup_sdp(adapter);
657 return 0;
658}
659
660
661
662
663
664
665
666
667void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter)
668{
669 struct ixgbe_hw *hw = &adapter->hw;
670 struct ptp_clock_event event;
671
672 event.type = PTP_CLOCK_PPS;
673
674
675
676
677
678 if (!adapter->ptp_clock)
679 return;
680
681 switch (hw->mac.type) {
682 case ixgbe_mac_X540:
683 ptp_clock_event(adapter->ptp_clock, &event);
684 break;
685 default:
686 break;
687 }
688}
689
690
691
692
693
694
695
696
697
698void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
699{
700 bool timeout = time_is_before_jiffies(adapter->last_overflow_check +
701 IXGBE_OVERFLOW_PERIOD);
702 unsigned long flags;
703
704 if (timeout) {
705
706 spin_lock_irqsave(&adapter->tmreg_lock, flags);
707 timecounter_read(&adapter->hw_tc);
708 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
709
710 adapter->last_overflow_check = jiffies;
711 }
712}
713
714
715
716
717
718
719
720
721
722
723void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter)
724{
725 struct ixgbe_hw *hw = &adapter->hw;
726 u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
727 struct ixgbe_ring *rx_ring;
728 unsigned long rx_event;
729 int n;
730
731
732
733
734 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) {
735 adapter->last_rx_ptp_check = jiffies;
736 return;
737 }
738
739
740 rx_event = adapter->last_rx_ptp_check;
741 for (n = 0; n < adapter->num_rx_queues; n++) {
742 rx_ring = adapter->rx_ring[n];
743 if (time_after(rx_ring->last_rx_timestamp, rx_event))
744 rx_event = rx_ring->last_rx_timestamp;
745 }
746
747
748 if (time_is_before_jiffies(rx_event + 5 * HZ)) {
749 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
750 adapter->last_rx_ptp_check = jiffies;
751
752 adapter->rx_hwtstamp_cleared++;
753 e_warn(drv, "clearing RX Timestamp hang\n");
754 }
755}
756
757
758
759
760
761
762
763
764
765static void ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter *adapter)
766{
767 struct ixgbe_hw *hw = &adapter->hw;
768
769 IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
770 if (adapter->ptp_tx_skb) {
771 dev_kfree_skb_any(adapter->ptp_tx_skb);
772 adapter->ptp_tx_skb = NULL;
773 }
774 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
775}
776
777
778
779
780
781void ixgbe_ptp_tx_hang(struct ixgbe_adapter *adapter)
782{
783 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
784 IXGBE_PTP_TX_TIMEOUT);
785
786 if (!adapter->ptp_tx_skb)
787 return;
788
789 if (!test_bit(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state))
790 return;
791
792
793
794
795
796 if (timeout) {
797 cancel_work_sync(&adapter->ptp_tx_work);
798 ixgbe_ptp_clear_tx_timestamp(adapter);
799 adapter->tx_hwtstamp_timeouts++;
800 e_warn(drv, "clearing Tx timestamp hang\n");
801 }
802}
803
804
805
806
807
808
809
810
811
812static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter)
813{
814 struct sk_buff *skb = adapter->ptp_tx_skb;
815 struct ixgbe_hw *hw = &adapter->hw;
816 struct skb_shared_hwtstamps shhwtstamps;
817 u64 regval = 0;
818
819 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
820 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
821 ixgbe_ptp_convert_to_hwtstamp(adapter, &shhwtstamps, regval);
822
823
824
825
826
827
828 adapter->ptp_tx_skb = NULL;
829 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state);
830
831
832 skb_tstamp_tx(skb, &shhwtstamps);
833 dev_kfree_skb_any(skb);
834}
835
836
837
838
839
840
841
842
843
844static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work)
845{
846 struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter,
847 ptp_tx_work);
848 struct ixgbe_hw *hw = &adapter->hw;
849 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
850 IXGBE_PTP_TX_TIMEOUT);
851 u32 tsynctxctl;
852
853
854 if (!adapter->ptp_tx_skb) {
855 ixgbe_ptp_clear_tx_timestamp(adapter);
856 return;
857 }
858
859
860 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
861 if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID) {
862 ixgbe_ptp_tx_hwtstamp(adapter);
863 return;
864 }
865
866 if (timeout) {
867 ixgbe_ptp_clear_tx_timestamp(adapter);
868 adapter->tx_hwtstamp_timeouts++;
869 e_warn(drv, "clearing Tx Timestamp hang\n");
870 } else {
871
872 schedule_work(&adapter->ptp_tx_work);
873 }
874}
875
876
877
878
879
880
881
882
883
884
885void ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector *q_vector,
886 struct sk_buff *skb)
887{
888 __le64 regval;
889
890
891 skb_copy_bits(skb, skb->len - IXGBE_TS_HDR_LEN, ®val,
892 IXGBE_TS_HDR_LEN);
893 __pskb_trim(skb, skb->len - IXGBE_TS_HDR_LEN);
894
895
896
897
898
899
900
901 ixgbe_ptp_convert_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
902 le64_to_cpu(regval));
903}
904
905
906
907
908
909
910
911
912
913
914void ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector *q_vector,
915 struct sk_buff *skb)
916{
917 struct ixgbe_adapter *adapter;
918 struct ixgbe_hw *hw;
919 u64 regval = 0;
920 u32 tsyncrxctl;
921
922
923 if (!q_vector || !q_vector->adapter)
924 return;
925
926 adapter = q_vector->adapter;
927 hw = &adapter->hw;
928
929
930
931
932
933 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
934 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
935 return;
936
937 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
938 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
939
940 ixgbe_ptp_convert_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
941}
942
943
944
945
946
947
948
949
950
951
952int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
953{
954 struct hwtstamp_config *config = &adapter->tstamp_config;
955
956 return copy_to_user(ifr->ifr_data, config,
957 sizeof(*config)) ? -EFAULT : 0;
958}
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter,
986 struct hwtstamp_config *config)
987{
988 struct ixgbe_hw *hw = &adapter->hw;
989 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
990 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
991 u32 tsync_rx_mtrl = PTP_EV_PORT << 16;
992 bool is_l2 = false;
993 u32 regval;
994
995
996 if (config->flags)
997 return -EINVAL;
998
999 switch (config->tx_type) {
1000 case HWTSTAMP_TX_OFF:
1001 tsync_tx_ctl = 0;
1002 break;
1003 case HWTSTAMP_TX_ON:
1004 break;
1005 default:
1006 return -ERANGE;
1007 }
1008
1009 switch (config->rx_filter) {
1010 case HWTSTAMP_FILTER_NONE:
1011 tsync_rx_ctl = 0;
1012 tsync_rx_mtrl = 0;
1013 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1014 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1015 break;
1016 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1017 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
1018 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG;
1019 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1020 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1021 break;
1022 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1023 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
1024 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
1025 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1026 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1027 break;
1028 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1029 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1030 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1031 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1032 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1033 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1034 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1035 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1036 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1037 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
1038 is_l2 = true;
1039 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1040 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1041 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1042 break;
1043 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1044 case HWTSTAMP_FILTER_NTP_ALL:
1045 case HWTSTAMP_FILTER_ALL:
1046
1047
1048
1049 if (hw->mac.type >= ixgbe_mac_X550) {
1050 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_ALL;
1051 config->rx_filter = HWTSTAMP_FILTER_ALL;
1052 adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
1053 break;
1054 }
1055 fallthrough;
1056 default:
1057
1058
1059
1060
1061
1062
1063 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1064 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1065 config->rx_filter = HWTSTAMP_FILTER_NONE;
1066 return -ERANGE;
1067 }
1068
1069 if (hw->mac.type == ixgbe_mac_82598EB) {
1070 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED |
1071 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER);
1072 if (tsync_rx_ctl | tsync_tx_ctl)
1073 return -ERANGE;
1074 return 0;
1075 }
1076
1077
1078
1079
1080
1081 switch (hw->mac.type) {
1082 case ixgbe_mac_X550:
1083 case ixgbe_mac_X550EM_x:
1084 case ixgbe_mac_x550em_a:
1085
1086
1087
1088
1089 if (config->rx_filter == HWTSTAMP_FILTER_NONE)
1090 break;
1091
1092 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED |
1093 IXGBE_TSYNCRXCTL_TYPE_ALL |
1094 IXGBE_TSYNCRXCTL_TSIP_UT_EN;
1095 config->rx_filter = HWTSTAMP_FILTER_ALL;
1096 adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED;
1097 adapter->flags &= ~IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER;
1098 is_l2 = true;
1099 break;
1100 default:
1101 break;
1102 }
1103
1104
1105 if (is_l2)
1106 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
1107 (IXGBE_ETQF_FILTER_EN |
1108 IXGBE_ETQF_1588 |
1109 ETH_P_1588));
1110 else
1111 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
1112
1113
1114 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
1115 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
1116 regval |= tsync_tx_ctl;
1117 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
1118
1119
1120 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
1121 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
1122 regval |= tsync_rx_ctl;
1123 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
1124
1125
1126 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
1127
1128 IXGBE_WRITE_FLUSH(hw);
1129
1130
1131 ixgbe_ptp_clear_tx_timestamp(adapter);
1132 IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
1133
1134 return 0;
1135}
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr)
1146{
1147 struct hwtstamp_config config;
1148 int err;
1149
1150 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1151 return -EFAULT;
1152
1153 err = ixgbe_ptp_set_timestamp_mode(adapter, &config);
1154 if (err)
1155 return err;
1156
1157
1158 memcpy(&adapter->tstamp_config, &config,
1159 sizeof(adapter->tstamp_config));
1160
1161 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1162 -EFAULT : 0;
1163}
1164
1165static void ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter *adapter,
1166 u32 *shift, u32 *incval)
1167{
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182 switch (adapter->link_speed) {
1183 case IXGBE_LINK_SPEED_100_FULL:
1184 *shift = IXGBE_INCVAL_SHIFT_100;
1185 *incval = IXGBE_INCVAL_100;
1186 break;
1187 case IXGBE_LINK_SPEED_1GB_FULL:
1188 *shift = IXGBE_INCVAL_SHIFT_1GB;
1189 *incval = IXGBE_INCVAL_1GB;
1190 break;
1191 case IXGBE_LINK_SPEED_10GB_FULL:
1192 default:
1193 *shift = IXGBE_INCVAL_SHIFT_10GB;
1194 *incval = IXGBE_INCVAL_10GB;
1195 break;
1196 }
1197}
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
1210{
1211 struct ixgbe_hw *hw = &adapter->hw;
1212 struct cyclecounter cc;
1213 unsigned long flags;
1214 u32 incval = 0;
1215 u32 tsauxc = 0;
1216 u32 fuse0 = 0;
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229 cc.mask = CLOCKSOURCE_MASK(64);
1230 cc.mult = 1;
1231 cc.shift = 0;
1232
1233 switch (hw->mac.type) {
1234 case ixgbe_mac_X550EM_x:
1235
1236
1237
1238
1239
1240
1241 fuse0 = IXGBE_READ_REG(hw, IXGBE_FUSES0_GROUP(0));
1242 if (!(fuse0 & IXGBE_FUSES0_300MHZ)) {
1243 cc.mult = 3;
1244 cc.shift = 2;
1245 }
1246 fallthrough;
1247 case ixgbe_mac_x550em_a:
1248 case ixgbe_mac_X550:
1249 cc.read = ixgbe_ptp_read_X550;
1250
1251
1252 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMR, 0);
1253 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0);
1254 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0);
1255 tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC);
1256 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC,
1257 tsauxc & ~IXGBE_TSAUXC_DISABLE_SYSTIME);
1258 IXGBE_WRITE_REG(hw, IXGBE_TSIM, IXGBE_TSIM_TXTS);
1259 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_TIMESYNC);
1260
1261 IXGBE_WRITE_FLUSH(hw);
1262 break;
1263 case ixgbe_mac_X540:
1264 cc.read = ixgbe_ptp_read_82599;
1265
1266 ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
1267 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
1268 break;
1269 case ixgbe_mac_82599EB:
1270 cc.read = ixgbe_ptp_read_82599;
1271
1272 ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval);
1273 incval >>= IXGBE_INCVAL_SHIFT_82599;
1274 cc.shift -= IXGBE_INCVAL_SHIFT_82599;
1275 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
1276 BIT(IXGBE_INCPER_SHIFT_82599) | incval);
1277 break;
1278 default:
1279
1280 return;
1281 }
1282
1283
1284 WRITE_ONCE(adapter->base_incval, incval);
1285 smp_mb();
1286
1287
1288 spin_lock_irqsave(&adapter->tmreg_lock, flags);
1289 memcpy(&adapter->hw_cc, &cc, sizeof(adapter->hw_cc));
1290 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
1291}
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305void ixgbe_ptp_reset(struct ixgbe_adapter *adapter)
1306{
1307 struct ixgbe_hw *hw = &adapter->hw;
1308 unsigned long flags;
1309
1310
1311 ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
1312
1313
1314 if (hw->mac.type == ixgbe_mac_82598EB)
1315 return;
1316
1317 ixgbe_ptp_start_cyclecounter(adapter);
1318
1319 spin_lock_irqsave(&adapter->tmreg_lock, flags);
1320 timecounter_init(&adapter->hw_tc, &adapter->hw_cc,
1321 ktime_to_ns(ktime_get_real()));
1322 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
1323
1324 adapter->last_overflow_check = jiffies;
1325
1326
1327
1328
1329 if (adapter->ptp_setup_sdp)
1330 adapter->ptp_setup_sdp(adapter);
1331}
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343static long ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter)
1344{
1345 struct net_device *netdev = adapter->netdev;
1346 long err;
1347
1348
1349 if (!IS_ERR_OR_NULL(adapter->ptp_clock))
1350 return 0;
1351
1352 switch (adapter->hw.mac.type) {
1353 case ixgbe_mac_X540:
1354 snprintf(adapter->ptp_caps.name,
1355 sizeof(adapter->ptp_caps.name),
1356 "%s", netdev->name);
1357 adapter->ptp_caps.owner = THIS_MODULE;
1358 adapter->ptp_caps.max_adj = 250000000;
1359 adapter->ptp_caps.n_alarm = 0;
1360 adapter->ptp_caps.n_ext_ts = 0;
1361 adapter->ptp_caps.n_per_out = 0;
1362 adapter->ptp_caps.pps = 1;
1363 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
1364 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1365 adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
1366 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1367 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1368 adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X540;
1369 break;
1370 case ixgbe_mac_82599EB:
1371 snprintf(adapter->ptp_caps.name,
1372 sizeof(adapter->ptp_caps.name),
1373 "%s", netdev->name);
1374 adapter->ptp_caps.owner = THIS_MODULE;
1375 adapter->ptp_caps.max_adj = 250000000;
1376 adapter->ptp_caps.n_alarm = 0;
1377 adapter->ptp_caps.n_ext_ts = 0;
1378 adapter->ptp_caps.n_per_out = 0;
1379 adapter->ptp_caps.pps = 0;
1380 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599;
1381 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1382 adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
1383 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1384 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1385 break;
1386 case ixgbe_mac_X550:
1387 case ixgbe_mac_X550EM_x:
1388 case ixgbe_mac_x550em_a:
1389 snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name);
1390 adapter->ptp_caps.owner = THIS_MODULE;
1391 adapter->ptp_caps.max_adj = 30000000;
1392 adapter->ptp_caps.n_alarm = 0;
1393 adapter->ptp_caps.n_ext_ts = 0;
1394 adapter->ptp_caps.n_per_out = 0;
1395 adapter->ptp_caps.pps = 1;
1396 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_X550;
1397 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
1398 adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex;
1399 adapter->ptp_caps.settime64 = ixgbe_ptp_settime;
1400 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable;
1401 adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X550;
1402 break;
1403 default:
1404 adapter->ptp_clock = NULL;
1405 adapter->ptp_setup_sdp = NULL;
1406 return -EOPNOTSUPP;
1407 }
1408
1409 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
1410 &adapter->pdev->dev);
1411 if (IS_ERR(adapter->ptp_clock)) {
1412 err = PTR_ERR(adapter->ptp_clock);
1413 adapter->ptp_clock = NULL;
1414 e_dev_err("ptp_clock_register failed\n");
1415 return err;
1416 } else if (adapter->ptp_clock)
1417 e_dev_info("registered PHC device on %s\n", netdev->name);
1418
1419
1420
1421
1422
1423 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
1424 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
1425
1426 return 0;
1427}
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
1438{
1439
1440
1441
1442
1443 spin_lock_init(&adapter->tmreg_lock);
1444
1445
1446 if (ixgbe_ptp_create_clock(adapter))
1447 return;
1448
1449
1450 INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work);
1451
1452
1453 ixgbe_ptp_reset(adapter);
1454
1455
1456 set_bit(__IXGBE_PTP_RUNNING, &adapter->state);
1457
1458 return;
1459}
1460
1461
1462
1463
1464
1465
1466
1467
1468void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter)
1469{
1470
1471 if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state))
1472 return;
1473
1474 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED;
1475 if (adapter->ptp_setup_sdp)
1476 adapter->ptp_setup_sdp(adapter);
1477
1478
1479 cancel_work_sync(&adapter->ptp_tx_work);
1480 ixgbe_ptp_clear_tx_timestamp(adapter);
1481}
1482
1483
1484
1485
1486
1487
1488
1489
1490void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
1491{
1492
1493 ixgbe_ptp_suspend(adapter);
1494
1495
1496 if (adapter->ptp_clock) {
1497 ptp_clock_unregister(adapter->ptp_clock);
1498 adapter->ptp_clock = NULL;
1499 e_dev_info("removed PHC on %s\n",
1500 adapter->netdev->name);
1501 }
1502}
1503