1 From 5c01c03920c63630864d2b8641924a8c7c6cb62f Mon Sep 17 00:00:00 2001
2 From: Sean Wang <sean.wang@mediatek.com>
3 Date: Wed, 29 Mar 2017 17:38:20 +0800
4 Subject: [PATCH 28/57] net-next: dsa: add Mediatek tag RX/TX handler
6 Add the support for the 4-bytes tag for DSA port distinguishing inserted
7 allowing receiving and transmitting the packet via the particular port.
8 The tag is being added after the source MAC address in the ethernet
11 Signed-off-by: Sean Wang <sean.wang@mediatek.com>
12 Signed-off-by: Landen Chao <Landen.Chao@mediatek.com>
13 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
14 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
16 include/net/dsa.h | 1 +
18 net/dsa/Makefile | 1 +
20 net/dsa/dsa_priv.h | 3 ++
21 net/dsa/tag_mtk.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++
22 6 files changed, 127 insertions(+)
23 create mode 100644 net/dsa/tag_mtk.c
25 --- a/include/net/dsa.h
26 +++ b/include/net/dsa.h
27 @@ -27,6 +27,7 @@ enum dsa_tag_protocol {
32 DSA_TAG_LAST, /* MUST BE LAST */
37 @@ -41,4 +41,6 @@ config NET_DSA_TAG_TRAILER
38 config NET_DSA_TAG_QCA
41 +config NET_DSA_TAG_MTK
44 --- a/net/dsa/Makefile
45 +++ b/net/dsa/Makefile
46 @@ -8,3 +8,4 @@ dsa_core-$(CONFIG_NET_DSA_TAG_DSA) += ta
47 dsa_core-$(CONFIG_NET_DSA_TAG_EDSA) += tag_edsa.o
48 dsa_core-$(CONFIG_NET_DSA_TAG_TRAILER) += tag_trailer.o
49 dsa_core-$(CONFIG_NET_DSA_TAG_QCA) += tag_qca.o
50 +dsa_core-$(CONFIG_NET_DSA_TAG_MTK) += tag_mtk.o
53 @@ -57,6 +57,9 @@ const struct dsa_device_ops *dsa_device_
54 #ifdef CONFIG_NET_DSA_TAG_QCA
55 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
57 +#ifdef CONFIG_NET_DSA_TAG_MTK
58 + [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
60 [DSA_TAG_PROTO_NONE] = &none_ops,
63 --- a/net/dsa/dsa_priv.h
64 +++ b/net/dsa/dsa_priv.h
65 @@ -84,4 +84,7 @@ extern const struct dsa_device_ops brcm_
67 extern const struct dsa_device_ops qca_netdev_ops;
70 +extern const struct dsa_device_ops mtk_netdev_ops;
74 +++ b/net/dsa/tag_mtk.c
77 + * Mediatek DSA Tag support
78 + * Copyright (C) 2017 Landen Chao <landen.chao@mediatek.com>
79 + * Sean Wang <sean.wang@mediatek.com>
80 + * This program is free software; you can redistribute it and/or modify
81 + * it under the terms of the GNU General Public License version 2 and
82 + * only version 2 as published by the Free Software Foundation.
84 + * This program is distributed in the hope that it will be useful,
85 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
86 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
87 + * GNU General Public License for more details.
90 +#include <linux/etherdevice.h>
91 +#include "dsa_priv.h"
93 +#define MTK_HDR_LEN 4
94 +#define MTK_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
95 +#define MTK_HDR_XMIT_DP_BIT_MASK GENMASK(5, 0)
97 +static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb,
98 + struct net_device *dev)
100 + struct dsa_slave_priv *p = netdev_priv(dev);
103 + if (skb_cow_head(skb, MTK_HDR_LEN) < 0)
106 + skb_push(skb, MTK_HDR_LEN);
108 + memmove(skb->data, skb->data + MTK_HDR_LEN, 2 * ETH_ALEN);
110 + /* Build the tag after the MAC Source Address */
111 + mtk_tag = skb->data + 2 * ETH_ALEN;
113 + mtk_tag[1] = (1 << p->dp->index) & MTK_HDR_XMIT_DP_BIT_MASK;
124 +static int mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev,
125 + struct packet_type *pt, struct net_device *orig_dev)
127 + struct dsa_switch_tree *dst = dev->dsa_ptr;
128 + struct dsa_switch *ds;
132 + if (unlikely(!dst))
135 + skb = skb_unshare(skb, GFP_ATOMIC);
139 + if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN)))
142 + /* The MTK header is added by the switch between src addr
143 + * and ethertype at this point, skb->data points to 2 bytes
144 + * after src addr so header should be 2 bytes right before.
146 + phdr = (__be16 *)(skb->data - 2);
147 + hdr = ntohs(*phdr);
149 + /* Remove MTK tag and recalculate checksum. */
150 + skb_pull_rcsum(skb, MTK_HDR_LEN);
152 + memmove(skb->data - ETH_HLEN,
153 + skb->data - ETH_HLEN - MTK_HDR_LEN,
156 + /* This protocol doesn't support cascading multiple
157 + * switches so it's safe to assume the switch is first
164 + /* Get source port information */
165 + port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK);
166 + if (!ds->ports[port].netdev)
169 + /* Update skb & forward the frame accordingly */
170 + skb_push(skb, ETH_HLEN);
172 + skb->pkt_type = PACKET_HOST;
173 + skb->dev = ds->ports[port].netdev;
174 + skb->protocol = eth_type_trans(skb, skb->dev);
176 + skb->dev->stats.rx_packets++;
177 + skb->dev->stats.rx_bytes += skb->len;
179 + netif_receive_skb(skb);
189 +const struct dsa_device_ops mtk_netdev_ops = {
190 + .xmit = mtk_tag_xmit,
191 + .rcv = mtk_tag_rcv,