1dfedd3b6SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
2cf85d08fSLennert Buytenhek /*
3469ee5feSTobias Waldekranz * Regular and Ethertype DSA tagging
4e84665c9SLennert Buytenhek * Copyright (c) 2008-2009 Marvell Semiconductor
5469ee5feSTobias Waldekranz *
6469ee5feSTobias Waldekranz * Regular DSA
7469ee5feSTobias Waldekranz * -----------
8469ee5feSTobias Waldekranz
9469ee5feSTobias Waldekranz * For untagged (in 802.1Q terms) packets, the switch will splice in
10469ee5feSTobias Waldekranz * the tag between the SA and the ethertype of the original
11469ee5feSTobias Waldekranz * packet. Tagged frames will instead have their outermost .1Q tag
12469ee5feSTobias Waldekranz * converted to a DSA tag. It expects the same layout when receiving
13469ee5feSTobias Waldekranz * packets from the CPU.
14469ee5feSTobias Waldekranz *
15469ee5feSTobias Waldekranz * Example:
16469ee5feSTobias Waldekranz *
17469ee5feSTobias Waldekranz * .----.----.----.---------
18469ee5feSTobias Waldekranz * Pu: | DA | SA | ET | Payload ...
19469ee5feSTobias Waldekranz * '----'----'----'---------
20469ee5feSTobias Waldekranz * 6 6 2 N
21469ee5feSTobias Waldekranz * .----.----.--------.-----.----.---------
22469ee5feSTobias Waldekranz * Pt: | DA | SA | 0x8100 | TCI | ET | Payload ...
23469ee5feSTobias Waldekranz * '----'----'--------'-----'----'---------
24469ee5feSTobias Waldekranz * 6 6 2 2 2 N
25469ee5feSTobias Waldekranz * .----.----.-----.----.---------
26469ee5feSTobias Waldekranz * Pd: | DA | SA | DSA | ET | Payload ...
27469ee5feSTobias Waldekranz * '----'----'-----'----'---------
28469ee5feSTobias Waldekranz * 6 6 4 2 N
29469ee5feSTobias Waldekranz *
30469ee5feSTobias Waldekranz * No matter if a packet is received untagged (Pu) or tagged (Pt),
31469ee5feSTobias Waldekranz * they will both have the same layout (Pd) when they are sent to the
32469ee5feSTobias Waldekranz * CPU. This is done by ignoring 802.3, replacing the ethertype field
33469ee5feSTobias Waldekranz * with more metadata, among which is a bit to signal if the original
34469ee5feSTobias Waldekranz * packet was tagged or not.
35469ee5feSTobias Waldekranz *
36469ee5feSTobias Waldekranz * Ethertype DSA
37469ee5feSTobias Waldekranz * -------------
38469ee5feSTobias Waldekranz * Uses the exact same tag format as regular DSA, but also includes a
39469ee5feSTobias Waldekranz * proper ethertype field (which the mv88e6xxx driver sets to
40469ee5feSTobias Waldekranz * ETH_P_EDSA/0xdada) followed by two zero bytes:
41469ee5feSTobias Waldekranz *
42469ee5feSTobias Waldekranz * .----.----.--------.--------.-----.----.---------
43469ee5feSTobias Waldekranz * | DA | SA | 0xdada | 0x0000 | DSA | ET | Payload ...
44469ee5feSTobias Waldekranz * '----'----'--------'--------'-----'----'---------
45469ee5feSTobias Waldekranz * 6 6 2 2 4 2 N
46cf85d08fSLennert Buytenhek */
47cf85d08fSLennert Buytenhek
485bded825SVladimir Oltean #include <linux/dsa/mv88e6xxx.h>
49cf85d08fSLennert Buytenhek #include <linux/etherdevice.h>
50cf85d08fSLennert Buytenhek #include <linux/list.h>
515a0e3ad6STejun Heo #include <linux/slab.h>
52ea5dd34bSVivien Didelot
53*bd954b82SVladimir Oltean #include "tag.h"
54cf85d08fSLennert Buytenhek
5594793a56SVladimir Oltean #define DSA_NAME "dsa"
5694793a56SVladimir Oltean #define EDSA_NAME "edsa"
5794793a56SVladimir Oltean
58cf85d08fSLennert Buytenhek #define DSA_HLEN 4
59cf85d08fSLennert Buytenhek
60469ee5feSTobias Waldekranz /**
61469ee5feSTobias Waldekranz * enum dsa_cmd - DSA Command
62469ee5feSTobias Waldekranz * @DSA_CMD_TO_CPU: Set on packets that were trapped or mirrored to
63469ee5feSTobias Waldekranz * the CPU port. This is needed to implement control protocols,
64469ee5feSTobias Waldekranz * e.g. STP and LLDP, that must not allow those control packets to
65469ee5feSTobias Waldekranz * be switched according to the normal rules.
66469ee5feSTobias Waldekranz * @DSA_CMD_FROM_CPU: Used by the CPU to send a packet to a specific
67469ee5feSTobias Waldekranz * port, ignoring all the barriers that the switch normally
68469ee5feSTobias Waldekranz * enforces (VLANs, STP port states etc.). No source address
69469ee5feSTobias Waldekranz * learning takes place. "sudo send packet"
70469ee5feSTobias Waldekranz * @DSA_CMD_TO_SNIFFER: Set on the copies of packets that matched some
71469ee5feSTobias Waldekranz * user configured ingress or egress monitor criteria. These are
72469ee5feSTobias Waldekranz * forwarded by the switch tree to the user configured ingress or
73469ee5feSTobias Waldekranz * egress monitor port, which can be set to the CPU port or a
74469ee5feSTobias Waldekranz * regular port. If the destination is a regular port, the tag
75469ee5feSTobias Waldekranz * will be removed before egressing the port. If the destination
76469ee5feSTobias Waldekranz * is the CPU port, the tag will not be removed.
77469ee5feSTobias Waldekranz * @DSA_CMD_FORWARD: This tag is used on all bulk traffic passing
78469ee5feSTobias Waldekranz * through the switch tree, including the flows that are directed
79469ee5feSTobias Waldekranz * towards the CPU. Its device/port tuple encodes the original
80469ee5feSTobias Waldekranz * source port on which the packet ingressed. It can also be used
81469ee5feSTobias Waldekranz * on transmit by the CPU to defer the forwarding decision to the
82469ee5feSTobias Waldekranz * hardware, based on the current config of PVT/VTU/ATU
83469ee5feSTobias Waldekranz * etc. Source address learning takes places if enabled on the
84469ee5feSTobias Waldekranz * receiving DSA/CPU port.
85469ee5feSTobias Waldekranz */
86469ee5feSTobias Waldekranz enum dsa_cmd {
87469ee5feSTobias Waldekranz DSA_CMD_TO_CPU = 0,
88469ee5feSTobias Waldekranz DSA_CMD_FROM_CPU = 1,
89469ee5feSTobias Waldekranz DSA_CMD_TO_SNIFFER = 2,
90469ee5feSTobias Waldekranz DSA_CMD_FORWARD = 3
91469ee5feSTobias Waldekranz };
92e468d141STobias Waldekranz
93469ee5feSTobias Waldekranz /**
94469ee5feSTobias Waldekranz * enum dsa_code - TO_CPU Code
95469ee5feSTobias Waldekranz *
96469ee5feSTobias Waldekranz * @DSA_CODE_MGMT_TRAP: DA was classified as a management
97469ee5feSTobias Waldekranz * address. Typical examples include STP BPDUs and LLDP.
98469ee5feSTobias Waldekranz * @DSA_CODE_FRAME2REG: Response to a "remote management" request.
99469ee5feSTobias Waldekranz * @DSA_CODE_IGMP_MLD_TRAP: IGMP/MLD signaling.
100469ee5feSTobias Waldekranz * @DSA_CODE_POLICY_TRAP: Frame matched some policy configuration on
101469ee5feSTobias Waldekranz * the device. Typical examples are matching on DA/SA/VID and DHCP
102469ee5feSTobias Waldekranz * snooping.
103469ee5feSTobias Waldekranz * @DSA_CODE_ARP_MIRROR: The name says it all really.
104469ee5feSTobias Waldekranz * @DSA_CODE_POLICY_MIRROR: Same as @DSA_CODE_POLICY_TRAP, but the
105469ee5feSTobias Waldekranz * particular policy was set to trigger a mirror instead of a
106469ee5feSTobias Waldekranz * trap.
107469ee5feSTobias Waldekranz * @DSA_CODE_RESERVED_6: Unused on all devices up to at least 6393X.
108469ee5feSTobias Waldekranz * @DSA_CODE_RESERVED_7: Unused on all devices up to at least 6393X.
109469ee5feSTobias Waldekranz *
110469ee5feSTobias Waldekranz * A 3-bit code is used to relay why a particular frame was sent to
111469ee5feSTobias Waldekranz * the CPU. We only use this to determine if the packet was mirrored
112469ee5feSTobias Waldekranz * or trapped, i.e. whether the packet has been forwarded by hardware
113469ee5feSTobias Waldekranz * or not.
114469ee5feSTobias Waldekranz *
115469ee5feSTobias Waldekranz * This is the superset of all possible codes. Any particular device
116469ee5feSTobias Waldekranz * may only implement a subset.
117469ee5feSTobias Waldekranz */
118469ee5feSTobias Waldekranz enum dsa_code {
119469ee5feSTobias Waldekranz DSA_CODE_MGMT_TRAP = 0,
120469ee5feSTobias Waldekranz DSA_CODE_FRAME2REG = 1,
121469ee5feSTobias Waldekranz DSA_CODE_IGMP_MLD_TRAP = 2,
122469ee5feSTobias Waldekranz DSA_CODE_POLICY_TRAP = 3,
123469ee5feSTobias Waldekranz DSA_CODE_ARP_MIRROR = 4,
124469ee5feSTobias Waldekranz DSA_CODE_POLICY_MIRROR = 5,
125469ee5feSTobias Waldekranz DSA_CODE_RESERVED_6 = 6,
126469ee5feSTobias Waldekranz DSA_CODE_RESERVED_7 = 7
127469ee5feSTobias Waldekranz };
128e468d141STobias Waldekranz
dsa_xmit_ll(struct sk_buff * skb,struct net_device * dev,u8 extra)129469ee5feSTobias Waldekranz static struct sk_buff *dsa_xmit_ll(struct sk_buff *skb, struct net_device *dev,
130469ee5feSTobias Waldekranz u8 extra)
131cf85d08fSLennert Buytenhek {
132d945097bSVivien Didelot struct dsa_port *dp = dsa_slave_to_port(dev);
1336c43a920STobias Waldekranz struct net_device *br_dev;
134d82f8ab0STobias Waldekranz u8 tag_dev, tag_port;
135d82f8ab0STobias Waldekranz enum dsa_cmd cmd;
136cf85d08fSLennert Buytenhek u8 *dsa_header;
137d82f8ab0STobias Waldekranz
138d82f8ab0STobias Waldekranz if (skb->offload_fwd_mark) {
13936cbf39bSVladimir Oltean unsigned int bridge_num = dsa_port_bridge_num_get(dp);
140d82f8ab0STobias Waldekranz struct dsa_switch_tree *dst = dp->ds->dst;
141d82f8ab0STobias Waldekranz
142d82f8ab0STobias Waldekranz cmd = DSA_CMD_FORWARD;
143d82f8ab0STobias Waldekranz
144d82f8ab0STobias Waldekranz /* When offloading forwarding for a bridge, inject FORWARD
145d82f8ab0STobias Waldekranz * packets on behalf of a virtual switch device with an index
146d82f8ab0STobias Waldekranz * past the physical switches.
147d82f8ab0STobias Waldekranz */
14836cbf39bSVladimir Oltean tag_dev = dst->last_switch + bridge_num;
149d82f8ab0STobias Waldekranz tag_port = 0;
150d82f8ab0STobias Waldekranz } else {
151d82f8ab0STobias Waldekranz cmd = DSA_CMD_FROM_CPU;
152d82f8ab0STobias Waldekranz tag_dev = dp->ds->index;
153d82f8ab0STobias Waldekranz tag_port = dp->index;
154d82f8ab0STobias Waldekranz }
155cf85d08fSLennert Buytenhek
1566c43a920STobias Waldekranz br_dev = dsa_port_bridge_dev_get(dp);
1576c43a920STobias Waldekranz
1586c43a920STobias Waldekranz /* If frame is already 802.1Q tagged, we can convert it to a DSA
1596c43a920STobias Waldekranz * tag (avoiding a memmove), but only if the port is standalone
1606c43a920STobias Waldekranz * (in which case we always send FROM_CPU) or if the port's
1616c43a920STobias Waldekranz * bridge has VLAN filtering enabled (in which case the CPU port
1626c43a920STobias Waldekranz * will be a member of the VLAN).
1636c43a920STobias Waldekranz */
1646c43a920STobias Waldekranz if (skb->protocol == htons(ETH_P_8021Q) &&
1656c43a920STobias Waldekranz (!br_dev || br_vlan_enabled(br_dev))) {
166469ee5feSTobias Waldekranz if (extra) {
167469ee5feSTobias Waldekranz skb_push(skb, extra);
1686bef794dSVladimir Oltean dsa_alloc_etype_header(skb, extra);
169469ee5feSTobias Waldekranz }
170469ee5feSTobias Waldekranz
171d82f8ab0STobias Waldekranz /* Construct tagged DSA tag from 802.1Q tag. */
172a72808b6SVladimir Oltean dsa_header = dsa_etype_header_pos_tx(skb) + extra;
173d82f8ab0STobias Waldekranz dsa_header[0] = (cmd << 6) | 0x20 | tag_dev;
174d82f8ab0STobias Waldekranz dsa_header[1] = tag_port << 3;
175cf85d08fSLennert Buytenhek
17613f49b6fSTobias Waldekranz /* Move CFI field from byte 2 to byte 1. */
177cf85d08fSLennert Buytenhek if (dsa_header[2] & 0x10) {
178cf85d08fSLennert Buytenhek dsa_header[1] |= 0x01;
179cf85d08fSLennert Buytenhek dsa_header[2] &= ~0x10;
180cf85d08fSLennert Buytenhek }
181cf85d08fSLennert Buytenhek } else {
1825bded825SVladimir Oltean u16 vid;
1835bded825SVladimir Oltean
1846c43a920STobias Waldekranz vid = br_dev ? MV88E6XXX_VID_BRIDGED : MV88E6XXX_VID_STANDALONE;
1855bded825SVladimir Oltean
186469ee5feSTobias Waldekranz skb_push(skb, DSA_HLEN + extra);
1876bef794dSVladimir Oltean dsa_alloc_etype_header(skb, DSA_HLEN + extra);
188cf85d08fSLennert Buytenhek
1895bded825SVladimir Oltean /* Construct DSA header from untagged frame. */
190a72808b6SVladimir Oltean dsa_header = dsa_etype_header_pos_tx(skb) + extra;
191d82f8ab0STobias Waldekranz
192d82f8ab0STobias Waldekranz dsa_header[0] = (cmd << 6) | tag_dev;
193d82f8ab0STobias Waldekranz dsa_header[1] = tag_port << 3;
1945bded825SVladimir Oltean dsa_header[2] = vid >> 8;
1955bded825SVladimir Oltean dsa_header[3] = vid & 0xff;
196cf85d08fSLennert Buytenhek }
197cf85d08fSLennert Buytenhek
1984ed70ce9SFlorian Fainelli return skb;
199cf85d08fSLennert Buytenhek }
200cf85d08fSLennert Buytenhek
dsa_rcv_ll(struct sk_buff * skb,struct net_device * dev,u8 extra)201469ee5feSTobias Waldekranz static struct sk_buff *dsa_rcv_ll(struct sk_buff *skb, struct net_device *dev,
202469ee5feSTobias Waldekranz u8 extra)
203cf85d08fSLennert Buytenhek {
204bea79078SVladimir Oltean bool trap = false, trunk = false;
205469ee5feSTobias Waldekranz int source_device, source_port;
206469ee5feSTobias Waldekranz enum dsa_code code;
207469ee5feSTobias Waldekranz enum dsa_cmd cmd;
208cf85d08fSLennert Buytenhek u8 *dsa_header;
209cf85d08fSLennert Buytenhek
21013f49b6fSTobias Waldekranz /* The ethertype field is part of the DSA header. */
2115d928ff4SVladimir Oltean dsa_header = dsa_etype_header_pos_rx(skb);
212cf85d08fSLennert Buytenhek
213469ee5feSTobias Waldekranz cmd = dsa_header[0] >> 6;
214469ee5feSTobias Waldekranz switch (cmd) {
215469ee5feSTobias Waldekranz case DSA_CMD_FORWARD:
216b44d52a5SAndrew Lunn trunk = !!(dsa_header[1] & 4);
217e468d141STobias Waldekranz break;
218e468d141STobias Waldekranz
219469ee5feSTobias Waldekranz case DSA_CMD_TO_CPU:
220469ee5feSTobias Waldekranz code = (dsa_header[1] & 0x6) | ((dsa_header[2] >> 4) & 1);
221469ee5feSTobias Waldekranz
222469ee5feSTobias Waldekranz switch (code) {
223469ee5feSTobias Waldekranz case DSA_CODE_FRAME2REG:
224469ee5feSTobias Waldekranz /* Remote management is not implemented yet,
225469ee5feSTobias Waldekranz * drop.
226469ee5feSTobias Waldekranz */
227469ee5feSTobias Waldekranz return NULL;
228469ee5feSTobias Waldekranz case DSA_CODE_ARP_MIRROR:
229469ee5feSTobias Waldekranz case DSA_CODE_POLICY_MIRROR:
230469ee5feSTobias Waldekranz /* Mark mirrored packets to notify any upper
231469ee5feSTobias Waldekranz * device (like a bridge) that forwarding has
232469ee5feSTobias Waldekranz * already been done by hardware.
233469ee5feSTobias Waldekranz */
234e468d141STobias Waldekranz break;
235469ee5feSTobias Waldekranz case DSA_CODE_MGMT_TRAP:
236469ee5feSTobias Waldekranz case DSA_CODE_IGMP_MLD_TRAP:
237469ee5feSTobias Waldekranz case DSA_CODE_POLICY_TRAP:
238469ee5feSTobias Waldekranz /* Traps have, by definition, not been
239469ee5feSTobias Waldekranz * forwarded by hardware, so don't mark them.
240469ee5feSTobias Waldekranz */
241bea79078SVladimir Oltean trap = true;
242469ee5feSTobias Waldekranz break;
243469ee5feSTobias Waldekranz default:
244469ee5feSTobias Waldekranz /* Reserved code, this could be anything. Drop
245469ee5feSTobias Waldekranz * seems like the safest option.
246469ee5feSTobias Waldekranz */
247469ee5feSTobias Waldekranz return NULL;
248469ee5feSTobias Waldekranz }
249469ee5feSTobias Waldekranz
250469ee5feSTobias Waldekranz break;
251e468d141STobias Waldekranz
252e468d141STobias Waldekranz default:
25354709795SVivien Didelot return NULL;
254e468d141STobias Waldekranz }
255cf85d08fSLennert Buytenhek
256e84665c9SLennert Buytenhek source_device = dsa_header[0] & 0x1f;
257cf85d08fSLennert Buytenhek source_port = (dsa_header[1] >> 3) & 0x1f;
258e84665c9SLennert Buytenhek
2595b60dadbSTobias Waldekranz if (trunk) {
2605b60dadbSTobias Waldekranz struct dsa_port *cpu_dp = dev->dsa_ptr;
261dedd6a00SVladimir Oltean struct dsa_lag *lag;
2625b60dadbSTobias Waldekranz
2635b60dadbSTobias Waldekranz /* The exact source port is not available in the tag,
2645b60dadbSTobias Waldekranz * so we inject the frame directly on the upper
2655b60dadbSTobias Waldekranz * team/bond.
2665b60dadbSTobias Waldekranz */
267dedd6a00SVladimir Oltean lag = dsa_lag_by_id(cpu_dp->dst, source_port + 1);
268dedd6a00SVladimir Oltean skb->dev = lag ? lag->dev : NULL;
2695b60dadbSTobias Waldekranz } else {
2705b60dadbSTobias Waldekranz skb->dev = dsa_master_find_slave(dev, source_device,
2715b60dadbSTobias Waldekranz source_port);
2725b60dadbSTobias Waldekranz }
2735b60dadbSTobias Waldekranz
2743775b1b7SVivien Didelot if (!skb->dev)
27554709795SVivien Didelot return NULL;
276cf85d08fSLennert Buytenhek
277bea79078SVladimir Oltean /* When using LAG offload, skb->dev is not a DSA slave interface,
278bea79078SVladimir Oltean * so we cannot call dsa_default_offload_fwd_mark and we need to
279bea79078SVladimir Oltean * special-case it.
280bea79078SVladimir Oltean */
281bea79078SVladimir Oltean if (trunk)
282bea79078SVladimir Oltean skb->offload_fwd_mark = true;
283bea79078SVladimir Oltean else if (!trap)
284bea79078SVladimir Oltean dsa_default_offload_fwd_mark(skb);
285bea79078SVladimir Oltean
286469ee5feSTobias Waldekranz /* If the 'tagged' bit is set; convert the DSA tag to a 802.1Q
287469ee5feSTobias Waldekranz * tag, and delete the ethertype (extra) if applicable. If the
288469ee5feSTobias Waldekranz * 'tagged' bit is cleared; delete the DSA tag, and ethertype
289469ee5feSTobias Waldekranz * if applicable.
290cf85d08fSLennert Buytenhek */
291cf85d08fSLennert Buytenhek if (dsa_header[0] & 0x20) {
292cf85d08fSLennert Buytenhek u8 new_header[4];
293cf85d08fSLennert Buytenhek
29413f49b6fSTobias Waldekranz /* Insert 802.1Q ethertype and copy the VLAN-related
295cf85d08fSLennert Buytenhek * fields, but clear the bit that will hold CFI (since
296cf85d08fSLennert Buytenhek * DSA uses that bit location for another purpose).
297cf85d08fSLennert Buytenhek */
298cf85d08fSLennert Buytenhek new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
299cf85d08fSLennert Buytenhek new_header[1] = ETH_P_8021Q & 0xff;
300cf85d08fSLennert Buytenhek new_header[2] = dsa_header[2] & ~0x10;
301cf85d08fSLennert Buytenhek new_header[3] = dsa_header[3];
302cf85d08fSLennert Buytenhek
30313f49b6fSTobias Waldekranz /* Move CFI bit from its place in the DSA header to
30413f49b6fSTobias Waldekranz * its 802.1Q-designated place.
305cf85d08fSLennert Buytenhek */
306cf85d08fSLennert Buytenhek if (dsa_header[1] & 0x01)
307cf85d08fSLennert Buytenhek new_header[2] |= 0x10;
308cf85d08fSLennert Buytenhek
30913f49b6fSTobias Waldekranz /* Update packet checksum if skb is CHECKSUM_COMPLETE. */
310cf85d08fSLennert Buytenhek if (skb->ip_summed == CHECKSUM_COMPLETE) {
311cf85d08fSLennert Buytenhek __wsum c = skb->csum;
312cf85d08fSLennert Buytenhek c = csum_add(c, csum_partial(new_header + 2, 2, 0));
313cf85d08fSLennert Buytenhek c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
314cf85d08fSLennert Buytenhek skb->csum = c;
315cf85d08fSLennert Buytenhek }
316cf85d08fSLennert Buytenhek
317cf85d08fSLennert Buytenhek memcpy(dsa_header, new_header, DSA_HLEN);
318469ee5feSTobias Waldekranz
319469ee5feSTobias Waldekranz if (extra)
320f1dacd7aSVladimir Oltean dsa_strip_etype_header(skb, extra);
321cf85d08fSLennert Buytenhek } else {
322cf85d08fSLennert Buytenhek skb_pull_rcsum(skb, DSA_HLEN);
323f1dacd7aSVladimir Oltean dsa_strip_etype_header(skb, DSA_HLEN + extra);
324cf85d08fSLennert Buytenhek }
325cf85d08fSLennert Buytenhek
326a86d8becSFlorian Fainelli return skb;
327cf85d08fSLennert Buytenhek }
328cf85d08fSLennert Buytenhek
329469ee5feSTobias Waldekranz #if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
330469ee5feSTobias Waldekranz
dsa_xmit(struct sk_buff * skb,struct net_device * dev)331469ee5feSTobias Waldekranz static struct sk_buff *dsa_xmit(struct sk_buff *skb, struct net_device *dev)
332469ee5feSTobias Waldekranz {
333469ee5feSTobias Waldekranz return dsa_xmit_ll(skb, dev, 0);
334469ee5feSTobias Waldekranz }
335469ee5feSTobias Waldekranz
dsa_rcv(struct sk_buff * skb,struct net_device * dev)33629a097b7SVladimir Oltean static struct sk_buff *dsa_rcv(struct sk_buff *skb, struct net_device *dev)
337469ee5feSTobias Waldekranz {
338469ee5feSTobias Waldekranz if (unlikely(!pskb_may_pull(skb, DSA_HLEN)))
339469ee5feSTobias Waldekranz return NULL;
340469ee5feSTobias Waldekranz
341469ee5feSTobias Waldekranz return dsa_rcv_ll(skb, dev, 0);
342469ee5feSTobias Waldekranz }
343469ee5feSTobias Waldekranz
344f81a43e8SAndrew Lunn static const struct dsa_device_ops dsa_netdev_ops = {
34594793a56SVladimir Oltean .name = DSA_NAME,
346056eed2fSAndrew Lunn .proto = DSA_TAG_PROTO_DSA,
3473e8a72d1SFlorian Fainelli .xmit = dsa_xmit,
3483e8a72d1SFlorian Fainelli .rcv = dsa_rcv,
3494e500251SVladimir Oltean .needed_headroom = DSA_HLEN,
350cf85d08fSLennert Buytenhek };
3510b42f033SAndrew Lunn
352469ee5feSTobias Waldekranz DSA_TAG_DRIVER(dsa_netdev_ops);
35394793a56SVladimir Oltean MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_DSA, DSA_NAME);
354469ee5feSTobias Waldekranz #endif /* CONFIG_NET_DSA_TAG_DSA */
355d3b8c049SAndrew Lunn
356469ee5feSTobias Waldekranz #if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
357469ee5feSTobias Waldekranz
358469ee5feSTobias Waldekranz #define EDSA_HLEN 8
359469ee5feSTobias Waldekranz
edsa_xmit(struct sk_buff * skb,struct net_device * dev)360469ee5feSTobias Waldekranz static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
361469ee5feSTobias Waldekranz {
362469ee5feSTobias Waldekranz u8 *edsa_header;
363469ee5feSTobias Waldekranz
364469ee5feSTobias Waldekranz skb = dsa_xmit_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
365469ee5feSTobias Waldekranz if (!skb)
366469ee5feSTobias Waldekranz return NULL;
367469ee5feSTobias Waldekranz
368a72808b6SVladimir Oltean edsa_header = dsa_etype_header_pos_tx(skb);
369469ee5feSTobias Waldekranz edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
370469ee5feSTobias Waldekranz edsa_header[1] = ETH_P_EDSA & 0xff;
371469ee5feSTobias Waldekranz edsa_header[2] = 0x00;
372469ee5feSTobias Waldekranz edsa_header[3] = 0x00;
373469ee5feSTobias Waldekranz return skb;
374469ee5feSTobias Waldekranz }
375469ee5feSTobias Waldekranz
edsa_rcv(struct sk_buff * skb,struct net_device * dev)37629a097b7SVladimir Oltean static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev)
377469ee5feSTobias Waldekranz {
378469ee5feSTobias Waldekranz if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
379469ee5feSTobias Waldekranz return NULL;
380469ee5feSTobias Waldekranz
381469ee5feSTobias Waldekranz skb_pull_rcsum(skb, EDSA_HLEN - DSA_HLEN);
382469ee5feSTobias Waldekranz
383469ee5feSTobias Waldekranz return dsa_rcv_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
384469ee5feSTobias Waldekranz }
385469ee5feSTobias Waldekranz
386469ee5feSTobias Waldekranz static const struct dsa_device_ops edsa_netdev_ops = {
38794793a56SVladimir Oltean .name = EDSA_NAME,
388469ee5feSTobias Waldekranz .proto = DSA_TAG_PROTO_EDSA,
389469ee5feSTobias Waldekranz .xmit = edsa_xmit,
390469ee5feSTobias Waldekranz .rcv = edsa_rcv,
3914e500251SVladimir Oltean .needed_headroom = EDSA_HLEN,
392469ee5feSTobias Waldekranz };
393469ee5feSTobias Waldekranz
394469ee5feSTobias Waldekranz DSA_TAG_DRIVER(edsa_netdev_ops);
39594793a56SVladimir Oltean MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_EDSA, EDSA_NAME);
396469ee5feSTobias Waldekranz #endif /* CONFIG_NET_DSA_TAG_EDSA */
397469ee5feSTobias Waldekranz
398469ee5feSTobias Waldekranz static struct dsa_tag_driver *dsa_tag_drivers[] = {
399469ee5feSTobias Waldekranz #if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
400469ee5feSTobias Waldekranz &DSA_TAG_DRIVER_NAME(dsa_netdev_ops),
401469ee5feSTobias Waldekranz #endif
402469ee5feSTobias Waldekranz #if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
403469ee5feSTobias Waldekranz &DSA_TAG_DRIVER_NAME(edsa_netdev_ops),
404469ee5feSTobias Waldekranz #endif
405469ee5feSTobias Waldekranz };
406469ee5feSTobias Waldekranz
407469ee5feSTobias Waldekranz module_dsa_tag_drivers(dsa_tag_drivers);
408469ee5feSTobias Waldekranz
409469ee5feSTobias Waldekranz MODULE_LICENSE("GPL");
410