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.
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.
15 #include <linux/etherdevice.h>
20 #define MTK_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
21 #define MTK_HDR_XMIT_DP_BIT_MASK GENMASK(5, 0)
23 static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb,
24 struct net_device *dev)
26 struct dsa_slave_priv *p = netdev_priv(dev);
29 if (skb_cow_head(skb, MTK_HDR_LEN) < 0)
32 skb_push(skb, MTK_HDR_LEN);
34 memmove(skb->data, skb->data + MTK_HDR_LEN, 2 * ETH_ALEN);
36 /* Build the tag after the MAC Source Address */
37 mtk_tag = skb->data + 2 * ETH_ALEN;
39 mtk_tag[1] = (1 << p->dp->index) & MTK_HDR_XMIT_DP_BIT_MASK;
50 static int mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev,
51 struct packet_type *pt, struct net_device *orig_dev)
53 struct dsa_switch_tree *dst = dev->dsa_ptr;
54 struct dsa_switch *ds;
58 skb = skb_unshare(skb, GFP_ATOMIC);
62 if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN)))
65 /* The MTK header is added by the switch between src addr
66 * and ethertype at this point, skb->data points to 2 bytes
67 * after src addr so header should be 2 bytes right before.
69 phdr = (__be16 *)(skb->data - 2);
72 /* Remove MTK tag and recalculate checksum. */
73 skb_pull_rcsum(skb, MTK_HDR_LEN);
75 memmove(skb->data - ETH_HLEN,
76 skb->data - ETH_HLEN - MTK_HDR_LEN,
79 /* This protocol doesn't support cascading multiple
80 * switches so it's safe to assume the switch is first
87 /* Get source port information */
88 port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK);
89 if (!ds->ports[port].netdev)
92 /* Update skb & forward the frame accordingly */
93 skb_push(skb, ETH_HLEN);
95 skb->pkt_type = PACKET_HOST;
96 skb->dev = ds->ports[port].netdev;
97 skb->protocol = eth_type_trans(skb, skb->dev);
99 skb->dev->stats.rx_packets++;
100 skb->dev->stats.rx_bytes += skb->len;
102 netif_receive_skb(skb);
112 const struct dsa_device_ops mtk_netdev_ops = {
113 .xmit = mtk_tag_xmit,