GNU Linux-libre 5.4.257-gnu1
[releases.git] / net / dsa / tag_ksz.c
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_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
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         if (pskb_trim_rcsum(skb, skb->len - len))
66                 return NULL;
67
68         skb->offload_fwd_mark = true;
69
70         return skb;
71 }
72
73 /*
74  * For Ingress (Host -> KSZ8795), 1 byte is added before FCS.
75  * ---------------------------------------------------------------------------
76  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag(1byte)|FCS(4bytes)
77  * ---------------------------------------------------------------------------
78  * tag : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
79  *
80  * For Egress (KSZ8795 -> Host), 1 byte is added before FCS.
81  * ---------------------------------------------------------------------------
82  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
83  * ---------------------------------------------------------------------------
84  * tag0 : zero-based value represents port
85  *        (eg, 0x00=port1, 0x02=port3, 0x06=port7)
86  */
87
88 #define KSZ8795_INGRESS_TAG_LEN         1
89
90 #define KSZ8795_TAIL_TAG_OVERRIDE       BIT(6)
91 #define KSZ8795_TAIL_TAG_LOOKUP         BIT(7)
92
93 static struct sk_buff *ksz8795_xmit(struct sk_buff *skb, struct net_device *dev)
94 {
95         struct dsa_port *dp = dsa_slave_to_port(dev);
96         struct sk_buff *nskb;
97         u8 *tag;
98         u8 *addr;
99
100         nskb = ksz_common_xmit(skb, dev, KSZ8795_INGRESS_TAG_LEN);
101         if (!nskb)
102                 return NULL;
103
104         /* Tag encoding */
105         tag = skb_put(nskb, KSZ8795_INGRESS_TAG_LEN);
106         addr = skb_mac_header(nskb);
107
108         *tag = 1 << dp->index;
109         if (is_link_local_ether_addr(addr))
110                 *tag |= KSZ8795_TAIL_TAG_OVERRIDE;
111
112         return nskb;
113 }
114
115 static struct sk_buff *ksz8795_rcv(struct sk_buff *skb, struct net_device *dev,
116                                   struct packet_type *pt)
117 {
118         u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
119
120         return ksz_common_rcv(skb, dev, tag[0] & 7, KSZ_EGRESS_TAG_LEN);
121 }
122
123 static const struct dsa_device_ops ksz8795_netdev_ops = {
124         .name   = "ksz8795",
125         .proto  = DSA_TAG_PROTO_KSZ8795,
126         .xmit   = ksz8795_xmit,
127         .rcv    = ksz8795_rcv,
128         .overhead = KSZ8795_INGRESS_TAG_LEN,
129 };
130
131 DSA_TAG_DRIVER(ksz8795_netdev_ops);
132 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795);
133
134 /*
135  * For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
136  * ---------------------------------------------------------------------------
137  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
138  * ---------------------------------------------------------------------------
139  * tag0 : Prioritization (not used now)
140  * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
141  *
142  * For Egress (KSZ9477 -> Host), 1 byte is added before FCS.
143  * ---------------------------------------------------------------------------
144  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
145  * ---------------------------------------------------------------------------
146  * tag0 : zero-based value represents port
147  *        (eg, 0x00=port1, 0x02=port3, 0x06=port7)
148  */
149
150 #define KSZ9477_INGRESS_TAG_LEN         2
151 #define KSZ9477_PTP_TAG_LEN             4
152 #define KSZ9477_PTP_TAG_INDICATION      0x80
153
154 #define KSZ9477_TAIL_TAG_OVERRIDE       BIT(9)
155 #define KSZ9477_TAIL_TAG_LOOKUP         BIT(10)
156
157 static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
158                                     struct net_device *dev)
159 {
160         struct dsa_port *dp = dsa_slave_to_port(dev);
161         struct sk_buff *nskb;
162         u16 *tag;
163         u8 *addr;
164
165         nskb = ksz_common_xmit(skb, dev, KSZ9477_INGRESS_TAG_LEN);
166         if (!nskb)
167                 return NULL;
168
169         /* Tag encoding */
170         tag = skb_put(nskb, KSZ9477_INGRESS_TAG_LEN);
171         addr = skb_mac_header(nskb);
172
173         *tag = BIT(dp->index);
174
175         if (is_link_local_ether_addr(addr))
176                 *tag |= KSZ9477_TAIL_TAG_OVERRIDE;
177
178         *tag = cpu_to_be16(*tag);
179
180         return nskb;
181 }
182
183 static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev,
184                                    struct packet_type *pt)
185 {
186         /* Tag decoding */
187         u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
188         unsigned int port = tag[0] & 7;
189         unsigned int len = KSZ_EGRESS_TAG_LEN;
190
191         /* Extra 4-bytes PTP timestamp */
192         if (tag[0] & KSZ9477_PTP_TAG_INDICATION)
193                 len += KSZ9477_PTP_TAG_LEN;
194
195         return ksz_common_rcv(skb, dev, port, len);
196 }
197
198 static const struct dsa_device_ops ksz9477_netdev_ops = {
199         .name   = "ksz9477",
200         .proto  = DSA_TAG_PROTO_KSZ9477,
201         .xmit   = ksz9477_xmit,
202         .rcv    = ksz9477_rcv,
203         .overhead = KSZ9477_INGRESS_TAG_LEN,
204 };
205
206 DSA_TAG_DRIVER(ksz9477_netdev_ops);
207 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9477);
208
209 #define KSZ9893_TAIL_TAG_OVERRIDE       BIT(5)
210 #define KSZ9893_TAIL_TAG_LOOKUP         BIT(6)
211
212 static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
213                                     struct net_device *dev)
214 {
215         struct dsa_port *dp = dsa_slave_to_port(dev);
216         struct sk_buff *nskb;
217         u8 *addr;
218         u8 *tag;
219
220         nskb = ksz_common_xmit(skb, dev, KSZ_INGRESS_TAG_LEN);
221         if (!nskb)
222                 return NULL;
223
224         /* Tag encoding */
225         tag = skb_put(nskb, KSZ_INGRESS_TAG_LEN);
226         addr = skb_mac_header(nskb);
227
228         *tag = BIT(dp->index);
229
230         if (is_link_local_ether_addr(addr))
231                 *tag |= KSZ9893_TAIL_TAG_OVERRIDE;
232
233         return nskb;
234 }
235
236 static const struct dsa_device_ops ksz9893_netdev_ops = {
237         .name   = "ksz9893",
238         .proto  = DSA_TAG_PROTO_KSZ9893,
239         .xmit   = ksz9893_xmit,
240         .rcv    = ksz9477_rcv,
241         .overhead = KSZ_INGRESS_TAG_LEN,
242 };
243
244 DSA_TAG_DRIVER(ksz9893_netdev_ops);
245 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893);
246
247 static struct dsa_tag_driver *dsa_tag_driver_array[] = {
248         &DSA_TAG_DRIVER_NAME(ksz8795_netdev_ops),
249         &DSA_TAG_DRIVER_NAME(ksz9477_netdev_ops),
250         &DSA_TAG_DRIVER_NAME(ksz9893_netdev_ops),
251 };
252
253 module_dsa_tag_drivers(dsa_tag_driver_array);
254
255 MODULE_LICENSE("GPL");