Commit | Line | Data |
---|---|---|
dfedd3b6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5cd8985a SW |
2 | /* |
3 | * Mediatek DSA Tag support | |
4 | * Copyright (C) 2017 Landen Chao <landen.chao@mediatek.com> | |
5 | * Sean Wang <sean.wang@mediatek.com> | |
5cd8985a SW |
6 | */ |
7 | ||
8 | #include <linux/etherdevice.h> | |
f0af3431 | 9 | #include <linux/if_vlan.h> |
ea5dd34b | 10 | |
bd954b82 | 11 | #include "tag.h" |
5cd8985a | 12 | |
94793a56 VO |
13 | #define MTK_NAME "mtk" |
14 | ||
5cd8985a | 15 | #define MTK_HDR_LEN 4 |
f0af3431 SW |
16 | #define MTK_HDR_XMIT_UNTAGGED 0 |
17 | #define MTK_HDR_XMIT_TAGGED_TPID_8100 1 | |
9200f515 | 18 | #define MTK_HDR_XMIT_TAGGED_TPID_88A8 2 |
5cd8985a SW |
19 | #define MTK_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0) |
20 | #define MTK_HDR_XMIT_DP_BIT_MASK GENMASK(5, 0) | |
5e5502e0 | 21 | #define MTK_HDR_XMIT_SA_DIS BIT(6) |
5cd8985a SW |
22 | |
23 | static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb, | |
24 | struct net_device *dev) | |
25 | { | |
d945097b | 26 | struct dsa_port *dp = dsa_slave_to_port(dev); |
9200f515 | 27 | u8 xmit_tpid; |
5cd8985a SW |
28 | u8 *mtk_tag; |
29 | ||
d169ecb5 FF |
30 | skb_set_queue_mapping(skb, dp->index); |
31 | ||
f0af3431 SW |
32 | /* Build the special tag after the MAC Source Address. If VLAN header |
33 | * is present, it's required that VLAN header and special tag is | |
34 | * being combined. Only in this way we can allow the switch can parse | |
35 | * the both special and VLAN tag at the same time and then look up VLAN | |
36 | * table with VID. | |
37 | */ | |
9200f515 DQ |
38 | switch (skb->protocol) { |
39 | case htons(ETH_P_8021Q): | |
40 | xmit_tpid = MTK_HDR_XMIT_TAGGED_TPID_8100; | |
41 | break; | |
42 | case htons(ETH_P_8021AD): | |
43 | xmit_tpid = MTK_HDR_XMIT_TAGGED_TPID_88A8; | |
44 | break; | |
45 | default: | |
46 | xmit_tpid = MTK_HDR_XMIT_UNTAGGED; | |
f0af3431 | 47 | skb_push(skb, MTK_HDR_LEN); |
6bef794d | 48 | dsa_alloc_etype_header(skb, MTK_HDR_LEN); |
f0af3431 | 49 | } |
5cd8985a | 50 | |
a72808b6 | 51 | mtk_tag = dsa_etype_header_pos_tx(skb); |
f0af3431 SW |
52 | |
53 | /* Mark tag attribute on special tag insertion to notify hardware | |
54 | * whether that's a combined special tag with 802.1Q header. | |
55 | */ | |
9200f515 | 56 | mtk_tag[0] = xmit_tpid; |
d945097b | 57 | mtk_tag[1] = (1 << dp->index) & MTK_HDR_XMIT_DP_BIT_MASK; |
f0af3431 SW |
58 | |
59 | /* Tag control information is kept for 802.1Q */ | |
9200f515 | 60 | if (xmit_tpid == MTK_HDR_XMIT_UNTAGGED) { |
f0af3431 SW |
61 | mtk_tag[2] = 0; |
62 | mtk_tag[3] = 0; | |
63 | } | |
5cd8985a SW |
64 | |
65 | return skb; | |
5cd8985a SW |
66 | } |
67 | ||
29a097b7 | 68 | static struct sk_buff *mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev) |
5cd8985a | 69 | { |
04d63f9d | 70 | u16 hdr; |
5cd8985a | 71 | int port; |
04d63f9d | 72 | __be16 *phdr; |
5cd8985a | 73 | |
5cd8985a | 74 | if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN))) |
54709795 | 75 | return NULL; |
5cd8985a | 76 | |
5d928ff4 | 77 | phdr = dsa_etype_header_pos_rx(skb); |
5cd8985a SW |
78 | hdr = ntohs(*phdr); |
79 | ||
80 | /* Remove MTK tag and recalculate checksum. */ | |
81 | skb_pull_rcsum(skb, MTK_HDR_LEN); | |
82 | ||
f1dacd7a | 83 | dsa_strip_etype_header(skb, MTK_HDR_LEN); |
5cd8985a | 84 | |
5cd8985a SW |
85 | /* Get source port information */ |
86 | port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK); | |
5cd8985a | 87 | |
2231c43b | 88 | skb->dev = dsa_master_find_slave(dev, 0, port); |
3775b1b7 VD |
89 | if (!skb->dev) |
90 | return NULL; | |
5cd8985a | 91 | |
bea79078 | 92 | dsa_default_offload_fwd_mark(skb); |
5e5502e0 | 93 | |
a86d8bec | 94 | return skb; |
5cd8985a SW |
95 | } |
96 | ||
f81a43e8 | 97 | static const struct dsa_device_ops mtk_netdev_ops = { |
94793a56 | 98 | .name = MTK_NAME, |
056eed2f | 99 | .proto = DSA_TAG_PROTO_MTK, |
2dd592b2 JC |
100 | .xmit = mtk_tag_xmit, |
101 | .rcv = mtk_tag_rcv, | |
4e500251 | 102 | .needed_headroom = MTK_HDR_LEN, |
5cd8985a | 103 | }; |
0b42f033 | 104 | |
f18bba50 | 105 | MODULE_LICENSE("GPL"); |
94793a56 | 106 | MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_MTK, MTK_NAME); |
d3b8c049 AL |
107 | |
108 | module_dsa_tag_driver(mtk_netdev_ops); |