Merge tag 'acpi-5.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-microblaze.git] / net / dsa / tag_ocelot.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 NXP
3  */
4 #include <linux/dsa/ocelot.h>
5 #include "dsa_priv.h"
6
7 /* If the port is under a VLAN-aware bridge, remove the VLAN header from the
8  * payload and move it into the DSA tag, which will make the switch classify
9  * the packet to the bridge VLAN. Otherwise, leave the classified VLAN at zero,
10  * which is the pvid of standalone and VLAN-unaware bridge ports.
11  */
12 static void ocelot_xmit_get_vlan_info(struct sk_buff *skb, struct dsa_port *dp,
13                                       u64 *vlan_tci, u64 *tag_type)
14 {
15         struct net_device *br = dsa_port_bridge_dev_get(dp);
16         struct vlan_ethhdr *hdr;
17         u16 proto, tci;
18
19         if (!br || !br_vlan_enabled(br)) {
20                 *vlan_tci = 0;
21                 *tag_type = IFH_TAG_TYPE_C;
22                 return;
23         }
24
25         hdr = (struct vlan_ethhdr *)skb_mac_header(skb);
26         br_vlan_get_proto(br, &proto);
27
28         if (ntohs(hdr->h_vlan_proto) == proto) {
29                 __skb_vlan_pop(skb, &tci);
30                 *vlan_tci = tci;
31         } else {
32                 rcu_read_lock();
33                 br_vlan_get_pvid_rcu(br, &tci);
34                 rcu_read_unlock();
35                 *vlan_tci = tci;
36         }
37
38         *tag_type = (proto != ETH_P_8021Q) ? IFH_TAG_TYPE_S : IFH_TAG_TYPE_C;
39 }
40
41 static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
42                                __be32 ifh_prefix, void **ifh)
43 {
44         struct dsa_port *dp = dsa_slave_to_port(netdev);
45         struct dsa_switch *ds = dp->ds;
46         u64 vlan_tci, tag_type;
47         void *injection;
48         __be32 *prefix;
49         u32 rew_op = 0;
50         u64 qos_class;
51
52         ocelot_xmit_get_vlan_info(skb, dp, &vlan_tci, &tag_type);
53
54         qos_class = netdev_get_num_tc(netdev) ?
55                     netdev_get_prio_tc_map(netdev, skb->priority) : skb->priority;
56
57         injection = skb_push(skb, OCELOT_TAG_LEN);
58         prefix = skb_push(skb, OCELOT_SHORT_PREFIX_LEN);
59
60         *prefix = ifh_prefix;
61         memset(injection, 0, OCELOT_TAG_LEN);
62         ocelot_ifh_set_bypass(injection, 1);
63         ocelot_ifh_set_src(injection, ds->num_ports);
64         ocelot_ifh_set_qos_class(injection, qos_class);
65         ocelot_ifh_set_vlan_tci(injection, vlan_tci);
66         ocelot_ifh_set_tag_type(injection, tag_type);
67
68         rew_op = ocelot_ptp_rew_op(skb);
69         if (rew_op)
70                 ocelot_ifh_set_rew_op(injection, rew_op);
71
72         *ifh = injection;
73 }
74
75 static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
76                                    struct net_device *netdev)
77 {
78         struct dsa_port *dp = dsa_slave_to_port(netdev);
79         void *injection;
80
81         ocelot_xmit_common(skb, netdev, cpu_to_be32(0x8880000a), &injection);
82         ocelot_ifh_set_dest(injection, BIT_ULL(dp->index));
83
84         return skb;
85 }
86
87 static struct sk_buff *seville_xmit(struct sk_buff *skb,
88                                     struct net_device *netdev)
89 {
90         struct dsa_port *dp = dsa_slave_to_port(netdev);
91         void *injection;
92
93         ocelot_xmit_common(skb, netdev, cpu_to_be32(0x88800005), &injection);
94         seville_ifh_set_dest(injection, BIT_ULL(dp->index));
95
96         return skb;
97 }
98
99 static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
100                                   struct net_device *netdev)
101 {
102         u64 src_port, qos_class;
103         u64 vlan_tci, tag_type;
104         u8 *start = skb->data;
105         struct dsa_port *dp;
106         u8 *extraction;
107         u16 vlan_tpid;
108         u64 rew_val;
109
110         /* Revert skb->data by the amount consumed by the DSA master,
111          * so it points to the beginning of the frame.
112          */
113         skb_push(skb, ETH_HLEN);
114         /* We don't care about the short prefix, it is just for easy entrance
115          * into the DSA master's RX filter. Discard it now by moving it into
116          * the headroom.
117          */
118         skb_pull(skb, OCELOT_SHORT_PREFIX_LEN);
119         /* And skb->data now points to the extraction frame header.
120          * Keep a pointer to it.
121          */
122         extraction = skb->data;
123         /* Now the EFH is part of the headroom as well */
124         skb_pull(skb, OCELOT_TAG_LEN);
125         /* Reset the pointer to the real MAC header */
126         skb_reset_mac_header(skb);
127         skb_reset_mac_len(skb);
128         /* And move skb->data to the correct location again */
129         skb_pull(skb, ETH_HLEN);
130
131         /* Remove from inet csum the extraction header */
132         skb_postpull_rcsum(skb, start, OCELOT_TOTAL_TAG_LEN);
133
134         ocelot_xfh_get_src_port(extraction, &src_port);
135         ocelot_xfh_get_qos_class(extraction, &qos_class);
136         ocelot_xfh_get_tag_type(extraction, &tag_type);
137         ocelot_xfh_get_vlan_tci(extraction, &vlan_tci);
138         ocelot_xfh_get_rew_val(extraction, &rew_val);
139
140         skb->dev = dsa_master_find_slave(netdev, 0, src_port);
141         if (!skb->dev)
142                 /* The switch will reflect back some frames sent through
143                  * sockets opened on the bare DSA master. These will come back
144                  * with src_port equal to the index of the CPU port, for which
145                  * there is no slave registered. So don't print any error
146                  * message here (ignore and drop those frames).
147                  */
148                 return NULL;
149
150         dsa_default_offload_fwd_mark(skb);
151         skb->priority = qos_class;
152         OCELOT_SKB_CB(skb)->tstamp_lo = rew_val;
153
154         /* Ocelot switches copy frames unmodified to the CPU. However, it is
155          * possible for the user to request a VLAN modification through
156          * VCAP_IS1_ACT_VID_REPLACE_ENA. In this case, what will happen is that
157          * the VLAN ID field from the Extraction Header gets updated, but the
158          * 802.1Q header does not (the classified VLAN only becomes visible on
159          * egress through the "port tag" of front-panel ports).
160          * So, for traffic extracted by the CPU, we want to pick up the
161          * classified VLAN and manually replace the existing 802.1Q header from
162          * the packet with it, so that the operating system is always up to
163          * date with the result of tc-vlan actions.
164          * NOTE: In VLAN-unaware mode, we don't want to do that, we want the
165          * frame to remain unmodified, because the classified VLAN is always
166          * equal to the pvid of the ingress port and should not be used for
167          * processing.
168          */
169         dp = dsa_slave_to_port(skb->dev);
170         vlan_tpid = tag_type ? ETH_P_8021AD : ETH_P_8021Q;
171
172         if (dsa_port_is_vlan_filtering(dp) &&
173             eth_hdr(skb)->h_proto == htons(vlan_tpid)) {
174                 u16 dummy_vlan_tci;
175
176                 skb_push_rcsum(skb, ETH_HLEN);
177                 __skb_vlan_pop(skb, &dummy_vlan_tci);
178                 skb_pull_rcsum(skb, ETH_HLEN);
179                 __vlan_hwaccel_put_tag(skb, htons(vlan_tpid), vlan_tci);
180         }
181
182         return skb;
183 }
184
185 static const struct dsa_device_ops ocelot_netdev_ops = {
186         .name                   = "ocelot",
187         .proto                  = DSA_TAG_PROTO_OCELOT,
188         .xmit                   = ocelot_xmit,
189         .rcv                    = ocelot_rcv,
190         .needed_headroom        = OCELOT_TOTAL_TAG_LEN,
191         .promisc_on_master      = true,
192 };
193
194 DSA_TAG_DRIVER(ocelot_netdev_ops);
195 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_OCELOT);
196
197 static const struct dsa_device_ops seville_netdev_ops = {
198         .name                   = "seville",
199         .proto                  = DSA_TAG_PROTO_SEVILLE,
200         .xmit                   = seville_xmit,
201         .rcv                    = ocelot_rcv,
202         .needed_headroom        = OCELOT_TOTAL_TAG_LEN,
203         .promisc_on_master      = true,
204 };
205
206 DSA_TAG_DRIVER(seville_netdev_ops);
207 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SEVILLE);
208
209 static struct dsa_tag_driver *ocelot_tag_driver_array[] = {
210         &DSA_TAG_DRIVER_NAME(ocelot_netdev_ops),
211         &DSA_TAG_DRIVER_NAME(seville_netdev_ops),
212 };
213
214 module_dsa_tag_drivers(ocelot_tag_driver_array);
215
216 MODULE_LICENSE("GPL v2");