diff mbox series

[net-next,v4,2/6] net: dsa: Add convenience functions for frame handling

Message ID 20220906063450.3698671-3-mattias.forsblad@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: dsa: mv88e6xxx: qca8k: rmon: Add RMU support | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 20 this patch: 20
netdev/cc_maintainers success CCed 9 of 9 maintainers
netdev/build_clang success Errors and warnings before: 13 this patch: 13
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 20 this patch: 20
netdev/checkpatch warning WARNING: line length of 99 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Mattias Forsblad Sept. 6, 2022, 6:34 a.m. UTC
Add common control functions for drivers that need
to send and wait for control frames.

Signed-off-by: Mattias Forsblad <mattias.forsblad@gmail.com>
---
 include/net/dsa.h | 13 +++++++++++++
 net/dsa/dsa.c     | 28 ++++++++++++++++++++++++++++
 net/dsa/dsa2.c    |  2 ++
 3 files changed, 43 insertions(+)

Comments

Andrew Lunn Sept. 6, 2022, 12:43 p.m. UTC | #1
On Tue, Sep 06, 2022 at 08:34:46AM +0200, Mattias Forsblad wrote:
> Add common control functions for drivers that need
> to send and wait for control frames.

It would be nice to explain why a custom complete is needed. Ideally,
it should not be needed at all.

> Signed-off-by: Mattias Forsblad <mattias.forsblad@gmail.com>
> ---
>  include/net/dsa.h | 13 +++++++++++++
>  net/dsa/dsa.c     | 28 ++++++++++++++++++++++++++++
>  net/dsa/dsa2.c    |  2 ++
>  3 files changed, 43 insertions(+)
> 
> diff --git a/include/net/dsa.h b/include/net/dsa.h
> index f2ce12860546..70a358641235 100644
> --- a/include/net/dsa.h
> +++ b/include/net/dsa.h
> @@ -495,6 +495,8 @@ struct dsa_switch {
>  	unsigned int		max_num_bridges;
>  
>  	unsigned int		num_ports;
> +
> +	struct completion	inband_done;
>  };
>  
>  static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
> @@ -1390,6 +1392,17 @@ void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
>  void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
>  				unsigned int count);
>  
> +int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
> +			 struct completion *completion, unsigned long timeout);

Blank line please.

> +static inline void dsa_switch_inband_complete(struct dsa_switch *ds, struct completion *completion)
> +{
> +	/* Custom completion? */
> +	if (completion)
> +		complete(completion);
> +	else
> +		complete(&ds->inband_done);
> +}
> +
>  #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
>  static int __init dsa_tag_driver_module_init(void)			\
>  {									\
> diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
> index be7b320cda76..2d7add779b6f 100644
> --- a/net/dsa/dsa.c
> +++ b/net/dsa/dsa.c
> @@ -324,6 +324,34 @@ int dsa_switch_resume(struct dsa_switch *ds)
>  EXPORT_SYMBOL_GPL(dsa_switch_resume);
>  #endif
>  
> +int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
> +			 struct completion *completion, unsigned long timeout)
> +{
> +	int ret;
> +	struct completion *com;

Reverse christmas tree. Longest lines first.

> +
> +	/* Custom completion? */
> +	if (completion)
> +		com = completion;
> +	else
> +		com = &ds->inband_done;
> +
> +	reinit_completion(com);
> +
> +	if (skb)
> +		dev_queue_xmit(skb);
> +
> +	ret = wait_for_completion_timeout(com, msecs_to_jiffies(timeout));
> +	if (ret <= 0) {
> +		dev_dbg(ds->dev, "DSA inband: timeout waiting for answer\n");
> +
> +		return -ETIMEDOUT;
> +	}

It looks like wait_for_completion_timeout() can return a negative
error code. You should return that error code, not replace it with
-ETIMEDOUT. If it returns 0, then it has timed out, and returning
-ETIMEDOUT does make sense. If the completion is indicated before the
timeout, the return value is the remaining time. So you can return a
positive number here. It is worth documenting that, since a common
patterns is:

	err = dsa_switch_inband_tx()
	if (err)
		return err;

does not work in this case.

     Andrew
Florian Fainelli Sept. 6, 2022, 9:44 p.m. UTC | #2
On 9/5/2022 11:34 PM, Mattias Forsblad wrote:
> Add common control functions for drivers that need
> to send and wait for control frames.
> 
> Signed-off-by: Mattias Forsblad <mattias.forsblad@gmail.com>

With the feedback from Andrew addressed:

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Mattias Forsblad Sept. 7, 2022, 6:19 a.m. UTC | #3
On 2022-09-06 14:43, Andrew Lunn wrote:
> On Tue, Sep 06, 2022 at 08:34:46AM +0200, Mattias Forsblad wrote:
>> Add common control functions for drivers that need
>> to send and wait for control frames.
> 
> It would be nice to explain why a custom complete is needed. Ideally,
> it should not be needed at all.
> 

My first approach was with without a custom complete as I only used
one single complete instance. However, when migrating the qca8k driver
I noticed they use two different complete instances, one in
qca8k_mgmt_eth_data and one in qca8k_mib_eth_data. This leads
to the suggestion that the qca8k implementation could have several
requests in-flight, thus the custom completion parameter.

>> Signed-off-by: Mattias Forsblad <mattias.forsblad@gmail.com>
>> ---
>>  include/net/dsa.h | 13 +++++++++++++
>>  net/dsa/dsa.c     | 28 ++++++++++++++++++++++++++++
>>  net/dsa/dsa2.c    |  2 ++
>>  3 files changed, 43 insertions(+)
>>
>> diff --git a/include/net/dsa.h b/include/net/dsa.h
>> index f2ce12860546..70a358641235 100644
>> --- a/include/net/dsa.h
>> +++ b/include/net/dsa.h
>> @@ -495,6 +495,8 @@ struct dsa_switch {
>>  	unsigned int		max_num_bridges;
>>  
>>  	unsigned int		num_ports;
>> +
>> +	struct completion	inband_done;
>>  };
>>  
>>  static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
>> @@ -1390,6 +1392,17 @@ void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
>>  void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
>>  				unsigned int count);
>>  
>> +int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
>> +			 struct completion *completion, unsigned long timeout);
> 
> Blank line please.
> 

