Merge tag 'for-5.13/libata-2021-04-27' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / net / dsa / tag_mtk.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Mediatek DSA Tag support
4  * Copyright (C) 2017 Landen Chao <landen.chao@mediatek.com>
5  *                    Sean Wang <sean.wang@mediatek.com>
6  */
7
8 #include <linux/etherdevice.h>
9 #include <linux/if_vlan.h>
10
11 #include "dsa_priv.h"
12
13 #define MTK_HDR_LEN             4
14 #define MTK_HDR_XMIT_UNTAGGED           0
15 #define MTK_HDR_XMIT_TAGGED_TPID_8100   1
16 #define MTK_HDR_XMIT_TAGGED_TPID_88A8   2
17 #define MTK_HDR_RECV_SOURCE_PORT_MASK   GENMASK(2, 0)
18 #define MTK_HDR_XMIT_DP_BIT_MASK        GENMASK(5, 0)
19 #define MTK_HDR_XMIT_SA_DIS             BIT(6)
20
21 static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb,
22                                     struct net_device *dev)
23 {
24         struct dsa_port *dp = dsa_slave_to_port(dev);
25         u8 xmit_tpid;
26         u8 *mtk_tag;
27         unsigned char *dest = eth_hdr(skb)->h_dest;
28         bool is_multicast_skb = is_multicast_ether_addr(dest) &&
29                                 !is_broadcast_ether_addr(dest);
30
31         /* Build the special tag after the MAC Source Address. If VLAN header
32          * is present, it's required that VLAN header and special tag is
33          * being combined. Only in this way we can allow the switch can parse
34          * the both special and VLAN tag at the same time and then look up VLAN
35          * table with VID.
36          */
37         switch (skb->protocol) {
38         case htons(ETH_P_8021Q):
39                 xmit_tpid = MTK_HDR_XMIT_TAGGED_TPID_8100;
40                 break;
41         case htons(ETH_P_8021AD):
42                 xmit_tpid = MTK_HDR_XMIT_TAGGED_TPID_88A8;
43                 break;
44         default:
45                 xmit_tpid = MTK_HDR_XMIT_UNTAGGED;
46                 skb_push(skb, MTK_HDR_LEN);
47                 memmove(skb->data, skb->data + MTK_HDR_LEN, 2 * ETH_ALEN);
48         }
49
50         mtk_tag = skb->data + 2 * ETH_ALEN;
51
52         /* Mark tag attribute on special tag insertion to notify hardware
53          * whether that's a combined special tag with 802.1Q header.
54          */
55         mtk_tag[0] = xmit_tpid;
56         mtk_tag[1] = (1 << dp->index) & MTK_HDR_XMIT_DP_BIT_MASK;
57
58         /* Disable SA learning for multicast frames */
59         if (unlikely(is_multicast_skb))
60                 mtk_tag[1] |= MTK_HDR_XMIT_SA_DIS;
61
62         /* Tag control information is kept for 802.1Q */
63         if (xmit_tpid == MTK_HDR_XMIT_UNTAGGED) {
64                 mtk_tag[2] = 0;
65                 mtk_tag[3] = 0;
66         }
67
68         return skb;
69 }
70
71 static struct sk_buff *mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev,
72                                    struct packet_type *pt)
73 {
74         u16 hdr;
75         int port;
76         __be16 *phdr;
77         unsigned char *dest = eth_hdr(skb)->h_dest;
78         bool is_multicast_skb = is_multicast_ether_addr(dest) &&
79                                 !is_broadcast_ether_addr(dest);
80
81         if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN)))
82                 return NULL;
83
84         /* The MTK header is added by the switch between src addr
85          * and ethertype at this point, skb->data points to 2 bytes
86          * after src addr so header should be 2 bytes right before.
87          */
88         phdr = (__be16 *)(skb->data - 2);
89         hdr = ntohs(*phdr);
90
91         /* Remove MTK tag and recalculate checksum. */
92         skb_pull_rcsum(skb, MTK_HDR_LEN);
93
94         memmove(skb->data - ETH_HLEN,
95                 skb->data - ETH_HLEN - MTK_HDR_LEN,
96                 2 * ETH_ALEN);
97
98         /* Get source port information */
99         port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK);
100
101         skb->dev = dsa_master_find_slave(dev, 0, port);
102         if (!skb->dev)
103                 return NULL;
104
105         /* Only unicast or broadcast frames are offloaded */
106         if (likely(!is_multicast_skb))
107                 skb->offload_fwd_mark = 1;
108
109         return skb;
110 }
111
112 static const struct dsa_device_ops mtk_netdev_ops = {
113         .name           = "mtk",
114         .proto          = DSA_TAG_PROTO_MTK,
115         .xmit           = mtk_tag_xmit,
116         .rcv            = mtk_tag_rcv,
117         .overhead       = MTK_HDR_LEN,
118 };
119
120 MODULE_LICENSE("GPL");
121 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_MTK);
122
123 module_dsa_tag_driver(mtk_netdev_ops);