2 * Copyright (C) 2017 Pengutronix, Juergen Borleis <jbe@pengutronix.de>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/etherdevice.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
20 /* To define the outgoing port and to discover the incoming port a regular
21 * VLAN tag is used by the LAN9303. But its VID meaning is 'special':
23 * Dest MAC Src MAC TAG Type
24 * ...| 1 2 3 4 5 6 | 1 2 3 4 5 6 | 1 2 3 4 | 1 2 |...
32 * VID bit 3 indicates a request for an ALR lookup.
34 * If VID bit 3 is zero, then bits 0 and 1 specify the destination port
35 * (0, 1, 2) or broadcast (3) or the source port (1, 2).
37 * VID bit 4 is used to specify if the STP port state should be overridden.
38 * Required when no forwarding between the external ports should happen.
41 #define LAN9303_TAG_LEN 4
42 #define LAN9303_MAX_PORTS 3
44 static struct sk_buff *lan9303_xmit(struct sk_buff *skb, struct net_device *dev)
46 struct dsa_slave_priv *p = netdev_priv(dev);
49 /* insert a special VLAN tag between the MAC addresses
50 * and the current ethertype field.
52 if (skb_cow_head(skb, LAN9303_TAG_LEN) < 0) {
54 "Cannot make room for the special tag. Dropping packet\n");
58 /* provide 'LAN9303_TAG_LEN' bytes additional space */
59 skb_push(skb, LAN9303_TAG_LEN);
61 /* make room between MACs and Ether-Type */
62 memmove(skb->data, skb->data + LAN9303_TAG_LEN, 2 * ETH_ALEN);
64 lan9303_tag = (u16 *)(skb->data + 2 * ETH_ALEN);
65 lan9303_tag[0] = htons(ETH_P_8021Q);
66 lan9303_tag[1] = htons(p->dp->index | BIT(4));
74 static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev,
75 struct packet_type *pt, struct net_device *orig_dev)
78 struct dsa_switch_tree *dst = dev->dsa_ptr;
79 struct dsa_switch *ds;
80 unsigned int source_port;
85 dev_warn_ratelimited(&dev->dev, "Dropping packet, due to missing DSA switch device\n");
89 if (unlikely(!pskb_may_pull(skb, LAN9303_TAG_LEN))) {
90 dev_warn_ratelimited(&dev->dev,
91 "Dropping packet, cannot pull\n");
95 /* '->data' points into the middle of our special VLAN tag information:
97 * ~ MAC src | 0x81 | 0x00 | 0xyy | 0xzz | ether type
101 lan9303_tag = (u16 *)(skb->data - 2);
103 if (lan9303_tag[0] != htons(ETH_P_8021Q)) {
104 dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid VLAN marker\n");
108 source_port = ntohs(lan9303_tag[1]) & 0x3;
110 if (source_port >= LAN9303_MAX_PORTS) {
111 dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid source port\n");
115 if (!ds->ports[source_port].netdev) {
116 dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid netdev or device\n");
120 /* remove the special VLAN tag between the MAC addresses
121 * and the current ethertype field.
123 skb_pull_rcsum(skb, 2 + 2);
124 memmove(skb->data - ETH_HLEN, skb->data - (ETH_HLEN + LAN9303_TAG_LEN),
127 /* forward the packet to the dedicated interface */
128 skb->dev = ds->ports[source_port].netdev;
133 const struct dsa_device_ops lan9303_netdev_ops = {
134 .xmit = lan9303_xmit,