1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2019 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
4 */
5
6
7 #include <linux/bitfield.h>
8 #include <linux/etherdevice.h>
9
10 #include "dsa_priv.h"
11
12 #define AR9331_HDR_LEN 2
13 #define AR9331_HDR_VERSION 1
14
15 #define AR9331_HDR_VERSION_MASK GENMASK(15, 14)
16 #define AR9331_HDR_PRIORITY_MASK GENMASK(13, 12)
17 #define AR9331_HDR_TYPE_MASK GENMASK(10, 8)
18 #define AR9331_HDR_BROADCAST BIT(7)
19 #define AR9331_HDR_FROM_CPU BIT(6)
20 /* AR9331_HDR_RESERVED - not used or may be version field.
21 * According to the AR8216 doc it should 0b10. On AR9331 it is 0b11 on RX path
22 * and should be set to 0b11 to make it work.
23 */
24 #define AR9331_HDR_RESERVED_MASK GENMASK(5, 4)
25 #define AR9331_HDR_PORT_NUM_MASK GENMASK(3, 0)
26
ar9331_tag_xmit(struct sk_buff * skb,struct net_device * dev)27 static struct sk_buff *ar9331_tag_xmit(struct sk_buff *skb,
28 struct net_device *dev)
29 {
30 struct dsa_port *dp = dsa_slave_to_port(dev);
31 __le16 *phdr;
32 u16 hdr;
33
34 phdr = skb_push(skb, AR9331_HDR_LEN);
35
36 hdr = FIELD_PREP(AR9331_HDR_VERSION_MASK, AR9331_HDR_VERSION);
37 hdr |= AR9331_HDR_FROM_CPU | dp->index;
38 /* 0b10 for AR8216 and 0b11 for AR9331 */
39 hdr |= AR9331_HDR_RESERVED_MASK;
40
41 phdr[0] = cpu_to_le16(hdr);
42
43 return skb;
44 }
45
ar9331_tag_rcv(struct sk_buff * skb,struct net_device * ndev)46 static struct sk_buff *ar9331_tag_rcv(struct sk_buff *skb,
47 struct net_device *ndev)
48 {
49 u8 ver, port;
50 u16 hdr;
51
52 if (unlikely(!pskb_may_pull(skb, AR9331_HDR_LEN)))
53 return NULL;
54
55 hdr = le16_to_cpu(*(__le16 *)skb_mac_header(skb));
56
57 ver = FIELD_GET(AR9331_HDR_VERSION_MASK, hdr);
58 if (unlikely(ver != AR9331_HDR_VERSION)) {
59 netdev_warn_once(ndev, "%s:%i wrong header version 0x%2x\n",
60 __func__, __LINE__, hdr);
61 return NULL;
62 }
63
64 if (unlikely(hdr & AR9331_HDR_FROM_CPU)) {
65 netdev_warn_once(ndev, "%s:%i packet should not be from cpu 0x%2x\n",
66 __func__, __LINE__, hdr);
67 return NULL;
68 }
69
70 skb_pull_rcsum(skb, AR9331_HDR_LEN);
71
72 /* Get source port information */
73 port = FIELD_GET(AR9331_HDR_PORT_NUM_MASK, hdr);
74
75 skb->dev = dsa_master_find_slave(ndev, 0, port);
76 if (!skb->dev)
77 return NULL;
78
79 return skb;
80 }
81
82 static const struct dsa_device_ops ar9331_netdev_ops = {
83 .name = "ar9331",
84 .proto = DSA_TAG_PROTO_AR9331,
85 .xmit = ar9331_tag_xmit,
86 .rcv = ar9331_tag_rcv,
87 .needed_headroom = AR9331_HDR_LEN,
88 };
89
90 MODULE_LICENSE("GPL v2");
91 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_AR9331);
92 module_dsa_tag_driver(ar9331_netdev_ops);
93