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