Merge tag 'wireless-drivers-next-2021-02-12' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-microblaze.git] / net / dsa / tag_ocelot_8021q.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2020-2021 NXP Semiconductors
3  *
4  * An implementation of the software-defined tag_8021q.c tagger format, which
5  * also preserves full functionality under a vlan_filtering bridge. It does
6  * this by using the TCAM engines for:
7  * - pushing the RX VLAN as a second, outer tag, on egress towards the CPU port
8  * - redirecting towards the correct front port based on TX VLAN and popping
9  *   that on egress
10  */
11 #include <linux/dsa/8021q.h>
12 #include "dsa_priv.h"
13
14 static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
15                                    struct net_device *netdev)
16 {
17         struct dsa_port *dp = dsa_slave_to_port(netdev);
18         u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
19         u16 queue_mapping = skb_get_queue_mapping(skb);
20         u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
21
22         return dsa_8021q_xmit(skb, netdev, ETH_P_8021Q,
23                               ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
24 }
25
26 static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
27                                   struct net_device *netdev,
28                                   struct packet_type *pt)
29 {
30         int src_port, switch_id, qos_class;
31         u16 vid, tci;
32
33         skb_push_rcsum(skb, ETH_HLEN);
34         if (skb_vlan_tag_present(skb)) {
35                 tci = skb_vlan_tag_get(skb);
36                 __vlan_hwaccel_clear_tag(skb);
37         } else {
38                 __skb_vlan_pop(skb, &tci);
39         }
40         skb_pull_rcsum(skb, ETH_HLEN);
41
42         vid = tci & VLAN_VID_MASK;
43         src_port = dsa_8021q_rx_source_port(vid);
44         switch_id = dsa_8021q_rx_switch_id(vid);
45         qos_class = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
46
47         skb->dev = dsa_master_find_slave(netdev, switch_id, src_port);
48         if (!skb->dev)
49                 return NULL;
50
51         skb->offload_fwd_mark = 1;
52         skb->priority = qos_class;
53
54         return skb;
55 }
56
57 static const struct dsa_device_ops ocelot_8021q_netdev_ops = {
58         .name                   = "ocelot-8021q",
59         .proto                  = DSA_TAG_PROTO_OCELOT_8021Q,
60         .xmit                   = ocelot_xmit,
61         .rcv                    = ocelot_rcv,
62         .overhead               = VLAN_HLEN,
63 };
64
65 MODULE_LICENSE("GPL v2");
66 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_OCELOT_8021Q);
67
68 module_dsa_tag_driver(ocelot_8021q_netdev_ops);