1 // SPDX-License-Identifier: GPL-2.0+
3 * net/dsa/tag_ksz.c - Microchip KSZ Switch tag format handling
4 * Copyright (c) 2017 Microchip Technology
7 #include <linux/dsa/ksz_common.h>
8 #include <linux/etherdevice.h>
9 #include <linux/list.h>
10 #include <linux/ptp_classify.h>
15 #define KSZ8795_NAME "ksz8795"
16 #define KSZ9477_NAME "ksz9477"
17 #define KSZ9893_NAME "ksz9893"
18 #define LAN937X_NAME "lan937x"
20 /* Typically only one byte is used for tail tag. */
21 #define KSZ_PTP_TAG_LEN 4
22 #define KSZ_EGRESS_TAG_LEN 1
23 #define KSZ_INGRESS_TAG_LEN 1
27 struct ksz_tagger_private {
28 struct ksz_tagger_data data; /* Must be first */
30 struct kthread_worker *xmit_worker;
33 static struct ksz_tagger_private *
34 ksz_tagger_private(struct dsa_switch *ds)
36 return ds->tagger_data;
39 static void ksz_hwtstamp_set_state(struct dsa_switch *ds, bool on)
41 struct ksz_tagger_private *priv = ksz_tagger_private(ds);
44 set_bit(KSZ_HWTS_EN, &priv->state);
46 clear_bit(KSZ_HWTS_EN, &priv->state);
49 static void ksz_disconnect(struct dsa_switch *ds)
51 struct ksz_tagger_private *priv = ds->tagger_data;
53 kthread_destroy_worker(priv->xmit_worker);
55 ds->tagger_data = NULL;
58 static int ksz_connect(struct dsa_switch *ds)
60 struct ksz_tagger_data *tagger_data;
61 struct kthread_worker *xmit_worker;
62 struct ksz_tagger_private *priv;
65 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
69 xmit_worker = kthread_create_worker(0, "dsa%d:%d_xmit",
70 ds->dst->index, ds->index);
71 if (IS_ERR(xmit_worker)) {
72 ret = PTR_ERR(xmit_worker);
77 priv->xmit_worker = xmit_worker;
78 /* Export functions for switch driver use */
79 tagger_data = &priv->data;
80 tagger_data->hwtstamp_set_state = ksz_hwtstamp_set_state;
81 ds->tagger_data = priv;
86 static struct sk_buff *ksz_common_rcv(struct sk_buff *skb,
87 struct net_device *dev,
88 unsigned int port, unsigned int len)
90 skb->dev = dsa_master_find_slave(dev, 0, port);
94 if (pskb_trim_rcsum(skb, skb->len - len))
97 dsa_default_offload_fwd_mark(skb);
103 * For Ingress (Host -> KSZ8795), 1 byte is added before FCS.
104 * ---------------------------------------------------------------------------
105 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag(1byte)|FCS(4bytes)
106 * ---------------------------------------------------------------------------
107 * tag : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
109 * For Egress (KSZ8795 -> Host), 1 byte is added before FCS.
110 * ---------------------------------------------------------------------------
111 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
112 * ---------------------------------------------------------------------------
113 * tag0 : zero-based value represents port
114 * (eg, 0x00=port1, 0x02=port3, 0x06=port7)
117 #define KSZ8795_TAIL_TAG_OVERRIDE BIT(6)
118 #define KSZ8795_TAIL_TAG_LOOKUP BIT(7)
120 static struct sk_buff *ksz8795_xmit(struct sk_buff *skb, struct net_device *dev)
122 struct dsa_port *dp = dsa_slave_to_port(dev);
126 if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
130 tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
131 addr = skb_mac_header(skb);
133 *tag = 1 << dp->index;
134 if (is_link_local_ether_addr(addr))
135 *tag |= KSZ8795_TAIL_TAG_OVERRIDE;
140 static struct sk_buff *ksz8795_rcv(struct sk_buff *skb, struct net_device *dev)
142 u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
144 return ksz_common_rcv(skb, dev, tag[0] & 7, KSZ_EGRESS_TAG_LEN);
147 static const struct dsa_device_ops ksz8795_netdev_ops = {
148 .name = KSZ8795_NAME,
149 .proto = DSA_TAG_PROTO_KSZ8795,
150 .xmit = ksz8795_xmit,
152 .needed_tailroom = KSZ_INGRESS_TAG_LEN,
155 DSA_TAG_DRIVER(ksz8795_netdev_ops);
156 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795, KSZ8795_NAME);
159 * For Ingress (Host -> KSZ9477), 2/6 bytes are added before FCS.
160 * ---------------------------------------------------------------------------
161 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|tag1(1byte)|
163 * ---------------------------------------------------------------------------
164 * ts : time stamp (Present only if PTP is enabled in the Hardware)
165 * tag0 : Prioritization (not used now)
166 * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
168 * For Egress (KSZ9477 -> Host), 1/5 bytes is added before FCS.
169 * ---------------------------------------------------------------------------
170 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|FCS(4bytes)
171 * ---------------------------------------------------------------------------
172 * ts : time stamp (Present only if bit 7 of tag0 is set)
173 * tag0 : zero-based value represents port
174 * (eg, 0x00=port1, 0x02=port3, 0x06=port7)
177 #define KSZ9477_INGRESS_TAG_LEN 2
178 #define KSZ9477_PTP_TAG_LEN 4
179 #define KSZ9477_PTP_TAG_INDICATION 0x80
181 #define KSZ9477_TAIL_TAG_OVERRIDE BIT(9)
182 #define KSZ9477_TAIL_TAG_LOOKUP BIT(10)
184 static void ksz_rcv_timestamp(struct sk_buff *skb, u8 *tag)
186 u8 *tstamp_raw = tag - KSZ_PTP_TAG_LEN;
189 tstamp = ksz_decode_tstamp(get_unaligned_be32(tstamp_raw));
190 KSZ_SKB_CB(skb)->tstamp = tstamp;
193 /* Time stamp tag *needs* to be inserted if PTP is enabled in hardware.
194 * Regardless of Whether it is a PTP frame or not.
196 static void ksz_xmit_timestamp(struct dsa_port *dp, struct sk_buff *skb)
198 struct ksz_tagger_private *priv;
199 struct ptp_header *ptp_hdr;
200 unsigned int ptp_type;
204 priv = ksz_tagger_private(dp->ds);
206 if (!test_bit(KSZ_HWTS_EN, &priv->state))
209 if (!KSZ_SKB_CB(skb)->update_correction)
212 ptp_type = KSZ_SKB_CB(skb)->ptp_type;
214 ptp_hdr = ptp_parse_header(skb, ptp_type);
218 correction = (s64)get_unaligned_be64(&ptp_hdr->correction);
220 if (correction < 0) {
221 struct timespec64 ts;
223 ts = ns_to_timespec64(-correction >> 16);
224 tstamp_raw = ((ts.tv_sec & 3) << 30) | ts.tv_nsec;
226 /* Set correction field to 0 and update UDP checksum */
227 ptp_header_update_correction(skb, ptp_type, ptp_hdr, 0);
231 put_unaligned_be32(tstamp_raw, skb_put(skb, KSZ_PTP_TAG_LEN));
234 /* Defer transmit if waiting for egress time stamp is required. */
235 static struct sk_buff *ksz_defer_xmit(struct dsa_port *dp, struct sk_buff *skb)
237 struct ksz_tagger_data *tagger_data = ksz_tagger_data(dp->ds);
238 struct ksz_tagger_private *priv = ksz_tagger_private(dp->ds);
239 void (*xmit_work_fn)(struct kthread_work *work);
240 struct sk_buff *clone = KSZ_SKB_CB(skb)->clone;
241 struct ksz_deferred_xmit_work *xmit_work;
242 struct kthread_worker *xmit_worker;
245 return skb; /* no deferred xmit for this packet */
247 xmit_work_fn = tagger_data->xmit_work_fn;
248 xmit_worker = priv->xmit_worker;
250 if (!xmit_work_fn || !xmit_worker)
253 xmit_work = kzalloc(sizeof(*xmit_work), GFP_ATOMIC);
257 kthread_init_work(&xmit_work->work, xmit_work_fn);
258 /* Increase refcount so the kfree_skb in dsa_slave_xmit
259 * won't really free the packet.
262 xmit_work->skb = skb_get(skb);
264 kthread_queue_work(xmit_worker, &xmit_work->work);
269 static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
270 struct net_device *dev)
272 struct dsa_port *dp = dsa_slave_to_port(dev);
277 if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
281 ksz_xmit_timestamp(dp, skb);
283 tag = skb_put(skb, KSZ9477_INGRESS_TAG_LEN);
284 addr = skb_mac_header(skb);
286 val = BIT(dp->index);
288 if (is_link_local_ether_addr(addr))
289 val |= KSZ9477_TAIL_TAG_OVERRIDE;
291 *tag = cpu_to_be16(val);
293 return ksz_defer_xmit(dp, skb);
296 static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev)
299 u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
300 unsigned int port = tag[0] & 7;
301 unsigned int len = KSZ_EGRESS_TAG_LEN;
303 /* Extra 4-bytes PTP timestamp */
304 if (tag[0] & KSZ9477_PTP_TAG_INDICATION) {
305 ksz_rcv_timestamp(skb, tag);
306 len += KSZ_PTP_TAG_LEN;
309 return ksz_common_rcv(skb, dev, port, len);
312 static const struct dsa_device_ops ksz9477_netdev_ops = {
313 .name = KSZ9477_NAME,
314 .proto = DSA_TAG_PROTO_KSZ9477,
315 .xmit = ksz9477_xmit,
317 .connect = ksz_connect,
318 .disconnect = ksz_disconnect,
319 .needed_tailroom = KSZ9477_INGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
322 DSA_TAG_DRIVER(ksz9477_netdev_ops);
323 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9477, KSZ9477_NAME);
325 #define KSZ9893_TAIL_TAG_OVERRIDE BIT(5)
326 #define KSZ9893_TAIL_TAG_LOOKUP BIT(6)
328 static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
329 struct net_device *dev)
331 struct dsa_port *dp = dsa_slave_to_port(dev);
335 if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
339 ksz_xmit_timestamp(dp, skb);
341 tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
342 addr = skb_mac_header(skb);
344 *tag = BIT(dp->index);
346 if (is_link_local_ether_addr(addr))
347 *tag |= KSZ9893_TAIL_TAG_OVERRIDE;
349 return ksz_defer_xmit(dp, skb);
352 static const struct dsa_device_ops ksz9893_netdev_ops = {
353 .name = KSZ9893_NAME,
354 .proto = DSA_TAG_PROTO_KSZ9893,
355 .xmit = ksz9893_xmit,
357 .connect = ksz_connect,
358 .disconnect = ksz_disconnect,
359 .needed_tailroom = KSZ_INGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
362 DSA_TAG_DRIVER(ksz9893_netdev_ops);
363 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893, KSZ9893_NAME);
365 /* For xmit, 2/6 bytes are added before FCS.
366 * ---------------------------------------------------------------------------
367 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|tag1(1byte)|
369 * ---------------------------------------------------------------------------
370 * ts : time stamp (Present only if PTP is enabled in the Hardware)
371 * tag0 : represents tag override, lookup and valid
372 * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x80=port8)
374 * For rcv, 1/5 bytes is added before FCS.
375 * ---------------------------------------------------------------------------
376 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|ts(4bytes)|tag0(1byte)|FCS(4bytes)
377 * ---------------------------------------------------------------------------
378 * ts : time stamp (Present only if bit 7 of tag0 is set)
379 * tag0 : zero-based value represents port
380 * (eg, 0x00=port1, 0x02=port3, 0x07=port8)
382 #define LAN937X_EGRESS_TAG_LEN 2
384 #define LAN937X_TAIL_TAG_BLOCKING_OVERRIDE BIT(11)
385 #define LAN937X_TAIL_TAG_LOOKUP BIT(12)
386 #define LAN937X_TAIL_TAG_VALID BIT(13)
387 #define LAN937X_TAIL_TAG_PORT_MASK 7
389 static struct sk_buff *lan937x_xmit(struct sk_buff *skb,
390 struct net_device *dev)
392 struct dsa_port *dp = dsa_slave_to_port(dev);
393 const struct ethhdr *hdr = eth_hdr(skb);
397 if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
400 ksz_xmit_timestamp(dp, skb);
402 tag = skb_put(skb, LAN937X_EGRESS_TAG_LEN);
404 val = BIT(dp->index);
406 if (is_link_local_ether_addr(hdr->h_dest))
407 val |= LAN937X_TAIL_TAG_BLOCKING_OVERRIDE;
409 /* Tail tag valid bit - This bit should always be set by the CPU */
410 val |= LAN937X_TAIL_TAG_VALID;
412 put_unaligned_be16(val, tag);
414 return ksz_defer_xmit(dp, skb);
417 static const struct dsa_device_ops lan937x_netdev_ops = {
418 .name = LAN937X_NAME,
419 .proto = DSA_TAG_PROTO_LAN937X,
420 .xmit = lan937x_xmit,
422 .connect = ksz_connect,
423 .disconnect = ksz_disconnect,
424 .needed_tailroom = LAN937X_EGRESS_TAG_LEN + KSZ_PTP_TAG_LEN,
427 DSA_TAG_DRIVER(lan937x_netdev_ops);
428 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_LAN937X, LAN937X_NAME);
430 static struct dsa_tag_driver *dsa_tag_driver_array[] = {
431 &DSA_TAG_DRIVER_NAME(ksz8795_netdev_ops),
432 &DSA_TAG_DRIVER_NAME(ksz9477_netdev_ops),
433 &DSA_TAG_DRIVER_NAME(ksz9893_netdev_ops),
434 &DSA_TAG_DRIVER_NAME(lan937x_netdev_ops),
437 module_dsa_tag_drivers(dsa_tag_driver_array);
439 MODULE_LICENSE("GPL");