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/etherdevice.h>
8 #include <linux/list.h>
9 #include <linux/slab.h>
13 /* Typically only one byte is used for tail tag. */
14 #define KSZ_EGRESS_TAG_LEN 1
15 #define KSZ_INGRESS_TAG_LEN 1
17 static struct sk_buff *ksz_common_rcv(struct sk_buff *skb,
18 struct net_device *dev,
19 unsigned int port, unsigned int len)
21 skb->dev = dsa_master_find_slave(dev, 0, port);
25 if (pskb_trim_rcsum(skb, skb->len - len))
28 skb->offload_fwd_mark = true;
34 * For Ingress (Host -> KSZ8795), 1 byte is added before FCS.
35 * ---------------------------------------------------------------------------
36 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag(1byte)|FCS(4bytes)
37 * ---------------------------------------------------------------------------
38 * tag : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
40 * For Egress (KSZ8795 -> Host), 1 byte is added before FCS.
41 * ---------------------------------------------------------------------------
42 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
43 * ---------------------------------------------------------------------------
44 * tag0 : zero-based value represents port
45 * (eg, 0x00=port1, 0x02=port3, 0x06=port7)
48 #define KSZ8795_TAIL_TAG_OVERRIDE BIT(6)
49 #define KSZ8795_TAIL_TAG_LOOKUP BIT(7)
51 static struct sk_buff *ksz8795_xmit(struct sk_buff *skb, struct net_device *dev)
53 struct dsa_port *dp = dsa_slave_to_port(dev);
58 tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
59 addr = skb_mac_header(skb);
61 *tag = 1 << dp->index;
62 if (is_link_local_ether_addr(addr))
63 *tag |= KSZ8795_TAIL_TAG_OVERRIDE;
68 static struct sk_buff *ksz8795_rcv(struct sk_buff *skb, struct net_device *dev,
69 struct packet_type *pt)
71 u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
73 return ksz_common_rcv(skb, dev, tag[0] & 7, KSZ_EGRESS_TAG_LEN);
76 static const struct dsa_device_ops ksz8795_netdev_ops = {
78 .proto = DSA_TAG_PROTO_KSZ8795,
81 .overhead = KSZ_INGRESS_TAG_LEN,
85 DSA_TAG_DRIVER(ksz8795_netdev_ops);
86 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795);
89 * For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
90 * ---------------------------------------------------------------------------
91 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
92 * ---------------------------------------------------------------------------
93 * tag0 : Prioritization (not used now)
94 * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
96 * For Egress (KSZ9477 -> Host), 1 byte is added before FCS.
97 * ---------------------------------------------------------------------------
98 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
99 * ---------------------------------------------------------------------------
100 * tag0 : zero-based value represents port
101 * (eg, 0x00=port1, 0x02=port3, 0x06=port7)
104 #define KSZ9477_INGRESS_TAG_LEN 2
105 #define KSZ9477_PTP_TAG_LEN 4
106 #define KSZ9477_PTP_TAG_INDICATION 0x80
108 #define KSZ9477_TAIL_TAG_OVERRIDE BIT(9)
109 #define KSZ9477_TAIL_TAG_LOOKUP BIT(10)
111 static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
112 struct net_device *dev)
114 struct dsa_port *dp = dsa_slave_to_port(dev);
120 tag = skb_put(skb, KSZ9477_INGRESS_TAG_LEN);
121 addr = skb_mac_header(skb);
123 val = BIT(dp->index);
125 if (is_link_local_ether_addr(addr))
126 val |= KSZ9477_TAIL_TAG_OVERRIDE;
128 *tag = cpu_to_be16(val);
133 static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev,
134 struct packet_type *pt)
137 u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
138 unsigned int port = tag[0] & 7;
139 unsigned int len = KSZ_EGRESS_TAG_LEN;
141 /* Extra 4-bytes PTP timestamp */
142 if (tag[0] & KSZ9477_PTP_TAG_INDICATION)
143 len += KSZ9477_PTP_TAG_LEN;
145 return ksz_common_rcv(skb, dev, port, len);
148 static const struct dsa_device_ops ksz9477_netdev_ops = {
150 .proto = DSA_TAG_PROTO_KSZ9477,
151 .xmit = ksz9477_xmit,
153 .overhead = KSZ9477_INGRESS_TAG_LEN,
157 DSA_TAG_DRIVER(ksz9477_netdev_ops);
158 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9477);
160 #define KSZ9893_TAIL_TAG_OVERRIDE BIT(5)
161 #define KSZ9893_TAIL_TAG_LOOKUP BIT(6)
163 static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
164 struct net_device *dev)
166 struct dsa_port *dp = dsa_slave_to_port(dev);
171 tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
172 addr = skb_mac_header(skb);
174 *tag = BIT(dp->index);
176 if (is_link_local_ether_addr(addr))
177 *tag |= KSZ9893_TAIL_TAG_OVERRIDE;
182 static const struct dsa_device_ops ksz9893_netdev_ops = {
184 .proto = DSA_TAG_PROTO_KSZ9893,
185 .xmit = ksz9893_xmit,
187 .overhead = KSZ_INGRESS_TAG_LEN,
191 DSA_TAG_DRIVER(ksz9893_netdev_ops);
192 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893);
194 static struct dsa_tag_driver *dsa_tag_driver_array[] = {
195 &DSA_TAG_DRIVER_NAME(ksz8795_netdev_ops),
196 &DSA_TAG_DRIVER_NAME(ksz9477_netdev_ops),
197 &DSA_TAG_DRIVER_NAME(ksz9893_netdev_ops),
200 module_dsa_tag_drivers(dsa_tag_driver_array);
202 MODULE_LICENSE("GPL");