This source file includes following definitions.
- sja1105_is_link_local
- sja1105_meta_unpack
- sja1105_is_meta_frame
- sja1105_filter
- sja1105_xmit
- sja1105_transfer_meta
- sja1105_rcv_meta_state_machine
- sja1105_rcv
1
2
3
4 #include <linux/if_vlan.h>
5 #include <linux/dsa/sja1105.h>
6 #include <linux/dsa/8021q.h>
7 #include <linux/packing.h>
8 #include "dsa_priv.h"
9
10
11 static inline bool sja1105_is_link_local(const struct sk_buff *skb)
12 {
13 const struct ethhdr *hdr = eth_hdr(skb);
14 u64 dmac = ether_addr_to_u64(hdr->h_dest);
15
16 if (ntohs(hdr->h_proto) == ETH_P_SJA1105_META)
17 return false;
18 if ((dmac & SJA1105_LINKLOCAL_FILTER_A_MASK) ==
19 SJA1105_LINKLOCAL_FILTER_A)
20 return true;
21 if ((dmac & SJA1105_LINKLOCAL_FILTER_B_MASK) ==
22 SJA1105_LINKLOCAL_FILTER_B)
23 return true;
24 return false;
25 }
26
27 struct sja1105_meta {
28 u64 tstamp;
29 u64 dmac_byte_4;
30 u64 dmac_byte_3;
31 u64 source_port;
32 u64 switch_id;
33 };
34
35 static void sja1105_meta_unpack(const struct sk_buff *skb,
36 struct sja1105_meta *meta)
37 {
38 u8 *buf = skb_mac_header(skb) + ETH_HLEN;
39
40
41
42
43
44
45
46
47
48
49
50 packing(buf, &meta->tstamp, 31, 0, 4, UNPACK, 0);
51 packing(buf + 4, &meta->dmac_byte_4, 7, 0, 1, UNPACK, 0);
52 packing(buf + 5, &meta->dmac_byte_3, 7, 0, 1, UNPACK, 0);
53 packing(buf + 6, &meta->source_port, 7, 0, 1, UNPACK, 0);
54 packing(buf + 7, &meta->switch_id, 7, 0, 1, UNPACK, 0);
55 }
56
57 static inline bool sja1105_is_meta_frame(const struct sk_buff *skb)
58 {
59 const struct ethhdr *hdr = eth_hdr(skb);
60 u64 smac = ether_addr_to_u64(hdr->h_source);
61 u64 dmac = ether_addr_to_u64(hdr->h_dest);
62
63 if (smac != SJA1105_META_SMAC)
64 return false;
65 if (dmac != SJA1105_META_DMAC)
66 return false;
67 if (ntohs(hdr->h_proto) != ETH_P_SJA1105_META)
68 return false;
69 return true;
70 }
71
72
73
74
75 static bool sja1105_filter(const struct sk_buff *skb, struct net_device *dev)
76 {
77 if (!dsa_port_is_vlan_filtering(dev->dsa_ptr))
78 return true;
79 if (sja1105_is_link_local(skb))
80 return true;
81 if (sja1105_is_meta_frame(skb))
82 return true;
83 return false;
84 }
85
86 static struct sk_buff *sja1105_xmit(struct sk_buff *skb,
87 struct net_device *netdev)
88 {
89 struct dsa_port *dp = dsa_slave_to_port(netdev);
90 struct dsa_switch *ds = dp->ds;
91 u16 tx_vid = dsa_8021q_tx_vid(ds, dp->index);
92 u16 queue_mapping = skb_get_queue_mapping(skb);
93 u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
94
95
96
97
98
99 if (unlikely(sja1105_is_link_local(skb)))
100 return dsa_defer_xmit(skb, netdev);
101
102
103
104
105
106
107 if (dsa_port_is_vlan_filtering(dp))
108 return skb;
109
110 return dsa_8021q_xmit(skb, netdev, ETH_P_SJA1105,
111 ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
112 }
113
114 static void sja1105_transfer_meta(struct sk_buff *skb,
115 const struct sja1105_meta *meta)
116 {
117 struct ethhdr *hdr = eth_hdr(skb);
118
119 hdr->h_dest[3] = meta->dmac_byte_3;
120 hdr->h_dest[4] = meta->dmac_byte_4;
121 SJA1105_SKB_CB(skb)->meta_tstamp = meta->tstamp;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 static struct sk_buff
145 *sja1105_rcv_meta_state_machine(struct sk_buff *skb,
146 struct sja1105_meta *meta,
147 bool is_link_local,
148 bool is_meta)
149 {
150 struct sja1105_port *sp;
151 struct dsa_port *dp;
152
153 dp = dsa_slave_to_port(skb->dev);
154 sp = dp->priv;
155
156
157
158
159 if (is_link_local) {
160 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
161
162 return skb;
163
164 spin_lock(&sp->data->meta_lock);
165
166
167
168 if (sp->data->stampable_skb) {
169 dev_err_ratelimited(dp->ds->dev,
170 "Expected meta frame, is %12llx "
171 "in the DSA master multicast filter?\n",
172 SJA1105_META_DMAC);
173 kfree_skb(sp->data->stampable_skb);
174 }
175
176
177
178
179 sp->data->stampable_skb = skb_get(skb);
180 spin_unlock(&sp->data->meta_lock);
181
182
183 return NULL;
184
185
186
187
188
189
190
191 } else if (is_meta) {
192 struct sk_buff *stampable_skb;
193
194
195
196
197 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
198 return NULL;
199
200 spin_lock(&sp->data->meta_lock);
201
202 stampable_skb = sp->data->stampable_skb;
203 sp->data->stampable_skb = NULL;
204
205
206
207
208 if (!stampable_skb) {
209 dev_err_ratelimited(dp->ds->dev,
210 "Unexpected meta frame\n");
211 spin_unlock(&sp->data->meta_lock);
212 return NULL;
213 }
214
215 if (stampable_skb->dev != skb->dev) {
216 dev_err_ratelimited(dp->ds->dev,
217 "Meta frame on wrong port\n");
218 spin_unlock(&sp->data->meta_lock);
219 return NULL;
220 }
221
222
223
224
225 kfree_skb(skb);
226 skb = stampable_skb;
227 sja1105_transfer_meta(skb, meta);
228
229 spin_unlock(&sp->data->meta_lock);
230 }
231
232 return skb;
233 }
234
235 static struct sk_buff *sja1105_rcv(struct sk_buff *skb,
236 struct net_device *netdev,
237 struct packet_type *pt)
238 {
239 struct sja1105_meta meta = {0};
240 int source_port, switch_id;
241 struct ethhdr *hdr;
242 u16 tpid, vid, tci;
243 bool is_link_local;
244 bool is_tagged;
245 bool is_meta;
246
247 hdr = eth_hdr(skb);
248 tpid = ntohs(hdr->h_proto);
249 is_tagged = (tpid == ETH_P_SJA1105);
250 is_link_local = sja1105_is_link_local(skb);
251 is_meta = sja1105_is_meta_frame(skb);
252
253 skb->offload_fwd_mark = 1;
254
255 if (is_tagged) {
256
257 skb_push_rcsum(skb, ETH_HLEN);
258 __skb_vlan_pop(skb, &tci);
259 skb_pull_rcsum(skb, ETH_HLEN);
260 skb_reset_network_header(skb);
261 skb_reset_transport_header(skb);
262
263 vid = tci & VLAN_VID_MASK;
264 source_port = dsa_8021q_rx_source_port(vid);
265 switch_id = dsa_8021q_rx_switch_id(vid);
266 skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
267 } else if (is_link_local) {
268
269
270
271
272 source_port = hdr->h_dest[3];
273 switch_id = hdr->h_dest[4];
274
275 hdr->h_dest[3] = 0;
276 hdr->h_dest[4] = 0;
277 } else if (is_meta) {
278 sja1105_meta_unpack(skb, &meta);
279 source_port = meta.source_port;
280 switch_id = meta.switch_id;
281 } else {
282 return NULL;
283 }
284
285 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
286 if (!skb->dev) {
287 netdev_warn(netdev, "Couldn't decode source port\n");
288 return NULL;
289 }
290
291 return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local,
292 is_meta);
293 }
294
295 static struct dsa_device_ops sja1105_netdev_ops = {
296 .name = "sja1105",
297 .proto = DSA_TAG_PROTO_SJA1105,
298 .xmit = sja1105_xmit,
299 .rcv = sja1105_rcv,
300 .filter = sja1105_filter,
301 .overhead = VLAN_HLEN,
302 };
303
304 MODULE_LICENSE("GPL v2");
305 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1105);
306
307 module_dsa_tag_driver(sja1105_netdev_ops);