net: stmmac: implement the SIOCGHWTSTAMP ioctl
Subsystems (4)
PATCH 1/1 net: stmmac: implement the SIOCGHWTSTAMP ioctl
From: Tema <[email protected]>
This patch adds support for the SIOCGHWTSTAMP ioctl which enables user processes to read the current hwtstamp_config settings non-destructively.
--- drivers/net/ethernet/stmicro/stmmac/stmmac.h | 2 + .../net/ethernet/stmicro/stmmac/stmmac_main.c | 77 ++++++++++++------- 2 files changed, 52 insertions(+), 27 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac.h +2 -0
@@ -28,6 +28,7 @@#include <linux/pci.h> #include "common.h" #include <linux/ptp_clock_kernel.h> +#include <linux/net_tstamp.h> #include <linux/reset.h> struct stmmac_resources {
@@ -174,6 +175,7 @@ struct stmmac_priv {unsigned int mode;unsigned int chain_mode;int extend_desc; +struct hwtstamp_config tstamp_config;struct ptp_clock *ptp_clock;struct ptp_clock_info ptp_clock_ops;unsigned int default_addend;
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +50 -27
193 lines not on this page · raw message
Signature · 1 line
2.19.1
Hello Artem,
Please specify in patch subject whether this is for -net or -net-next. More comments inline.
Quoted 5 lines · Artem Panfilov
From: Tema <[email protected]>
This patch adds support for the SIOCGHWTSTAMP ioctl which enables user processes to read the current
hwtstamp_configsettings non-destructively.
I think you should only save the config structure *after* validating the parameters. i.e. use a local config and save it in priv only if ret == 0.
Quoted 54 lines
Signed-off-by Artem Panfilov <[email protected]>--- drivers/net/ethernet/stmicro/stmmac/stmmac.h | 2 + .../net/ethernet/stmicro/stmmac/stmmac_main.c | 77 ++++++++++++------- 2 files changed, 52 insertions(+), 27 deletions(-)drivers/net/ethernet/stmicro/stmmac/stmmac.h +2
-0@@ -28,6 +28,7 @@#include <linux/pci.h> #include "common.h" #include <linux/ptp_clock_kernel.h> +#include <linux/net_tstamp.h> #include <linux/reset.h> struct stmmac_resources {@@ -174,6 +175,7 @@ struct stmmac_priv {unsigned int mode;unsigned int chain_mode;int extend_desc; +struct hwtstamp_config tstamp_config;struct ptp_clock *ptp_clock;struct ptp_clock_info ptp_clock_ops;unsigned int default_addend;drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +3
-3@@ -534,7 +534,7 @@ static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p, }/** -* stmmac_hwtstamp_ioctl - control hardware timestamping. +* stmmac_hwtstamp_set - control hardware timestamping. * @dev: device pointer. * @ifr: An IOCTL specific structure, that can contain a pointer to * a proprietary structure used to pass information to the driver. @@ -544,10 +544,10 @@ static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,* Return Value: * 0 on success and an appropriate -ve integer on failure. */ -static int stmmac_hwtstamp_ioctl (struct net_device *dev,struct ifreq *ifr) +static int stmmac_hwtstamp_set (struct net_device *dev,struct ifreq *ifr) {struct stmmac_priv *priv =netdev_priv (dev); -struct hwtstamp_config config; +struct hwtstamp_config *config = &priv->tstamp_config;struct timespec64 now;u64 temp =0 ;u32 ptp_v2 =0 ;
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr) · line 576
Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1
-1@@ -572,31 +572,31 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)return -EOPNOTSUPP ; } -if (copy_from_user (&config, ifr->ifr_data, +if (copy_from_user (config, ifr->ifr_data,sizeof (struct hwtstamp_config )))
Please use "sizeof(*config)" instead. It makes the code more readable and occupy less characters.
Quoted 139 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +21
-21return -EFAULT ;netdev_dbg (priv->dev,"%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n" , - __func__, config.flags, config.tx_type, config.rx_filter); + __func__, config->flags, config->tx_type, config->rx_filter);/* reserved for future extensions */ -if (config.flags) +if (config->flags)return -EINVAL ; -if (config.tx_type !=HWTSTAMP_TX_OFF && - config.tx_type !=HWTSTAMP_TX_ON ) +if (config->tx_type !=HWTSTAMP_TX_OFF && + config->tx_type !=HWTSTAMP_TX_ON )return -ERANGE ;if (priv->adv_ts) { -switch (config.rx_filter) { +switch (config->rx_filter) {case HWTSTAMP_FILTER_NONE :/* time stamp no incoming packet at all */ - config.rx_filter =HWTSTAMP_FILTER_NONE ; + config->rx_filter =HWTSTAMP_FILTER_NONE ;break ;case HWTSTAMP_FILTER_PTP_V1_L4_EVENT :/* PTP v1, UDP, any kind of event packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_EVENT ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_EVENT ;/* take time stamp for all event messages */ if (xmac) snap_type_sel =PTP_GMAC4_TCR_SNAPTYPSEL_1 ;@@ -609,7 +609,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V1_L4_SYNC :/* PTP v1, UDP, Sync packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_SYNC ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_SYNC ;/* take time stamp for SYNC messages only */ ts_event_en =PTP_TCR_TSEVNTENA ;@@ -619,7 +619,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ :/* PTP v1, UDP, Delay_req packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ ;/* take time stamp for Delay_Req messages only */ ts_master_en =PTP_TCR_TSMSTRENA ; ts_event_en =PTP_TCR_TSEVNTENA ;@@ -630,7 +630,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V2_L4_EVENT :/* PTP v2, UDP, any kind of event packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V2_L4_EVENT ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V2_L4_EVENT ; ptp_v2 =PTP_TCR_TSVER2ENA ;/* take time stamp for all event messages */ if (xmac)@@ -644,7 +644,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V2_L4_SYNC :/* PTP v2, UDP, Sync packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V2_L4_SYNC ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V2_L4_SYNC ; ptp_v2 =PTP_TCR_TSVER2ENA ;/* take time stamp for SYNC messages only */ ts_event_en =PTP_TCR_TSEVNTENA ;@@ -655,7 +655,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ :/* PTP v2, UDP, Delay_req packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ ; ptp_v2 =PTP_TCR_TSVER2ENA ;/* take time stamp for Delay_Req messages only */ ts_master_en =PTP_TCR_TSMSTRENA ;@@ -667,7 +667,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V2_EVENT :/* PTP v2/802.AS1 any layer, any kind of event packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V2_EVENT ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V2_EVENT ; ptp_v2 =PTP_TCR_TSVER2ENA ;/* take time stamp for all event messages */ if (xmac)@@ -682,7 +682,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V2_SYNC :/* PTP v2/802.AS1, any layer, Sync packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V2_SYNC ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V2_SYNC ; ptp_v2 =PTP_TCR_TSVER2ENA ;/* take time stamp for SYNC messages only */ ts_event_en =PTP_TCR_TSEVNTENA ;@@ -694,7 +694,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ :/* PTP v2/802.AS1, any layer, Delay_req packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V2_DELAY_REQ ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V2_DELAY_REQ ; ptp_v2 =PTP_TCR_TSVER2ENA ;/* take time stamp for Delay_Req messages only */ ts_master_en =PTP_TCR_TSMSTRENA ;@@ -708,7 +708,7 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)case HWTSTAMP_FILTER_NTP_ALL :case HWTSTAMP_FILTER_ALL :/* time stamp any incoming packet */ - config.rx_filter =HWTSTAMP_FILTER_ALL ; + config->rx_filter =HWTSTAMP_FILTER_ALL ; tstamp_all =PTP_TCR_TSENALL ;break ;@@ -716,18 +716,18 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)return -ERANGE ; } }else { -switch (config.rx_filter) { +switch (config->rx_filter) {case HWTSTAMP_FILTER_NONE : - config.rx_filter =HWTSTAMP_FILTER_NONE ; + config->rx_filter =HWTSTAMP_FILTER_NONE ;break ;default :/* PTP v1, UDP, any kind of event packet */ - config.rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_EVENT ; + config->rx_filter =HWTSTAMP_FILTER_PTP_V1_L4_EVENT ;break ; } } - priv->hwts_rx_en = ((config.rx_filter ==HWTSTAMP_FILTER_NONE ) ?0 :1 ); - priv->hwts_tx_en = config.tx_type ==HWTSTAMP_TX_ON ; + priv->hwts_rx_en = ((config->rx_filter ==HWTSTAMP_FILTER_NONE ) ?0 :1 ); + priv->hwts_tx_en = config->tx_type ==HWTSTAMP_TX_ON ;if (!priv->hwts_tx_en && !priv->hwts_rx_en)stmmac_config_hw_tstamping (priv, priv->ptpaddr,0 );
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr) · line 769
Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2
-1@@ -765,7 +765,28 @@ static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr) (u32)now.tv_sec, now.tv_nsec); } - return copy_to_user(ifr->ifr_data, &config, +return copy_to_user (ifr->ifr_data, config, +sizeof (struct hwtstamp_config )) ? -EFAULT :0 ;
sizeof(*config) also.
Quoted 11 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +11
-0+} + +/** +* stmmac_hwtstamp_get - read hardware timestamping. +* @dev: device pointer. +* @ifr: An IOCTL specific structure, that can contain a pointer to +* a proprietary structure used to pass information to the driver. +* Description: +* This function obtain the current hardware timestamping settings +as requested. +*/
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr) · line 790
Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +9
-0+static int stmmac_hwtstamp_get (struct net_device *dev,struct ifreq *ifr) +{ +struct stmmac_priv *priv =netdev_priv (dev); +struct hwtstamp_config *config = &priv->tstamp_config; + +if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp)) +return -EOPNOTSUPP ; + +return copy_to_user (ifr->ifr_data, config,sizeof (struct hwtstamp_config )) ? -EFAULT :0 ;
sizeof(*config).
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) · line 3791
Quoted 8 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1
-1}@@ -3767,7 +3788,9 @@ static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) ret =phy_mii_ioctl (dev->phydev, rq, cmd);break ;case SIOCSHWTSTAMP : - ret =stmmac_hwtstamp_ioctl (dev, rq); + ret =stmmac_hwtstamp_set (dev, rq);
Missing break.
Thanks,
Jose Miguel Abreu
Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2
-0+case SIOCGHWTSTAMP : + ret =stmmac_hwtstamp_get (dev, rq);break ;default :break ;