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 #include <linux/bitfield.h>
8 #include <net/dsa.h>
9 #include <linux/dsa/tag_qca.h>
10
11 #include "tag.h"
12
13 #define QCA_NAME "qca"
14
qca_tag_xmit(struct sk_buff * skb,struct net_device * dev)15 static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
16 {
17 __be16 *phdr;
18 u16 hdr;
19
20 skb_push(skb, QCA_HDR_LEN);
21
22 dsa_alloc_etype_header(skb, QCA_HDR_LEN);
23 phdr = dsa_etype_header_pos_tx(skb);
24
25 /* Set the version field, and set destination port information */
26 hdr = FIELD_PREP(QCA_HDR_XMIT_VERSION, QCA_HDR_VERSION);
27 hdr |= QCA_HDR_XMIT_FROM_CPU;
28 hdr |= FIELD_PREP(QCA_HDR_XMIT_DP_BIT, dsa_xmit_port_mask(skb, dev));
29
30 *phdr = htons(hdr);
31
32 return skb;
33 }
34
qca_tag_rcv(struct sk_buff * skb,struct net_device * dev)35 static struct sk_buff *qca_tag_rcv(struct sk_buff *skb, struct net_device *dev)
36 {
37 struct qca_tagger_data *tagger_data;
38 struct dsa_port *dp = dev->dsa_ptr;
39 struct dsa_switch *ds = dp->ds;
40 u8 ver, pk_type;
41 __be16 *phdr;
42 int port;
43 u16 hdr;
44
45 BUILD_BUG_ON(sizeof(struct qca_mgmt_ethhdr) != QCA_HDR_MGMT_HEADER_LEN + QCA_HDR_LEN);
46
47 tagger_data = ds->tagger_data;
48
49 if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN))) {
50 kfree_skb(skb);
51 return NULL;
52 }
53
54 phdr = dsa_etype_header_pos_rx(skb);
55 hdr = ntohs(*phdr);
56
57 /* Make sure the version is correct */
58 ver = FIELD_GET(QCA_HDR_RECV_VERSION, hdr);
59 if (unlikely(ver != QCA_HDR_VERSION)) {
60 kfree_skb(skb);
61 return NULL;
62 }
63
64 /* Get pk type */
65 pk_type = FIELD_GET(QCA_HDR_RECV_TYPE, hdr);
66
67 /* Ethernet mgmt read/write packet */
68 if (pk_type == QCA_HDR_RECV_TYPE_RW_REG_ACK) {
69 if (likely(tagger_data->rw_reg_ack_handler))
70 tagger_data->rw_reg_ack_handler(ds, skb);
71 kfree_skb(skb);
72 return NULL;
73 }
74
75 /* Ethernet MIB counter packet */
76 if (pk_type == QCA_HDR_RECV_TYPE_MIB) {
77 if (likely(tagger_data->mib_autocast_handler))
78 tagger_data->mib_autocast_handler(ds, skb);
79 kfree_skb(skb);
80 return NULL;
81 }
82
83 /* Get source port information */
84 port = FIELD_GET(QCA_HDR_RECV_SOURCE_PORT, hdr);
85
86 skb->dev = dsa_conduit_find_user(dev, 0, port);
87 if (!skb->dev) {
88 kfree_skb(skb);
89 return NULL;
90 }
91
92 /* Remove QCA tag and recalculate checksum */
93 skb_pull_rcsum(skb, QCA_HDR_LEN);
94 dsa_strip_etype_header(skb, QCA_HDR_LEN);
95
96 return skb;
97 }
98
qca_tag_connect(struct dsa_switch * ds)99 static int qca_tag_connect(struct dsa_switch *ds)
100 {
101 struct qca_tagger_data *tagger_data;
102
103 tagger_data = kzalloc_obj(*tagger_data);
104 if (!tagger_data)
105 return -ENOMEM;
106
107 ds->tagger_data = tagger_data;
108
109 return 0;
110 }
111
qca_tag_disconnect(struct dsa_switch * ds)112 static void qca_tag_disconnect(struct dsa_switch *ds)
113 {
114 kfree(ds->tagger_data);
115 ds->tagger_data = NULL;
116 }
117
118 static const struct dsa_device_ops qca_netdev_ops = {
119 .name = QCA_NAME,
120 .proto = DSA_TAG_PROTO_QCA,
121 .connect = qca_tag_connect,
122 .disconnect = qca_tag_disconnect,
123 .xmit = qca_tag_xmit,
124 .rcv = qca_tag_rcv,
125 .needed_headroom = QCA_HDR_LEN,
126 .promisc_on_conduit = true,
127 };
128
129 MODULE_DESCRIPTION("DSA tag driver for Qualcomm Atheros QCA8K switches");
130 MODULE_LICENSE("GPL");
131 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_QCA, QCA_NAME);
132
133 module_dsa_tag_driver(qca_netdev_ops);
134