• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ksz_common_xmit(struct sk_buff * skb,struct net_device * dev,int len)17 static struct sk_buff *ksz_common_xmit(struct sk_buff *skb,
18 				       struct net_device *dev, int len)
19 {
20 	struct sk_buff *nskb;
21 	int padlen;
22 
23 	padlen = (skb->len >= ETH_ZLEN) ? 0 : ETH_ZLEN - skb->len;
24 
25 	if (skb_tailroom(skb) >= padlen + len) {
26 		/* Let dsa_slave_xmit() free skb */
27 		if (__skb_put_padto(skb, skb->len + padlen, false))
28 			return NULL;
29 
30 		nskb = skb;
31 	} else {
32 		nskb = alloc_skb(NET_IP_ALIGN + skb->len +
33 				 padlen + len, GFP_ATOMIC);
34 		if (!nskb)
35 			return NULL;
36 		skb_reserve(nskb, NET_IP_ALIGN);
37 
38 		skb_reset_mac_header(nskb);
39 		skb_set_network_header(nskb,
40 				       skb_network_header(skb) - skb->head);
41 		skb_set_transport_header(nskb,
42 					 skb_transport_header(skb) - skb->head);
43 		skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
44 
45 		/* Let skb_put_padto() free nskb, and let dsa_slave_xmit() free
46 		 * skb
47 		 */
48 		if (skb_put_padto(nskb, nskb->len + padlen))
49 			return NULL;
50 
51 		consume_skb(skb);
52 	}
53 
54 	return nskb;
55 }
56 
ksz_common_rcv(struct sk_buff * skb,struct net_device * dev,unsigned int port,unsigned int len)57 static struct sk_buff *ksz_common_rcv(struct sk_buff *skb,
58 				      struct net_device *dev,
59 				      unsigned int port, unsigned int len)
60 {
61 	skb->dev = dsa_master_find_slave(dev, 0, port);
62 	if (!skb->dev)
63 		return NULL;
64 
65 	pskb_trim_rcsum(skb, skb->len - len);
66 
67 	skb->offload_fwd_mark = true;
68 
69 	return skb;
70 }
71 
72 /*
73  * For Ingress (Host -> KSZ8795), 1 byte is added before FCS.
74  * ---------------------------------------------------------------------------
75  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag(1byte)|FCS(4bytes)
76  * ---------------------------------------------------------------------------
77  * tag : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
78  *
79  * For Egress (KSZ8795 -> Host), 1 byte is added before FCS.
80  * ---------------------------------------------------------------------------
81  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
82  * ---------------------------------------------------------------------------
83  * tag0 : zero-based value represents port
84  *	  (eg, 0x00=port1, 0x02=port3, 0x06=port7)
85  */
86 
87 #define KSZ8795_INGRESS_TAG_LEN		1
88 
89 #define KSZ8795_TAIL_TAG_OVERRIDE	BIT(6)
90 #define KSZ8795_TAIL_TAG_LOOKUP		BIT(7)
91 
ksz8795_xmit(struct sk_buff * skb,struct net_device * dev)92 static struct sk_buff *ksz8795_xmit(struct sk_buff *skb, struct net_device *dev)
93 {
94 	struct dsa_port *dp = dsa_slave_to_port(dev);
95 	struct sk_buff *nskb;
96 	u8 *tag;
97 	u8 *addr;
98 
99 	nskb = ksz_common_xmit(skb, dev, KSZ8795_INGRESS_TAG_LEN);
100 	if (!nskb)
101 		return NULL;
102 
103 	/* Tag encoding */
104 	tag = skb_put(nskb, KSZ8795_INGRESS_TAG_LEN);
105 	addr = skb_mac_header(nskb);
106 
107 	*tag = 1 << dp->index;
108 	if (is_link_local_ether_addr(addr))
109 		*tag |= KSZ8795_TAIL_TAG_OVERRIDE;
110 
111 	return nskb;
112 }
113 
ksz8795_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt)114 static struct sk_buff *ksz8795_rcv(struct sk_buff *skb, struct net_device *dev,
115 				  struct packet_type *pt)
116 {
117 	u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
118 
119 	return ksz_common_rcv(skb, dev, tag[0] & 7, KSZ_EGRESS_TAG_LEN);
120 }
121 
122 static const struct dsa_device_ops ksz8795_netdev_ops = {
123 	.name	= "ksz8795",
124 	.proto	= DSA_TAG_PROTO_KSZ8795,
125 	.xmit	= ksz8795_xmit,
126 	.rcv	= ksz8795_rcv,
127 	.overhead = KSZ8795_INGRESS_TAG_LEN,
128 };
129 
130 DSA_TAG_DRIVER(ksz8795_netdev_ops);
131 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795);
132 
133 /*
134  * For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
135  * ---------------------------------------------------------------------------
136  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
137  * ---------------------------------------------------------------------------
138  * tag0 : Prioritization (not used now)
139  * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
140  *
141  * For Egress (KSZ9477 -> Host), 1 byte is added before FCS.
142  * ---------------------------------------------------------------------------
143  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
144  * ---------------------------------------------------------------------------
145  * tag0 : zero-based value represents port
146  *	  (eg, 0x00=port1, 0x02=port3, 0x06=port7)
147  */
148 
149 #define KSZ9477_INGRESS_TAG_LEN		2
150 #define KSZ9477_PTP_TAG_LEN		4
151 #define KSZ9477_PTP_TAG_INDICATION	0x80
152 
153 #define KSZ9477_TAIL_TAG_OVERRIDE	BIT(9)
154 #define KSZ9477_TAIL_TAG_LOOKUP		BIT(10)
155 
ksz9477_xmit(struct sk_buff * skb,struct net_device * dev)156 static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
157 				    struct net_device *dev)
158 {
159 	struct dsa_port *dp = dsa_slave_to_port(dev);
160 	struct sk_buff *nskb;
161 	u16 *tag;
162 	u8 *addr;
163 
164 	nskb = ksz_common_xmit(skb, dev, KSZ9477_INGRESS_TAG_LEN);
165 	if (!nskb)
166 		return NULL;
167 
168 	/* Tag encoding */
169 	tag = skb_put(nskb, KSZ9477_INGRESS_TAG_LEN);
170 	addr = skb_mac_header(nskb);
171 
172 	*tag = BIT(dp->index);
173 
174 	if (is_link_local_ether_addr(addr))
175 		*tag |= KSZ9477_TAIL_TAG_OVERRIDE;
176 
177 	*tag = cpu_to_be16(*tag);
178 
179 	return nskb;
180 }
181 
ksz9477_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt)182 static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev,
183 				   struct packet_type *pt)
184 {
185 	/* Tag decoding */
186 	u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
187 	unsigned int port = tag[0] & 7;
188 	unsigned int len = KSZ_EGRESS_TAG_LEN;
189 
190 	/* Extra 4-bytes PTP timestamp */
191 	if (tag[0] & KSZ9477_PTP_TAG_INDICATION)
192 		len += KSZ9477_PTP_TAG_LEN;
193 
194 	return ksz_common_rcv(skb, dev, port, len);
195 }
196 
197 static const struct dsa_device_ops ksz9477_netdev_ops = {
198 	.name	= "ksz9477",
199 	.proto	= DSA_TAG_PROTO_KSZ9477,
200 	.xmit	= ksz9477_xmit,
201 	.rcv	= ksz9477_rcv,
202 	.overhead = KSZ9477_INGRESS_TAG_LEN,
203 };
204 
205 DSA_TAG_DRIVER(ksz9477_netdev_ops);
206 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9477);
207 
208 #define KSZ9893_TAIL_TAG_OVERRIDE	BIT(5)
209 #define KSZ9893_TAIL_TAG_LOOKUP		BIT(6)
210 
ksz9893_xmit(struct sk_buff * skb,struct net_device * dev)211 static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
212 				    struct net_device *dev)
213 {
214 	struct dsa_port *dp = dsa_slave_to_port(dev);
215 	struct sk_buff *nskb;
216 	u8 *addr;
217 	u8 *tag;
218 
219 	nskb = ksz_common_xmit(skb, dev, KSZ_INGRESS_TAG_LEN);
220 	if (!nskb)
221 		return NULL;
222 
223 	/* Tag encoding */
224 	tag = skb_put(nskb, KSZ_INGRESS_TAG_LEN);
225 	addr = skb_mac_header(nskb);
226 
227 	*tag = BIT(dp->index);
228 
229 	if (is_link_local_ether_addr(addr))
230 		*tag |= KSZ9893_TAIL_TAG_OVERRIDE;
231 
232 	return nskb;
233 }
234 
235 static const struct dsa_device_ops ksz9893_netdev_ops = {
236 	.name	= "ksz9893",
237 	.proto	= DSA_TAG_PROTO_KSZ9893,
238 	.xmit	= ksz9893_xmit,
239 	.rcv	= ksz9477_rcv,
240 	.overhead = KSZ_INGRESS_TAG_LEN,
241 };
242 
243 DSA_TAG_DRIVER(ksz9893_netdev_ops);
244 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893);
245 
246 static struct dsa_tag_driver *dsa_tag_driver_array[] = {
247 	&DSA_TAG_DRIVER_NAME(ksz8795_netdev_ops),
248 	&DSA_TAG_DRIVER_NAME(ksz9477_netdev_ops),
249 	&DSA_TAG_DRIVER_NAME(ksz9893_netdev_ops),
250 };
251 
252 module_dsa_tag_drivers(dsa_tag_driver_array);
253 
254 MODULE_LICENSE("GPL");
255