1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
4 * This module is not a complete tagger implementation. It only provides
5 * primitives for taggers that rely on 802.1Q VLAN tags to use. The
6 * dsa_8021q_netdev_ops is registered for API compliance and not used
9 #include <linux/if_vlan.h>
10 #include <linux/dsa/8021q.h>
14 /* Binary structure of the fake 12-bit VID field (when the TPID is
17 * | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
18 * +-----------+-----+-----------------+-----------+-----------------------+
19 * | RSV | VBID| SWITCH_ID | VBID | PORT |
20 * +-----------+-----+-----------------+-----------+-----------------------+
23 * Reserved. Must be set to 3 (0b11).
25 * SWITCH_ID - VID[8:6]:
26 * Index of switch within DSA tree. Must be between 0 and 7.
28 * VBID - { VID[9], VID[5:4] }:
29 * Virtual bridge ID. If between 1 and 7, packet targets the broadcast
30 * domain of a bridge. If transmitted as zero, packet targets a single
34 * Index of switch port. Must be between 0 and 15.
37 #define DSA_8021Q_RSV_VAL 3
38 #define DSA_8021Q_RSV_SHIFT 10
39 #define DSA_8021Q_RSV_MASK GENMASK(11, 10)
40 #define DSA_8021Q_RSV ((DSA_8021Q_RSV_VAL << DSA_8021Q_RSV_SHIFT) & \
43 #define DSA_8021Q_SWITCH_ID_SHIFT 6
44 #define DSA_8021Q_SWITCH_ID_MASK GENMASK(8, 6)
45 #define DSA_8021Q_SWITCH_ID(x) (((x) << DSA_8021Q_SWITCH_ID_SHIFT) & \
46 DSA_8021Q_SWITCH_ID_MASK)
48 #define DSA_8021Q_VBID_HI_SHIFT 9
49 #define DSA_8021Q_VBID_HI_MASK GENMASK(9, 9)
50 #define DSA_8021Q_VBID_LO_SHIFT 4
51 #define DSA_8021Q_VBID_LO_MASK GENMASK(5, 4)
52 #define DSA_8021Q_VBID_HI(x) (((x) & GENMASK(2, 2)) >> 2)
53 #define DSA_8021Q_VBID_LO(x) ((x) & GENMASK(1, 0))
54 #define DSA_8021Q_VBID(x) \
55 (((DSA_8021Q_VBID_LO(x) << DSA_8021Q_VBID_LO_SHIFT) & \
56 DSA_8021Q_VBID_LO_MASK) | \
57 ((DSA_8021Q_VBID_HI(x) << DSA_8021Q_VBID_HI_SHIFT) & \
58 DSA_8021Q_VBID_HI_MASK))
60 #define DSA_8021Q_PORT_SHIFT 0
61 #define DSA_8021Q_PORT_MASK GENMASK(3, 0)
62 #define DSA_8021Q_PORT(x) (((x) << DSA_8021Q_PORT_SHIFT) & \
65 u16 dsa_tag_8021q_bridge_vid(unsigned int bridge_num)
67 /* The VBID value of 0 is reserved for precise TX, but it is also
68 * reserved/invalid for the bridge_num, so all is well.
70 return DSA_8021Q_RSV | DSA_8021Q_VBID(bridge_num);
72 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_vid);
74 /* Returns the VID that will be installed as pvid for this switch port, sent as
75 * tagged egress towards the CPU port and decoded by the rcv function.
77 u16 dsa_tag_8021q_standalone_vid(const struct dsa_port *dp)
79 return DSA_8021Q_RSV | DSA_8021Q_SWITCH_ID(dp->ds->index) |
80 DSA_8021Q_PORT(dp->index);
82 EXPORT_SYMBOL_GPL(dsa_tag_8021q_standalone_vid);
84 /* Returns the decoded switch ID from the RX VID. */
85 int dsa_8021q_rx_switch_id(u16 vid)
87 return (vid & DSA_8021Q_SWITCH_ID_MASK) >> DSA_8021Q_SWITCH_ID_SHIFT;
89 EXPORT_SYMBOL_GPL(dsa_8021q_rx_switch_id);
91 /* Returns the decoded port ID from the RX VID. */
92 int dsa_8021q_rx_source_port(u16 vid)
94 return (vid & DSA_8021Q_PORT_MASK) >> DSA_8021Q_PORT_SHIFT;
96 EXPORT_SYMBOL_GPL(dsa_8021q_rx_source_port);
98 /* Returns the decoded VBID from the RX VID. */
99 static int dsa_tag_8021q_rx_vbid(u16 vid)
101 u16 vbid_hi = (vid & DSA_8021Q_VBID_HI_MASK) >> DSA_8021Q_VBID_HI_SHIFT;
102 u16 vbid_lo = (vid & DSA_8021Q_VBID_LO_MASK) >> DSA_8021Q_VBID_LO_SHIFT;
104 return (vbid_hi << 2) | vbid_lo;
107 bool vid_is_dsa_8021q(u16 vid)
109 u16 rsv = (vid & DSA_8021Q_RSV_MASK) >> DSA_8021Q_RSV_SHIFT;
111 return rsv == DSA_8021Q_RSV_VAL;
113 EXPORT_SYMBOL_GPL(vid_is_dsa_8021q);
115 static struct dsa_tag_8021q_vlan *
116 dsa_tag_8021q_vlan_find(struct dsa_8021q_context *ctx, int port, u16 vid)
118 struct dsa_tag_8021q_vlan *v;
120 list_for_each_entry(v, &ctx->vlans, list)
121 if (v->vid == vid && v->port == port)
127 static int dsa_port_do_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid,
130 struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
131 struct dsa_switch *ds = dp->ds;
132 struct dsa_tag_8021q_vlan *v;
133 int port = dp->index;
136 /* No need to bother with refcounting for user ports */
137 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
138 return ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
140 v = dsa_tag_8021q_vlan_find(ctx, port, vid);
142 refcount_inc(&v->refcount);
146 v = kzalloc(sizeof(*v), GFP_KERNEL);
150 err = ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
158 refcount_set(&v->refcount, 1);
159 list_add_tail(&v->list, &ctx->vlans);
164 static int dsa_port_do_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid)
166 struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
167 struct dsa_switch *ds = dp->ds;
168 struct dsa_tag_8021q_vlan *v;
169 int port = dp->index;
172 /* No need to bother with refcounting for user ports */
173 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
174 return ds->ops->tag_8021q_vlan_del(ds, port, vid);
176 v = dsa_tag_8021q_vlan_find(ctx, port, vid);
180 if (!refcount_dec_and_test(&v->refcount))
183 err = ds->ops->tag_8021q_vlan_del(ds, port, vid);
185 refcount_inc(&v->refcount);
196 dsa_port_tag_8021q_vlan_match(struct dsa_port *dp,
197 struct dsa_notifier_tag_8021q_vlan_info *info)
199 struct dsa_switch *ds = dp->ds;
201 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
204 if (ds->dst->index == info->tree_index && ds->index == info->sw_index)
205 return dp->index == info->port;
210 int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
211 struct dsa_notifier_tag_8021q_vlan_info *info)
216 /* Since we use dsa_broadcast(), there might be other switches in other
217 * trees which don't support tag_8021q, so don't return an error.
218 * Or they might even support tag_8021q but have not registered yet to
219 * use it (maybe they use another tagger currently).
221 if (!ds->ops->tag_8021q_vlan_add || !ds->tag_8021q_ctx)
224 dsa_switch_for_each_port(dp, ds) {
225 if (dsa_port_tag_8021q_vlan_match(dp, info)) {
228 if (dsa_port_is_user(dp))
229 flags |= BRIDGE_VLAN_INFO_UNTAGGED |
230 BRIDGE_VLAN_INFO_PVID;
232 err = dsa_port_do_tag_8021q_vlan_add(dp, info->vid,
242 int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
243 struct dsa_notifier_tag_8021q_vlan_info *info)
248 if (!ds->ops->tag_8021q_vlan_del || !ds->tag_8021q_ctx)
251 dsa_switch_for_each_port(dp, ds) {
252 if (dsa_port_tag_8021q_vlan_match(dp, info)) {
253 err = dsa_port_do_tag_8021q_vlan_del(dp, info->vid);
262 /* There are 2 ways of offloading tag_8021q VLANs.
264 * One is to use a hardware TCAM to push the port's standalone VLAN into the
265 * frame when forwarding it to the CPU, as an egress modification rule on the
266 * CPU port. This is preferable because it has no side effects for the
267 * autonomous forwarding path, and accomplishes tag_8021q's primary goal of
268 * identifying the source port of each packet based on VLAN ID.
270 * The other is to commit the tag_8021q VLAN as a PVID to the VLAN table, and
271 * to configure the port as VLAN-unaware. This is less preferable because
272 * unique source port identification can only be done for standalone ports;
273 * under a VLAN-unaware bridge, all ports share the same tag_8021q VLAN as
274 * PVID, and under a VLAN-aware bridge, packets received by software will not
275 * have tag_8021q VLANs appended, just bridge VLANs.
277 * For tag_8021q implementations of the second type, this method is used to
278 * replace the standalone tag_8021q VLAN of a port with the tag_8021q VLAN to
279 * be used for VLAN-unaware bridging.
281 int dsa_tag_8021q_bridge_join(struct dsa_switch *ds, int port,
282 struct dsa_bridge bridge)
284 struct dsa_port *dp = dsa_to_port(ds, port);
285 u16 standalone_vid, bridge_vid;
288 /* Delete the standalone VLAN of the port and replace it with a
291 standalone_vid = dsa_tag_8021q_standalone_vid(dp);
292 bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
294 err = dsa_port_tag_8021q_vlan_add(dp, bridge_vid, true);
298 dsa_port_tag_8021q_vlan_del(dp, standalone_vid, false);
302 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_join);
304 void dsa_tag_8021q_bridge_leave(struct dsa_switch *ds, int port,
305 struct dsa_bridge bridge)
307 struct dsa_port *dp = dsa_to_port(ds, port);
308 u16 standalone_vid, bridge_vid;
311 /* Delete the bridging VLAN of the port and replace it with a
314 standalone_vid = dsa_tag_8021q_standalone_vid(dp);
315 bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
317 err = dsa_port_tag_8021q_vlan_add(dp, standalone_vid, false);
320 "Failed to delete tag_8021q standalone VLAN %d from port %d: %pe\n",
321 standalone_vid, port, ERR_PTR(err));
324 dsa_port_tag_8021q_vlan_del(dp, bridge_vid, true);
326 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_leave);
328 /* Set up a port's standalone tag_8021q VLAN */
329 static int dsa_tag_8021q_port_setup(struct dsa_switch *ds, int port)
331 struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
332 struct dsa_port *dp = dsa_to_port(ds, port);
333 u16 vid = dsa_tag_8021q_standalone_vid(dp);
334 struct net_device *master;
337 /* The CPU port is implicitly configured by
338 * configuring the front-panel ports
340 if (!dsa_port_is_user(dp))
343 master = dp->cpu_dp->master;
345 err = dsa_port_tag_8021q_vlan_add(dp, vid, false);
348 "Failed to apply standalone VID %d to port %d: %pe\n",
349 vid, port, ERR_PTR(err));
353 /* Add the VLAN to the master's RX filter. */
354 vlan_vid_add(master, ctx->proto, vid);
359 static void dsa_tag_8021q_port_teardown(struct dsa_switch *ds, int port)
361 struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
362 struct dsa_port *dp = dsa_to_port(ds, port);
363 u16 vid = dsa_tag_8021q_standalone_vid(dp);
364 struct net_device *master;
366 /* The CPU port is implicitly configured by
367 * configuring the front-panel ports
369 if (!dsa_port_is_user(dp))
372 master = dp->cpu_dp->master;
374 dsa_port_tag_8021q_vlan_del(dp, vid, false);
376 vlan_vid_del(master, ctx->proto, vid);
379 static int dsa_tag_8021q_setup(struct dsa_switch *ds)
385 for (port = 0; port < ds->num_ports; port++) {
386 err = dsa_tag_8021q_port_setup(ds, port);
389 "Failed to setup VLAN tagging for port %d: %pe\n",
398 static void dsa_tag_8021q_teardown(struct dsa_switch *ds)
404 for (port = 0; port < ds->num_ports; port++)
405 dsa_tag_8021q_port_teardown(ds, port);
408 int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto)
410 struct dsa_8021q_context *ctx;
412 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
419 INIT_LIST_HEAD(&ctx->vlans);
421 ds->tag_8021q_ctx = ctx;
423 return dsa_tag_8021q_setup(ds);
425 EXPORT_SYMBOL_GPL(dsa_tag_8021q_register);
427 void dsa_tag_8021q_unregister(struct dsa_switch *ds)
429 struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
430 struct dsa_tag_8021q_vlan *v, *n;
432 dsa_tag_8021q_teardown(ds);
434 list_for_each_entry_safe(v, n, &ctx->vlans, list) {
439 ds->tag_8021q_ctx = NULL;
443 EXPORT_SYMBOL_GPL(dsa_tag_8021q_unregister);
445 struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev,
448 /* skb->data points at skb_mac_header, which
449 * is fine for vlan_insert_tag.
451 return vlan_insert_tag(skb, htons(tpid), tci);
453 EXPORT_SYMBOL_GPL(dsa_8021q_xmit);
455 struct net_device *dsa_tag_8021q_find_port_by_vbid(struct net_device *master,
458 struct dsa_port *cpu_dp = master->dsa_ptr;
459 struct dsa_switch_tree *dst = cpu_dp->dst;
465 dsa_tree_for_each_user_port(dp, dst) {
469 if (dp->stp_state != BR_STATE_LEARNING &&
470 dp->stp_state != BR_STATE_FORWARDING)
473 if (dp->cpu_dp != cpu_dp)
476 if (dsa_port_bridge_num_get(dp) == vbid)
482 EXPORT_SYMBOL_GPL(dsa_tag_8021q_find_port_by_vbid);
484 void dsa_8021q_rcv(struct sk_buff *skb, int *source_port, int *switch_id,
489 if (skb_vlan_tag_present(skb)) {
490 tci = skb_vlan_tag_get(skb);
491 __vlan_hwaccel_clear_tag(skb);
493 skb_push_rcsum(skb, ETH_HLEN);
494 __skb_vlan_pop(skb, &tci);
495 skb_pull_rcsum(skb, ETH_HLEN);
498 vid = tci & VLAN_VID_MASK;
500 *source_port = dsa_8021q_rx_source_port(vid);
501 *switch_id = dsa_8021q_rx_switch_id(vid);
504 *vbid = dsa_tag_8021q_rx_vbid(vid);
506 skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
508 EXPORT_SYMBOL_GPL(dsa_8021q_rcv);