blob: 60ca9861d51baaa5257e8d4a55afc20bb507d3a0 [file] [log] [blame]
developerac95e9f2024-03-06 21:54:37 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Airoha DSA Tag support
4 * Copyright (C) 2023 Min Yao <min.yao@airoha.com>
5 */
6
7#include <linux/etherdevice.h>
8#include <linux/if_vlan.h>
9
10#include "dsa_priv.h"
11
12#define AIR_HDR_LEN 4
13#define AIR_HDR_XMIT_UNTAGGED 0
14#define AIR_HDR_XMIT_TAGGED_TPID_8100 1
15#define AIR_HDR_XMIT_TAGGED_TPID_88A8 2
16#define AIR_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
17#define AIR_HDR_XMIT_DP_BIT_MASK GENMASK(5, 0)
18
19static struct sk_buff *air_tag_xmit(struct sk_buff *skb,
20 struct net_device *dev)
21{
22 struct dsa_port *dp = dsa_slave_to_port(dev);
23 u8 xmit_tpid;
24 u8 *air_tag;
25 unsigned char *dest = eth_hdr(skb)->h_dest;
26
27 /* Build the special tag after the MAC Source Address. If VLAN header
28 * is present, it's required that VLAN header and special tag is
29 * being combined. Only in this way we can allow the switch can parse
30 * the both special and VLAN tag at the same time and then look up VLAN
31 * table with VID.
32 */
33 switch (skb->protocol) {
34 case htons(ETH_P_8021Q):
35 xmit_tpid = AIR_HDR_XMIT_TAGGED_TPID_8100;
36 break;
37 case htons(ETH_P_8021AD):
38 xmit_tpid = AIR_HDR_XMIT_TAGGED_TPID_88A8;
39 break;
40 default:
41 if (skb_cow_head(skb, AIR_HDR_LEN) < 0)
42 return NULL;
43
44 xmit_tpid = AIR_HDR_XMIT_UNTAGGED;
45 skb_push(skb, AIR_HDR_LEN);
46 memmove(skb->data, skb->data + AIR_HDR_LEN, 2 * ETH_ALEN);
47 }
48
49 air_tag = skb->data + 2 * ETH_ALEN;
50
51 /* Mark tag attribute on special tag insertion to notify hardware
52 * whether that's a combined special tag with 802.1Q header.
53 */
54 air_tag[0] = xmit_tpid;
55 air_tag[1] = (1 << dp->index) & AIR_HDR_XMIT_DP_BIT_MASK;
56
57 /* Tag control information is kept for 802.1Q */
58 if (xmit_tpid == AIR_HDR_XMIT_UNTAGGED) {
59 air_tag[2] = 0;
60 air_tag[3] = 0;
61 }
62
63 return skb;
64}
65
66static struct sk_buff *air_tag_rcv(struct sk_buff *skb, struct net_device *dev,
67 struct packet_type *pt)
68{
69 int port;
70 __be16 *phdr, hdr;
71 unsigned char *dest = eth_hdr(skb)->h_dest;
72 bool is_multicast_skb = is_multicast_ether_addr(dest) &&
73 !is_broadcast_ether_addr(dest);
74
75 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) {
76 hdr = ntohs(skb->vlan_proto);
77 skb->vlan_proto = 0;
78 skb->vlan_tci = 0;
79 } else {
80 if (unlikely(!pskb_may_pull(skb, AIR_HDR_LEN)))
81 return NULL;
82
83 /* The AIR header is added by the switch between src addr
84 * and ethertype at this point, skb->data points to 2 bytes
85 * after src addr so header should be 2 bytes right before.
86 */
87 phdr = (__be16 *)(skb->data - 2);
88 hdr = ntohs(*phdr);
89
90 /* Remove AIR tag and recalculate checksum. */
91 skb_pull_rcsum(skb, AIR_HDR_LEN);
92
93 memmove(skb->data - ETH_HLEN,
94 skb->data - ETH_HLEN - AIR_HDR_LEN,
95 2 * ETH_ALEN);
96 }
97
98 /* Get source port information */
99 port = (hdr & AIR_HDR_RECV_SOURCE_PORT_MASK);
100
101 skb->dev = dsa_master_find_slave(dev, 0, port);
102 if (!skb->dev)
103 return NULL;
104
105 /* Only unicast or broadcast frames are offloaded */
106 if (likely(!is_multicast_skb))
107 skb->offload_fwd_mark = 1;
108
109 return skb;
110}
111
112static int air_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto,
113 int *offset)
114{
115 *offset = 4;
116 *proto = ((__be16 *)skb->data)[1];
117
118 return 0;
119}
120
121static const struct dsa_device_ops air_netdev_ops = {
122 .name = "air",
123 .proto = DSA_TAG_PROTO_ARHT,
124 .xmit = air_tag_xmit,
125 .rcv = air_tag_rcv,
126 .flow_dissect = air_tag_flow_dissect,
127 .overhead = AIR_HDR_LEN,
128};
129
130MODULE_LICENSE("GPL");
131MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_AIR);
132
133module_dsa_tag_driver(air_netdev_ops);