1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 NXP Semiconductors
4 #include <linux/dsa/ocelot.h>
5 #include <soc/mscc/ocelot.h>
8 static void ocelot_xmit_ptp(struct dsa_port *dp, void *injection,
11 struct ocelot *ocelot = dp->ds->priv;
12 struct ocelot_port *ocelot_port;
15 ocelot_port = ocelot->ports[dp->index];
16 rew_op = ocelot_port->ptp_cmd;
18 /* Retrieve timestamp ID populated inside skb->cb[0] of the
19 * clone by ocelot_port_add_txtstamp_skb
21 if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
22 rew_op |= clone->cb[0] << 3;
24 ocelot_ifh_set_rew_op(injection, rew_op);
27 static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
28 __be32 ifh_prefix, void **ifh)
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;
36 injection = skb_push(skb, OCELOT_TAG_LEN);
37 prefix = skb_push(skb, OCELOT_SHORT_PREFIX_LEN);
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);
45 /* TX timestamping was requested */
47 ocelot_xmit_ptp(dp, injection, clone);
52 static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
53 struct net_device *netdev)
55 struct dsa_port *dp = dsa_slave_to_port(netdev);
58 ocelot_xmit_common(skb, netdev, cpu_to_be32(0x8880000a), &injection);
59 ocelot_ifh_set_dest(injection, BIT_ULL(dp->index));
64 static struct sk_buff *seville_xmit(struct sk_buff *skb,
65 struct net_device *netdev)
67 struct dsa_port *dp = dsa_slave_to_port(netdev);
70 ocelot_xmit_common(skb, netdev, cpu_to_be32(0x88800005), &injection);
71 seville_ifh_set_dest(injection, BIT_ULL(dp->index));
76 static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
77 struct net_device *netdev,
78 struct packet_type *pt)
80 u64 src_port, qos_class;
81 u64 vlan_tci, tag_type;
82 u8 *start = skb->data;
88 /* Revert skb->data by the amount consumed by the DSA master,
89 * so it points to the beginning of the frame.
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
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.
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);
109 /* Remove from inet csum the extraction header */
110 skb_postpull_rcsum(skb, start, OCELOT_TOTAL_TAG_LEN);
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);
118 skb->dev = dsa_master_find_slave(netdev, 0, src_port);
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).
128 skb->offload_fwd_mark = 1;
129 skb->priority = qos_class;
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;
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
152 dp = dsa_slave_to_port(skb->dev);
153 vlan_tpid = tag_type ? ETH_P_8021AD : ETH_P_8021Q;
155 if (dsa_port_is_vlan_filtering(dp) &&
156 eth_hdr(skb)->h_proto == htons(vlan_tpid)) {
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);
168 static const struct dsa_device_ops ocelot_netdev_ops = {
170 .proto = DSA_TAG_PROTO_OCELOT,
173 .overhead = OCELOT_TOTAL_TAG_LEN,
174 .promisc_on_master = true,
177 DSA_TAG_DRIVER(ocelot_netdev_ops);
178 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_OCELOT);
180 static const struct dsa_device_ops seville_netdev_ops = {
182 .proto = DSA_TAG_PROTO_SEVILLE,
183 .xmit = seville_xmit,
185 .overhead = OCELOT_TOTAL_TAG_LEN,
186 .promisc_on_master = true,
189 DSA_TAG_DRIVER(seville_netdev_ops);
190 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SEVILLE);
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),
197 module_dsa_tag_drivers(ocelot_tag_driver_array);
199 MODULE_LICENSE("GPL v2");