net: dsa: microchip: ptp: add 4 bytes in tail tag when ptp enabled
authorArun Ramadoss <arun.ramadoss@microchip.com>
Tue, 10 Jan 2023 08:49:20 +0000 (14:19 +0530)
committerDavid S. Miller <davem@davemloft.net>
Fri, 13 Jan 2023 08:40:40 +0000 (08:40 +0000)
When the PTP is enabled in hardware bit 6 of PTP_MSG_CONF1 register, the
transmit frame needs additional 4 bytes before the tail tag. It is
needed for all the transmission packets irrespective of PTP packets or
not.
The 4-byte timestamp field is 0 for frames other than Pdelay_Resp. For
the one-step Pdelay_Resp, the switch needs the receive timestamp of the
Pdelay_Req message so that it can put the turnaround time in the
correction field.
Since PTP has to be enabled for both Transmission and reception
timestamping, driver needs to track of the tx and rx setting of the all
the user ports in the switch.
Two flags hw_tx_en and hw_rx_en are added in ksz_port to track the
timestampping setting of each port. When any one of ports has tx or rx
timestampping enabled, bit 6 of PTP_MSG_CONF1 is set and it is indicated
to tag_ksz.c through tagger bytes. This flag adds 4 additional bytes to
the tail tag.  When tx and rx timestamping of all the ports are disabled,
then 4 bytes are not added.

Tested using hwstamp -i <interface>

Signed-off-by: Arun Ramadoss <arun.ramadoss@microchip.com>
Reviewed-by: Vladimir Oltean <olteanv@gmail.com> # mostly api
Signed-off-by: David S. Miller <davem@davemloft.net>
MAINTAINERS
drivers/net/dsa/microchip/ksz_common.h
drivers/net/dsa/microchip/ksz_ptp.c
include/linux/dsa/ksz_common.h [new file with mode: 0644]
net/dsa/tag_ksz.c

index c3d796e12bb8eb538618dd6e01dfd758a2cdc07f..5d6a5d51fca0ac6313de0da28f479a77108e91b5 100644 (file)
@@ -13710,6 +13710,7 @@ S:      Maintained
 F:     Documentation/devicetree/bindings/net/dsa/microchip,ksz.yaml
 F:     Documentation/devicetree/bindings/net/dsa/microchip,lan937x.yaml
 F:     drivers/net/dsa/microchip/*
+F:     include/linux/dsa/ksz_common.h
 F:     include/linux/platform_data/microchip-ksz.h
 F:     net/dsa/tag_ksz.c
 
index a5ce7ec30ba2a9d27431f10a76f9e0a4e188c166..641aca78ef05eab33a7bfd5e7d5785272bd2abe9 100644 (file)
@@ -104,6 +104,8 @@ struct ksz_port {
        u8 num;
 #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)
        struct hwtstamp_config tstamp_config;
+       bool hwts_tx_en;
+       bool hwts_rx_en;
 #endif
 };
 
index 6f67476716106f3d76d3b1a64e8f28c4fd1748c4..5281aeb84db6e0ed6bf38fc397d3c5a9246271a7 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (C) 2022 Microchip Technology Inc.
  */
 
+#include <linux/dsa/ksz_common.h>
 #include <linux/kernel.h>
 #include <linux/ptp_classify.h>
 #include <linux/ptp_clock_kernel.h>
 #define KSZ_PTP_INC_NS 40ULL  /* HW clock is incremented every 40 ns (by 40) */
 #define KSZ_PTP_SUBNS_BITS 32
 
+static int ksz_ptp_enable_mode(struct ksz_device *dev)
+{
+       struct ksz_tagger_data *tagger_data = ksz_tagger_data(dev->ds);
+       struct ksz_port *prt;
+       struct dsa_port *dp;
+       bool tag_en = false;
+
+       dsa_switch_for_each_user_port(dp, dev->ds) {
+               prt = &dev->ports[dp->index];
+               if (prt->hwts_tx_en || prt->hwts_rx_en) {
+                       tag_en = true;
+                       break;
+               }
+       }
+
+       tagger_data->hwtstamp_set_state(dev->ds, tag_en);
+
+       return ksz_rmw16(dev, REG_PTP_MSG_CONF1, PTP_ENABLE,
+                        tag_en ? PTP_ENABLE : 0);
+}
+
 /* The function is return back the capability of timestamping feature when
  * requested through ethtool -T <interface> utility
  */
