xref: /openbmc/linux/net/dsa/tag_qca.c (revision bd5874da)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/etherdevice.h>
7 
8 #include "dsa_priv.h"
9 
10 #define QCA_HDR_LEN	2
11 #define QCA_HDR_VERSION	0x2
12 
13 #define QCA_HDR_RECV_VERSION_MASK	GENMASK(15, 14)
14 #define QCA_HDR_RECV_VERSION_S		14
15 #define QCA_HDR_RECV_PRIORITY_MASK	GENMASK(13, 11)
16 #define QCA_HDR_RECV_PRIORITY_S		11
17 #define QCA_HDR_RECV_TYPE_MASK		GENMASK(10, 6)
18 #define QCA_HDR_RECV_TYPE_S		6
19 #define QCA_HDR_RECV_FRAME_IS_TAGGED	BIT(3)
20 #define QCA_HDR_RECV_SOURCE_PORT_MASK	GENMASK(2, 0)
21 
22 #define QCA_HDR_XMIT_VERSION_MASK	GENMASK(15, 14)
23 #define QCA_HDR_XMIT_VERSION_S		14
24 #define QCA_HDR_XMIT_PRIORITY_MASK	GENMASK(13, 11)
25 #define QCA_HDR_XMIT_PRIORITY_S		11
26 #define QCA_HDR_XMIT_CONTROL_MASK	GENMASK(10, 8)
27 #define QCA_HDR_XMIT_CONTROL_S		8
28 #define QCA_HDR_XMIT_FROM_CPU		BIT(7)
29 #define QCA_HDR_XMIT_DP_BIT_MASK	GENMASK(6, 0)
30 
31 static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
32 {
33 	struct dsa_port *dp = dsa_slave_to_port(dev);
34 	u16 *phdr, hdr;
35 
36 	if (skb_cow_head(skb, 0) < 0)
37 		return NULL;
38 
39 	skb_push(skb, QCA_HDR_LEN);
40 
41 	memmove(skb->data, skb->data + QCA_HDR_LEN, 2 * ETH_ALEN);
42 	phdr = (u16 *)(skb->data + 2 * ETH_ALEN);
43 
44 	/* Set the version field, and set destination port information */
45 	hdr = QCA_HDR_VERSION << QCA_HDR_XMIT_VERSION_S |
46 		QCA_HDR_XMIT_FROM_CPU | BIT(dp->index);
47 
48 	*phdr = htons(hdr);
49 
50 	return skb;
51 }
52 
53 static struct sk_buff *qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
54 				   struct packet_type *pt)
55 {
56 	u8 ver;
57 	int port;
58 	__be16 *phdr, hdr;
59 
60 	if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
61 		return NULL;
62 
63 	/* The QCA header is added by the switch between src addr and Ethertype
64 	 * At this point, skb->data points to ethertype so header should be
65 	 * right before
66 	 */
67 	phdr = (__be16 *)(skb->data - 2);
68 	hdr = ntohs(*phdr);
69 
70 	/* Make sure the version is correct */
71 	ver = (hdr & QCA_HDR_RECV_VERSION_MASK) >> QCA_HDR_RECV_VERSION_S;
72 	if (unlikely(ver != QCA_HDR_VERSION))
73 		return NULL;
74 
75 	/* Remove QCA tag and recalculate checksum */
76 	skb_pull_rcsum(skb, QCA_HDR_LEN);
77 	memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
78 		ETH_HLEN - QCA_HDR_LEN);
79 
80 	/* Get source port information */
81 	port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
82 
83 	skb->dev = dsa_master_find_slave(dev, 0, port);
84 	if (!skb->dev)
85 		return NULL;
86 
87 	return skb;
88 }
89 
90 static int qca_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto,
91                                 int *offset)
92 {
93 	*offset = QCA_HDR_LEN;
94 	*proto = ((__be16 *)skb->data)[0];
95 
96 	return 0;
97 }
98 
99 static const struct dsa_device_ops qca_netdev_ops = {
100 	.name	= "qca",
101 	.proto	= DSA_TAG_PROTO_QCA,
102 	.xmit	= qca_tag_xmit,
103 	.rcv	= qca_tag_rcv,
104 	.flow_dissect = qca_tag_flow_dissect,
105 	.overhead = QCA_HDR_LEN,
106 };
107 
108 MODULE_LICENSE("GPL");
109 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_QCA);
110 
111 module_dsa_tag_driver(qca_netdev_ops);
112