hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/net/ethernet/intel/igb/igb_ptp.c
....@@ -53,13 +53,15 @@
5353 * 2^40 * 10^-9 / 60 = 18.3 minutes.
5454 *
5555 * SYSTIM is converted to real time using a timecounter. As
56
- * timecounter_cyc2time() allows old timestamps, the timecounter
57
- * needs to be updated at least once per half of the SYSTIM interval.
58
- * Scheduling of delayed work is not very accurate, so we aim for 8
59
- * minutes to be sure the actual interval is shorter than 9.16 minutes.
56
+ * timecounter_cyc2time() allows old timestamps, the timecounter needs
57
+ * to be updated at least once per half of the SYSTIM interval.
58
+ * Scheduling of delayed work is not very accurate, and also the NIC
59
+ * clock can be adjusted to run up to 6% faster and the system clock
60
+ * up to 10% slower, so we aim for 6 minutes to be sure the actual
61
+ * interval in the NIC time is shorter than 9.16 minutes.
6062 */
6163
62
-#define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 8)
64
+#define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 6)
6365 #define IGB_PTP_TX_TIMEOUT (HZ * 15)
6466 #define INCPERIOD_82576 BIT(E1000_TIMINCA_16NS_SHIFT)
6567 #define INCVALUE_82576_MASK GENMASK(E1000_TIMINCA_16NS_SHIFT - 1, 0)
....@@ -275,17 +277,25 @@
275277 return 0;
276278 }
277279
278
-static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
279
- struct timespec64 *ts)
280
+static int igb_ptp_gettimex_82576(struct ptp_clock_info *ptp,
281
+ struct timespec64 *ts,
282
+ struct ptp_system_timestamp *sts)
280283 {
281284 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
282285 ptp_caps);
286
+ struct e1000_hw *hw = &igb->hw;
283287 unsigned long flags;
288
+ u32 lo, hi;
284289 u64 ns;
285290
286291 spin_lock_irqsave(&igb->tmreg_lock, flags);
287292
288
- ns = timecounter_read(&igb->tc);
293
+ ptp_read_system_prets(sts);
294
+ lo = rd32(E1000_SYSTIML);
295
+ ptp_read_system_postts(sts);
296
+ hi = rd32(E1000_SYSTIMH);
297
+
298
+ ns = timecounter_cyc2time(&igb->tc, ((u64)hi << 32) | lo);
289299
290300 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
291301
....@@ -294,16 +304,50 @@
294304 return 0;
295305 }
296306
297
-static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
298
- struct timespec64 *ts)
307
+static int igb_ptp_gettimex_82580(struct ptp_clock_info *ptp,
308
+ struct timespec64 *ts,
309
+ struct ptp_system_timestamp *sts)
299310 {
300311 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
301312 ptp_caps);
313
+ struct e1000_hw *hw = &igb->hw;
314
+ unsigned long flags;
315
+ u32 lo, hi;
316
+ u64 ns;
317
+
318
+ spin_lock_irqsave(&igb->tmreg_lock, flags);
319
+
320
+ ptp_read_system_prets(sts);
321
+ rd32(E1000_SYSTIMR);
322
+ ptp_read_system_postts(sts);
323
+ lo = rd32(E1000_SYSTIML);
324
+ hi = rd32(E1000_SYSTIMH);
325
+
326
+ ns = timecounter_cyc2time(&igb->tc, ((u64)hi << 32) | lo);
327
+
328
+ spin_unlock_irqrestore(&igb->tmreg_lock, flags);
329
+
330
+ *ts = ns_to_timespec64(ns);
331
+
332
+ return 0;
333
+}
334
+
335
+static int igb_ptp_gettimex_i210(struct ptp_clock_info *ptp,
336
+ struct timespec64 *ts,
337
+ struct ptp_system_timestamp *sts)
338
+{
339
+ struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
340
+ ptp_caps);
341
+ struct e1000_hw *hw = &igb->hw;
302342 unsigned long flags;
303343
304344 spin_lock_irqsave(&igb->tmreg_lock, flags);
305345
306
- igb_ptp_read_i210(igb, ts);
346
+ ptp_read_system_prets(sts);
347
+ rd32(E1000_SYSTIMR);
348
+ ptp_read_system_postts(sts);
349
+ ts->tv_nsec = rd32(E1000_SYSTIML);
350
+ ts->tv_sec = rd32(E1000_SYSTIMH);
307351
308352 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
309353
....@@ -477,6 +521,19 @@
477521
478522 switch (rq->type) {
479523 case PTP_CLK_REQ_EXTTS:
524
+ /* Reject requests with unsupported flags */
525
+ if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
526
+ PTP_RISING_EDGE |
527
+ PTP_FALLING_EDGE |
528
+ PTP_STRICT_FLAGS))
529
+ return -EOPNOTSUPP;
530
+
531
+ /* Reject requests failing to enable both edges. */
532
+ if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
533
+ (rq->extts.flags & PTP_ENABLE_FEATURE) &&
534
+ (rq->extts.flags & PTP_EXTTS_EDGES) != PTP_EXTTS_EDGES)
535
+ return -EOPNOTSUPP;
536
+
480537 if (on) {
481538 pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
482539 rq->extts.index);
....@@ -507,6 +564,10 @@
507564 return 0;
508565
509566 case PTP_CLK_REQ_PEROUT:
567
+ /* Reject requests with unsupported flags */
568
+ if (rq->perout.flags)
569
+ return -EOPNOTSUPP;
570
+
510571 if (on) {
511572 pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
512573 rq->perout.index);
....@@ -656,9 +717,12 @@
656717 struct igb_adapter *igb =
657718 container_of(work, struct igb_adapter, ptp_overflow_work.work);
658719 struct timespec64 ts;
720
+ u64 ns;
659721
660
- igb->ptp_caps.gettime64(&igb->ptp_caps, &ts);
722
+ /* Update the timecounter */
723
+ ns = timecounter_read(&igb->tc);
661724
725
+ ts = ns_to_timespec64(ns);
662726 pr_debug("igb overflow check at %lld.%09lu\n",
663727 (long long) ts.tv_sec, ts.tv_nsec);
664728
....@@ -792,6 +856,9 @@
792856 dev_kfree_skb_any(skb);
793857 }
794858
859
+#define IGB_RET_PTP_DISABLED 1
860
+#define IGB_RET_PTP_INVALID 2
861
+
795862 /**
796863 * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
797864 * @q_vector: Pointer to interrupt specific structure
....@@ -800,19 +867,29 @@
800867 *
801868 * This function is meant to retrieve a timestamp from the first buffer of an
802869 * incoming frame. The value is stored in little endian format starting on
803
- * byte 8.
870
+ * byte 8
871
+ *
872
+ * Returns: 0 if success, nonzero if failure
804873 **/
805
-void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector, void *va,
806
- struct sk_buff *skb)
874
+int igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector, void *va,
875
+ struct sk_buff *skb)
807876 {
808
- __le64 *regval = (__le64 *)va;
809877 struct igb_adapter *adapter = q_vector->adapter;
878
+ __le64 *regval = (__le64 *)va;
810879 int adjust = 0;
880
+
881
+ if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
882
+ return IGB_RET_PTP_DISABLED;
811883
812884 /* The timestamp is recorded in little endian format.
813885 * DWORD: 0 1 2 3
814886 * Field: Reserved Reserved SYSTIML SYSTIMH
815887 */
888
+
889
+ /* check reserved dwords are zero, be/le doesn't matter for zero */
890
+ if (regval[0])
891
+ return IGB_RET_PTP_INVALID;
892
+
816893 igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb),
817894 le64_to_cpu(regval[1]));
818895
....@@ -832,6 +909,8 @@
832909 }
833910 skb_hwtstamps(skb)->hwtstamp =
834911 ktime_sub_ns(skb_hwtstamps(skb)->hwtstamp, adjust);
912
+
913
+ return 0;
835914 }
836915
837916 /**
....@@ -842,13 +921,15 @@
842921 * This function is meant to retrieve a timestamp from the internal registers
843922 * of the adapter and store it in the skb.
844923 **/
845
-void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
846
- struct sk_buff *skb)
924
+void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector, struct sk_buff *skb)
847925 {
848926 struct igb_adapter *adapter = q_vector->adapter;
849927 struct e1000_hw *hw = &adapter->hw;
850
- u64 regval;
851928 int adjust = 0;
929
+ u64 regval;
930
+
931
+ if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
932
+ return;
852933
853934 /* If this bit is set, then the RX registers contain the time stamp. No
854935 * other packet will be time stamped until we read these registers, so
....@@ -893,8 +974,8 @@
893974
894975 /**
895976 * igb_ptp_get_ts_config - get hardware time stamping config
896
- * @netdev:
897
- * @ifreq:
977
+ * @netdev: netdev struct
978
+ * @ifr: interface struct
898979 *
899980 * Get the hwtstamp_config settings to return to the user. Rather than attempt
900981 * to deconstruct the settings from the registers, just return a shadow copy
....@@ -989,7 +1070,7 @@
9891070 config->rx_filter = HWTSTAMP_FILTER_ALL;
9901071 break;
9911072 }
992
- /* fall through */
1073
+ fallthrough;
9931074 default:
9941075 config->rx_filter = HWTSTAMP_FILTER_NONE;
9951076 return -ERANGE;
....@@ -1077,8 +1158,8 @@
10771158
10781159 /**
10791160 * igb_ptp_set_ts_config - set hardware time stamping config
1080
- * @netdev:
1081
- * @ifreq:
1161
+ * @netdev: netdev struct
1162
+ * @ifr: interface struct
10821163 *
10831164 **/
10841165 int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
....@@ -1124,7 +1205,7 @@
11241205 adapter->ptp_caps.pps = 0;
11251206 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
11261207 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
1127
- adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
1208
+ adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_82576;
11281209 adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
11291210 adapter->ptp_caps.enable = igb_ptp_feature_enable;
11301211 adapter->cc.read = igb_ptp_read_82576;
....@@ -1143,7 +1224,7 @@
11431224 adapter->ptp_caps.pps = 0;
11441225 adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;
11451226 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
1146
- adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
1227
+ adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_82580;
11471228 adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
11481229 adapter->ptp_caps.enable = igb_ptp_feature_enable;
11491230 adapter->cc.read = igb_ptp_read_82580;
....@@ -1171,7 +1252,7 @@
11711252 adapter->ptp_caps.pin_config = adapter->sdp_config;
11721253 adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;
11731254 adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
1174
- adapter->ptp_caps.gettime64 = igb_ptp_gettime_i210;
1255
+ adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_i210;
11751256 adapter->ptp_caps.settime64 = igb_ptp_settime_i210;
11761257 adapter->ptp_caps.enable = igb_ptp_feature_enable_i210;
11771258 adapter->ptp_caps.verify = igb_ptp_verify_pin;
....@@ -1180,18 +1261,6 @@
11801261 adapter->ptp_clock = NULL;
11811262 return;
11821263 }
1183
-
1184
- spin_lock_init(&adapter->tmreg_lock);
1185
- INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
1186
-
1187
- if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
1188
- INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
1189
- igb_ptp_overflow_check);
1190
-
1191
- adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
1192
- adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
1193
-
1194
- igb_ptp_reset(adapter);
11951264
11961265 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
11971266 &adapter->pdev->dev);
....@@ -1202,6 +1271,18 @@
12021271 dev_info(&adapter->pdev->dev, "added PHC on %s\n",
12031272 adapter->netdev->name);
12041273 adapter->ptp_flags |= IGB_PTP_ENABLED;
1274
+
1275
+ spin_lock_init(&adapter->tmreg_lock);
1276
+ INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
1277
+
1278
+ if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
1279
+ INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
1280
+ igb_ptp_overflow_check);
1281
+
1282
+ adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
1283
+ adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
1284
+
1285
+ igb_ptp_reset(adapter);
12051286 }
12061287 }
12071288