@@ -67,6 +89,7 @@ int ksz_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr)
 }
 
 static int ksz_set_hwtstamp_config(struct ksz_device *dev,
+                                  struct ksz_port *prt,
                                   struct hwtstamp_config *config)
 {
        if (config->flags)
@@ -74,7 +97,10 @@ static int ksz_set_hwtstamp_config(struct ksz_device *dev,
 
        switch (config->tx_type) {
        case HWTSTAMP_TX_OFF:
+               prt->hwts_tx_en = false;
+               break;
        case HWTSTAMP_TX_ONESTEP_P2P:
+               prt->hwts_tx_en = true;
                break;
        default:
                return -ERANGE;
@@ -82,25 +108,29 @@ static int ksz_set_hwtstamp_config(struct ksz_device *dev,
 
        switch (config->rx_filter) {
        case HWTSTAMP_FILTER_NONE:
+               prt->hwts_rx_en = false;
                break;
        case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
        case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
                config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
+               prt->hwts_rx_en = true;
                break;
        case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
        case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
                config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
+               prt->hwts_rx_en = true;
                break;
        case HWTSTAMP_FILTER_PTP_V2_EVENT:
        case HWTSTAMP_FILTER_PTP_V2_SYNC:
                config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
+               prt->hwts_rx_en = true;
                break;
        default:
                config->rx_filter = HWTSTAMP_FILTER_NONE;
                return -ERANGE;
        }
 
-       return 0;
+       return ksz_ptp_enable_mode(dev);
 }
 
 int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr)
@@ -116,7 +146,7 @@ int ksz_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr)
        if (ret)
                return ret;
 
-       ret = ksz_set_hwtstamp_config(dev, &config);
+       ret = ksz_set_hwtstamp_config(dev, prt, &config);
        if (ret)
                return ret;
 
diff --git a/include/linux/dsa/ksz_common.h b/include/linux/dsa/ksz_common.h
new file mode 100644 (file)
index 0000000..d2a5416
--- /dev/null
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Microchip switch tag common header
+ *
+ * Copyright (C) 2022 Microchip Technology Inc.
+ */
+
+#ifndef _NET_DSA_KSZ_COMMON_H_
+#define _NET_DSA_KSZ_COMMON_H_
+
+#include <net/dsa.h>
+
+struct ksz_tagger_data {
+       void (*hwtstamp_set_state)(struct dsa_switch *ds, bool on);
+};
+
+static inline struct ksz_tagger_data *
+ksz_tagger_data(struct dsa_switch *ds)
+{
+       return ds->tagger_data;
+}
+
+#endif /* _NET_DSA_KSZ_COMMON_H_ */
index 080e5c369f5b928067816da651da7c6de4c2f740..420a12853676bf4bdba06e1363f954e1a7ab3d12 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (c) 2017 Microchip Technology
  */
 
+#include <linux/dsa/ksz_common.h>
 #include <linux/etherdevice.h>
 #include <linux/list.h>
 #include <net/dsa.h>
 #define LAN937X_NAME "lan937x"
 
 /* Typically only one byte is used for tail tag. */
+#define KSZ_PTP_TAG_LEN                        4
 #define KSZ_EGRESS_TAG_LEN             1
 #define KSZ_INGRESS_TAG_LEN            1
 
+#define KSZ_HWTS_EN  0
+
+struct ksz_tagger_private {
+       struct ksz_tagger_data data; /* Must be first */
+       unsigned long state;
+};
+
+static struct ksz_tagger_private *
+ksz_tagger_private(struct dsa_switch *ds)
+{
+       return ds->tagger_data;
+}
+
+static void ksz_hwtstamp_set_state(struct dsa_switch *ds, bool on)
+{
+       struct ksz_tagger_private *priv = ksz_tagger_private(ds);
+
+       if (on)
+               set_bit(KSZ_HWTS_EN, &priv->state);
+       else
+               clear_bit(KSZ_HWTS_EN, &priv->state);
+}
+
+static void ksz_disconnect(struct dsa_switch *ds)
+{
+       struct ksz_tagger_private *priv = ds->tagger_data;
+
+       kfree(priv);
+       ds->tagger_data = NULL;
+}
+
+static int ksz_connect(struct dsa_switch *ds)
+{
+       struct ksz_tagger_data *tagger_data;
+       struct ksz_tagger_private *priv;
+
+       priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+       if (!priv)
+               return -ENOMEM;
+
+       /* Export functions for switch driver use */
+       tagger_data = &priv->data;
+       tagger_data->hwtstamp_set_state = ksz_hwtstamp_set_state;
+       ds->tagger_data = priv;
+
+       return 0;
+}
+
 static struct sk_buff *ksz_common_rcv(struct sk_buff *skb,
                                      struct net_device *dev,
                                      unsigned int port, unsigned int len)
@@ -92,10 +142,12 @@ DSA_TAG_DRIVER(ksz8795_netdev_ops);
 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795, KSZ8795_NAME);
 
 /*
- * For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
+ * For Ingress (Host -> KSZ9477), 2/6 bytes are added before FCS.
  * ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|tag1(1byte)|
+ * FCS(4bytes)
  * ---------------------------------------------------------------------------
+ * ts   : time stamp (Present only if PTP is enabled in the Hardware)
  * tag0 : Prioritization (not used now)
  * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
  *
@@ -114,6 +166,21 @@ MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795, KSZ8795_NAME);
 #define KSZ9477_TAIL_TAG_OVERRIDE      BIT(9)
 #define KSZ9477_TAIL_TAG_LOOKUP                BIT(10)
 
+/* Time stamp tag *needs* to be inserted if PTP is enabled in hardware.
+ * Regardless of Whether it is a PTP frame or not.
+ */
+static void ksz_xmit_timestamp(struct dsa_port *dp, struct sk_buff *skb)
+{
+       struct ksz_tagger_private *priv;
+
+       priv = ksz_tagger_private(dp->ds);
+
+       if (!test_bit(KSZ_HWTS_EN, &priv->state))
+               return;
+
+       put_unaligned_be32(0, skb_put(skb, KSZ_PTP_TAG_LEN));
+}
+
 static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
                                    struct net_device *dev)
 {
@@ -126,6 +193,8 @@ static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
                return NULL;
 
        /* Tag encoding */
+       ksz_xmit_timestamp(dp, skb);
+
        tag = skb_put(skb, KSZ9477_INGRESS_TAG_LEN);
        addr = skb_mac_header(skb);
 
@@ -158,7 +227,9 @@ static const struct dsa_device_ops ksz9477_netdev_ops = {
        .proto  = DSA_TAG_PROTO_KSZ9477,
        .xmit   = ksz9477_xmit,
        .rcv    = ksz9477_rcv,
-       .needed_tailroom = KSZ9477_INGRESS_TAG_LEN,
+       .connect = ksz_connect,
+       .disconnect = ksz_disconnect,
+       .needed_tailroom = KSZ9477_INGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
 };
 
 DSA_TAG_DRIVER(ksz9477_netdev_ops);
@@ -178,6 +249,8 @@ static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
                return NULL;
 
        /* Tag encoding */
+       ksz_xmit_timestamp(dp, skb);
+
        tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
        addr = skb_mac_header(skb);
 
@@ -194,16 +267,20 @@ static const struct dsa_device_ops ksz9893_netdev_ops = {
        .proto  = DSA_TAG_PROTO_KSZ9893,
        .xmit   = ksz9893_xmit,
        .rcv    = ksz9477_rcv,
-       .needed_tailroom = KSZ_INGRESS_TAG_LEN,
+       .connect = ksz_connect,
+       .disconnect = ksz_disconnect,
+       .needed_tailroom = KSZ_INGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
 };
 
 DSA_TAG_DRIVER(ksz9893_netdev_ops);
 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893, KSZ9893_NAME);
 
-/* For xmit, 2 bytes are added before FCS.
+/* For xmit, 2/6 bytes are added before FCS.
  * ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|tag1(1byte)|
+ * FCS(4bytes)
  * ---------------------------------------------------------------------------
+ * ts   : time stamp (Present only if PTP is enabled in the Hardware)
  * tag0 : represents tag override, lookup and valid
  * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x80=port8)
  *
@@ -232,6 +309,8 @@ static struct sk_buff *lan937x_xmit(struct sk_buff *skb,
        if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
                return NULL;
 
+       ksz_xmit_timestamp(dp, skb);
+
        tag = skb_put(skb, LAN937X_EGRESS_TAG_LEN);
 
        val = BIT(dp->index);
@@ -252,7 +331,9 @@ static const struct dsa_device_ops lan937x_netdev_ops = {
        .proto  = DSA_TAG_PROTO_LAN937X,
        .xmit   = lan937x_xmit,
        .rcv    = ksz9477_rcv,
-       .needed_tailroom = LAN937X_EGRESS_TAG_LEN,
+       .connect = ksz_connect,
+       .disconnect = ksz_disconnect,
+       .needed_tailroom = LAN937X_EGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
 };
 
 DSA_TAG_DRIVER(lan937x_netdev_ops);