Commit | Line | Data |
---|---|---|
dfedd3b6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
e8fe177a JB |
2 | /* |
3 | * Copyright (C) 2017 Pengutronix, Juergen Borleis <jbe@pengutronix.de> | |
e8fe177a | 4 | */ |
535f010d | 5 | #include <linux/dsa/lan9303.h> |
e8fe177a JB |
6 | #include <linux/etherdevice.h> |
7 | #include <linux/list.h> | |
8 | #include <linux/slab.h> | |
ea5dd34b | 9 | |
bd954b82 | 10 | #include "tag.h" |
e8fe177a JB |
11 | |
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': | |
14 | * | |
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 |... | |
17 | * |<------->| | |
18 | * TAG: | |
19 | * |<------------->| | |
20 | * | 1 2 | 3 4 | | |
21 | * TPID VID | |
22 | * 0x8100 | |
23 | * | |
24 | * VID bit 3 indicates a request for an ALR lookup. | |
25 | * | |
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). | |
28 | * | |
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. | |
31 | */ | |
32 | ||
94793a56 VO |
33 | #define LAN9303_NAME "lan9303" |
34 | ||
e8fe177a | 35 | #define LAN9303_TAG_LEN 4 |
535f010d EH |
36 | # define LAN9303_TAG_TX_USE_ALR BIT(3) |
37 | # define LAN9303_TAG_TX_STP_OVERRIDE BIT(4) | |
1a48fbd9 EH |
38 | # define LAN9303_TAG_RX_IGMP BIT(3) |
39 | # define LAN9303_TAG_RX_STP BIT(4) | |
40 | # define LAN9303_TAG_RX_TRAPPED_TO_CPU (LAN9303_TAG_RX_IGMP | \ | |
41 | LAN9303_TAG_RX_STP) | |
535f010d EH |
42 | |
43 | /* Decide whether to transmit using ALR lookup, or transmit directly to | |
44 | * port using tag. ALR learning is performed only when using ALR lookup. | |
909fb9ae | 45 | * If the two external ports are bridged and the frame is unicast, |
535f010d EH |
46 | * then use ALR lookup to allow ALR learning on CPU port. |
47 | * Otherwise transmit directly to port with STP state override. | |
48 | * See also: lan9303_separate_ports() and lan9303.pdf 6.4.10.1 | |
49 | */ | |
50 | static int lan9303_xmit_use_arl(struct dsa_port *dp, u8 *dest_addr) | |
51 | { | |
52 | struct lan9303 *chip = dp->ds->priv; | |
53 | ||
909fb9ae | 54 | return chip->is_bridged && !is_multicast_ether_addr(dest_addr); |
535f010d | 55 | } |
e8fe177a JB |
56 | |
57 | static struct sk_buff *lan9303_xmit(struct sk_buff *skb, struct net_device *dev) | |
58 | { | |
d945097b | 59 | struct dsa_port *dp = dsa_slave_to_port(dev); |
802734ad AL |
60 | __be16 *lan9303_tag; |
61 | u16 tag; | |
e8fe177a | 62 | |
e8fe177a JB |
63 | /* provide 'LAN9303_TAG_LEN' bytes additional space */ |
64 | skb_push(skb, LAN9303_TAG_LEN); | |
65 | ||
66 | /* make room between MACs and Ether-Type */ | |
6bef794d | 67 | dsa_alloc_etype_header(skb, LAN9303_TAG_LEN); |
e8fe177a | 68 | |
a72808b6 VO |
69 | lan9303_tag = dsa_etype_header_pos_tx(skb); |
70 | ||
802734ad AL |
71 | tag = lan9303_xmit_use_arl(dp, skb->data) ? |
72 | LAN9303_TAG_TX_USE_ALR : | |
73 | dp->index | LAN9303_TAG_TX_STP_OVERRIDE; | |
e8fe177a | 74 | lan9303_tag[0] = htons(ETH_P_8021Q); |
802734ad | 75 | lan9303_tag[1] = htons(tag); |
e8fe177a JB |
76 | |
77 | return skb; | |
e8fe177a JB |
78 | } |
79 | ||
29a097b7 | 80 | static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev) |
e8fe177a | 81 | { |
1a48fbd9 | 82 | u16 lan9303_tag1; |
e8fe177a JB |
83 | unsigned int source_port; |
84 | ||
e8fe177a JB |
85 | if (unlikely(!pskb_may_pull(skb, LAN9303_TAG_LEN))) { |
86 | dev_warn_ratelimited(&dev->dev, | |
87 | "Dropping packet, cannot pull\n"); | |
88 | return NULL; | |
89 | } | |
90 | ||
017b355b MR |
91 | if (skb_vlan_tag_present(skb)) { |
92 | lan9303_tag1 = skb_vlan_tag_get(skb); | |
93 | __vlan_hwaccel_clear_tag(skb); | |
94 | } else { | |
95 | skb_push_rcsum(skb, ETH_HLEN); | |
96 | __skb_vlan_pop(skb, &lan9303_tag1); | |
97 | skb_pull_rcsum(skb, ETH_HLEN); | |
e8fe177a JB |
98 | } |
99 | ||
1a48fbd9 | 100 | source_port = lan9303_tag1 & 0x3; |
e8fe177a | 101 | |
2231c43b | 102 | skb->dev = dsa_master_find_slave(dev, 0, source_port); |
3775b1b7 | 103 | if (!skb->dev) { |
e8fe177a JB |
104 | dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid source port\n"); |
105 | return NULL; | |
106 | } | |
107 | ||
bea79078 VO |
108 | if (!(lan9303_tag1 & LAN9303_TAG_RX_TRAPPED_TO_CPU)) |
109 | dsa_default_offload_fwd_mark(skb); | |
4672cd36 | 110 | |
e8fe177a JB |
111 | return skb; |
112 | } | |
113 | ||
f81a43e8 | 114 | static const struct dsa_device_ops lan9303_netdev_ops = { |
94793a56 | 115 | .name = LAN9303_NAME, |
056eed2f | 116 | .proto = DSA_TAG_PROTO_LAN9303, |
e8fe177a JB |
117 | .xmit = lan9303_xmit, |
118 | .rcv = lan9303_rcv, | |
4e500251 | 119 | .needed_headroom = LAN9303_TAG_LEN, |
e8fe177a | 120 | }; |
0b42f033 | 121 | |
f18bba50 | 122 | MODULE_LICENSE("GPL"); |
94793a56 | 123 | MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_LAN9303, LAN9303_NAME); |
d3b8c049 AL |
124 | |
125 | module_dsa_tag_driver(lan9303_netdev_ops); |