Skip to content

Commit 7c571ac

Browse files
jacob-kellerkuba-moo
authored andcommitted
net: ptp: introduce .supported_extts_flags to ptp_clock_info
The PTP_EXTTS_REQUEST(2) ioctl has a flags field which specifies how the external timestamp request should behave. This includes which edge of the signal to timestamp, as well as a specialized "offset" mode. It is expected that more flags will be added in the future. Driver authors routinely do not check the flags, often accepting requests with flags which they do not support. Even drivers which do check flags may not be future-proofed to reject flags not yet defined. Thus, any future flag additions often require manually updating drivers to reject these flags. This approach of hoping we catch flag checks during review, or playing whack-a-mole after the fact is the wrong approach. Introduce the "supported_extts_flags" field to the ptp_clock_info structure. This field defines the set of flags the device actually supports. Update the core character device logic to check this field and reject unsupported requests. Getting this right is somewhat tricky. First, to avoid unnecessary repetition and make basic functionality work when .supported_extts_flags is 0, the core always accepts the PTP_ENABLE_FEATURE flag. This flag is used to set the 'on' parameter to the .enable function and is thus always 'supported' by all drivers. For backwards compatibility, the PTP_RISING_EDGE and PTP_FALLING_EDGE flags are merely "hints" when using the old PTP_EXTTS_REQUEST ioctl, and are not expected to be enforced. If the user issues PTP_EXTTS_REQUEST2, the PTP_STRICT_FLAGS flag is added which is supposed to inform the driver to strictly validate the flags and reject unsupported requests. To handle this, first check if the driver reports PTP_STRICT_FLAGS support. If it does not, then always allow the PTP_RISING_EDGE and PTP_FALLING_EDGE flags. This keeps backwards compatibility with the original PTP_EXTTS_REQUEST ioctl where these flags are not guaranteed to be honored. This way, drivers which do not set the supported_extts_flags will continue to accept requests for the original PTP_EXTTS_REQUEST ioctl. The core will automatically reject requests with new flags, and correctly reject requests with PTP_STRICT_FLAGS, where the driver is supposed to strictly validate the flags. Update the various drivers, refactoring their validation logic into the .supported_extts_flags field. For consistency and readability, PTP_ENABLE_FEATURE is not set in the supported flags list, and PTP_EXTTS_EDGES is expanded to PTP_RISING_EDGE | PTP_FALLING_EDGE in all cases. Note the following driver files set n_ext_ts to a non-zero value but did not check flags at all: • drivers/net/ethernet/freescale/dpaa2/dpaa2-ptp.c • drivers/net/ethernet/freescale/enetc/enetc_ptp.c • drivers/net/ethernet/intel/i40e/i40e_ptp.c • drivers/net/ethernet/marvell/octeontx2/nic/otx2_ptp.c • drivers/net/ethernet/renesas/ravb_ptp.c • drivers/net/ethernet/renesas/rtsn.c • drivers/net/ethernet/renesas/rtsn.h • drivers/net/ethernet/ti/am65-cpts.c • drivers/net/ethernet/ti/cpts.h • drivers/net/ethernet/ti/icssg/icss_iep.c • drivers/net/ethernet/xscale/ptp_ixp46x.c • drivers/net/phy/bcm-phy-ptp.c • drivers/ptp/ptp_ocp.c • drivers/ptp/ptp_pch.c • drivers/ptp/ptp_qoriq.c These drivers behavior does change slightly: they will now reject the PTP_EXTTS_REQUEST2 ioctl, because they do not strictly validate their flags. This also makes them no longer incorrectly accept PTP_EXT_OFFSET. Also note that the renesas ravb driver does not support PTP_STRICT_FLAGS. We could leave the .supported_extts_flags as 0, but I added the PTP_RISING_EDGE | PTP_FALLING_EDGE since the driver previously manually validated these flags. This is equivalent to 0 because the core will allow these flags regardless unless PTP_STRICT_FLAGS is also set. Signed-off-by: Jacob Keller <[email protected]> Reviewed-by: Kory Maincent <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent a496d2f commit 7c571ac

File tree

17 files changed

+70
-111
lines changed

17 files changed

+70
-111
lines changed

