diff mbox series

[v4,2/9] ptp: Add generic ptp message type function

Message ID 20200818103251.20421-3-kurt@linutronix.de
State Accepted
Delegated to: David Miller
Headers show
Series ptp: Add generic helper functions | expand

Commit Message

Kurt Kanzenbach Aug. 18, 2020, 10:32 a.m. UTC
The message type is located at different offsets within the ptp header depending
on the ptp version (v1 or v2). Therefore, drivers which also deal with ptp v1
have some code for it.

Extract this into a helper function for drivers to be used.

Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
Reviewed-by: Richard Cochran <richardcochran@gmail.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
---
 include/linux/ptp_classify.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Comments

Russell King (Oracle) Aug. 18, 2020, 10:40 a.m. UTC | #1
On Tue, Aug 18, 2020 at 12:32:44PM +0200, Kurt Kanzenbach wrote:
> The message type is located at different offsets within the ptp header depending
> on the ptp version (v1 or v2). Therefore, drivers which also deal with ptp v1
> have some code for it.
> 
> Extract this into a helper function for drivers to be used.
> 
> Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
> Reviewed-by: Richard Cochran <richardcochran@gmail.com>
> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
> ---
>  include/linux/ptp_classify.h | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/include/linux/ptp_classify.h b/include/linux/ptp_classify.h
> index 996f31e8f35d..39bad015d1d6 100644
> --- a/include/linux/ptp_classify.h
> +++ b/include/linux/ptp_classify.h
> @@ -96,6 +96,31 @@ unsigned int ptp_classify_raw(const struct sk_buff *skb);
>   */
>  struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
>  
> +/**
> + * ptp_get_msgtype - Extract ptp message type from given header
> + * @hdr: ptp header
> + * @type: type of the packet (see ptp_classify_raw())
> + *
> + * This function returns the message type for a given ptp header. It takes care
> + * of the different ptp header versions (v1 or v2).
> + *
> + * Return: The message type
> + */
> +static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
> +				 unsigned int type)
> +{
> +	u8 msgtype;
> +
> +	if (unlikely(type & PTP_CLASS_V1)) {
> +		/* msg type is located at the control field for ptp v1 */
> +		msgtype = hdr->control;
> +	} else {
> +		msgtype = hdr->tsmt & 0x0f;
> +	}
> +
> +	return msgtype;
> +}

Are there 256 different message types in V1? I wonder how hardware
that uses the message type as an index into a 16-bit lookup table
and supports both V1 and V2 cope with that (such as the Marvell PTP
implementation found in their DSA switches and PHYs.)
Kurt Kanzenbach Aug. 19, 2020, 5:50 a.m. UTC | #2
On Tue Aug 18 2020, Russell King wrote:
> On Tue, Aug 18, 2020 at 12:32:44PM +0200, Kurt Kanzenbach wrote:
>> The message type is located at different offsets within the ptp header depending
>> on the ptp version (v1 or v2). Therefore, drivers which also deal with ptp v1
>> have some code for it.
>> 
>> Extract this into a helper function for drivers to be used.
>> 
>> Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
>> Reviewed-by: Richard Cochran <richardcochran@gmail.com>
>> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
>> ---
>>  include/linux/ptp_classify.h | 25 +++++++++++++++++++++++++
>>  1 file changed, 25 insertions(+)
>> 
>> diff --git a/include/linux/ptp_classify.h b/include/linux/ptp_classify.h
>> index 996f31e8f35d..39bad015d1d6 100644
>> --- a/include/linux/ptp_classify.h
>> +++ b/include/linux/ptp_classify.h
>> @@ -96,6 +96,31 @@ unsigned int ptp_classify_raw(const struct sk_buff *skb);
>>   */
>>  struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
>>  
>> +/**
>> + * ptp_get_msgtype - Extract ptp message type from given header
>> + * @hdr: ptp header
>> + * @type: type of the packet (see ptp_classify_raw())
>> + *
>> + * This function returns the message type for a given ptp header. It takes care
>> + * of the different ptp header versions (v1 or v2).
>> + *
>> + * Return: The message type
>> + */
>> +static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
>> +				 unsigned int type)
>> +{
>> +	u8 msgtype;
>> +
>> +	if (unlikely(type & PTP_CLASS_V1)) {
>> +		/* msg type is located at the control field for ptp v1 */
>> +		msgtype = hdr->control;
>> +	} else {
>> +		msgtype = hdr->tsmt & 0x0f;
>> +	}
>> +
>> +	return msgtype;
>> +}
>
> Are there 256 different message types in V1?

There are only a few messages in PTP V1. But, the control field which
indicates the message type is a byte whereas in PTP v2 it's a nibble.

Thanks,
Kurt
diff mbox series

Patch

diff --git a/include/linux/ptp_classify.h b/include/linux/ptp_classify.h
index 996f31e8f35d..39bad015d1d6 100644
--- a/include/linux/ptp_classify.h
+++ b/include/linux/ptp_classify.h
@@ -96,6 +96,31 @@  unsigned int ptp_classify_raw(const struct sk_buff *skb);
  */
 struct ptp_header *ptp_parse_header(struct sk_buff *skb, unsigned int type);
 
+/**
+ * ptp_get_msgtype - Extract ptp message type from given header
+ * @hdr: ptp header
+ * @type: type of the packet (see ptp_classify_raw())
+ *
+ * This function returns the message type for a given ptp header. It takes care
+ * of the different ptp header versions (v1 or v2).
+ *
+ * Return: The message type
+ */
+static inline u8 ptp_get_msgtype(const struct ptp_header *hdr,
+				 unsigned int type)
+{
+	u8 msgtype;
+
+	if (unlikely(type & PTP_CLASS_V1)) {
+		/* msg type is located at the control field for ptp v1 */
+		msgtype = hdr->control;
+	} else {
+		msgtype = hdr->tsmt & 0x0f;
+	}
+
+	return msgtype;
+}
+
 void __init ptp_classifier_init(void);
 #else
 static inline void ptp_classifier_init(void)