1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
7 #include <linux/bitfield.h>
8 #include <linux/etherdevice.h>
12 #define AR9331_NAME "ar9331"
14 #define AR9331_HDR_LEN 2
15 #define AR9331_HDR_VERSION 1
17 #define AR9331_HDR_VERSION_MASK GENMASK(15, 14)
18 #define AR9331_HDR_PRIORITY_MASK GENMASK(13, 12)
19 #define AR9331_HDR_TYPE_MASK GENMASK(10, 8)
20 #define AR9331_HDR_BROADCAST BIT(7)
21 #define AR9331_HDR_FROM_CPU BIT(6)
22 /* AR9331_HDR_RESERVED - not used or may be version field.
23 * According to the AR8216 doc it should 0b10. On AR9331 it is 0b11 on RX path
24 * and should be set to 0b11 to make it work.
26 #define AR9331_HDR_RESERVED_MASK GENMASK(5, 4)
27 #define AR9331_HDR_PORT_NUM_MASK GENMASK(3, 0)
29 static struct sk_buff
*ar9331_tag_xmit(struct sk_buff
*skb
,
30 struct net_device
*dev
)
32 struct dsa_port
*dp
= dsa_user_to_port(dev
);
36 phdr
= skb_push(skb
, AR9331_HDR_LEN
);
38 hdr
= FIELD_PREP(AR9331_HDR_VERSION_MASK
, AR9331_HDR_VERSION
);
39 hdr
|= AR9331_HDR_FROM_CPU
| dp
->index
;
40 /* 0b10 for AR8216 and 0b11 for AR9331 */
41 hdr
|= AR9331_HDR_RESERVED_MASK
;
43 phdr
[0] = cpu_to_le16(hdr
);
48 static struct sk_buff
*ar9331_tag_rcv(struct sk_buff
*skb
,
49 struct net_device
*ndev
)
54 if (unlikely(!pskb_may_pull(skb
, AR9331_HDR_LEN
)))
57 hdr
= le16_to_cpu(*(__le16
*)skb_mac_header(skb
));
59 ver
= FIELD_GET(AR9331_HDR_VERSION_MASK
, hdr
);
60 if (unlikely(ver
!= AR9331_HDR_VERSION
)) {
61 netdev_warn_once(ndev
, "%s:%i wrong header version 0x%2x\n",
62 __func__
, __LINE__
, hdr
);
66 if (unlikely(hdr
& AR9331_HDR_FROM_CPU
)) {
67 netdev_warn_once(ndev
, "%s:%i packet should not be from cpu 0x%2x\n",
68 __func__
, __LINE__
, hdr
);
72 skb_pull_rcsum(skb
, AR9331_HDR_LEN
);
74 /* Get source port information */
75 port
= FIELD_GET(AR9331_HDR_PORT_NUM_MASK
, hdr
);
77 skb
->dev
= dsa_conduit_find_user(ndev
, 0, port
);
84 static const struct dsa_device_ops ar9331_netdev_ops
= {
86 .proto
= DSA_TAG_PROTO_AR9331
,
87 .xmit
= ar9331_tag_xmit
,
88 .rcv
= ar9331_tag_rcv
,
89 .needed_headroom
= AR9331_HDR_LEN
,
92 MODULE_DESCRIPTION("DSA tag driver for Atheros AR9331 SoC with built-in switch");
93 MODULE_LICENSE("GPL v2");
94 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_AR9331
, AR9331_NAME
);
95 module_dsa_tag_driver(ar9331_netdev_ops
);