From 6f45968dd2e4585818ea6acf8420bfed38246604 Mon Sep 17 00:00:00 2001 From: Fabio Estevam Date: Thu, 10 Nov 2022 18:50:59 -0300 Subject: [PATCH net-next] net: dsa: Introduce dsa_port_supports_hwtstamp() Currently, during SIOCGHWTSTAMP/SIOCSHWTSTAMP ioctl operations, the criteria for deciding whether PTP operations on the master port is allowed or not is simply based on the presence of the port_hwtstamp_get/set() operations. This is not a robust method because on the mv88e6xxx driver, for example, the port_hwtstamp_get()/set() hooks are always implemented. Even when CONFIG_NET_DSA_MV88E6XXX_PTP is disabled there are still stub implementations for these functions that return -EOPNOTSUPP. Instead of relying on the presence of port_hwtstamp_get/set(), introduce the dsa_port_supports_hwtstamp() function, which checks the return value of port_hwtstamp_get/set() to decide. Without this change: # hwstamp_ctl -i eth0 SIOCGHWTSTAMP failed: Device or resource busy With this change applied, it is possible to set and get the timestamping options: # hwstamp_ctl -i eth0 current settings: tx_type 0 rx_filter 0 # hwstamp_ctl -i eth0 -r 1 -t 1 current settings: tx_type 0 rx_filter 0 new settings: tx_type 1 rx_filter 1 Tested on a custom i.MX8MN board with a 88E6320 switch. Signed-off-by: Fabio Estevam --- net/dsa/master.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/net/dsa/master.c b/net/dsa/master.c index 2851e44c4cf0..f078db44a8b3 100644 --- a/net/dsa/master.c +++ b/net/dsa/master.c @@ -187,29 +187,45 @@ static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset, } } -static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) +static bool dsa_port_supports_hwtstamp(struct net_device *dev, struct ifreq *ifr, int cmd) { struct dsa_port *cpu_dp = dev->dsa_ptr; struct dsa_switch *ds = cpu_dp->ds; - struct dsa_switch_tree *dst; + int port = cpu_dp->index; int err = -EOPNOTSUPP; - struct dsa_port *dp; - - dst = ds->dst; switch (cmd) { case SIOCGHWTSTAMP: + if (ds->ops->port_hwtstamp_get) { + err = ds->ops->port_hwtstamp_get(ds, port, ifr); + if (err == -EOPNOTSUPP) + return false; + } + break; case SIOCSHWTSTAMP: - /* Deny PTP operations on master if there is at least one - * switch in the tree that is PTP capable. - */ - list_for_each_entry(dp, &dst->ports, list) - if (dp->ds->ops->port_hwtstamp_get || - dp->ds->ops->port_hwtstamp_set) - return -EBUSY; + if (ds->ops->port_hwtstamp_set) { + err = ds->ops->port_hwtstamp_set(ds, port, ifr); + if (err == -EOPNOTSUPP) + return false; + } break; } + return !!err; +} + +static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) +{ + struct dsa_port *cpu_dp = dev->dsa_ptr; + struct dsa_switch *ds = cpu_dp->ds; + struct dsa_switch_tree *dst; + int err = -EOPNOTSUPP; + + dst = ds->dst; + + if (dsa_port_supports_hwtstamp(dev, ifr, cmd)) + return -EBUSY; + if (dev->netdev_ops->ndo_eth_ioctl) err = dev->netdev_ops->ndo_eth_ioctl(dev, ifr, cmd); -- 2.25.1