Blame view

net/dsa/tag_qca.c 2.9 KB
dfedd3b62   Andrew Lunn   dsa: Add SPDX hea...
1
  // SPDX-License-Identifier: GPL-2.0
cafdc45c9   John Crispin   net-next: dsa: ad...
2
3
  /*
   * Copyright (c) 2015, The Linux Foundation. All rights reserved.
cafdc45c9   John Crispin   net-next: dsa: ad...
4
5
6
   */
  
  #include <linux/etherdevice.h>
ea5dd34be   Vivien Didelot   net: dsa: include...
7

cafdc45c9   John Crispin   net-next: dsa: ad...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include "dsa_priv.h"
  
  #define QCA_HDR_LEN	2
  #define QCA_HDR_VERSION	0x2
  
  #define QCA_HDR_RECV_VERSION_MASK	GENMASK(15, 14)
  #define QCA_HDR_RECV_VERSION_S		14
  #define QCA_HDR_RECV_PRIORITY_MASK	GENMASK(13, 11)
  #define QCA_HDR_RECV_PRIORITY_S		11
  #define QCA_HDR_RECV_TYPE_MASK		GENMASK(10, 6)
  #define QCA_HDR_RECV_TYPE_S		6
  #define QCA_HDR_RECV_FRAME_IS_TAGGED	BIT(3)
  #define QCA_HDR_RECV_SOURCE_PORT_MASK	GENMASK(2, 0)
  
  #define QCA_HDR_XMIT_VERSION_MASK	GENMASK(15, 14)
  #define QCA_HDR_XMIT_VERSION_S		14
  #define QCA_HDR_XMIT_PRIORITY_MASK	GENMASK(13, 11)
  #define QCA_HDR_XMIT_PRIORITY_S		11
  #define QCA_HDR_XMIT_CONTROL_MASK	GENMASK(10, 8)
  #define QCA_HDR_XMIT_CONTROL_S		8
  #define QCA_HDR_XMIT_FROM_CPU		BIT(7)
  #define QCA_HDR_XMIT_DP_BIT_MASK	GENMASK(6, 0)
  
  static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
  {
d945097bb   Vivien Didelot   net: dsa: add sla...
33
  	struct dsa_port *dp = dsa_slave_to_port(dev);
cafdc45c9   John Crispin   net-next: dsa: ad...
34
35
36
37
38
39
  	u16 *phdr, hdr;
  
  	dev->stats.tx_packets++;
  	dev->stats.tx_bytes += skb->len;
  
  	if (skb_cow_head(skb, 0) < 0)
fe47d5630   Vivien Didelot   net: dsa: factor ...
40
  		return NULL;
cafdc45c9   John Crispin   net-next: dsa: ad...
41
42
43
44
45
46
47
48
  
  	skb_push(skb, QCA_HDR_LEN);
  
  	memmove(skb->data, skb->data + QCA_HDR_LEN, 2 * ETH_ALEN);
  	phdr = (u16 *)(skb->data + 2 * ETH_ALEN);
  
  	/* Set the version field, and set destination port information */
  	hdr = QCA_HDR_VERSION << QCA_HDR_XMIT_VERSION_S |
d945097bb   Vivien Didelot   net: dsa: add sla...
49
  		QCA_HDR_XMIT_FROM_CPU | BIT(dp->index);
cafdc45c9   John Crispin   net-next: dsa: ad...
50
51
52
53
  
  	*phdr = htons(hdr);
  
  	return skb;
cafdc45c9   John Crispin   net-next: dsa: ad...
54
  }
a86d8becc   Florian Fainelli   net: dsa: Factor ...
55
  static struct sk_buff *qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
89e49506b   Florian Westphal   dsa: remove unuse...
56
  				   struct packet_type *pt)
cafdc45c9   John Crispin   net-next: dsa: ad...
57
  {
cafdc45c9   John Crispin   net-next: dsa: ad...
58
59
60
  	u8 ver;
  	int port;
  	__be16 *phdr, hdr;
cafdc45c9   John Crispin   net-next: dsa: ad...
61
  	if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
547097958   Vivien Didelot   net: dsa: remove ...
62
  		return NULL;
cafdc45c9   John Crispin   net-next: dsa: ad...
63
64
65
66
67
68
69
70
71
72
73
  
  	/* The QCA header is added by the switch between src addr and Ethertype
  	 * At this point, skb->data points to ethertype so header should be
  	 * right before
  	 */
  	phdr = (__be16 *)(skb->data - 2);
  	hdr = ntohs(*phdr);
  
  	/* Make sure the version is correct */
  	ver = (hdr & QCA_HDR_RECV_VERSION_MASK) >> QCA_HDR_RECV_VERSION_S;
  	if (unlikely(ver != QCA_HDR_VERSION))
547097958   Vivien Didelot   net: dsa: remove ...
74
  		return NULL;
cafdc45c9   John Crispin   net-next: dsa: ad...
75
76
77
78
79
  
  	/* Remove QCA tag and recalculate checksum */
  	skb_pull_rcsum(skb, QCA_HDR_LEN);
  	memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
  		ETH_HLEN - QCA_HDR_LEN);
cafdc45c9   John Crispin   net-next: dsa: ad...
80
81
  	/* Get source port information */
  	port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
cafdc45c9   John Crispin   net-next: dsa: ad...
82

2231c43b5   Vivien Didelot   net: dsa: rename ...
83
  	skb->dev = dsa_master_find_slave(dev, 0, port);
3775b1b7f   Vivien Didelot   net: dsa: add mas...
84
85
  	if (!skb->dev)
  		return NULL;
cafdc45c9   John Crispin   net-next: dsa: ad...
86

a86d8becc   Florian Fainelli   net: dsa: Factor ...
87
  	return skb;
cafdc45c9   John Crispin   net-next: dsa: ad...
88
  }
6e57d72a8   xiaofeis   net: dsa: Impleme...
89
90
91
92
93
94
95
96
  static int qca_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto,
                                  int *offset)
  {
  	*offset = QCA_HDR_LEN;
  	*proto = ((__be16 *)skb->data)[0];
  
  	return 0;
  }
f81a43e8d   Andrew Lunn   dsa: Cleanup unne...
97
  static const struct dsa_device_ops qca_netdev_ops = {
875138f81   Andrew Lunn   dsa: Move tagger ...
98
  	.name	= "qca",
056eed2fb   Andrew Lunn   dsa: Add TAG prot...
99
  	.proto	= DSA_TAG_PROTO_QCA,
cafdc45c9   John Crispin   net-next: dsa: ad...
100
101
  	.xmit	= qca_tag_xmit,
  	.rcv	= qca_tag_rcv,
6e57d72a8   xiaofeis   net: dsa: Impleme...
102
  	.flow_dissect = qca_tag_flow_dissect,
a5dd30877   Andrew Lunn   net: dsa: Add ove...
103
  	.overhead = QCA_HDR_LEN,
cafdc45c9   John Crispin   net-next: dsa: ad...
104
  };
0b42f0336   Andrew Lunn   dsa: Add MODULE_A...
105

f18bba50d   Andrew Lunn   dsa: Add MODULE_L...
106
  MODULE_LICENSE("GPL");
0b42f0336   Andrew Lunn   dsa: Add MODULE_A...
107
  MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_QCA);
d3b8c0498   Andrew Lunn   dsa: Add boilerpl...
108
109
  
  module_dsa_tag_driver(qca_netdev_ops);