net: dsa: don't call switchdev_bridge_port_unoffload for unoffloaded bridge ports
[linux-2.6-microblaze.git] / net / dsa / tag_sja1105.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
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 /* Is this a TX or an RX header? */
11 #define SJA1110_HEADER_HOST_TO_SWITCH           BIT(15)
12
13 /* RX header */
14 #define SJA1110_RX_HEADER_IS_METADATA           BIT(14)
15 #define SJA1110_RX_HEADER_HOST_ONLY             BIT(13)
16 #define SJA1110_RX_HEADER_HAS_TRAILER           BIT(12)
17
18 /* Trap-to-host format (no trailer present) */
19 #define SJA1110_RX_HEADER_SRC_PORT(x)           (((x) & GENMASK(7, 4)) >> 4)
20 #define SJA1110_RX_HEADER_SWITCH_ID(x)          ((x) & GENMASK(3, 0))
21
22 /* Timestamp format (trailer present) */
23 #define SJA1110_RX_HEADER_TRAILER_POS(x)        ((x) & GENMASK(11, 0))
24
25 #define SJA1110_RX_TRAILER_SWITCH_ID(x)         (((x) & GENMASK(7, 4)) >> 4)
26 #define SJA1110_RX_TRAILER_SRC_PORT(x)          ((x) & GENMASK(3, 0))
27
28 /* Meta frame format (for 2-step TX timestamps) */
29 #define SJA1110_RX_HEADER_N_TS(x)               (((x) & GENMASK(8, 4)) >> 4)
30
31 /* TX header */
32 #define SJA1110_TX_HEADER_UPDATE_TC             BIT(14)
33 #define SJA1110_TX_HEADER_TAKE_TS               BIT(13)
34 #define SJA1110_TX_HEADER_TAKE_TS_CASC          BIT(12)
35 #define SJA1110_TX_HEADER_HAS_TRAILER           BIT(11)
36
37 /* Only valid if SJA1110_TX_HEADER_HAS_TRAILER is false */
38 #define SJA1110_TX_HEADER_PRIO(x)               (((x) << 7) & GENMASK(10, 7))
39 #define SJA1110_TX_HEADER_TSTAMP_ID(x)          ((x) & GENMASK(7, 0))
40
41 /* Only valid if SJA1110_TX_HEADER_HAS_TRAILER is true */
42 #define SJA1110_TX_HEADER_TRAILER_POS(x)        ((x) & GENMASK(10, 0))
43
44 #define SJA1110_TX_TRAILER_TSTAMP_ID(x)         (((x) << 24) & GENMASK(31, 24))
45 #define SJA1110_TX_TRAILER_PRIO(x)              (((x) << 21) & GENMASK(23, 21))
46 #define SJA1110_TX_TRAILER_SWITCHID(x)          (((x) << 12) & GENMASK(15, 12))
47 #define SJA1110_TX_TRAILER_DESTPORTS(x)         (((x) << 1) & GENMASK(11, 1))
48
49 #define SJA1110_META_TSTAMP_SIZE                10
50
51 #define SJA1110_HEADER_LEN                      4
52 #define SJA1110_RX_TRAILER_LEN                  13
53 #define SJA1110_TX_TRAILER_LEN                  4
54 #define SJA1110_MAX_PADDING_LEN                 15
55
56 /* Similar to is_link_local_ether_addr(hdr->h_dest) but also covers PTP */
57 static inline bool sja1105_is_link_local(const struct sk_buff *skb)
58 {
59         const struct ethhdr *hdr = eth_hdr(skb);
60         u64 dmac = ether_addr_to_u64(hdr->h_dest);
61
62         if (ntohs(hdr->h_proto) == ETH_P_SJA1105_META)
63                 return false;
64         if ((dmac & SJA1105_LINKLOCAL_FILTER_A_MASK) ==
65                     SJA1105_LINKLOCAL_FILTER_A)
66                 return true;
67         if ((dmac & SJA1105_LINKLOCAL_FILTER_B_MASK) ==
68                     SJA1105_LINKLOCAL_FILTER_B)
69                 return true;
70         return false;
71 }
72
73 struct sja1105_meta {
74         u64 tstamp;
75         u64 dmac_byte_4;
76         u64 dmac_byte_3;
77         u64 source_port;
78         u64 switch_id;
79 };
80
81 static void sja1105_meta_unpack(const struct sk_buff *skb,
82                                 struct sja1105_meta *meta)
83 {
84         u8 *buf = skb_mac_header(skb) + ETH_HLEN;
85
86         /* UM10944.pdf section 4.2.17 AVB Parameters:
87          * Structure of the meta-data follow-up frame.
88          * It is in network byte order, so there are no quirks
89          * while unpacking the meta frame.
90          *
91          * Also SJA1105 E/T only populates bits 23:0 of the timestamp
92          * whereas P/Q/R/S does 32 bits. Since the structure is the
93          * same and the E/T puts zeroes in the high-order byte, use
94          * a unified unpacking command for both device series.
95          */
96         packing(buf,     &meta->tstamp,     31, 0, 4, UNPACK, 0);
97         packing(buf + 4, &meta->dmac_byte_4, 7, 0, 1, UNPACK, 0);
98         packing(buf + 5, &meta->dmac_byte_3, 7, 0, 1, UNPACK, 0);
99         packing(buf + 6, &meta->source_port, 7, 0, 1, UNPACK, 0);
100         packing(buf + 7, &meta->switch_id,   7, 0, 1, UNPACK, 0);
101 }
102
103 static inline bool sja1105_is_meta_frame(const struct sk_buff *skb)
104 {
105         const struct ethhdr *hdr = eth_hdr(skb);
106         u64 smac = ether_addr_to_u64(hdr->h_source);
107         u64 dmac = ether_addr_to_u64(hdr->h_dest);
108
109         if (smac != SJA1105_META_SMAC)
110                 return false;
111         if (dmac != SJA1105_META_DMAC)
112                 return false;
113         if (ntohs(hdr->h_proto) != ETH_P_SJA1105_META)
114                 return false;
115         return true;
116 }
117
118 /* Calls sja1105_port_deferred_xmit in sja1105_main.c */
119 static struct sk_buff *sja1105_defer_xmit(struct dsa_port *dp,
120                                           struct sk_buff *skb)
121 {
122         struct sja1105_port *sp = dp->priv;
123
124         if (!dsa_port_is_sja1105(dp))
125                 return skb;
126
127         /* Increase refcount so the kfree_skb in dsa_slave_xmit
128          * won't really free the packet.
129          */
130         skb_queue_tail(&sp->xmit_queue, skb_get(skb));
131         kthread_queue_work(sp->xmit_worker, &sp->xmit_work);
132
133         return NULL;
134 }
135
136 static u16 sja1105_xmit_tpid(struct dsa_port *dp)
137 {
138         struct sja1105_port *sp = dp->priv;
139
140         if (unlikely(!dsa_port_is_sja1105(dp)))
141                 return ETH_P_8021Q;
142
143         return sp->xmit_tpid;
144 }
145
146 static struct sk_buff *sja1105_imprecise_xmit(struct sk_buff *skb,
147                                               struct net_device *netdev)
148 {
149         struct dsa_port *dp = dsa_slave_to_port(netdev);
150         struct net_device *br = dp->bridge_dev;
151         u16 tx_vid;
152
153         /* If the port is under a VLAN-aware bridge, just slide the
154          * VLAN-tagged packet into the FDB and hope for the best.
155          * This works because we support a single VLAN-aware bridge
156          * across the entire dst, and its VLANs cannot be shared with
157          * any standalone port.
158          */
159         if (br_vlan_enabled(br))
160                 return skb;
161
162         /* If the port is under a VLAN-unaware bridge, use an imprecise
163          * TX VLAN that targets the bridge's entire broadcast domain,
164          * instead of just the specific port.
165          */
166         tx_vid = dsa_8021q_bridge_tx_fwd_offload_vid(dp->bridge_num);
167
168         return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp), tx_vid);
169 }
170
171 static struct sk_buff *sja1105_xmit(struct sk_buff *skb,
172                                     struct net_device *netdev)
173 {
174         struct dsa_port *dp = dsa_slave_to_port(netdev);
175         u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
176         u16 queue_mapping = skb_get_queue_mapping(skb);
177         u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
178
179         if (skb->offload_fwd_mark)
180                 return sja1105_imprecise_xmit(skb, netdev);
181
182         /* Transmitting management traffic does not rely upon switch tagging,
183          * but instead SPI-installed management routes. Part 2 of this
184          * is the .port_deferred_xmit driver callback.
185          */
186         if (unlikely(sja1105_is_link_local(skb)))
187                 return sja1105_defer_xmit(dp, skb);
188
189         return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp),
190                              ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
191 }
192
193 static struct sk_buff *sja1110_xmit(struct sk_buff *skb,
194                                     struct net_device *netdev)
195 {
196         struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
197         struct dsa_port *dp = dsa_slave_to_port(netdev);
198         u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
199         u16 queue_mapping = skb_get_queue_mapping(skb);
200         u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
201         __be32 *tx_trailer;
202         __be16 *tx_header;
203         int trailer_pos;
204
205         if (skb->offload_fwd_mark)
206                 return sja1105_imprecise_xmit(skb, netdev);
207
208         /* Transmitting control packets is done using in-band control
209          * extensions, while data packets are transmitted using
210          * tag_8021q TX VLANs.
211          */
212         if (likely(!sja1105_is_link_local(skb)))
213                 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp),
214                                      ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
215
216         skb_push(skb, SJA1110_HEADER_LEN);
217
218         dsa_alloc_etype_header(skb, SJA1110_HEADER_LEN);
219
220         trailer_pos = skb->len;
221
222         tx_header = dsa_etype_header_pos_tx(skb);
223         tx_trailer = skb_put(skb, SJA1110_TX_TRAILER_LEN);
224
225         tx_header[0] = htons(ETH_P_SJA1110);
226         tx_header[1] = htons(SJA1110_HEADER_HOST_TO_SWITCH |
227                              SJA1110_TX_HEADER_HAS_TRAILER |
228                              SJA1110_TX_HEADER_TRAILER_POS(trailer_pos));
229         *tx_trailer = cpu_to_be32(SJA1110_TX_TRAILER_PRIO(pcp) |
230                                   SJA1110_TX_TRAILER_SWITCHID(dp->ds->index) |
231                                   SJA1110_TX_TRAILER_DESTPORTS(BIT(dp->index)));
232         if (clone) {
233                 u8 ts_id = SJA1105_SKB_CB(clone)->ts_id;
234
235                 tx_header[1] |= htons(SJA1110_TX_HEADER_TAKE_TS);
236                 *tx_trailer |= cpu_to_be32(SJA1110_TX_TRAILER_TSTAMP_ID(ts_id));
237         }
238
239         return skb;
240 }
241
242 static void sja1105_transfer_meta(struct sk_buff *skb,
243                                   const struct sja1105_meta *meta)
244 {
245         struct ethhdr *hdr = eth_hdr(skb);
246
247         hdr->h_dest[3] = meta->dmac_byte_3;
248         hdr->h_dest[4] = meta->dmac_byte_4;
249         SJA1105_SKB_CB(skb)->tstamp = meta->tstamp;
250 }
251
252 /* This is a simple state machine which follows the hardware mechanism of
253  * generating RX timestamps:
254  *
255  * After each timestampable skb (all traffic for which send_meta1 and
256  * send_meta0 is true, aka all MAC-filtered link-local traffic) a meta frame
257  * containing a partial timestamp is immediately generated by the switch and
258  * sent as a follow-up to the link-local frame on the CPU port.
259  *
260  * The meta frames have no unique identifier (such as sequence number) by which
261  * one may pair them to the correct timestampable frame.
262  * Instead, the switch has internal logic that ensures no frames are sent on
263  * the CPU port between a link-local timestampable frame and its corresponding
264  * meta follow-up. It also ensures strict ordering between ports (lower ports
265  * have higher priority towards the CPU port). For this reason, a per-port
266  * data structure is not needed/desirable.
267  *
268  * This function pairs the link-local frame with its partial timestamp from the
269  * meta follow-up frame. The full timestamp will be reconstructed later in a
270  * work queue.
271  */
272 static struct sk_buff
273 *sja1105_rcv_meta_state_machine(struct sk_buff *skb,
274                                 struct sja1105_meta *meta,
275                                 bool is_link_local,
276                                 bool is_meta)
277 {
278         /* Step 1: A timestampable frame was received.
279          * Buffer it until we get its meta frame.
280          */
281         if (is_link_local) {
282                 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
283                 struct sja1105_port *sp = dp->priv;
284
285                 if (unlikely(!dsa_port_is_sja1105(dp)))
286                         return skb;
287
288                 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
289                         /* Do normal processing. */
290                         return skb;
291
292                 spin_lock(&sp->data->meta_lock);
293                 /* Was this a link-local frame instead of the meta
294                  * that we were expecting?
295                  */
296                 if (sp->data->stampable_skb) {
297                         dev_err_ratelimited(dp->ds->dev,
298                                             "Expected meta frame, is %12llx "
299                                             "in the DSA master multicast filter?\n",
300                                             SJA1105_META_DMAC);
301                         kfree_skb(sp->data->stampable_skb);
302                 }
303
304                 /* Hold a reference to avoid dsa_switch_rcv
305                  * from freeing the skb.
306                  */
307                 sp->data->stampable_skb = skb_get(skb);
308                 spin_unlock(&sp->data->meta_lock);
309
310                 /* Tell DSA we got nothing */
311                 return NULL;
312
313         /* Step 2: The meta frame arrived.
314          * Time to take the stampable skb out of the closet, annotate it
315          * with the partial timestamp, and pretend that we received it
316          * just now (basically masquerade the buffered frame as the meta
317          * frame, which serves no further purpose).
318          */
319         } else if (is_meta) {
320                 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
321                 struct sja1105_port *sp = dp->priv;
322                 struct sk_buff *stampable_skb;
323
324                 if (unlikely(!dsa_port_is_sja1105(dp)))
325                         return skb;
326
327                 /* Drop the meta frame if we're not in the right state
328                  * to process it.
329                  */
330                 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
331                         return NULL;
332
333                 spin_lock(&sp->data->meta_lock);
334
335                 stampable_skb = sp->data->stampable_skb;
336                 sp->data->stampable_skb = NULL;
337
338                 /* Was this a meta frame instead of the link-local
339                  * that we were expecting?
340                  */
341                 if (!stampable_skb) {
342                         dev_err_ratelimited(dp->ds->dev,
343                                             "Unexpected meta frame\n");
344                         spin_unlock(&sp->data->meta_lock);
345                         return NULL;
346                 }
347
348                 if (stampable_skb->dev != skb->dev) {
349                         dev_err_ratelimited(dp->ds->dev,
350                                             "Meta frame on wrong port\n");
351                         spin_unlock(&sp->data->meta_lock);
352                         return NULL;
353                 }
354
355                 /* Free the meta frame and give DSA the buffered stampable_skb
356                  * for further processing up the network stack.
357                  */
358                 kfree_skb(skb);
359                 skb = stampable_skb;
360                 sja1105_transfer_meta(skb, meta);
361
362                 spin_unlock(&sp->data->meta_lock);
363         }
364
365         return skb;
366 }
367
368 static bool sja1105_skb_has_tag_8021q(const struct sk_buff *skb)
369 {
370         u16 tpid = ntohs(eth_hdr(skb)->h_proto);
371
372         return tpid == ETH_P_SJA1105 || tpid == ETH_P_8021Q ||
373                skb_vlan_tag_present(skb);
374 }
375
376 static bool sja1110_skb_has_inband_control_extension(const struct sk_buff *skb)
377 {
378         return ntohs(eth_hdr(skb)->h_proto) == ETH_P_SJA1110;
379 }
380
381 /* If the VLAN in the packet is a tag_8021q one, set @source_port and
382  * @switch_id and strip the header. Otherwise set @vid and keep it in the
383  * packet.
384  */
385 static void sja1105_vlan_rcv(struct sk_buff *skb, int *source_port,
386                              int *switch_id, u16 *vid)
387 {
388         struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)skb_mac_header(skb);
389         u16 vlan_tci;
390
391         if (skb_vlan_tag_present(skb))
392                 vlan_tci = skb_vlan_tag_get(skb);
393         else
394                 vlan_tci = ntohs(hdr->h_vlan_TCI);
395
396         if (vid_is_dsa_8021q_rxvlan(vlan_tci & VLAN_VID_MASK))
397                 return dsa_8021q_rcv(skb, source_port, switch_id);
398
399         /* Try our best with imprecise RX */
400         *vid = vlan_tci & VLAN_VID_MASK;
401 }
402
403 static struct sk_buff *sja1105_rcv(struct sk_buff *skb,
404                                    struct net_device *netdev)
405 {
406         int source_port = -1, switch_id = -1;
407         struct sja1105_meta meta = {0};
408         struct ethhdr *hdr;
409         bool is_link_local;
410         bool is_meta;
411         u16 vid;
412
413         hdr = eth_hdr(skb);
414         is_link_local = sja1105_is_link_local(skb);
415         is_meta = sja1105_is_meta_frame(skb);
416
417         if (sja1105_skb_has_tag_8021q(skb)) {
418                 /* Normal traffic path. */
419                 sja1105_vlan_rcv(skb, &source_port, &switch_id, &vid);
420         } else if (is_link_local) {
421                 /* Management traffic path. Switch embeds the switch ID and
422                  * port ID into bytes of the destination MAC, courtesy of
423                  * the incl_srcpt options.
424                  */
425                 source_port = hdr->h_dest[3];
426                 switch_id = hdr->h_dest[4];
427                 /* Clear the DMAC bytes that were mangled by the switch */
428                 hdr->h_dest[3] = 0;
429                 hdr->h_dest[4] = 0;
430         } else if (is_meta) {
431                 sja1105_meta_unpack(skb, &meta);
432                 source_port = meta.source_port;
433                 switch_id = meta.switch_id;
434         } else {
435                 return NULL;
436         }
437
438         if (source_port == -1 || switch_id == -1)
439                 skb->dev = dsa_find_designated_bridge_port_by_vid(netdev, vid);
440         else
441                 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
442         if (!skb->dev) {
443                 netdev_warn(netdev, "Couldn't decode source port\n");
444                 return NULL;
445         }
446
447         if (!is_link_local)
448                 dsa_default_offload_fwd_mark(skb);
449
450         return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local,
451                                               is_meta);
452 }
453
454 static struct sk_buff *sja1110_rcv_meta(struct sk_buff *skb, u16 rx_header)
455 {
456         u8 *buf = dsa_etype_header_pos_rx(skb) + SJA1110_HEADER_LEN;
457         int switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
458         int n_ts = SJA1110_RX_HEADER_N_TS(rx_header);
459         struct net_device *master = skb->dev;
460         struct dsa_port *cpu_dp;
461         struct dsa_switch *ds;
462         int i;
463
464         cpu_dp = master->dsa_ptr;
465         ds = dsa_switch_find(cpu_dp->dst->index, switch_id);
466         if (!ds) {
467                 net_err_ratelimited("%s: cannot find switch id %d\n",
468                                     master->name, switch_id);
469                 return NULL;
470         }
471
472         for (i = 0; i <= n_ts; i++) {
473                 u8 ts_id, source_port, dir;
474                 u64 tstamp;
475
476                 ts_id = buf[0];
477                 source_port = (buf[1] & GENMASK(7, 4)) >> 4;
478                 dir = (buf[1] & BIT(3)) >> 3;
479                 tstamp = be64_to_cpu(*(__be64 *)(buf + 2));
480
481                 sja1110_process_meta_tstamp(ds, source_port, ts_id, dir,
482                                             tstamp);
483
484                 buf += SJA1110_META_TSTAMP_SIZE;
485         }
486
487         /* Discard the meta frame, we've consumed the timestamps it contained */
488         return NULL;
489 }
490
491 static struct sk_buff *sja1110_rcv_inband_control_extension(struct sk_buff *skb,
492                                                             int *source_port,
493                                                             int *switch_id,
494                                                             bool *host_only)
495 {
496         u16 rx_header;
497
498         if (unlikely(!pskb_may_pull(skb, SJA1110_HEADER_LEN)))
499                 return NULL;
500
501         /* skb->data points to skb_mac_header(skb) + ETH_HLEN, which is exactly
502          * what we need because the caller has checked the EtherType (which is
503          * located 2 bytes back) and we just need a pointer to the header that
504          * comes afterwards.
505          */
506         rx_header = ntohs(*(__be16 *)skb->data);
507
508         if (rx_header & SJA1110_RX_HEADER_HOST_ONLY)
509                 *host_only = true;
510
511         if (rx_header & SJA1110_RX_HEADER_IS_METADATA)
512                 return sja1110_rcv_meta(skb, rx_header);
513
514         /* Timestamp frame, we have a trailer */
515         if (rx_header & SJA1110_RX_HEADER_HAS_TRAILER) {
516                 int start_of_padding = SJA1110_RX_HEADER_TRAILER_POS(rx_header);
517                 u8 *rx_trailer = skb_tail_pointer(skb) - SJA1110_RX_TRAILER_LEN;
518                 u64 *tstamp = &SJA1105_SKB_CB(skb)->tstamp;
519                 u8 last_byte = rx_trailer[12];
520
521                 /* The timestamp is unaligned, so we need to use packing()
522                  * to get it
523                  */
524                 packing(rx_trailer, tstamp, 63, 0, 8, UNPACK, 0);
525
526                 *source_port = SJA1110_RX_TRAILER_SRC_PORT(last_byte);
527                 *switch_id = SJA1110_RX_TRAILER_SWITCH_ID(last_byte);
528
529                 /* skb->len counts from skb->data, while start_of_padding
530                  * counts from the destination MAC address. Right now skb->data
531                  * is still as set by the DSA master, so to trim away the
532                  * padding and trailer we need to account for the fact that
533                  * skb->data points to skb_mac_header(skb) + ETH_HLEN.
534                  */
535                 pskb_trim_rcsum(skb, start_of_padding - ETH_HLEN);
536         /* Trap-to-host frame, no timestamp trailer */
537         } else {
538                 *source_port = SJA1110_RX_HEADER_SRC_PORT(rx_header);
539                 *switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
540         }
541
542         /* Advance skb->data past the DSA header */
543         skb_pull_rcsum(skb, SJA1110_HEADER_LEN);
544
545         dsa_strip_etype_header(skb, SJA1110_HEADER_LEN);
546
547         /* With skb->data in its final place, update the MAC header
548          * so that eth_hdr() continues to works properly.
549          */
550         skb_set_mac_header(skb, -ETH_HLEN);
551
552         return skb;
553 }
554
555 static struct sk_buff *sja1110_rcv(struct sk_buff *skb,
556                                    struct net_device *netdev)
557 {
558         int source_port = -1, switch_id = -1;
559         bool host_only = false;
560         u16 vid = 0;
561
562         if (sja1110_skb_has_inband_control_extension(skb)) {
563                 skb = sja1110_rcv_inband_control_extension(skb, &source_port,
564                                                            &switch_id,
565                                                            &host_only);
566                 if (!skb)
567                         return NULL;
568         }
569
570         /* Packets with in-band control extensions might still have RX VLANs */
571         if (likely(sja1105_skb_has_tag_8021q(skb)))
572                 sja1105_vlan_rcv(skb, &source_port, &switch_id, &vid);
573
574         if (source_port == -1 || switch_id == -1)
575                 skb->dev = dsa_find_designated_bridge_port_by_vid(netdev, vid);
576         else
577                 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
578         if (!skb->dev) {
579                 netdev_warn(netdev, "Couldn't decode source port\n");
580                 return NULL;
581         }
582
583         if (!host_only)
584                 dsa_default_offload_fwd_mark(skb);
585
586         return skb;
587 }
588
589 static void sja1105_flow_dissect(const struct sk_buff *skb, __be16 *proto,
590                                  int *offset)
591 {
592         /* No tag added for management frames, all ok */
593         if (unlikely(sja1105_is_link_local(skb)))
594                 return;
595
596         dsa_tag_generic_flow_dissect(skb, proto, offset);
597 }
598
599 static void sja1110_flow_dissect(const struct sk_buff *skb, __be16 *proto,
600                                  int *offset)
601 {
602         /* Management frames have 2 DSA tags on RX, so the needed_headroom we
603          * declared is fine for the generic dissector adjustment procedure.
604          */
605         if (unlikely(sja1105_is_link_local(skb)))
606                 return dsa_tag_generic_flow_dissect(skb, proto, offset);
607
608         /* For the rest, there is a single DSA tag, the tag_8021q one */
609         *offset = VLAN_HLEN;
610         *proto = ((__be16 *)skb->data)[(VLAN_HLEN / 2) - 1];
611 }
612
613 static const struct dsa_device_ops sja1105_netdev_ops = {
614         .name = "sja1105",
615         .proto = DSA_TAG_PROTO_SJA1105,
616         .xmit = sja1105_xmit,
617         .rcv = sja1105_rcv,
618         .needed_headroom = VLAN_HLEN,
619         .flow_dissect = sja1105_flow_dissect,
620         .promisc_on_master = true,
621 };
622
623 DSA_TAG_DRIVER(sja1105_netdev_ops);
624 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1105);
625
626 static const struct dsa_device_ops sja1110_netdev_ops = {
627         .name = "sja1110",
628         .proto = DSA_TAG_PROTO_SJA1110,
629         .xmit = sja1110_xmit,
630         .rcv = sja1110_rcv,
631         .flow_dissect = sja1110_flow_dissect,
632         .needed_headroom = SJA1110_HEADER_LEN + VLAN_HLEN,
633         .needed_tailroom = SJA1110_RX_TRAILER_LEN + SJA1110_MAX_PADDING_LEN,
634 };
635
636 DSA_TAG_DRIVER(sja1110_netdev_ops);
637 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1110);
638
639 static struct dsa_tag_driver *sja1105_tag_driver_array[] = {
640         &DSA_TAG_DRIVER_NAME(sja1105_netdev_ops),
641         &DSA_TAG_DRIVER_NAME(sja1110_netdev_ops),
642 };
643
644 module_dsa_tag_drivers(sja1105_tag_driver_array);
645
646 MODULE_LICENSE("GPL v2");