Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 void *injection;
50
51 ocelot_xmit_common(skb, netdev, cpu_to_be32(0x8880000a), &injection);
52 ocelot_ifh_set_dest(injection, dsa_xmit_port_mask(skb, netdev));
53
54 return skb;
55}
56
57static struct sk_buff *seville_xmit(struct sk_buff *skb,
58 struct net_device *netdev)
59{
60 void *injection;
61
62 ocelot_xmit_common(skb, netdev, cpu_to_be32(0x88800005), &injection);
63 seville_ifh_set_dest(injection, dsa_xmit_port_mask(skb, netdev));
64
65 return skb;
66}
67
68static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
69 struct net_device *netdev)
70{
71 u64 src_port, qos_class;
72 u64 vlan_tci, tag_type;
73 u8 *start = skb->data;
74 struct dsa_port *dp;
75 u8 *extraction;
76 u16 vlan_tpid;
77 u64 rew_val;
78
79 /* Revert skb->data by the amount consumed by the DSA conduit,
80 * so it points to the beginning of the frame.
81 */
82 skb_push(skb, ETH_HLEN);
83 /* We don't care about the short prefix, it is just for easy entrance
84 * into the DSA conduit's RX filter. Discard it now by moving it into
85 * the headroom.
86 */
87 skb_pull(skb, OCELOT_SHORT_PREFIX_LEN);
88 /* And skb->data now points to the extraction frame header.
89 * Keep a pointer to it.
90 */
91 extraction = skb->data;
92 /* Now the EFH is part of the headroom as well */
93 skb_pull(skb, OCELOT_TAG_LEN);
94 /* Reset the pointer to the real MAC header */
95 skb_reset_mac_header(skb);
96 skb_reset_mac_len(skb);
97 /* And move skb->data to the correct location again */
98 skb_pull(skb, ETH_HLEN);
99
100 /* Remove from inet csum the extraction header */
101 skb_postpull_rcsum(skb, start, OCELOT_TOTAL_TAG_LEN);
102
103 ocelot_xfh_get_src_port(extraction, &src_port);
104 ocelot_xfh_get_qos_class(extraction, &qos_class);
105 ocelot_xfh_get_tag_type(extraction, &tag_type);
106 ocelot_xfh_get_vlan_tci(extraction, &vlan_tci);
107 ocelot_xfh_get_rew_val(extraction, &rew_val);
108
109 skb->dev = dsa_conduit_find_user(netdev, 0, src_port);
110 if (!skb->dev)
111 /* The switch will reflect back some frames sent through
112 * sockets opened on the bare DSA conduit. These will come back
113 * with src_port equal to the index of the CPU port, for which
114 * there is no user registered. So don't print any error
115 * message here (ignore and drop those frames).
116 */
117 return NULL;
118
119 dsa_default_offload_fwd_mark(skb);
120 skb->priority = qos_class;
121 OCELOT_SKB_CB(skb)->tstamp_lo = rew_val;
122
123 /* Ocelot switches copy frames unmodified to the CPU. However, it is
124 * possible for the user to request a VLAN modification through
125 * VCAP_IS1_ACT_VID_REPLACE_ENA. In this case, what will happen is that
126 * the VLAN ID field from the Extraction Header gets updated, but the
127 * 802.1Q header does not (the classified VLAN only becomes visible on
128 * egress through the "port tag" of front-panel ports).
129 * So, for traffic extracted by the CPU, we want to pick up the
130 * classified VLAN and manually replace the existing 802.1Q header from
131 * the packet with it, so that the operating system is always up to
132 * date with the result of tc-vlan actions.
133 * NOTE: In VLAN-unaware mode, we don't want to do that, we want the
134 * frame to remain unmodified, because the classified VLAN is always
135 * equal to the pvid of the ingress port and should not be used for
136 * processing.
137 */
138 dp = dsa_user_to_port(skb->dev);
139 vlan_tpid = tag_type ? ETH_P_8021AD : ETH_P_8021Q;
140
141 if (dsa_port_is_vlan_filtering(dp) &&
142 eth_hdr(skb)->h_proto == htons(vlan_tpid)) {
143 u16 dummy_vlan_tci;
144
145 skb_push_rcsum(skb, ETH_HLEN);
146 __skb_vlan_pop(skb, &dummy_vlan_tci);
147 skb_pull_rcsum(skb, ETH_HLEN);
148 __vlan_hwaccel_put_tag(skb, htons(vlan_tpid), vlan_tci);
149 }
150
151 return skb;
152}
153
154static const struct dsa_device_ops ocelot_netdev_ops = {
155 .name = OCELOT_NAME,
156 .proto = DSA_TAG_PROTO_OCELOT,
157 .xmit = ocelot_xmit,
158 .rcv = ocelot_rcv,
159 .needed_headroom = OCELOT_TOTAL_TAG_LEN,
160 .promisc_on_conduit = true,
161};
162
163DSA_TAG_DRIVER(ocelot_netdev_ops);
164MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_OCELOT, OCELOT_NAME);
165
166static const struct dsa_device_ops seville_netdev_ops = {
167 .name = SEVILLE_NAME,
168 .proto = DSA_TAG_PROTO_SEVILLE,
169 .xmit = seville_xmit,
170 .rcv = ocelot_rcv,
171 .needed_headroom = OCELOT_TOTAL_TAG_LEN,
172 .promisc_on_conduit = true,
173};
174
175DSA_TAG_DRIVER(seville_netdev_ops);
176MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SEVILLE, SEVILLE_NAME);
177
178static struct dsa_tag_driver *ocelot_tag_driver_array[] = {
179 &DSA_TAG_DRIVER_NAME(ocelot_netdev_ops),
180 &DSA_TAG_DRIVER_NAME(seville_netdev_ops),
181};
182
183module_dsa_tag_drivers(ocelot_tag_driver_array);
184
185MODULE_DESCRIPTION("DSA tag driver for Ocelot family of switches, using NPI port");
186MODULE_LICENSE("GPL v2");