Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Mediatek DSA Tag support
0004  * Copyright (C) 2017 Landen Chao <landen.chao@mediatek.com>
0005  *            Sean Wang <sean.wang@mediatek.com>
0006  */
0007 
0008 #include <linux/etherdevice.h>
0009 #include <linux/if_vlan.h>
0010 
0011 #include "dsa_priv.h"
0012 
0013 #define MTK_HDR_LEN     4
0014 #define MTK_HDR_XMIT_UNTAGGED       0
0015 #define MTK_HDR_XMIT_TAGGED_TPID_8100   1
0016 #define MTK_HDR_XMIT_TAGGED_TPID_88A8   2
0017 #define MTK_HDR_RECV_SOURCE_PORT_MASK   GENMASK(2, 0)
0018 #define MTK_HDR_XMIT_DP_BIT_MASK    GENMASK(5, 0)
0019 #define MTK_HDR_XMIT_SA_DIS     BIT(6)
0020 
0021 static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb,
0022                     struct net_device *dev)
0023 {
0024     struct dsa_port *dp = dsa_slave_to_port(dev);
0025     u8 xmit_tpid;
0026     u8 *mtk_tag;
0027 
0028     /* Build the special tag after the MAC Source Address. If VLAN header
0029      * is present, it's required that VLAN header and special tag is
0030      * being combined. Only in this way we can allow the switch can parse
0031      * the both special and VLAN tag at the same time and then look up VLAN
0032      * table with VID.
0033      */
0034     switch (skb->protocol) {
0035     case htons(ETH_P_8021Q):
0036         xmit_tpid = MTK_HDR_XMIT_TAGGED_TPID_8100;
0037         break;
0038     case htons(ETH_P_8021AD):
0039         xmit_tpid = MTK_HDR_XMIT_TAGGED_TPID_88A8;
0040         break;
0041     default:
0042         xmit_tpid = MTK_HDR_XMIT_UNTAGGED;
0043         skb_push(skb, MTK_HDR_LEN);
0044         dsa_alloc_etype_header(skb, MTK_HDR_LEN);
0045     }
0046 
0047     mtk_tag = dsa_etype_header_pos_tx(skb);
0048 
0049     /* Mark tag attribute on special tag insertion to notify hardware
0050      * whether that's a combined special tag with 802.1Q header.
0051      */
0052     mtk_tag[0] = xmit_tpid;
0053     mtk_tag[1] = (1 << dp->index) & MTK_HDR_XMIT_DP_BIT_MASK;
0054 
0055     /* Tag control information is kept for 802.1Q */
0056     if (xmit_tpid == MTK_HDR_XMIT_UNTAGGED) {
0057         mtk_tag[2] = 0;
0058         mtk_tag[3] = 0;
0059     }
0060 
0061     return skb;
0062 }
0063 
0064 static struct sk_buff *mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev)
0065 {
0066     u16 hdr;
0067     int port;
0068     __be16 *phdr;
0069 
0070     if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN)))
0071         return NULL;
0072 
0073     phdr = dsa_etype_header_pos_rx(skb);
0074     hdr = ntohs(*phdr);
0075 
0076     /* Remove MTK tag and recalculate checksum. */
0077     skb_pull_rcsum(skb, MTK_HDR_LEN);
0078 
0079     dsa_strip_etype_header(skb, MTK_HDR_LEN);
0080 
0081     /* Get source port information */
0082     port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK);
0083 
0084     skb->dev = dsa_master_find_slave(dev, 0, port);
0085     if (!skb->dev)
0086         return NULL;
0087 
0088     dsa_default_offload_fwd_mark(skb);
0089 
0090     return skb;
0091 }
0092 
0093 static const struct dsa_device_ops mtk_netdev_ops = {
0094     .name       = "mtk",
0095     .proto      = DSA_TAG_PROTO_MTK,
0096     .xmit       = mtk_tag_xmit,
0097     .rcv        = mtk_tag_rcv,
0098     .needed_headroom = MTK_HDR_LEN,
0099 };
0100 
0101 MODULE_LICENSE("GPL");
0102 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_MTK);
0103 
0104 module_dsa_tag_driver(mtk_netdev_ops);