diff mbox series

[v3,6/9] ethernet: ti: cpts: Use generic helper function

Message ID 20200730080048.32553-7-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/cpts.c | 37 +++++++++-------------------------
 1 file changed, 9 insertions(+), 28 deletions(-)

Comments

Florian Fainelli Aug. 2, 2020, 8:22 p.m. UTC | #1
On 7/30/2020 1:00 AM, 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>
> ---
[snip]
> -	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);

Same comment as patch 5 would probably apply here as well, with using
ntohs():

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
--
Florian
Grygorii Strashko Aug. 5, 2020, 6:52 p.m. UTC | #2
On 02/08/2020 23:22, Florian Fainelli wrote:
> 
> 
> On 7/30/2020 1:00 AM, 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>
>> ---
> [snip]
>> -	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);
> 
> Same comment as patch 5 would probably apply here as well, with using
> ntohs():
> 
> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>


As reported in [1] this patch as is broke cpts and below diff on top restore it

[1] https://lore.kernel.org/netdev/20200805152503.GB9122@hoboy/T/#mcf2bd0322805e6706ee9fe4f10805e657fd0103e
diff mbox series

Patch

diff --git a/drivers/net/ethernet/ti/cpts.c b/drivers/net/ethernet/ti/cpts.c
index 7c55d395de2c..2c5c05620e6e 100644
--- a/drivers/net/ethernet/ti/cpts.c
+++ b/drivers/net/ethernet/ti/cpts.c
@@ -446,41 +446,22 @@  static const struct ptp_clock_info cpts_info = {
 static int cpts_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;
-	u16 *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 = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
-	*mtype_seqid = (*msgtype & MESSAGE_TYPE_MASK) << MESSAGE_TYPE_SHIFT;
-	*mtype_seqid |= (ntohs(*seqid) & SEQUENCE_ID_MASK) << SEQUENCE_ID_SHIFT;
+	*mtype_seqid  = (msgtype & MESSAGE_TYPE_MASK) << MESSAGE_TYPE_SHIFT;
+	*mtype_seqid |= (seqid & SEQUENCE_ID_MASK) << SEQUENCE_ID_SHIFT;
 
 	return 1;
 }