linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jose Abreu <jose.abreu@synopsys.com>
To: Artem Panfilov <panfilov.artyom@gmail.com>
Cc: Tema <panfilovartyom@gmail.com>,
	Giuseppe Cavallaro <peppe.cavallaro@st.com>,
	Alexandre Torgue <alexandre.torgue@st.com>,
	"Jose Abreu" <jose.abreu@synopsys.com>,
	"David S. Miller" <davem@davemloft.net>,
	Maxime Coquelin <mcoquelin.stm32@gmail.com>,
	<netdev@vger.kernel.org>,
	<linux-stm32@st-md-mailman.stormreply.com>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/1] net: stmmac: implement the SIOCGHWTSTAMP ioctl
Date: Fri, 18 Jan 2019 13:21:37 +0000	[thread overview]
Message-ID: <c0404f52-e875-ffb4-dcf4-05b4b5c4579b@synopsys.com> (raw)
In-Reply-To: <20190117191536.24699-1-panfilov.artyom@gmail.com>

Hello Artem,

Please specify in patch subject whether this is for -net or
-net-next. More comments inline.

On 1/17/2019 7:15 PM, Artem Panfilov wrote:
> From: Tema <panfilovartyom@gmail.com>
> 
> This patch adds support for the SIOCGHWTSTAMP ioctl which enables user
> processes to read the current hwtstamp_config settings
> 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.

> 
> Signed-off-by: Artem Panfilov <panfilov.artyom@gmail.com>
> ---
>  drivers/net/ethernet/stmicro/stmmac/stmmac.h  |  2 +
>  .../net/ethernet/stmicro/stmmac/stmmac_main.c | 77 ++++++++++++-------
>  2 files changed, 52 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> index 63e1064b27a2..f9b42b0c20f4 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
> @@ -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;
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> index 5afba69981cf..a6dc40a70428 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> @@ -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;
> @@ -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.

>  		return -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);
> @@ -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.

> +}
> +
> +/**
> + *  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.
> + */
> +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).

>  }
>  
> @@ -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

> +	case SIOCGHWTSTAMP:
> +		ret = stmmac_hwtstamp_get(dev, rq);
>  		break;
>  	default:
>  		break;
> 

      reply	other threads:[~2019-01-18 13:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-17 19:15 [PATCH 1/1] net: stmmac: implement the SIOCGHWTSTAMP ioctl Artem Panfilov
2019-01-18 13:21 ` Jose Abreu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c0404f52-e875-ffb4-dcf4-05b4b5c4579b@synopsys.com \
    --to=jose.abreu@synopsys.com \
    --cc=alexandre.torgue@st.com \
    --cc=davem@davemloft.net \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=panfilov.artyom@gmail.com \
    --cc=panfilovartyom@gmail.com \
    --cc=peppe.cavallaro@st.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).