Will fix.

>> +static inline void dsa_switch_inband_complete(struct dsa_switch *ds, struct completion *completion)
>> +{
>> +	/* Custom completion? */
>> +	if (completion)
>> +		complete(completion);
>> +	else
>> +		complete(&ds->inband_done);
>> +}
>> +
>>  #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
>>  static int __init dsa_tag_driver_module_init(void)			\
>>  {									\
>> diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
>> index be7b320cda76..2d7add779b6f 100644
>> --- a/net/dsa/dsa.c
>> +++ b/net/dsa/dsa.c
>> @@ -324,6 +324,34 @@ int dsa_switch_resume(struct dsa_switch *ds)
>>  EXPORT_SYMBOL_GPL(dsa_switch_resume);
>>  #endif
>>  
>> +int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
>> +			 struct completion *completion, unsigned long timeout)
>> +{
>> +	int ret;
>> +	struct completion *com;
> 
> Reverse christmas tree. Longest lines first.
> 

Duh, again? Sorry, will fix.

>> +
>> +	/* Custom completion? */
>> +	if (completion)
>> +		com = completion;
>> +	else
>> +		com = &ds->inband_done;
>> +
>> +	reinit_completion(com);
>> +
>> +	if (skb)
>> +		dev_queue_xmit(skb);
>> +
>> +	ret = wait_for_completion_timeout(com, msecs_to_jiffies(timeout));
>> +	if (ret <= 0) {
>> +		dev_dbg(ds->dev, "DSA inband: timeout waiting for answer\n");
>> +
>> +		return -ETIMEDOUT;
>> +	}
> 
> It looks like wait_for_completion_timeout() can return a negative
> error code. You should return that error code, not replace it with
> -ETIMEDOUT. If it returns 0, then it has timed out, and returning
> -ETIMEDOUT does make sense. If the completion is indicated before the
> timeout, the return value is the remaining time. So you can return a
> positive number here. It is worth documenting that, since a common
> patterns is:
> 
> 	err = dsa_switch_inband_tx()
> 	if (err)
> 		return err;
> 
> does not work in this case.
> 
>      Andrew

Will fix.
	
	Mattias
Paolo Abeni Sept. 8, 2022, 11:32 a.m. UTC | #4
On Tue, 2022-09-06 at 08:34 +0200, Mattias Forsblad wrote:
> Add common control functions for drivers that need
> to send and wait for control frames.
> 
> Signed-off-by: Mattias Forsblad <mattias.forsblad@gmail.com>
> ---
>  include/net/dsa.h | 13 +++++++++++++
>  net/dsa/dsa.c     | 28 ++++++++++++++++++++++++++++
>  net/dsa/dsa2.c    |  2 ++
>  3 files changed, 43 insertions(+)
> 
> diff --git a/include/net/dsa.h b/include/net/dsa.h
> index f2ce12860546..70a358641235 100644
> --- a/include/net/dsa.h
> +++ b/include/net/dsa.h
> @@ -495,6 +495,8 @@ struct dsa_switch {
>  	unsigned int		max_num_bridges;
>  
>  	unsigned int		num_ports;
> +
> +	struct completion	inband_done;
>  };
>  
>  static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
> @@ -1390,6 +1392,17 @@ void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
>  void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
>  				unsigned int count);
>  
> +int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
> +			 struct completion *completion, unsigned long timeout);
> +static inline void dsa_switch_inband_complete(struct dsa_switch *ds, struct completion *completion)
> +{
> +	/* Custom completion? */
> +	if (completion)
> +		complete(completion);
> +	else
> +		complete(&ds->inband_done);
> +}
> +
>  #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
>  static int __init dsa_tag_driver_module_init(void)			\
>  {									\
> diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
> index be7b320cda76..2d7add779b6f 100644
> --- a/net/dsa/dsa.c
> +++ b/net/dsa/dsa.c
> @@ -324,6 +324,34 @@ int dsa_switch_resume(struct dsa_switch *ds)
>  EXPORT_SYMBOL_GPL(dsa_switch_resume);
>  #endif
>  
> +int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
> +			 struct completion *completion, unsigned long timeout)
> +{
> +	int ret;
> +	struct completion *com;
> +
> +	/* Custom completion? */
> +	if (completion)
> +		com = completion;
> +	else
> +		com = &ds->inband_done;

Since a new version is need, you can as well do:

	com = completion ? : &ds->inband_done;

Thanks!

Paolo
diff mbox series

Patch

diff --git a/include/net/dsa.h b/include/net/dsa.h
index f2ce12860546..70a358641235 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -495,6 +495,8 @@  struct dsa_switch {
 	unsigned int		max_num_bridges;
 
 	unsigned int		num_ports;
+
+	struct completion	inband_done;
 };
 
 static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
@@ -1390,6 +1392,17 @@  void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
 				unsigned int count);
 
