From: Tristram Ha <tristram...@microchip.com>

Modify tag_ksz.c so that tail tag code can be used by other KSZ switch
drivers.

Signed-off-by: Tristram Ha <tristram...@microchip.com>
Reviewed-by: Woojung Huh <woojung....@microchip.com>
---
 drivers/net/dsa/microchip/Kconfig   |  2 +-
 drivers/net/dsa/microchip/ksz9477.c |  2 +-
 include/net/dsa.h                   |  2 +-
 net/dsa/Kconfig                     |  4 ++
 net/dsa/dsa.c                       |  8 +--
 net/dsa/dsa_priv.h                  |  2 +-
 net/dsa/tag_ksz.c                   | 99 ++++++++++++++++++++++++++-----------
 7 files changed, 81 insertions(+), 38 deletions(-)

diff --git a/drivers/net/dsa/microchip/Kconfig 
b/drivers/net/dsa/microchip/Kconfig
index a8caf92..bea29fd 100644
--- a/drivers/net/dsa/microchip/Kconfig
+++ b/drivers/net/dsa/microchip/Kconfig
@@ -4,7 +4,7 @@ config NET_DSA_MICROCHIP_KSZ_COMMON
 menuconfig NET_DSA_MICROCHIP_KSZ9477
        tristate "Microchip KSZ9477 series switch support"
        depends on NET_DSA
-       select NET_DSA_TAG_KSZ
+       select NET_DSA_TAG_KSZ9477
        select NET_DSA_MICROCHIP_KSZ_COMMON
        help
          This driver adds support for Microchip KSZ9477 switch chips.
diff --git a/drivers/net/dsa/microchip/ksz9477.c 
b/drivers/net/dsa/microchip/ksz9477.c
index 7b810af..c399fa2 100644
--- a/drivers/net/dsa/microchip/ksz9477.c
+++ b/drivers/net/dsa/microchip/ksz9477.c
@@ -344,7 +344,7 @@ static void ksz9477_port_init_cnt(struct ksz_device *dev, 
int port)
 static enum dsa_tag_protocol ksz9477_get_tag_protocol(struct dsa_switch *ds,
                                                      int port)
 {
-       return DSA_TAG_PROTO_KSZ;
+       return DSA_TAG_PROTO_KSZ9477;
 }
 
 static int ksz9477_phy_read16(struct dsa_switch *ds, int addr, int reg)
diff --git a/include/net/dsa.h b/include/net/dsa.h
index 23690c4..8e54a4c 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -36,7 +36,7 @@ enum dsa_tag_protocol {
        DSA_TAG_PROTO_DSA,
        DSA_TAG_PROTO_EDSA,
        DSA_TAG_PROTO_GSWIP,
-       DSA_TAG_PROTO_KSZ,
+       DSA_TAG_PROTO_KSZ9477,
        DSA_TAG_PROTO_LAN9303,
        DSA_TAG_PROTO_MTK,
        DSA_TAG_PROTO_QCA,
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index 48c4191..91e5297 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -44,6 +44,10 @@ config NET_DSA_TAG_GSWIP
 config NET_DSA_TAG_KSZ
        bool
 
+config NET_DSA_TAG_KSZ9477
+       bool
+       select NET_DSA_TAG_KSZ
+
 config NET_DSA_TAG_LAN9303
        bool
 
diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index a69c179..aee909b 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -55,8 +55,8 @@ static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff 
*skb,
 #ifdef CONFIG_NET_DSA_TAG_GSWIP
        [DSA_TAG_PROTO_GSWIP] = &gswip_netdev_ops,
 #endif
-#ifdef CONFIG_NET_DSA_TAG_KSZ
-       [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
+#ifdef CONFIG_NET_DSA_TAG_KSZ9477
+       [DSA_TAG_PROTO_KSZ9477] = &ksz9477_netdev_ops,
 #endif
 #ifdef CONFIG_NET_DSA_TAG_LAN9303
        [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
@@ -91,8 +91,8 @@ const char *dsa_tag_protocol_to_str(const struct 
dsa_device_ops *ops)
 #ifdef CONFIG_NET_DSA_TAG_GSWIP
                [DSA_TAG_PROTO_GSWIP] = "gswip",
 #endif
-#ifdef CONFIG_NET_DSA_TAG_KSZ
-               [DSA_TAG_PROTO_KSZ] = "ksz",
+#ifdef CONFIG_NET_DSA_TAG_KSZ9477
+               [DSA_TAG_PROTO_KSZ9477] = "ksz9477",
 #endif
 #ifdef CONFIG_NET_DSA_TAG_LAN9303
                [DSA_TAG_PROTO_LAN9303] = "lan9303",
diff --git a/net/dsa/dsa_priv.h b/net/dsa/dsa_priv.h
index 9e4fd04..026a057 100644
--- a/net/dsa/dsa_priv.h
+++ b/net/dsa/dsa_priv.h
@@ -210,7 +210,7 @@ static inline struct dsa_port *dsa_slave_to_port(const 
struct net_device *dev)
 extern const struct dsa_device_ops gswip_netdev_ops;
 
 /* tag_ksz.c */
-extern const struct dsa_device_ops ksz_netdev_ops;
+extern const struct dsa_device_ops ksz9477_netdev_ops;
 
 /* tag_lan9303.c */
 extern const struct dsa_device_ops lan9303_netdev_ops;
diff --git a/net/dsa/tag_ksz.c b/net/dsa/tag_ksz.c
index 0f62eff..e2c5dd4 100644
--- a/net/dsa/tag_ksz.c
+++ b/net/dsa/tag_ksz.c
@@ -1,6 +1,6 @@
 /*
  * net/dsa/tag_ksz.c - Microchip KSZ Switch tag format handling
- * Copyright (c) 2017 Microchip Technology
+ * Copyright (c) 2017-2018 Microchip Technology
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,34 +14,25 @@
 #include <net/dsa.h>
 #include "dsa_priv.h"
 
-/* For Ingress (Host -> KSZ), 2 bytes are added before FCS.
- * ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
- * ---------------------------------------------------------------------------
- * tag0 : Prioritization (not used now)
- * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
- *
- * For Egress (KSZ -> Host), 1 byte is added before FCS.
- * ---------------------------------------------------------------------------
- * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
- * ---------------------------------------------------------------------------
- * tag0 : zero-based value represents port
- *       (eg, 0x00=port1, 0x02=port3, 0x06=port7)
- */
+/* Usually only one byte is used for tail tag. */
+#define KSZ_INGRESS_TAG_LEN            1
+#define KSZ_EGRESS_TAG_LEN             1
 
-#define        KSZ_INGRESS_TAG_LEN     2
-#define        KSZ_EGRESS_TAG_LEN      1
+struct ksz_tag_ops {
+       int (*get_tag)(u8 *tag, int *port);
+       void (*set_tag)(void *ptr, u8 *addr, int p);
+};
 
-static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev)
+static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev,
+                               int len, const struct ksz_tag_ops *ops)
 {
        struct dsa_port *dp = dsa_slave_to_port(dev);
        struct sk_buff *nskb;
        int padlen;
-       u8 *tag;
 
        padlen = (skb->len >= ETH_ZLEN) ? 0 : ETH_ZLEN - skb->len;
 
-       if (skb_tailroom(skb) >= padlen + KSZ_INGRESS_TAG_LEN) {
+       if (skb_tailroom(skb) >= padlen + len) {
                /* Let dsa_slave_xmit() free skb */
                if (__skb_put_padto(skb, skb->len + padlen, false))
                        return NULL;
@@ -49,7 +40,7 @@ static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct 
net_device *dev)
                nskb = skb;
        } else {
                nskb = alloc_skb(NET_IP_ALIGN + skb->len +
-                                padlen + KSZ_INGRESS_TAG_LEN, GFP_ATOMIC);
+                                padlen + len, GFP_ATOMIC);
                if (!nskb)
                        return NULL;
                skb_reserve(nskb, NET_IP_ALIGN);
@@ -70,33 +61,81 @@ static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct 
net_device *dev)
                consume_skb(skb);
        }
 