drivers/net/dsa/mv88e6xxx/ptp.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,6 @@ static int mv88e6352_ptp_enable_extts(struct mv88e6xxx_chip *chip,
332332
int pin;
333333
int err;
334334

335-
/* Reject requests with unsupported flags */
336-
if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
337-
PTP_RISING_EDGE |
338-
PTP_FALLING_EDGE |
339-
PTP_STRICT_FLAGS))
340-
return -EOPNOTSUPP;
341-
342335
/* Reject requests to enable time stamping on both edges. */
343336
if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
344337
(rq->extts.flags & PTP_ENABLE_FEATURE) &&
@@ -566,6 +559,10 @@ int mv88e6xxx_ptp_setup(struct mv88e6xxx_chip *chip)
566559
chip->ptp_clock_info.verify = ptp_ops->ptp_verify;
567560
chip->ptp_clock_info.do_aux_work = mv88e6xxx_hwtstamp_work;
568561

562+
chip->ptp_clock_info.supported_extts_flags = PTP_RISING_EDGE |
563+
PTP_FALLING_EDGE |
564+
PTP_STRICT_FLAGS;
565+
569566
if (ptp_ops->set_ptp_cpu_port) {
570567
struct dsa_port *dp;
571568
int upstream = 0;

drivers/net/dsa/sja1105/sja1105_ptp.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -820,13 +820,6 @@ static int sja1105_extts_enable(struct sja1105_private *priv,
820820
if (extts->index != 0)
821821
return -EOPNOTSUPP;
822822

823-
/* Reject requests with unsupported flags */
824-
if (extts->flags & ~(PTP_ENABLE_FEATURE |
825-
PTP_RISING_EDGE |
826-
PTP_FALLING_EDGE |
827-
PTP_STRICT_FLAGS))
828-
return -EOPNOTSUPP;
829-
830823
/* We can only enable time stamping on both edges, sadly. */
831824
if ((extts->flags & PTP_STRICT_FLAGS) &&
832825
(extts->flags & PTP_ENABLE_FEATURE) &&
@@ -912,6 +905,9 @@ int sja1105_ptp_clock_register(struct dsa_switch *ds)
912905
.n_pins = 1,
913906
.n_ext_ts = 1,
914907
.n_per_out = 1,
908+
.supported_extts_flags = PTP_RISING_EDGE |
909+
PTP_FALLING_EDGE |
910+
PTP_STRICT_FLAGS,
915911
};
916912

917913
/* Only used on SJA1105 */

drivers/net/ethernet/intel/ice/ice_ptp.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,14 +1627,6 @@ static int ice_ptp_cfg_extts(struct ice_pf *pf, struct ptp_extts_request *rq,
16271627
int pin_desc_idx;
16281628
u8 tmr_idx;
16291629

1630-
/* Reject requests with unsupported flags */
1631-
1632-
if (rq->flags & ~(PTP_ENABLE_FEATURE |
1633-
PTP_RISING_EDGE |
1634-
PTP_FALLING_EDGE |
1635-
PTP_STRICT_FLAGS))
1636-
return -EOPNOTSUPP;
1637-
16381630
tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
16391631
chan = rq->index;
16401632

@@ -2740,6 +2732,10 @@ static void ice_ptp_set_caps(struct ice_pf *pf)
27402732
info->enable = ice_ptp_gpio_enable;
27412733
info->verify = ice_verify_pin;
27422734

2735+
info->supported_extts_flags = PTP_RISING_EDGE |
2736+
PTP_FALLING_EDGE |
2737+
PTP_STRICT_FLAGS;
2738+
27432739
switch (pf->hw.mac_type) {
27442740
case ICE_MAC_E810:
27452741
ice_ptp_set_funcs_e810(pf);

drivers/net/ethernet/intel/igb/igb_ptp.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,6 @@ static int igb_ptp_feature_enable_82580(struct ptp_clock_info *ptp,
502502

503503
switch (rq->type) {
504504
case PTP_CLK_REQ_EXTTS:
505-
/* Reject requests with unsupported flags */
506-
if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
507-
PTP_RISING_EDGE |
508-
PTP_FALLING_EDGE |
509-
PTP_STRICT_FLAGS))
510-
return -EOPNOTSUPP;
511-
512505
/* Both the rising and falling edge are timestamped */
513506
if (rq->extts.flags & PTP_STRICT_FLAGS &&
514507
(rq->extts.flags & PTP_ENABLE_FEATURE) &&
@@ -658,13 +651,6 @@ static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp,
658651

659652
switch (rq->type) {
660653
case PTP_CLK_REQ_EXTTS:
661-
/* Reject requests with unsupported flags */
662-
if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
663-
PTP_RISING_EDGE |
664-
PTP_FALLING_EDGE |
665-
PTP_STRICT_FLAGS))
666-
return -EOPNOTSUPP;
667-
668654
/* Reject requests failing to enable both edges. */
669655
if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
670656
(rq->extts.flags & PTP_ENABLE_FEATURE) &&
@@ -1356,6 +1342,9 @@ void igb_ptp_init(struct igb_adapter *adapter)
13561342
adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
13571343
adapter->ptp_caps.n_pins = IGB_N_SDP;
13581344
adapter->ptp_caps.pps = 0;
1345+
adapter->ptp_caps.supported_extts_flags = PTP_RISING_EDGE |
1346+
PTP_FALLING_EDGE |
1347+
PTP_STRICT_FLAGS;
13591348
adapter->ptp_caps.pin_config = adapter->sdp_config;
13601349
adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;
13611350
adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
@@ -1378,6 +1367,9 @@ void igb_ptp_init(struct igb_adapter *adapter)
13781367
adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
13791368
adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
13801369
adapter->ptp_caps.n_pins = IGB_N_SDP;
1370+
adapter->ptp_caps.supported_extts_flags = PTP_RISING_EDGE |
1371+
PTP_FALLING_EDGE |
1372+
PTP_STRICT_FLAGS;
13811373
adapter->ptp_caps.pps = 1;
13821374
adapter->ptp_caps.pin_config = adapter->sdp_config;
13831375
adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;

drivers/net/ethernet/intel/igc/igc_ptp.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,6 @@ static int igc_ptp_feature_enable_i225(struct ptp_clock_info *ptp,
257257

258258
switch (rq->type) {
259259
case PTP_CLK_REQ_EXTTS:
260-
/* Reject requests with unsupported flags */
261-
if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
262-
PTP_RISING_EDGE |
263-
PTP_FALLING_EDGE |
264-
PTP_STRICT_FLAGS))
265-
return -EOPNOTSUPP;
266-
267260
/* Reject requests failing to enable both edges. */
268261
if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
269262
(rq->extts.flags & PTP_ENABLE_FEATURE) &&
@@ -1146,6 +1139,9 @@ void igc_ptp_init(struct igc_adapter *adapter)
11461139
adapter->ptp_caps.pin_config = adapter->sdp_config;
11471140
adapter->ptp_caps.n_ext_ts = IGC_N_EXTTS;
11481141
adapter->ptp_caps.n_per_out = IGC_N_PEROUT;
1142+
adapter->ptp_caps.supported_extts_flags = PTP_RISING_EDGE |
1143+
PTP_FALLING_EDGE |
1144+
PTP_STRICT_FLAGS;
11491145
adapter->ptp_caps.n_pins = IGC_N_SDP;
11501146
adapter->ptp_caps.verify = igc_ptp_verify_pin;
11511147

drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,6 @@ static int mlx5_extts_configure(struct ptp_clock_info *ptp,
643643
int pin = -1;
644644
int err = 0;
645645

646-
/* Reject requests with unsupported flags */
647-
if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
648-
PTP_RISING_EDGE |
649-
PTP_FALLING_EDGE |
650-
PTP_STRICT_FLAGS))
651-
return -EOPNOTSUPP;
652-
653646
/* Reject requests to enable time stamping on both edges. */
654647
if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
655648
(rq->extts.flags & PTP_ENABLE_FEATURE) &&
@@ -1034,6 +1027,10 @@ static void mlx5_init_pin_config(struct mlx5_core_dev *mdev)
10341027
clock->ptp_info.verify = mlx5_ptp_verify;
10351028
clock->ptp_info.pps = 1;
10361029

1030+
clock->ptp_info.supported_extts_flags = PTP_RISING_EDGE |
1031+
PTP_FALLING_EDGE |
1032+
PTP_STRICT_FLAGS;
1033+
10371034
for (i = 0; i < clock->ptp_info.n_pins; i++) {
10381035
snprintf(clock->ptp_info.pin_config[i].name,
10391036
sizeof(clock->ptp_info.pin_config[i].name),

drivers/net/ethernet/microchip/lan743x_ptp.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,6 @@ static int lan743x_ptp_io_extts(struct lan743x_adapter *adapter, int on,
942942

943943
extts = &ptp->extts[index];
944944

945-
if (extts_request->flags & ~(PTP_ENABLE_FEATURE |
946-
PTP_RISING_EDGE |
947-
PTP_FALLING_EDGE |
948-
PTP_STRICT_FLAGS))
949-
return -EOPNOTSUPP;
950-
951945
if (on) {
952946
extts_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS, index);
953947
if (extts_pin < 0)
@@ -1543,6 +1537,9 @@ int lan743x_ptp_open(struct lan743x_adapter *adapter)
15431537
ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
15441538
ptp->ptp_clock_info.n_pins = n_pins;
15451539
ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
1540+
ptp->ptp_clock_info.supported_extts_flags = PTP_RISING_EDGE |
1541+
PTP_FALLING_EDGE |
1542+
PTP_STRICT_FLAGS;
15461543
ptp->ptp_clock_info.pin_config = ptp->pin_config;
15471544
ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
15481545
ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;

drivers/net/ethernet/microchip/lan966x/lan966x_ptp.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -917,12 +917,6 @@ static int lan966x_ptp_extts(struct ptp_clock_info *ptp,
917917
if (lan966x->ptp_ext_irq <= 0)
918918
return -EOPNOTSUPP;
919919

920-
/* Reject requests with unsupported flags */
921-
if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
922-
PTP_RISING_EDGE |
923-
PTP_STRICT_FLAGS))
924-
return -EOPNOTSUPP;
925-
926920
pin = ptp_find_pin(phc->clock, PTP_PF_EXTTS, rq->extts.index);
927921
if (pin == -1 || pin >= LAN966X_PHC_PINS_NUM)
928922
return -EINVAL;
@@ -978,6 +972,8 @@ static struct ptp_clock_info lan966x_ptp_clock_info = {
978972
.n_per_out = LAN966X_PHC_PINS_NUM,
979973
.n_ext_ts = LAN966X_PHC_PINS_NUM,
980974
.n_pins = LAN966X_PHC_PINS_NUM,
975+
.supported_extts_flags = PTP_RISING_EDGE |
976+
PTP_STRICT_FLAGS,
981977
};
982978

983979
static int lan966x_ptp_phc_init(struct lan966x *lan966x,

drivers/net/ethernet/renesas/ravb_ptp.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,6 @@ static int ravb_ptp_extts(struct ptp_clock_info *ptp,
176176
struct net_device *ndev = priv->ndev;
177177
unsigned long flags;
178178

179-
/* Reject requests with unsupported flags */
180-
if (req->flags & ~(PTP_ENABLE_FEATURE |
181-
PTP_RISING_EDGE |
182-
PTP_FALLING_EDGE))
183-
return -EOPNOTSUPP;
184-
185179
if (req->index)
186180
return -EINVAL;
187181

@@ -287,6 +281,7 @@ static const struct ptp_clock_info ravb_ptp_info = {
287281
.max_adj = 50000000,
288282
.n_ext_ts = N_EXT_TS,
289283
.n_per_out = N_PER_OUT,
284+
.supported_extts_flags = PTP_RISING_EDGE | PTP_FALLING_EDGE,
290285
.adjfine = ravb_ptp_adjfine,
291286
.adjtime = ravb_ptp_adjtime,
292287
.gettime64 = ravb_ptp_gettime64,

drivers/net/phy/dp83640.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,6 @@ static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
478478

479479
switch (rq->type) {
480480
case PTP_CLK_REQ_EXTTS:
481-
/* Reject requests with unsupported flags */
482-
if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
483-
PTP_RISING_EDGE |
484-
PTP_FALLING_EDGE |
485-
PTP_STRICT_FLAGS))
486-
return -EOPNOTSUPP;
487-
488481
/* Reject requests to enable time stamping on both edges. */
489482
if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
490483
(rq->extts.flags & PTP_ENABLE_FEATURE) &&
@@ -1002,6 +995,9 @@ static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
1002995
clock->caps.n_per_out = N_PER_OUT;
1003996
clock->caps.n_pins = DP83640_N_PINS;
1004997
clock->caps.pps = 0;
998+
clock->caps.supported_extts_flags = PTP_RISING_EDGE |
999+
PTP_FALLING_EDGE |
1000+
PTP_STRICT_FLAGS;
10051001
clock->caps.adjfine = ptp_dp83640_adjfine;
10061002
clock->caps.adjtime = ptp_dp83640_adjtime;
10071003
clock->caps.gettime64 = ptp_dp83640_gettime;

0 commit comments

Comments
 (0)