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