-       tag = skb_put(nskb, KSZ_INGRESS_TAG_LEN);
-       tag[0] = 0;
-       tag[1] = 1 << dp->index; /* destination port */
+       ops->set_tag(skb_put(nskb, len), skb_mac_header(nskb), dp->index);
 
        return nskb;
 }
 
 static struct sk_buff *ksz_rcv(struct sk_buff *skb, struct net_device *dev,
-                              struct packet_type *pt)
+                              struct packet_type *pt,
+                              const struct ksz_tag_ops *ops)
 {
        u8 *tag;
+       int len;
        int source_port;
 
        tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
 
-       source_port = tag[0] & 7;
+       len = ops->get_tag(tag, &source_port);
 
        skb->dev = dsa_master_find_slave(dev, 0, source_port);
        if (!skb->dev)
                return NULL;
 
-       pskb_trim_rcsum(skb, skb->len - KSZ_EGRESS_TAG_LEN);
+       pskb_trim_rcsum(skb, skb->len - len);
 
        return skb;
 }
 
-const struct dsa_device_ops ksz_netdev_ops = {
-       .xmit   = ksz_xmit,
-       .rcv    = ksz_rcv,
+/* For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
+ * ---------------------------------------------------------------------------
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
+ * ---------------------------------------------------------------------------
+ * tag0 : Prioritization (not used now)
+ * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
+ *
+ * For Egress (KSZ9477 -> Host), 1 byte is added before FCS.
+ * ---------------------------------------------------------------------------
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
+ * ---------------------------------------------------------------------------
+ * tag0 : zero-based value represents port
+ *       (eg, 0x00=port1, 0x02=port3, 0x06=port7)
+ */
+
+#define KSZ9477_INGRESS_TAG_LEN                2
+
+static int ksz9477_get_tag(u8 *tag, int *port)
+{
+       *port = tag[0] & 7;
+       return KSZ_EGRESS_TAG_LEN;
+}
+
+static void ksz9477_set_tag(void *ptr, u8 *addr, int p)
+{
+       u16 *tag = (u16 *)ptr;
+
+       *tag = 1 << p;
+       *tag = cpu_to_be16(*tag);
+}
+
+const struct ksz_tag_ops ksz9477_tag_ops = {
+       .get_tag        = ksz9477_get_tag,
+       .set_tag        = ksz9477_set_tag,
+};
+
+static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
+                                   struct net_device *dev)
+{
+       return ksz_xmit(skb, dev, KSZ9477_INGRESS_TAG_LEN, &ksz9477_tag_ops);
+}
+
+static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev,
+                                  struct packet_type *pt)
+{
+       return ksz_rcv(skb, dev, pt, &ksz9477_tag_ops);
+}
+
+const struct dsa_device_ops ksz9477_netdev_ops = {
+       .xmit   = ksz9477_xmit,
+       .rcv    = ksz9477_rcv,
 };
-- 
1.9.1

Reply via email to