net: Convert PHYs hwtstamp callback to use kernel_hwtstamp_config
authorKory Maincent <kory.maincent@bootlin.com>
Tue, 14 Nov 2023 11:28:29 +0000 (12:28 +0100)
committerDavid S. Miller <davem@davemloft.net>
Sat, 18 Nov 2023 14:52:56 +0000 (14:52 +0000)
The PHYs hwtstamp callback are still getting the timestamp config from
ifreq and using copy_from/to_user.
Get rid of these functions by using timestamp configuration in parameter.
This also allow to move on to kernel_hwtstamp_config and be similar to
net devices using the new ndo_hwstamp_get/set.

This adds the possibility to manipulate the timestamp configuration
from the kernel which was not possible with the copy_from/to_user.

Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/phy/bcm-phy-ptp.c
drivers/net/phy/dp83640.c
drivers/net/phy/micrel.c
drivers/net/phy/mscc/mscc_ptp.c
drivers/net/phy/nxp-c45-tja11xx.c
drivers/net/phy/phy.c
drivers/ptp/ptp_ines.c
include/linux/mii_timestamper.h
include/linux/phy.h

index cb4b91af5e17337cc22ba94efbf3f912c47fb084..617d384d4551ac31dd8a7752b24c9087883e70d2 100644 (file)
@@ -782,16 +782,13 @@ out:
 }
 
 static int bcm_ptp_hwtstamp(struct mii_timestamper *mii_ts,
-                           struct ifreq *ifr)
+                           struct kernel_hwtstamp_config *cfg,
+                           struct netlink_ext_ack *extack)
 {
        struct bcm_ptp_private *priv = mii2priv(mii_ts);
-       struct hwtstamp_config cfg;
        u16 mode, ctrl;
 
-       if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-               return -EFAULT;
-
-       switch (cfg.rx_filter) {
+       switch (cfg->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
                priv->hwts_rx = false;
                break;
@@ -804,14 +801,14 @@ static int bcm_ptp_hwtstamp(struct mii_timestamper *mii_ts,
        case HWTSTAMP_FILTER_PTP_V2_EVENT:
        case HWTSTAMP_FILTER_PTP_V2_SYNC:
        case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
-               cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
+               cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
                priv->hwts_rx = true;
                break;
        default:
                return -ERANGE;
        }
 
-       priv->tx_type = cfg.tx_type;
+       priv->tx_type = cfg->tx_type;
 
        ctrl  = priv->hwts_rx ? SLICE_RX_EN : 0;
        ctrl |= priv->tx_type != HWTSTAMP_TX_OFF ? SLICE_TX_EN : 0;
@@ -840,7 +837,7 @@ static int bcm_ptp_hwtstamp(struct mii_timestamper *mii_ts,
        /* purge existing data */
        skb_queue_purge(&priv->tx_queue);
 
-       return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
+       return 0;
 }
 
 static int bcm_ptp_ts_info(struct mii_timestamper *mii_ts,
index 2657be7cc049f863464fb165aef3a0b405603336..5c42c47dc56469349a0162e35e928812d339746e 100644 (file)
@@ -1207,22 +1207,20 @@ static irqreturn_t dp83640_handle_interrupt(struct phy_device *phydev)
        return IRQ_HANDLED;
 }
 
-static int dp83640_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
+static int dp83640_hwtstamp(struct mii_timestamper *mii_ts,
+                           struct kernel_hwtstamp_config *cfg,
+                           struct netlink_ext_ack *extack)
 {
        struct dp83640_private *dp83640 =
                container_of(mii_ts, struct dp83640_private, mii_ts);
-       struct hwtstamp_config cfg;
        u16 txcfg0, rxcfg0;
 
-       if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-               return -EFAULT;
-
-       if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
+       if (cfg->tx_type < 0 || cfg->tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
                return -ERANGE;
 
-       dp83640->hwts_tx_en = cfg.tx_type;
+       dp83640->hwts_tx_en = cfg->tx_type;
 
-       switch (cfg.rx_filter) {
+       switch (cfg->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
                dp83640->hwts_rx_en = 0;
                dp83640->layer = 0;
@@ -1234,7 +1232,7 @@ static int dp83640_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                dp83640->hwts_rx_en = 1;
                dp83640->layer = PTP_CLASS_L4;
                dp83640->version = PTP_CLASS_V1;
-               cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
+               cfg->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
                break;
        case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
        case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
@@ -1242,7 +1240,7 @@ static int dp83640_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                dp83640->hwts_rx_en = 1;
                dp83640->layer = PTP_CLASS_L4;
                dp83640->version = PTP_CLASS_V2;
-               cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
+               cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
                break;
        case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
        case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
@@ -1250,7 +1248,7 @@ static int dp83640_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                dp83640->hwts_rx_en = 1;
                dp83640->layer = PTP_CLASS_L2;
                dp83640->version = PTP_CLASS_V2;
-               cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
+               cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
                break;
        case HWTSTAMP_FILTER_PTP_V2_EVENT:
        case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -1258,7 +1256,7 @@ static int dp83640_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                dp83640->hwts_rx_en = 1;
                dp83640->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
                dp83640->version = PTP_CLASS_V2;
-               cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
+               cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
                break;
        default:
                return -ERANGE;
@@ -1292,7 +1290,7 @@ static int dp83640_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
 
        mutex_unlock(&dp83640->clock->extreg_lock);
 
-       return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
+       return 0;
 }
 
 static void rx_timestamp_work(struct work_struct *work)
index 08e3915001c3fd9c129c63eb0852c5a4b0b81167..99af1e500c6c250633df73391200023cff86554f 100644 (file)
@@ -2395,24 +2395,22 @@ static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
        lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
 }
 
-static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
+static int lan8814_hwtstamp(struct mii_timestamper *mii_ts,
+                           struct kernel_hwtstamp_config *config,
+                           struct netlink_ext_ack *extack)
 {
        struct kszphy_ptp_priv *ptp_priv =
                          container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
        struct phy_device *phydev = ptp_priv->phydev;
        struct lan8814_shared_priv *shared = phydev->shared->priv;
        struct lan8814_ptp_rx_ts *rx_ts, *tmp;
-       struct hwtstamp_config config;
        int txcfg = 0, rxcfg = 0;
        int pkt_ts_enable;
 
-       if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-               return -EFAULT;
+       ptp_priv->hwts_tx_type = config->tx_type;
+       ptp_priv->rx_filter = config->rx_filter;
 
-       ptp_priv->hwts_tx_type = config.tx_type;
-       ptp_priv->rx_filter = config.rx_filter;
-
-       switch (config.rx_filter) {
+       switch (config->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
                ptp_priv->layer = 0;
                ptp_priv->version = 0;
@@ -2458,13 +2456,13 @@ static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
                                      PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
 
-       if (config.rx_filter != HWTSTAMP_FILTER_NONE)
+       if (config->rx_filter != HWTSTAMP_FILTER_NONE)
                lan8814_config_ts_intr(ptp_priv->phydev, true);
        else
                lan8814_config_ts_intr(ptp_priv->phydev, false);
 
        mutex_lock(&shared->shared_lock);
-       if (config.rx_filter != HWTSTAMP_FILTER_NONE)
+       if (config->rx_filter != HWTSTAMP_FILTER_NONE)
                shared->ref++;
        else
                shared->ref--;
@@ -2488,7 +2486,7 @@ static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
        lan8814_flush_fifo(ptp_priv->phydev, false);
        lan8814_flush_fifo(ptp_priv->phydev, true);
 
-       return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
+       return 0;
 }
 
 static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
@@ -3703,21 +3701,19 @@ static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv *ptp_priv,
 #define LAN8841_PTP_TX_TIMESTAMP_EN            443
 #define LAN8841_PTP_TX_MOD                     445
 
-static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
+static int lan8841_hwtstamp(struct mii_timestamper *mii_ts,
+                           struct kernel_hwtstamp_config *config,
+                           struct netlink_ext_ack *extack)
 {
        struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
        struct phy_device *phydev = ptp_priv->phydev;
-       struct hwtstamp_config config;
        int txcfg = 0, rxcfg = 0;
        int pkt_ts_enable;
 
-       if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-               return -EFAULT;
+       ptp_priv->hwts_tx_type = config->tx_type;
+       ptp_priv->rx_filter = config->rx_filter;
 
-       ptp_priv->hwts_tx_type = config.tx_type;
-       ptp_priv->rx_filter = config.rx_filter;
-
-       switch (config.rx_filter) {
+       switch (config->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
                ptp_priv->layer = 0;
                ptp_priv->version = 0;
@@ -3771,13 +3767,13 @@ static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
 
        /* Now enable/disable the timestamping */
        lan8841_ptp_enable_processing(ptp_priv,
-                                     config.rx_filter != HWTSTAMP_FILTER_NONE);
+                                     config->rx_filter != HWTSTAMP_FILTER_NONE);
 
        skb_queue_purge(&ptp_priv->tx_queue);
 
        lan8841_ptp_flush_fifo(ptp_priv);
 
-       return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
+       return 0;
 }
 
 static bool lan8841_rxtstamp(struct mii_timestamper *mii_ts,
index cf728bfd83e22c53ebba22d056f376322b63875c..eb0b032cb613d1c91c798323664918707a3dd0a3 100644 (file)
@@ -1045,19 +1045,17 @@ static void vsc85xx_ts_reset_fifo(struct phy_device *phydev)
                             val);
 }
 
-static int vsc85xx_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
+static int vsc85xx_hwtstamp(struct mii_timestamper *mii_ts,
+                           struct kernel_hwtstamp_config *cfg,
+                           struct netlink_ext_ack *extack)
 {
        struct vsc8531_private *vsc8531 =
                container_of(mii_ts, struct vsc8531_private, mii_ts);
        struct phy_device *phydev = vsc8531->ptp->phydev;
-       struct hwtstamp_config cfg;
        bool one_step = false;
        u32 val;
 
-       if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-               return -EFAULT;
-
-       switch (cfg.tx_type) {
+       switch (cfg->tx_type) {
        case HWTSTAMP_TX_ONESTEP_SYNC:
                one_step = true;
                break;
@@ -1069,9 +1067,9 @@ static int vsc85xx_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                return -ERANGE;
        }
 
-       vsc8531->ptp->tx_type = cfg.tx_type;
+       vsc8531->ptp->tx_type = cfg->tx_type;
 
-       switch (cfg.rx_filter) {
+       switch (cfg->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
                break;
        case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
@@ -1084,7 +1082,7 @@ static int vsc85xx_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                return -ERANGE;
        }
 
-       vsc8531->ptp->rx_filter = cfg.rx_filter;
+       vsc8531->ptp->rx_filter = cfg->rx_filter;
 
        mutex_lock(&vsc8531->ts_lock);
 
@@ -1132,7 +1130,7 @@ static int vsc85xx_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
        vsc8531->ptp->configured = 1;
        mutex_unlock(&vsc8531->ts_lock);
 
-       return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
+       return 0;
 }
 
 static int vsc85xx_ts_info(struct mii_timestamper *mii_ts,
index 7ab080ff02dfa69d923fa4964d302593203740d0..780ad353cf55774e8ea2aa5becb9d335d6592d3e 100644 (file)
@@ -1022,24 +1022,21 @@ static bool nxp_c45_rxtstamp(struct mii_timestamper *mii_ts,
 }
 
 static int nxp_c45_hwtstamp(struct mii_timestamper *mii_ts,
-                           struct ifreq *ifreq)
+                           struct kernel_hwtstamp_config *cfg,
+                           struct netlink_ext_ack *extack)
 {
        struct nxp_c45_phy *priv = container_of(mii_ts, struct nxp_c45_phy,
                                                mii_ts);
        struct phy_device *phydev = priv->phydev;
        const struct nxp_c45_phy_data *data;
-       struct hwtstamp_config cfg;
 
-       if (copy_from_user(&cfg, ifreq->ifr_data, sizeof(cfg)))
-               return -EFAULT;
-
-       if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ON)
+       if (cfg->tx_type < 0 || cfg->tx_type > HWTSTAMP_TX_ON)
                return -ERANGE;
 
        data = nxp_c45_get_data(phydev);
-       priv->hwts_tx = cfg.tx_type;
+       priv->hwts_tx = cfg->tx_type;
 
-       switch (cfg.rx_filter) {
+       switch (cfg->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
                priv->hwts_rx = 0;
                break;
@@ -1047,7 +1044,7 @@ static int nxp_c45_hwtstamp(struct mii_timestamper *mii_ts,
        case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
        case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
                priv->hwts_rx = 1;
-               cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
+               cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
                break;
        default:
                return -ERANGE;
@@ -1074,7 +1071,7 @@ static int nxp_c45_hwtstamp(struct mii_timestamper *mii_ts,
                nxp_c45_clear_reg_field(phydev, &data->regmap->irq_egr_ts_en);
 
 nxp_c45_no_ptp_irq:
-       return copy_to_user(ifreq->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
+       return 0;
 }
 
 static int nxp_c45_ts_info(struct mii_timestamper *mii_ts,
index a5fa077650e8383ddd65ff2fff0925fc5a6f32c8..d058316666ba0d06fe5cf9413f2910ce7ad61307 100644 (file)
@@ -325,9 +325,13 @@ EXPORT_SYMBOL(phy_ethtool_ksettings_get);
 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
 {
        struct mii_ioctl_data *mii_data = if_mii(ifr);
+       struct kernel_hwtstamp_config kernel_cfg;
+       struct netlink_ext_ack extack = {};
        u16 val = mii_data->val_in;
        bool change_autoneg = false;
+       struct hwtstamp_config cfg;
        int prtad, devad;
+       int ret;
 
        switch (cmd) {
        case SIOCGMIIPHY:
@@ -411,8 +415,21 @@ int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
                return 0;
 
        case SIOCSHWTSTAMP:
-               if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
-                       return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
+               if (phydev->mii_ts && phydev->mii_ts->hwtstamp) {
+                       if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
+                               return -EFAULT;
+
+                       hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
+                       ret = phydev->mii_ts->hwtstamp(phydev->mii_ts, &kernel_cfg, &extack);
+                       if (ret)
+                               return ret;
+
+                       hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
+                       if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
+                               return -EFAULT;
+
+                       return 0;
+               }
                fallthrough;
 
        default:
index ed215b458183088a4a78b19b614ead1a2894dbd8..1d2940a7845571a86efe53dfb78726a0d567e112 100644 (file)
@@ -328,17 +328,15 @@ static u64 ines_find_txts(struct ines_port *port, struct sk_buff *skb)
        return ns;
 }
 
-static int ines_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
+static int ines_hwtstamp(struct mii_timestamper *mii_ts,
+                        struct kernel_hwtstamp_config *cfg,
+                        struct netlink_ext_ack *extack)
 {
        struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
        u32 cm_one_step = 0, port_conf, ts_stat_rx, ts_stat_tx;
-       struct hwtstamp_config cfg;
        unsigned long flags;
 
-       if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-               return -EFAULT;
-
-       switch (cfg.tx_type) {
+       switch (cfg->tx_type) {
        case HWTSTAMP_TX_OFF:
                ts_stat_tx = 0;
                break;
@@ -353,7 +351,7 @@ static int ines_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
                return -ERANGE;
        }
 
-       switch (cfg.rx_filter) {
+       switch (cfg->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
                ts_stat_rx = 0;
                break;
@@ -372,7 +370,7 @@ static int ines_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
        case HWTSTAMP_FILTER_PTP_V2_SYNC:
        case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
                ts_stat_rx = TS_ENABLE;
-               cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
+               cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
                break;
        default:
                return -ERANGE;
@@ -393,7 +391,7 @@ static int ines_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
 
        spin_unlock_irqrestore(&port->lock, flags);
 
-       return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
+       return 0;
 }
 
 static void ines_link_state(struct mii_timestamper *mii_ts,
index fa940bbaf8ae4cbaf250bf77fd0b72afa2e130f6..26b04f73f214b9f7a8e89dd10434c4d01689113c 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/device.h>
 #include <linux/ethtool.h>
 #include <linux/skbuff.h>
+#include <linux/net_tstamp.h>
 
 struct phy_device;
 
@@ -51,7 +52,8 @@ struct mii_timestamper {
                         struct sk_buff *skb, int type);
 
        int  (*hwtstamp)(struct mii_timestamper *mii_ts,
-                        struct ifreq *ifreq);
+                        struct kernel_hwtstamp_config *kernel_config,
+                        struct netlink_ext_ack *extack);
 
        void (*link_state)(struct mii_timestamper *mii_ts,
                           struct phy_device *phydev);
index 3cc52826f18e94288f965b6b2526e481e1beee79..e5f1f41e399c70aef54ff78608f839399d177312 100644 (file)
@@ -1560,9 +1560,11 @@ static inline bool phy_has_txtstamp(struct phy_device *phydev)
        return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
 }
 
-static inline int phy_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
+static inline int phy_hwtstamp(struct phy_device *phydev,
+                              struct kernel_hwtstamp_config *cfg,
+                              struct netlink_ext_ack *extack)
 {
-       return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
+       return phydev->mii_ts->hwtstamp(phydev->mii_ts, cfg, extack);
 }
 
 static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,