blob: b5705cba831848189c8981e1027e0ad62eba0efe [file] [log] [blame]
Andrew Lunndfedd3b2019-04-28 19:37:10 +02001// SPDX-License-Identifier: GPL-2.0
Sean Wang5cd89852017-04-07 16:45:06 +08002/*
3 * Mediatek DSA Tag support
4 * Copyright (C) 2017 Landen Chao <landen.chao@mediatek.com>
5 * Sean Wang <sean.wang@mediatek.com>
Sean Wang5cd89852017-04-07 16:45:06 +08006 */
7
8#include <linux/etherdevice.h>
Sean Wangf0af3432017-12-15 12:47:01 +08009#include <linux/if_vlan.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040010
Sean Wang5cd89852017-04-07 16:45:06 +080011#include "dsa_priv.h"
12
13#define MTK_HDR_LEN 4
Sean Wangf0af3432017-12-15 12:47:01 +080014#define MTK_HDR_XMIT_UNTAGGED 0
15#define MTK_HDR_XMIT_TAGGED_TPID_8100 1
Sean Wang5cd89852017-04-07 16:45:06 +080016#define MTK_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
17#define MTK_HDR_XMIT_DP_BIT_MASK GENMASK(5, 0)
18
19static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb,
20 struct net_device *dev)
21{
Vivien Didelotd9450972017-10-16 11:12:15 -040022 struct dsa_port *dp = dsa_slave_to_port(dev);
Sean Wang5cd89852017-04-07 16:45:06 +080023 u8 *mtk_tag;
Sean Wangf0af3432017-12-15 12:47:01 +080024 bool is_vlan_skb = true;
Sean Wang5cd89852017-04-07 16:45:06 +080025
Sean Wangf0af3432017-12-15 12:47:01 +080026 /* Build the special tag after the MAC Source Address. If VLAN header
27 * is present, it's required that VLAN header and special tag is
28 * being combined. Only in this way we can allow the switch can parse
29 * the both special and VLAN tag at the same time and then look up VLAN
30 * table with VID.
31 */
32 if (!skb_vlan_tagged(skb)) {
33 if (skb_cow_head(skb, MTK_HDR_LEN) < 0)
34 return NULL;
Sean Wang5cd89852017-04-07 16:45:06 +080035
Sean Wangf0af3432017-12-15 12:47:01 +080036 skb_push(skb, MTK_HDR_LEN);
37 memmove(skb->data, skb->data + MTK_HDR_LEN, 2 * ETH_ALEN);
38 is_vlan_skb = false;
39 }
Sean Wang5cd89852017-04-07 16:45:06 +080040
Sean Wang5cd89852017-04-07 16:45:06 +080041 mtk_tag = skb->data + 2 * ETH_ALEN;
Sean Wangf0af3432017-12-15 12:47:01 +080042
43 /* Mark tag attribute on special tag insertion to notify hardware
44 * whether that's a combined special tag with 802.1Q header.
45 */
46 mtk_tag[0] = is_vlan_skb ? MTK_HDR_XMIT_TAGGED_TPID_8100 :
47 MTK_HDR_XMIT_UNTAGGED;
Vivien Didelotd9450972017-10-16 11:12:15 -040048 mtk_tag[1] = (1 << dp->index) & MTK_HDR_XMIT_DP_BIT_MASK;
Sean Wangf0af3432017-12-15 12:47:01 +080049
50 /* Tag control information is kept for 802.1Q */
51 if (!is_vlan_skb) {
52 mtk_tag[2] = 0;
53 mtk_tag[3] = 0;
54 }
Sean Wang5cd89852017-04-07 16:45:06 +080055
56 return skb;
Sean Wang5cd89852017-04-07 16:45:06 +080057}
58
Florian Fainellia86d8be2017-04-08 08:55:23 -070059static struct sk_buff *mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev,
Florian Westphal89e49502017-08-17 16:47:00 +020060 struct packet_type *pt)
Sean Wang5cd89852017-04-07 16:45:06 +080061{
Sean Wang5cd89852017-04-07 16:45:06 +080062 int port;
63 __be16 *phdr, hdr;
64
Sean Wang5cd89852017-04-07 16:45:06 +080065 if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN)))
Vivien Didelot54709792017-06-01 16:07:14 -040066 return NULL;
Sean Wang5cd89852017-04-07 16:45:06 +080067
68 /* The MTK header is added by the switch between src addr
69 * and ethertype at this point, skb->data points to 2 bytes
70 * after src addr so header should be 2 bytes right before.
71 */
72 phdr = (__be16 *)(skb->data - 2);
73 hdr = ntohs(*phdr);
74
75 /* Remove MTK tag and recalculate checksum. */
76 skb_pull_rcsum(skb, MTK_HDR_LEN);
77
78 memmove(skb->data - ETH_HLEN,
79 skb->data - ETH_HLEN - MTK_HDR_LEN,
80 2 * ETH_ALEN);
81
Sean Wang5cd89852017-04-07 16:45:06 +080082 /* Get source port information */
83 port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK);
Sean Wang5cd89852017-04-07 16:45:06 +080084
Vivien Didelot2231c432017-10-16 11:12:17 -040085 skb->dev = dsa_master_find_slave(dev, 0, port);
Vivien Didelot3775b1b2017-09-29 17:19:15 -040086 if (!skb->dev)
87 return NULL;
Sean Wang5cd89852017-04-07 16:45:06 +080088
Florian Fainellia86d8be2017-04-08 08:55:23 -070089 return skb;
Sean Wang5cd89852017-04-07 16:45:06 +080090}
91
John Crispin2dd592b2017-08-09 14:41:18 +020092static int mtk_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto,
93 int *offset)
94{
95 *offset = 4;
96 *proto = ((__be16 *)skb->data)[1];
97
98 return 0;
99}
100
Andrew Lunnf81a43e2019-04-28 19:37:21 +0200101static const struct dsa_device_ops mtk_netdev_ops = {
Andrew Lunn875138f2019-04-28 19:37:11 +0200102 .name = "mtk",
Andrew Lunn056eed22019-04-28 19:37:14 +0200103 .proto = DSA_TAG_PROTO_MTK,
John Crispin2dd592b2017-08-09 14:41:18 +0200104 .xmit = mtk_tag_xmit,
105 .rcv = mtk_tag_rcv,
106 .flow_dissect = mtk_tag_flow_dissect,
Andrew Lunna5dd3082018-12-06 11:36:04 +0100107 .overhead = MTK_HDR_LEN,
Sean Wang5cd89852017-04-07 16:45:06 +0800108};
Andrew Lunn0b42f032019-04-28 19:37:12 +0200109
Andrew Lunnf18bba52019-04-28 19:37:13 +0200110MODULE_LICENSE("GPL");
Andrew Lunn0b42f032019-04-28 19:37:12 +0200111MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_MTK);
Andrew Lunnd3b8c042019-04-28 19:37:15 +0200112
113module_dsa_tag_driver(mtk_netdev_ops);