xref: /openbmc/linux/net/dsa/tag_ocelot_8021q.c (revision 0fac6aa0)
17c83a7c5SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
27c83a7c5SVladimir Oltean /* Copyright 2020-2021 NXP Semiconductors
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>
120a6f17c6SVladimir Oltean #include <soc/mscc/ocelot.h>
130a6f17c6SVladimir Oltean #include <soc/mscc/ocelot_ptp.h>
147c83a7c5SVladimir Oltean #include "dsa_priv.h"
157c83a7c5SVladimir Oltean 
167c83a7c5SVladimir Oltean static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
177c83a7c5SVladimir Oltean 				   struct net_device *netdev)
187c83a7c5SVladimir Oltean {
197c83a7c5SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(netdev);
207c83a7c5SVladimir Oltean 	u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
217c83a7c5SVladimir Oltean 	u16 queue_mapping = skb_get_queue_mapping(skb);
227c83a7c5SVladimir Oltean 	u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
2339e5308bSYangbo Lu 	struct ocelot *ocelot = dp->ds->priv;
2439e5308bSYangbo Lu 	int port = dp->index;
2539e5308bSYangbo Lu 	u32 rew_op = 0;
260a6f17c6SVladimir Oltean 
2739e5308bSYangbo Lu 	rew_op = ocelot_ptp_rew_op(skb);
2839e5308bSYangbo Lu 	if (rew_op) {
2939e5308bSYangbo Lu 		if (!ocelot_can_inject(ocelot, 0))
3039e5308bSYangbo Lu 			return NULL;
3139e5308bSYangbo Lu 
3239e5308bSYangbo Lu 		ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
3339e5308bSYangbo Lu 		return NULL;
3439e5308bSYangbo Lu 	}
357c83a7c5SVladimir Oltean 
367c83a7c5SVladimir Oltean 	return dsa_8021q_xmit(skb, netdev, ETH_P_8021Q,
377c83a7c5SVladimir Oltean 			      ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
387c83a7c5SVladimir Oltean }
397c83a7c5SVladimir Oltean 
407c83a7c5SVladimir Oltean static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
417c83a7c5SVladimir Oltean 				  struct net_device *netdev,
427c83a7c5SVladimir Oltean 				  struct packet_type *pt)
437c83a7c5SVladimir Oltean {
44*0fac6aa0SVladimir Oltean 	int src_port, switch_id;
457c83a7c5SVladimir Oltean 
46*0fac6aa0SVladimir 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 
527c83a7c5SVladimir Oltean 	skb->offload_fwd_mark = 1;
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