Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2017 Pengutronix, Juergen Borleis <jbe@pengutronix.de>
  4 */
  5#include <linux/dsa/lan9303.h>
  6#include <linux/etherdevice.h>
  7#include <linux/list.h>
  8#include <linux/slab.h>
  9
 10#include "dsa_priv.h"
 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
 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 | \
 39					LAN9303_TAG_RX_STP)
 40
 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
 47 */
 48static int lan9303_xmit_use_arl(struct dsa_port *dp, u8 *dest_addr)
 49{
 50	struct lan9303 *chip = dp->ds->priv;
 51
 52	return chip->is_bridged && !is_multicast_ether_addr(dest_addr);
 53}
 54
 55static struct sk_buff *lan9303_xmit(struct sk_buff *skb, struct net_device *dev)
 56{
 57	struct dsa_port *dp = dsa_slave_to_port(dev);
 58	__be16 *lan9303_tag;
 59	u16 tag;
 60
 61	/* insert a special VLAN tag between the MAC addresses
 62	 * and the current ethertype field.
 63	 */
 64	if (skb_cow_head(skb, LAN9303_TAG_LEN) < 0) {
 65		dev_dbg(&dev->dev,
 66			"Cannot make room for the special tag. Dropping packet\n");
 67		return NULL;
 68	}
 69
 70	/* provide 'LAN9303_TAG_LEN' bytes additional space */
 71	skb_push(skb, LAN9303_TAG_LEN);
 72
 73	/* make room between MACs and Ether-Type */
 74	memmove(skb->data, skb->data + LAN9303_TAG_LEN, 2 * ETH_ALEN);
 75
 76	lan9303_tag = (__be16 *)(skb->data + 2 * ETH_ALEN);
 77	tag = lan9303_xmit_use_arl(dp, skb->data) ?
 78		LAN9303_TAG_TX_USE_ALR :
 79		dp->index | LAN9303_TAG_TX_STP_OVERRIDE;
 80	lan9303_tag[0] = htons(ETH_P_8021Q);
 81	lan9303_tag[1] = htons(tag);
 82
 83	return skb;
 84}
 85
 86static struct sk_buff *lan9303_rcv(struct sk_buff *skb, struct net_device *dev,
 87				   struct packet_type *pt)
 88{
 89	__be16 *lan9303_tag;
 90	u16 lan9303_tag1;
 91	unsigned int source_port;
 92
 93	if (unlikely(!pskb_may_pull(skb, LAN9303_TAG_LEN))) {
 94		dev_warn_ratelimited(&dev->dev,
 95				     "Dropping packet, cannot pull\n");
 96		return NULL;
 97	}
 98
 99	/* '->data' points into the middle of our special VLAN tag information:
100	 *
101	 * ~ MAC src   | 0x81 | 0x00 | 0xyy | 0xzz | ether type
102	 *                           ^
103	 *                        ->data
104	 */
105	lan9303_tag = (__be16 *)(skb->data - 2);
106
107	if (lan9303_tag[0] != htons(ETH_P_8021Q)) {
108		dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid VLAN marker\n");
109		return NULL;
110	}
111
112	lan9303_tag1 = ntohs(lan9303_tag[1]);
113	source_port = lan9303_tag1 & 0x3;
114
115	skb->dev = dsa_master_find_slave(dev, 0, source_port);
116	if (!skb->dev) {
117		dev_warn_ratelimited(&dev->dev, "Dropping packet due to invalid source port\n");
118		return NULL;
119	}
120
121	/* remove the special VLAN tag between the MAC addresses
122	 * and the current ethertype field.
123	 */
124	skb_pull_rcsum(skb, 2 + 2);
125	memmove(skb->data - ETH_HLEN, skb->data - (ETH_HLEN + LAN9303_TAG_LEN),
126		2 * ETH_ALEN);
127	skb->offload_fwd_mark = !(lan9303_tag1 & LAN9303_TAG_RX_TRAPPED_TO_CPU);
128
129	return skb;
130}
131
132static const struct dsa_device_ops lan9303_netdev_ops = {
133	.name = "lan9303",
134	.proto	= DSA_TAG_PROTO_LAN9303,
135	.xmit = lan9303_xmit,
136	.rcv = lan9303_rcv,
137	.overhead = LAN9303_TAG_LEN,
138};
139
140MODULE_LICENSE("GPL");
141MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_LAN9303);
142
143module_dsa_tag_driver(lan9303_netdev_ops);