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