xref: /openbmc/linux/net/dsa/tag_ocelot_8021q.c (revision deab6b1c)
17c83a7c5SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
23c9cfb52SVladimir Oltean /* Copyright 2020-2021 NXP
37c83a7c5SVladimir Oltean  *
47c83a7c5SVladimir Oltean  * An implementation of the software-defined tag_8021q.c tagger format, which
57c83a7c5SVladimir Oltean  * also preserves full functionality under a vlan_filtering bridge. It does
67c83a7c5SVladimir Oltean  * this by using the TCAM engines for:
77c83a7c5SVladimir Oltean  * - pushing the RX VLAN as a second, outer tag, on egress towards the CPU port
87c83a7c5SVladimir Oltean  * - redirecting towards the correct front port based on TX VLAN and popping
97c83a7c5SVladimir Oltean  *   that on egress
107c83a7c5SVladimir Oltean  */
117c83a7c5SVladimir Oltean #include <linux/dsa/8021q.h>
12*deab6b1cSVladimir Oltean #include <linux/dsa/ocelot.h>
130a6f17c6SVladimir Oltean #include <soc/mscc/ocelot.h>
140a6f17c6SVladimir Oltean #include <soc/mscc/ocelot_ptp.h>
157c83a7c5SVladimir Oltean #include "dsa_priv.h"
167c83a7c5SVladimir Oltean 
177c83a7c5SVladimir Oltean static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
187c83a7c5SVladimir Oltean 				   struct net_device *netdev)
197c83a7c5SVladimir Oltean {
207c83a7c5SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(netdev);
217c83a7c5SVladimir Oltean 	u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
227c83a7c5SVladimir Oltean 	u16 queue_mapping = skb_get_queue_mapping(skb);
237c83a7c5SVladimir Oltean 	u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
2439e5308bSYangbo Lu 	struct ocelot *ocelot = dp->ds->priv;
2539e5308bSYangbo Lu 	int port = dp->index;
2639e5308bSYangbo Lu 	u32 rew_op = 0;
270a6f17c6SVladimir Oltean 
2839e5308bSYangbo Lu 	rew_op = ocelot_ptp_rew_op(skb);
2939e5308bSYangbo Lu 	if (rew_op) {
3039e5308bSYangbo Lu 		if (!ocelot_can_inject(ocelot, 0))
3139e5308bSYangbo Lu 			return NULL;
3239e5308bSYangbo Lu 
3339e5308bSYangbo Lu 		ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
3439e5308bSYangbo Lu 		return NULL;
3539e5308bSYangbo Lu 	}
367c83a7c5SVladimir Oltean 
377c83a7c5SVladimir Oltean 	return dsa_8021q_xmit(skb, netdev, ETH_P_8021Q,
387c83a7c5SVladimir Oltean 			      ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
397c83a7c5SVladimir Oltean }
407c83a7c5SVladimir Oltean 
417c83a7c5SVladimir Oltean static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
4229a097b7SVladimir Oltean 				  struct net_device *netdev)
437c83a7c5SVladimir Oltean {
440fac6aa0SVladimir Oltean 	int src_port, switch_id;
457c83a7c5SVladimir Oltean 
460fac6aa0SVladimir Oltean 	dsa_8021q_rcv(skb, &src_port, &switch_id);
477c83a7c5SVladimir Oltean 
487c83a7c5SVladimir Oltean 	skb->dev = dsa_master_find_slave(netdev, switch_id, src_port);
497c83a7c5SVladimir Oltean 	if (!skb->dev)
507c83a7c5SVladimir Oltean 		return NULL;
517c83a7c5SVladimir Oltean 
52bea79078SVladimir Oltean 	dsa_default_offload_fwd_mark(skb);
537c83a7c5SVladimir Oltean 
547c83a7c5SVladimir Oltean 	return skb;
557c83a7c5SVladimir Oltean }
567c83a7c5SVladimir Oltean 
577c83a7c5SVladimir Oltean static const struct dsa_device_ops ocelot_8021q_netdev_ops = {
587c83a7c5SVladimir Oltean 	.name			= "ocelot-8021q",
597c83a7c5SVladimir Oltean 	.proto			= DSA_TAG_PROTO_OCELOT_8021Q,
607c83a7c5SVladimir Oltean 	.xmit			= ocelot_xmit,
617c83a7c5SVladimir Oltean 	.rcv			= ocelot_rcv,
624e500251SVladimir Oltean 	.needed_headroom	= VLAN_HLEN,
63c8c0ba4fSVladimir Oltean 	.promisc_on_master	= true,
647c83a7c5SVladimir Oltean };
657c83a7c5SVladimir Oltean 
667c83a7c5SVladimir Oltean MODULE_LICENSE("GPL v2");
677c83a7c5SVladimir Oltean MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_OCELOT_8021Q);
687c83a7c5SVladimir Oltean 
697c83a7c5SVladimir Oltean module_dsa_tag_driver(ocelot_8021q_netdev_ops);
70