blob: 7f3b7d730b850d83774a59f8192e3e4cd186ef89 [file] [log] [blame]
Oleksij Rempel48fda742019-12-18 09:02:14 +01001// 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
Vladimir Olteanbd954b82022-11-21 15:55:47 +020010#include "tag.h"
Oleksij Rempel48fda742019-12-18 09:02:14 +010011
Vladimir Oltean94793a52022-11-15 03:18:44 +020012#define AR9331_NAME "ar9331"
13
Oleksij Rempel48fda742019-12-18 09:02:14 +010014#define AR9331_HDR_LEN 2
15#define AR9331_HDR_VERSION 1
16
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.
25 */
26#define AR9331_HDR_RESERVED_MASK GENMASK(5, 4)
27#define AR9331_HDR_PORT_NUM_MASK GENMASK(3, 0)
28
29static struct sk_buff *ar9331_tag_xmit(struct sk_buff *skb,
30 struct net_device *dev)
31{
32 struct dsa_port *dp = dsa_slave_to_port(dev);
33 __le16 *phdr;
34 u16 hdr;
35
Oleksij Rempel48fda742019-12-18 09:02:14 +010036 phdr = skb_push(skb, AR9331_HDR_LEN);
37
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;
42
43 phdr[0] = cpu_to_le16(hdr);
44
45 return skb;
46}
47
48static struct sk_buff *ar9331_tag_rcv(struct sk_buff *skb,
Vladimir Oltean29a097b2021-07-31 17:14:32 +030049 struct net_device *ndev)
Oleksij Rempel48fda742019-12-18 09:02:14 +010050{
51 u8 ver, port;
52 u16 hdr;
53
54 if (unlikely(!pskb_may_pull(skb, AR9331_HDR_LEN)))
55 return NULL;
56
57 hdr = le16_to_cpu(*(__le16 *)skb_mac_header(skb));
58
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);
63 return NULL;
64 }
65
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);
69 return NULL;
70 }
71
72 skb_pull_rcsum(skb, AR9331_HDR_LEN);
73
74 /* Get source port information */
75 port = FIELD_GET(AR9331_HDR_PORT_NUM_MASK, hdr);
76
77 skb->dev = dsa_master_find_slave(ndev, 0, port);
78 if (!skb->dev)
79 return NULL;
80
81 return skb;
82}
83
84static const struct dsa_device_ops ar9331_netdev_ops = {
Vladimir Oltean94793a52022-11-15 03:18:44 +020085 .name = AR9331_NAME,
Oleksij Rempel48fda742019-12-18 09:02:14 +010086 .proto = DSA_TAG_PROTO_AR9331,
87 .xmit = ar9331_tag_xmit,
88 .rcv = ar9331_tag_rcv,
Vladimir Oltean4e500252021-06-11 22:01:24 +030089 .needed_headroom = AR9331_HDR_LEN,
Oleksij Rempel48fda742019-12-18 09:02:14 +010090};
91
92MODULE_LICENSE("GPL v2");
Vladimir Oltean94793a52022-11-15 03:18:44 +020093MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_AR9331, AR9331_NAME);
Oleksij Rempel48fda742019-12-18 09:02:14 +010094module_dsa_tag_driver(ar9331_netdev_ops);