diff mbox series

[v3,5/9] ethernet: ti: am65-cpts: Use generic helper function

Message ID 20200730080048.32553-6-kurt@linutronix.de
State Changes Requested
Delegated to: David Miller
Headers show
Series ptp: Add generic helper functions | expand

Commit Message

Kurt Kanzenbach July 30, 2020, 8 a.m. UTC
In order to reduce code duplication between ptp drivers, generic helper
functions were introduced. Use them.

Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
---
 drivers/net/ethernet/ti/am65-cpts.c | 37 +++++++----------------------
 1 file changed, 9 insertions(+), 28 deletions(-)

Comments

Grygorii Strashko July 30, 2020, 9:19 a.m. UTC | #1
On 30/07/2020 11:00, Kurt Kanzenbach wrote:
> In order to reduce code duplication between ptp drivers, generic helper
> functions were introduced. Use them.
> 
> Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
> ---
>   drivers/net/ethernet/ti/am65-cpts.c | 37 +++++++----------------------
>   1 file changed, 9 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/net/ethernet/ti/am65-cpts.c b/drivers/net/ethernet/ti/am65-cpts.c
> index c59a289e428c..2548324afa42 100644
> --- a/drivers/net/ethernet/ti/am65-cpts.c
> +++ b/drivers/net/ethernet/ti/am65-cpts.c
> @@ -748,42 +748,23 @@ EXPORT_SYMBOL_GPL(am65_cpts_rx_enable);
>   static int am65_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
>   {
>   	unsigned int ptp_class = ptp_classify_raw(skb);
> -	u8 *msgtype, *data = skb->data;
> -	unsigned int offset = 0;
> -	__be16 *seqid;
> +	struct ptp_header *hdr;
> +	u8 msgtype;
> +	u16 seqid;
>   
>   	if (ptp_class == PTP_CLASS_NONE)
>   		return 0;
>   
> -	if (ptp_class & PTP_CLASS_VLAN)
> -		offset += VLAN_HLEN;
> -
> -	switch (ptp_class & PTP_CLASS_PMASK) {
> -	case PTP_CLASS_IPV4:
> -		offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
> -		break;
> -	case PTP_CLASS_IPV6:
> -		offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
> -		break;
> -	case PTP_CLASS_L2:
> -		offset += ETH_HLEN;
> -		break;
> -	default:
> -		return 0;
> -	}
> -
> -	if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
> +	hdr = ptp_parse_header(skb, ptp_class);
> +	if (!hdr)
>   		return 0;
>   
> -	if (unlikely(ptp_class & PTP_CLASS_V1))
> -		msgtype = data + offset + OFF_PTP_CONTROL;
> -	else
> -		msgtype = data + offset;
> +	msgtype = ptp_get_msgtype(hdr, ptp_class);
> +	seqid	= be16_to_cpu(hdr->sequence_id);

Is there any reason to not use "ntohs()"?

>   
> -	seqid = (__be16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
> -	*mtype_seqid = (*msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) &
> +	*mtype_seqid  = (msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) &
>   			AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK;
> -	*mtype_seqid |= (ntohs(*seqid) & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
> +	*mtype_seqid |= (seqid & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
>   
>   	return 1;
>   }
> 

I'll try to test it today.
Thank you.
Kurt Kanzenbach July 30, 2020, 9:36 a.m. UTC | #2
On Thu Jul 30 2020, Grygorii Strashko wrote:
> On 30/07/2020 11:00, Kurt Kanzenbach wrote:
>> +	msgtype = ptp_get_msgtype(hdr, ptp_class);
>> +	seqid	= be16_to_cpu(hdr->sequence_id);
>
> Is there any reason to not use "ntohs()"?

This is just my personal preference, because I think it's more
readable. Internally ntohs() uses be16_to_cpu(). There's no technical
reason for it.

>
>>   
>> -	seqid = (__be16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
>> -	*mtype_seqid = (*msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) &
>> +	*mtype_seqid  = (msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) &
>>   			AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK;
>> -	*mtype_seqid |= (ntohs(*seqid) & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
>> +	*mtype_seqid |= (seqid & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
>>   
>>   	return 1;
>>   }
>> 
>
> I'll try to test it today.
> Thank you.

Thanks,
Kurt
Arnd Bergmann July 30, 2020, 10:24 a.m. UTC | #3
On Thu, Jul 30, 2020 at 11:41 AM Kurt Kanzenbach <kurt@linutronix.de> wrote:
> On Thu Jul 30 2020, Grygorii Strashko wrote:
> > On 30/07/2020 11:00, Kurt Kanzenbach wrote:
> >> +    msgtype = ptp_get_msgtype(hdr, ptp_class);
> >> +    seqid   = be16_to_cpu(hdr->sequence_id);
> >
> > Is there any reason to not use "ntohs()"?
>
> This is just my personal preference, because I think it's more
> readable. Internally ntohs() uses be16_to_cpu(). There's no technical
> reason for it.

I think for traditional reasons, code in net/* tends to use ntohs()
while code in drivers/*  tends to use be16_to_cpu().

In drivers/net/* the two are used roughly the same, though I guess
one could make the argument that be16_to_cpu() would be
more appropriate for data structures exchanged with hardware
while ntohs() makes sense on data structures sent over the
network.

     Arnd
Kurt Kanzenbach July 31, 2020, 11:48 a.m. UTC | #4
On Thu Jul 30 2020, Arnd Bergmann wrote:
> On Thu, Jul 30, 2020 at 11:41 AM Kurt Kanzenbach <kurt@linutronix.de> wrote:
>> On Thu Jul 30 2020, Grygorii Strashko wrote:
>> > On 30/07/2020 11:00, Kurt Kanzenbach wrote:
>> >> +    msgtype = ptp_get_msgtype(hdr, ptp_class);
>> >> +    seqid   = be16_to_cpu(hdr->sequence_id);
>> >
>> > Is there any reason to not use "ntohs()"?
>>
>> This is just my personal preference, because I think it's more
>> readable. Internally ntohs() uses be16_to_cpu(). There's no technical
>> reason for it.
>
> I think for traditional reasons, code in net/* tends to use ntohs()
> while code in drivers/*  tends to use be16_to_cpu().
>
> In drivers/net/* the two are used roughly the same, though I guess
> one could make the argument that be16_to_cpu() would be
> more appropriate for data structures exchanged with hardware
> while ntohs() makes sense on data structures sent over the
> network.

I see, makes sense. I could simply keep it the way it was, or?

Thanks,
Kurt
Grygorii Strashko July 31, 2020, 12:55 p.m. UTC | #5
On 31/07/2020 14:48, Kurt Kanzenbach wrote:
> On Thu Jul 30 2020, Arnd Bergmann wrote:
>> On Thu, Jul 30, 2020 at 11:41 AM Kurt Kanzenbach <kurt@linutronix.de> wrote:
>>> On Thu Jul 30 2020, Grygorii Strashko wrote:
>>>> On 30/07/2020 11:00, Kurt Kanzenbach wrote:
>>>>> +    msgtype = ptp_get_msgtype(hdr, ptp_class);
>>>>> +    seqid   = be16_to_cpu(hdr->sequence_id);
>>>>
>>>> Is there any reason to not use "ntohs()"?
>>>
>>> This is just my personal preference, because I think it's more
>>> readable. Internally ntohs() uses be16_to_cpu(). There's no technical
>>> reason for it.
>>
>> I think for traditional reasons, code in net/* tends to use ntohs()
>> while code in drivers/*  tends to use be16_to_cpu().
>>
>> In drivers/net/* the two are used roughly the same, though I guess
>> one could make the argument that be16_to_cpu() would be
>> more appropriate for data structures exchanged with hardware
>> while ntohs() makes sense on data structures sent over the
>> network.
> 
> I see, makes sense. I could simply keep it the way it was, or?

  I prefer ntohs() as this packet data.
Kurt Kanzenbach July 31, 2020, 1:10 p.m. UTC | #6
On Fri Jul 31 2020, Grygorii Strashko wrote:
> On 31/07/2020 14:48, Kurt Kanzenbach wrote:
>> On Thu Jul 30 2020, Arnd Bergmann wrote:
>>> On Thu, Jul 30, 2020 at 11:41 AM Kurt Kanzenbach <kurt@linutronix.de> wrote:
>>>> On Thu Jul 30 2020, Grygorii Strashko wrote:
>>>>> On 30/07/2020 11:00, Kurt Kanzenbach wrote:
>>>>>> +    msgtype = ptp_get_msgtype(hdr, ptp_class);
>>>>>> +    seqid   = be16_to_cpu(hdr->sequence_id);
>>>>>
>>>>> Is there any reason to not use "ntohs()"?
>>>>
>>>> This is just my personal preference, because I think it's more
>>>> readable. Internally ntohs() uses be16_to_cpu(). There's no technical
>>>> reason for it.
>>>
>>> I think for traditional reasons, code in net/* tends to use ntohs()
>>> while code in drivers/*  tends to use be16_to_cpu().
>>>
>>> In drivers/net/* the two are used roughly the same, though I guess
>>> one could make the argument that be16_to_cpu() would be
>>> more appropriate for data structures exchanged with hardware
>>> while ntohs() makes sense on data structures sent over the
>>> network.
>> 
>> I see, makes sense. I could simply keep it the way it was, or?
>
>   I prefer ntohs() as this packet data.

OK. I'll change it in the next iteration.

Thanks,
Kurt
diff mbox series

Patch

diff --git a/drivers/net/ethernet/ti/am65-cpts.c b/drivers/net/ethernet/ti/am65-cpts.c
index c59a289e428c..2548324afa42 100644
--- a/drivers/net/ethernet/ti/am65-cpts.c
+++ b/drivers/net/ethernet/ti/am65-cpts.c
@@ -748,42 +748,23 @@  EXPORT_SYMBOL_GPL(am65_cpts_rx_enable);
 static int am65_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
 {
 	unsigned int ptp_class = ptp_classify_raw(skb);
-	u8 *msgtype, *data = skb->data;
-	unsigned int offset = 0;
-	__be16 *seqid;
+	struct ptp_header *hdr;
+	u8 msgtype;
+	u16 seqid;
 
 	if (ptp_class == PTP_CLASS_NONE)
 		return 0;
 
-	if (ptp_class & PTP_CLASS_VLAN)
-		offset += VLAN_HLEN;
-
-	switch (ptp_class & PTP_CLASS_PMASK) {
-	case PTP_CLASS_IPV4:
-		offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
-		break;
-	case PTP_CLASS_IPV6:
-		offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
-		break;
-	case PTP_CLASS_L2:
-		offset += ETH_HLEN;
-		break;
-	default:
-		return 0;
-	}
-
-	if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
+	hdr = ptp_parse_header(skb, ptp_class);
+	if (!hdr)
 		return 0;
 
-	if (unlikely(ptp_class & PTP_CLASS_V1))
-		msgtype = data + offset + OFF_PTP_CONTROL;
-	else
-		msgtype = data + offset;
+	msgtype = ptp_get_msgtype(hdr, ptp_class);
+	seqid	= be16_to_cpu(hdr->sequence_id);
 
-	seqid = (__be16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
-	*mtype_seqid = (*msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) &
+	*mtype_seqid  = (msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) &
 			AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK;
-	*mtype_seqid |= (ntohs(*seqid) & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
+	*mtype_seqid |= (seqid & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK);
 
 	return 1;
 }