+int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
+			 struct completion *completion, unsigned long timeout);
+static inline void dsa_switch_inband_complete(struct dsa_switch *ds, struct completion *completion)
+{
+	/* Custom completion? */
+	if (completion)
+		complete(completion);
+	else
+		complete(&ds->inband_done);
+}
+
 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count)	\
 static int __init dsa_tag_driver_module_init(void)			\
 {									\
diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index be7b320cda76..2d7add779b6f 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -324,6 +324,34 @@  int dsa_switch_resume(struct dsa_switch *ds)
 EXPORT_SYMBOL_GPL(dsa_switch_resume);
 #endif
 
+int dsa_switch_inband_tx(struct dsa_switch *ds, struct sk_buff *skb,
+			 struct completion *completion, unsigned long timeout)
+{
+	int ret;
+	struct completion *com;
+
+	/* Custom completion? */
+	if (completion)
+		com = completion;
+	else
+		com = &ds->inband_done;
+
+	reinit_completion(com);
+
+	if (skb)
+		dev_queue_xmit(skb);
+
+	ret = wait_for_completion_timeout(com, msecs_to_jiffies(timeout));
+	if (ret <= 0) {
+		dev_dbg(ds->dev, "DSA inband: timeout waiting for answer\n");
+
+		return -ETIMEDOUT;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(dsa_switch_inband_tx);
+
 static struct packet_type dsa_pack_type __read_mostly = {
 	.type	= cpu_to_be16(ETH_P_XDSA),
 	.func	= dsa_switch_rcv,
diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c
index ed56c7a554b8..a1b3ecfdffb8 100644
--- a/net/dsa/dsa2.c
+++ b/net/dsa/dsa2.c
@@ -1746,6 +1746,8 @@  static int dsa_switch_probe(struct dsa_switch *ds)
 		dsa_tree_put(dst);
 	}
 
+	init_completion(&ds->inband_done);
+
 	return err;
 }