Linux Audio

Check our new training course

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