xref: /openbmc/linux/net/dsa/tag_ksz.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * net/dsa/tag_ksz.c - Microchip KSZ Switch tag format handling
4  * Copyright (c) 2017 Microchip Technology
5  */
6 
7 #include <linux/etherdevice.h>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <net/dsa.h>
11 #include "dsa_priv.h"
12 
13 /* Typically only one byte is used for tail tag. */
14 #define KSZ_EGRESS_TAG_LEN		1
15 #define KSZ_INGRESS_TAG_LEN		1
16 
17 static struct sk_buff *ksz_common_rcv(struct sk_buff *skb,
18 				      struct net_device *dev,
19 				      unsigned int port, unsigned int len)
20 {
21 	skb->dev = dsa_master_find_slave(dev, 0, port);
22 	if (!skb->dev)
23 		return NULL;
24 
25 	pskb_trim_rcsum(skb, skb->len - len);
26 
27 	skb->offload_fwd_mark = true;
28 
29 	return skb;
30 }
31 
32 /*
33  * For Ingress (Host -> KSZ8795), 1 byte is added before FCS.
34  * ---------------------------------------------------------------------------
35  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag(1byte)|FCS(4bytes)
36  * ---------------------------------------------------------------------------
37  * tag : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
38  *
39  * For Egress (KSZ8795 -> Host), 1 byte is added before FCS.
40  * ---------------------------------------------------------------------------
41  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
42  * ---------------------------------------------------------------------------
43  * tag0 : zero-based value represents port
44  *	  (eg, 0x00=port1, 0x02=port3, 0x06=port7)
45  */
46 
47 #define KSZ8795_TAIL_TAG_OVERRIDE	BIT(6)
48 #define KSZ8795_TAIL_TAG_LOOKUP		BIT(7)
49 
50 static struct sk_buff *ksz8795_xmit(struct sk_buff *skb, struct net_device *dev)
51 {
52 	struct dsa_port *dp = dsa_slave_to_port(dev);
53 	u8 *tag;
54 	u8 *addr;
55 
56 	/* Tag encoding */
57 	tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
58 	addr = skb_mac_header(skb);
59 
60 	*tag = 1 << dp->index;
61 	if (is_link_local_ether_addr(addr))
62 		*tag |= KSZ8795_TAIL_TAG_OVERRIDE;
63 
64 	return skb;
65 }
66 
67 static struct sk_buff *ksz8795_rcv(struct sk_buff *skb, struct net_device *dev,
68 				  struct packet_type *pt)
69 {
70 	u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
71 
72 	return ksz_common_rcv(skb, dev, tag[0] & 7, KSZ_EGRESS_TAG_LEN);
73 }
74 
75 static const struct dsa_device_ops ksz8795_netdev_ops = {
76 	.name	= "ksz8795",
77 	.proto	= DSA_TAG_PROTO_KSZ8795,
78 	.xmit	= ksz8795_xmit,
79 	.rcv	= ksz8795_rcv,
80 	.overhead = KSZ_INGRESS_TAG_LEN,
81 	.tail_tag = true,
82 };
83 
84 DSA_TAG_DRIVER(ksz8795_netdev_ops);
85 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795);
86 
87 /*
88  * For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
89  * ---------------------------------------------------------------------------
90  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
91  * ---------------------------------------------------------------------------
92  * tag0 : Prioritization (not used now)
93  * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
94  *
95  * For Egress (KSZ9477 -> Host), 1 byte is added before FCS.
96  * ---------------------------------------------------------------------------
97  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
98  * ---------------------------------------------------------------------------
99  * tag0 : zero-based value represents port
100  *	  (eg, 0x00=port1, 0x02=port3, 0x06=port7)
101  */
102 
103 #define KSZ9477_INGRESS_TAG_LEN		2
104 #define KSZ9477_PTP_TAG_LEN		4
105 #define KSZ9477_PTP_TAG_INDICATION	0x80
106 
107 #define KSZ9477_TAIL_TAG_OVERRIDE	BIT(9)
108 #define KSZ9477_TAIL_TAG_LOOKUP		BIT(10)
109 
110 static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
111 				    struct net_device *dev)
112 {
113 	struct dsa_port *dp = dsa_slave_to_port(dev);
114 	__be16 *tag;
115 	u8 *addr;
116 	u16 val;
117 
118 	/* Tag encoding */
119 	tag = skb_put(skb, KSZ9477_INGRESS_TAG_LEN);
120 	addr = skb_mac_header(skb);
121 
122 	val = BIT(dp->index);
123 
124 	if (is_link_local_ether_addr(addr))
125 		val |= KSZ9477_TAIL_TAG_OVERRIDE;
126 
127 	*tag = cpu_to_be16(val);
128 
129 	return skb;
130 }
131 
132 static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev,
133 				   struct packet_type *pt)
134 {
135 	/* Tag decoding */
136 	u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
137 	unsigned int port = tag[0] & 7;
138 	unsigned int len = KSZ_EGRESS_TAG_LEN;
139 
140 	/* Extra 4-bytes PTP timestamp */
141 	if (tag[0] & KSZ9477_PTP_TAG_INDICATION)
142 		len += KSZ9477_PTP_TAG_LEN;
143 
144 	return ksz_common_rcv(skb, dev, port, len);
145 }
146 
147 static const struct dsa_device_ops ksz9477_netdev_ops = {
148 	.name	= "ksz9477",
149 	.proto	= DSA_TAG_PROTO_KSZ9477,
150 	.xmit	= ksz9477_xmit,
151 	.rcv	= ksz9477_rcv,
152 	.overhead = KSZ9477_INGRESS_TAG_LEN,
153 	.tail_tag = true,
154 };
155 
156 DSA_TAG_DRIVER(ksz9477_netdev_ops);
157 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9477);
158 
159 #define KSZ9893_TAIL_TAG_OVERRIDE	BIT(5)
160 #define KSZ9893_TAIL_TAG_LOOKUP		BIT(6)
161 
162 static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
163 				    struct net_device *dev)
164 {
165 	struct dsa_port *dp = dsa_slave_to_port(dev);
166 	u8 *addr;
167 	u8 *tag;
168 
169 	/* Tag encoding */
170 	tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
171 	addr = skb_mac_header(skb);
172 
173 	*tag = BIT(dp->index);
174 
175 	if (is_link_local_ether_addr(addr))
176 		*tag |= KSZ9893_TAIL_TAG_OVERRIDE;
177 
178 	return skb;
179 }
180 
181 static const struct dsa_device_ops ksz9893_netdev_ops = {
182 	.name	= "ksz9893",
183 	.proto	= DSA_TAG_PROTO_KSZ9893,
184 	.xmit	= ksz9893_xmit,
185 	.rcv	= ksz9477_rcv,
186 	.overhead = KSZ_INGRESS_TAG_LEN,
187 	.tail_tag = true,
188 };
189 
190 DSA_TAG_DRIVER(ksz9893_netdev_ops);
191 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893);
192 
193 static struct dsa_tag_driver *dsa_tag_driver_array[] = {
194 	&DSA_TAG_DRIVER_NAME(ksz8795_netdev_ops),
195 	&DSA_TAG_DRIVER_NAME(ksz9477_netdev_ops),
196 	&DSA_TAG_DRIVER_NAME(ksz9893_netdev_ops),
197 };
198 
199 module_dsa_tag_drivers(dsa_tag_driver_array);
200 
201 MODULE_LICENSE("GPL");
202