Loading...
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
31static 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 __be16 *phdr;
35 u16 hdr;
36
37 skb_push(skb, QCA_HDR_LEN);
38
39 memmove(skb->data, skb->data + QCA_HDR_LEN, 2 * ETH_ALEN);
40 phdr = (__be16 *)(skb->data + 2 * ETH_ALEN);
41
42 /* Set the version field, and set destination port information */
43 hdr = QCA_HDR_VERSION << QCA_HDR_XMIT_VERSION_S |
44 QCA_HDR_XMIT_FROM_CPU | BIT(dp->index);
45
46 *phdr = htons(hdr);
47
48 return skb;
49}
50
51static struct sk_buff *qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
52 struct packet_type *pt)
53{
54 u8 ver;
55 u16 hdr;
56 int port;
57 __be16 *phdr;
58
59 if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
60 return NULL;
61
62 /* The QCA header is added by the switch between src addr and Ethertype
63 * At this point, skb->data points to ethertype so header should be
64 * right before
65 */
66 phdr = (__be16 *)(skb->data - 2);
67 hdr = ntohs(*phdr);
68
69 /* Make sure the version is correct */
70 ver = (hdr & QCA_HDR_RECV_VERSION_MASK) >> QCA_HDR_RECV_VERSION_S;
71 if (unlikely(ver != QCA_HDR_VERSION))
72 return NULL;
73
74 /* Remove QCA tag and recalculate checksum */
75 skb_pull_rcsum(skb, QCA_HDR_LEN);
76 memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
77 ETH_HLEN - QCA_HDR_LEN);
78
79 /* Get source port information */
80 port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
81
82 skb->dev = dsa_master_find_slave(dev, 0, port);
83 if (!skb->dev)
84 return NULL;
85
86 return skb;
87}
88
89static const struct dsa_device_ops qca_netdev_ops = {
90 .name = "qca",
91 .proto = DSA_TAG_PROTO_QCA,
92 .xmit = qca_tag_xmit,
93 .rcv = qca_tag_rcv,
94 .needed_headroom = QCA_HDR_LEN,
95};
96
97MODULE_LICENSE("GPL");
98MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_QCA);
99
100module_dsa_tag_driver(qca_netdev_ops);
1/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
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.
12 */
13
14#include <linux/etherdevice.h>
15#include "dsa_priv.h"
16
17#define QCA_HDR_LEN 2
18#define QCA_HDR_VERSION 0x2
19
20#define QCA_HDR_RECV_VERSION_MASK GENMASK(15, 14)
21#define QCA_HDR_RECV_VERSION_S 14
22#define QCA_HDR_RECV_PRIORITY_MASK GENMASK(13, 11)
23#define QCA_HDR_RECV_PRIORITY_S 11
24#define QCA_HDR_RECV_TYPE_MASK GENMASK(10, 6)
25#define QCA_HDR_RECV_TYPE_S 6
26#define QCA_HDR_RECV_FRAME_IS_TAGGED BIT(3)
27#define QCA_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
28
29#define QCA_HDR_XMIT_VERSION_MASK GENMASK(15, 14)
30#define QCA_HDR_XMIT_VERSION_S 14
31#define QCA_HDR_XMIT_PRIORITY_MASK GENMASK(13, 11)
32#define QCA_HDR_XMIT_PRIORITY_S 11
33#define QCA_HDR_XMIT_CONTROL_MASK GENMASK(10, 8)
34#define QCA_HDR_XMIT_CONTROL_S 8
35#define QCA_HDR_XMIT_FROM_CPU BIT(7)
36#define QCA_HDR_XMIT_DP_BIT_MASK GENMASK(6, 0)
37
38static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
39{
40 struct dsa_slave_priv *p = netdev_priv(dev);
41 u16 *phdr, hdr;
42
43 dev->stats.tx_packets++;
44 dev->stats.tx_bytes += skb->len;
45
46 if (skb_cow_head(skb, 0) < 0)
47 goto out_free;
48
49 skb_push(skb, QCA_HDR_LEN);
50
51 memmove(skb->data, skb->data + QCA_HDR_LEN, 2 * ETH_ALEN);
52 phdr = (u16 *)(skb->data + 2 * ETH_ALEN);
53
54 /* Set the version field, and set destination port information */
55 hdr = QCA_HDR_VERSION << QCA_HDR_XMIT_VERSION_S |
56 QCA_HDR_XMIT_FROM_CPU |
57 BIT(p->port);
58
59 *phdr = htons(hdr);
60
61 return skb;
62
63out_free:
64 kfree_skb(skb);
65 return NULL;
66}
67
68static int qca_tag_rcv(struct sk_buff *skb, struct net_device *dev,
69 struct packet_type *pt, struct net_device *orig_dev)
70{
71 struct dsa_switch_tree *dst = dev->dsa_ptr;
72 struct dsa_switch *ds;
73 u8 ver;
74 int port;
75 __be16 *phdr, hdr;
76
77 if (unlikely(!dst))
78 goto out_drop;
79
80 skb = skb_unshare(skb, GFP_ATOMIC);
81 if (!skb)
82 goto out;
83
84 if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN)))
85 goto out_drop;
86
87 /* The QCA header is added by the switch between src addr and Ethertype
88 * At this point, skb->data points to ethertype so header should be
89 * right before
90 */
91 phdr = (__be16 *)(skb->data - 2);
92 hdr = ntohs(*phdr);
93
94 /* Make sure the version is correct */
95 ver = (hdr & QCA_HDR_RECV_VERSION_MASK) >> QCA_HDR_RECV_VERSION_S;
96 if (unlikely(ver != QCA_HDR_VERSION))
97 goto out_drop;
98
99 /* Remove QCA tag and recalculate checksum */
100 skb_pull_rcsum(skb, QCA_HDR_LEN);
101 memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - QCA_HDR_LEN,
102 ETH_HLEN - QCA_HDR_LEN);
103
104 /* This protocol doesn't support cascading multiple switches so it's
105 * safe to assume the switch is first in the tree
106 */
107 ds = dst->ds[0];
108 if (!ds)
109 goto out_drop;
110
111 /* Get source port information */
112 port = (hdr & QCA_HDR_RECV_SOURCE_PORT_MASK);
113 if (!ds->ports[port].netdev)
114 goto out_drop;
115
116 /* Update skb & forward the frame accordingly */
117 skb_push(skb, ETH_HLEN);
118 skb->pkt_type = PACKET_HOST;
119 skb->dev = ds->ports[port].netdev;
120 skb->protocol = eth_type_trans(skb, skb->dev);
121
122 skb->dev->stats.rx_packets++;
123 skb->dev->stats.rx_bytes += skb->len;
124
125 netif_receive_skb(skb);
126
127 return 0;
128
129out_drop:
130 kfree_skb(skb);
131out:
132 return 0;
133}
134
135const struct dsa_device_ops qca_netdev_ops = {
136 .xmit = qca_tag_xmit,
137 .rcv = qca_tag_rcv,
138};