1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017 Pengutronix, Juergen Borleis <jbe@pengutronix.de>
5 #include <linux/dsa/lan9303.h>
6 #include <linux/etherdevice.h>
7 #include <linux/list.h>
8 #include <linux/slab.h>
12 /* To define the outgoing port and to discover the incoming port a regular
13 * VLAN tag is used by the LAN9303. But its VID meaning is 'special':
15 * Dest MAC Src MAC TAG Type
16 * ...| 1 2 3 4 5 6 | 1 2 3 4 5 6 | 1 2 3 4 | 1 2 |...
24 * VID bit 3 indicates a request for an ALR lookup.
26 * If VID bit 3 is zero, then bits 0 and 1 specify the destination port
27 * (0, 1, 2) or broadcast (3) or the source port (1, 2).
29 * VID bit 4 is used to specify if the STP port state should be overridden.
30 * Required when no forwarding between the external ports should happen.
33 #define LAN9303_TAG_LEN 4
34 # define LAN9303_TAG_TX_USE_ALR BIT(3)
35 # define LAN9303_TAG_TX_STP_OVERRIDE BIT(4)
36 # define LAN9303_TAG_RX_IGMP BIT(3)
37 # define LAN9303_TAG_RX_STP BIT(4)
38 # define LAN9303_TAG_RX_TRAPPED_TO_CPU (LAN9303_TAG_RX_IGMP | \
41 /* Decide whether to transmit using ALR lookup, or transmit directly to
42 * port using tag. ALR learning is performed only when using ALR lookup.
43 * If the two external ports are bridged and the frame is unicast,
44 * then use ALR lookup to allow ALR learning on CPU port.
45 * Otherwise transmit directly to port with STP state override.
46 * See also: lan9303_separate_ports() and lan9303.pdf 6.4.10.1
48 static int lan9303_xmit_use_arl(struct dsa_port *dp, u8 *dest_addr)
50 struct lan9303 *chip = dp->ds->priv;
52 return chip->is_bridged && !is_multicast_ether_addr(dest_addr);
55 static struct sk_buff *lan9303_xmit(struct sk_buff *skb, struct net_device *dev)
57 struct dsa_port *dp = dsa_slave_to_port(dev);
61 /* provide 'LAN9303_TAG_LEN' bytes additional space */
62 skb_push(skb, LAN9303_TAG_LEN);
64 /* make room between MACs and Ether-Type */
65 dsa_alloc_etype_header(skb, LAN9303_TAG_LEN);
67 lan9303_tag = dsa_etype_header_pos_tx(skb);
69 tag = lan9303_xmit_use_arl(dp, skb->data) ?
70 LAN9303_TAG_TX_USE_ALR :
71 dp->index | LAN9303_TAG_TX_STP_OVERRIDE;
72 lan9303_tag[0] = htons(ETH_P_8021Q);
73 lan9303_tag[1] = htons(tag);
78 static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev)
81 unsigned int source_port;
83 if (unlikely(!pskb_may_pull(skb, LAN9303_TAG_LEN))) {
84 dev_warn_ratelimited(&dev->dev,
85 "Dropping packet, cannot pull\n");
89 if (skb_vlan_tag_present(skb)) {
90 lan9303_tag1 = skb_vlan_tag_get(skb);
91 __vlan_hwaccel_clear_tag(skb);
93 skb_push_rcsum(skb, ETH_HLEN);
94 __skb_vlan_pop(skb, &lan9303_tag1);
95 skb_pull_rcsum(skb, ETH_HLEN);
98 source_port = lan9303_tag1 & 0x3;
100 skb->dev = dsa_master_find_slave(dev, 0, source_port);
102 dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid source port\n");
106 if (!(lan9303_tag1 & LAN9303_TAG_RX_TRAPPED_TO_CPU))
107 dsa_default_offload_fwd_mark(skb);
112 static const struct dsa_device_ops lan9303_netdev_ops = {
114 .proto = DSA_TAG_PROTO_LAN9303,
115 .xmit = lan9303_xmit,
117 .needed_headroom = LAN9303_TAG_LEN,
120 MODULE_LICENSE("GPL");
121 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_LAN9303);
123 module_dsa_tag_driver(lan9303_netdev_ops);