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