GNU Linux-libre 4.9.318-gnu1
[releases.git] / net / dsa / tag_qca.c
1 /*
2  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/etherdevice.h>
15 #include "dsa_priv.h"
16
17 #define QCA_HDR_LEN     2
18 #define QCA_HDR_VERSION 0x2
19
20 #define QCA_HDR_RECV_VERSION_MASK       GENMASK(15, 14)
21 #define QCA_HDR_RECV_VERSION_S          14
22 #define QCA_HDR_RECV_PRIORITY_MASK      GENMASK(13, 11)
23 #define QCA_HDR_RECV_PRIORITY_S         11
24 #define QCA_HDR_RECV_TYPE_MASK          GENMASK(10, 6)
25 #define QCA_HDR_RECV_TYPE_S             6
26 #define QCA_HDR_RECV_FRAME_IS_TAGGED    BIT(3)
27 #define QCA_HDR_RECV_SOURCE_PORT_MASK   GENMASK(2, 0)
28
29 #define QCA_HDR_XMIT_VERSION_MASK       GENMASK(15, 14)
30 #define QCA_HDR_XMIT_VERSION_S          14
31 #define QCA_HDR_XMIT_PRIORITY_MASK      GENMASK(13, 11)
32 #define QCA_HDR_XMIT_PRIORITY_S         11
33 #define QCA_HDR_XMIT_CONTROL_MASK       GENMASK(10, 8)
34 #define QCA_HDR_XMIT_CONTROL_S          8
35 #define QCA_HDR_XMIT_FROM_CPU           BIT(7)
36 #define QCA_HDR_XMIT_DP_BIT_MASK        GENMASK(6, 0)
37
38 static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
39 {
40         struct dsa_slave_priv *p = netdev_priv(dev);
41         u16 *phdr, hdr;
42
43         if (skb_cow_head(skb, 0) < 0)
44                 goto out_free;
45
46         skb_push(skb, QCA_HDR_LEN);
47
48         memmove(skb->data, skb->data + QCA_HDR_LEN, 2 * ETH_ALEN);
49         phdr = (u16 *)(skb->data + 2 * ETH_ALEN);
50
51         /* Set the version field, and set destination port information */
52         hdr = QCA_HDR_VERSION << QCA_HDR_XMIT_VERSION_S |
53                 QCA_HDR_XMIT_FROM_CPU |
54                 BIT(p->port);
55
56         *phdr = htons(hdr);
57
58         return skb;
59
60 out_free:
61         kfree_skb(skb);
62         return NULL;
63 }
64
65 static int qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
66                        struct packet_type *pt, struct net_device *orig_dev)
67 {
68         struct dsa_switch_tree *dst = dev->dsa_ptr;
69         struct dsa_switch *ds;
70         u8 ver;
71         int port;
72         __be16 *phdr, hdr;
73
74         if (unlikely(!dst))
75                 goto out_drop;
76
77         skb = skb_unshare(skb, GFP_ATOMIC);
78         if (!skb)
79                 goto out;
80
81         if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
82                 goto out_drop;
83
84         /* The QCA header is added by the switch between src addr and Ethertype
85          * At this point, skb->data points to ethertype so header should be
86          * right before
87          */
88         phdr = (__be16 *)(skb->data - 2);
89         hdr = ntohs(*phdr);
90
91         /* Make sure the version is correct */
92         ver = (hdr & QCA_HDR_RECV_VERSION_MASK) >> QCA_HDR_RECV_VERSION_S;
93         if (unlikely(ver != QCA_HDR_VERSION))
94                 goto out_drop;
95
96         /* Remove QCA tag and recalculate checksum */
97         skb_pull_rcsum(skb, QCA_HDR_LEN);
98         memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
99                 ETH_HLEN - QCA_HDR_LEN);
100
101         /* This protocol doesn't support cascading multiple switches so it's
102          * safe to assume the switch is first in the tree
103          */
104         ds = dst->ds[0];
105         if (!ds)
106                 goto out_drop;
107
108         /* Get source port information */
109         port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
110         if (!ds->ports[port].netdev)
111                 goto out_drop;
112
113         /* Update skb & forward the frame accordingly */
114         skb_push(skb, ETH_HLEN);
115         skb->pkt_type = PACKET_HOST;
116         skb->dev = ds->ports[port].netdev;
117         skb->protocol = eth_type_trans(skb, skb->dev);
118
119         skb->dev->stats.rx_packets++;
120         skb->dev->stats.rx_bytes += skb->len;
121
122         netif_receive_skb(skb);
123
124         return 0;
125
126 out_drop:
127         kfree_skb(skb);
128 out:
129         return 0;
130 }
131
132 const struct dsa_device_ops qca_netdev_ops = {
133         .xmit   = qca_tag_xmit,
134         .rcv    = qca_tag_rcv,
135 };