usb: dwc3: dwc3-qcom: Fix typo in the dwc3 vbus override API
[linux-2.6-microblaze.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_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         .needed_tailroom = KSZ_INGRESS_TAG_LEN,
81 };
82
83 DSA_TAG_DRIVER(ksz8795_netdev_ops);
84 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ8795);
85
86 /*
87  * For Ingress (Host -> KSZ9477), 2 bytes are added before FCS.
88  * ---------------------------------------------------------------------------
89  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
90  * ---------------------------------------------------------------------------
91  * tag0 : Prioritization (not used now)
92  * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
93  *
94  * For Egress (KSZ9477 -> Host), 1 byte is added before FCS.
95  * ---------------------------------------------------------------------------
96  * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
97  * ---------------------------------------------------------------------------
98  * tag0 : zero-based value represents port
99  *        (eg, 0x00=port1, 0x02=port3, 0x06=port7)
100  */
101
102 #define KSZ9477_INGRESS_TAG_LEN         2
103 #define KSZ9477_PTP_TAG_LEN             4
104 #define KSZ9477_PTP_TAG_INDICATION      0x80
105
106 #define KSZ9477_TAIL_TAG_OVERRIDE       BIT(9)
107 #define KSZ9477_TAIL_TAG_LOOKUP         BIT(10)
108
109 static struct sk_buff *ksz9477_xmit(struct sk_buff *skb,
110                                     struct net_device *dev)
111 {
112         struct dsa_port *dp = dsa_slave_to_port(dev);
113         __be16 *tag;
114         u8 *addr;
115         u16 val;
116
117         /* Tag encoding */
118         tag = skb_put(skb, KSZ9477_INGRESS_TAG_LEN);
119         addr = skb_mac_header(skb);
120
121         val = BIT(dp->index);
122
123         if (is_link_local_ether_addr(addr))
124                 val |= KSZ9477_TAIL_TAG_OVERRIDE;
125
126         *tag = cpu_to_be16(val);
127
128         return skb;
129 }
130
131 static struct sk_buff *ksz9477_rcv(struct sk_buff *skb, struct net_device *dev,
132                                    struct packet_type *pt)
133 {
134         /* Tag decoding */
135         u8 *tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
136         unsigned int port = tag[0] & 7;
137         unsigned int len = KSZ_EGRESS_TAG_LEN;
138
139         /* Extra 4-bytes PTP timestamp */
140         if (tag[0] & KSZ9477_PTP_TAG_INDICATION)
141                 len += KSZ9477_PTP_TAG_LEN;
142
143         return ksz_common_rcv(skb, dev, port, len);
144 }
145
146 static const struct dsa_device_ops ksz9477_netdev_ops = {
147         .name   = "ksz9477",
148         .proto  = DSA_TAG_PROTO_KSZ9477,
149         .xmit   = ksz9477_xmit,
150         .rcv    = ksz9477_rcv,
151         .needed_tailroom = KSZ9477_INGRESS_TAG_LEN,
152 };
153
154 DSA_TAG_DRIVER(ksz9477_netdev_ops);
155 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9477);
156
157 #define KSZ9893_TAIL_TAG_OVERRIDE       BIT(5)
158 #define KSZ9893_TAIL_TAG_LOOKUP         BIT(6)
159
160 static struct sk_buff *ksz9893_xmit(struct sk_buff *skb,
161                                     struct net_device *dev)
162 {
163         struct dsa_port *dp = dsa_slave_to_port(dev);
164         u8 *addr;
165         u8 *tag;
166
167         /* Tag encoding */
168         tag = skb_put(skb, KSZ_INGRESS_TAG_LEN);
169         addr = skb_mac_header(skb);
170
171         *tag = BIT(dp->index);
172
173         if (is_link_local_ether_addr(addr))
174                 *tag |= KSZ9893_TAIL_TAG_OVERRIDE;
175
176         return skb;
177 }
178
179 static const struct dsa_device_ops ksz9893_netdev_ops = {
180         .name   = "ksz9893",
181         .proto  = DSA_TAG_PROTO_KSZ9893,
182         .xmit   = ksz9893_xmit,
183         .rcv    = ksz9477_rcv,
184         .needed_tailroom = KSZ_INGRESS_TAG_LEN,
185 };
186
187 DSA_TAG_DRIVER(ksz9893_netdev_ops);
188 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893);
189
190 static struct dsa_tag_driver *dsa_tag_driver_array[] = {
191         &DSA_TAG_DRIVER_NAME(ksz8795_netdev_ops),
192         &DSA_TAG_DRIVER_NAME(ksz9477_netdev_ops),
193         &DSA_TAG_DRIVER_NAME(ksz9893_netdev_ops),
194 };
195
196 module_dsa_tag_drivers(dsa_tag_driver_array);
197
198 MODULE_LICENSE("GPL");