Skip to content

Commit 8b8010f

Browse files
WoojungHuhdavem330
authored andcommitted
dsa: add support for Microchip KSZ tail tagging
Adding support for the Microchip KSZ switch family tail tagging. Reviewed-by: Andrew Lunn <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Signed-off-by: Woojung Huh <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6c21a2a commit 8b8010f

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

include/net/dsa.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum dsa_tag_protocol {
3131
DSA_TAG_PROTO_BRCM,
3232
DSA_TAG_PROTO_DSA,
3333
DSA_TAG_PROTO_EDSA,
34+
DSA_TAG_PROTO_KSZ,
3435
DSA_TAG_PROTO_LAN9303,
3536
DSA_TAG_PROTO_MTK,
3637
DSA_TAG_PROTO_QCA,

net/dsa/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ config NET_DSA_TAG_DSA
2525
config NET_DSA_TAG_EDSA
2626
bool
2727

28+
config NET_DSA_TAG_KSZ
29+
bool
30+
2831
config NET_DSA_TAG_LAN9303
2932
bool
3033

net/dsa/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dsa_core-y += dsa.o dsa2.o legacy.o port.o slave.o switch.o
66
dsa_core-$(CONFIG_NET_DSA_TAG_BRCM) += tag_brcm.o
77
dsa_core-$(CONFIG_NET_DSA_TAG_DSA) += tag_dsa.o
88
dsa_core-$(CONFIG_NET_DSA_TAG_EDSA) += tag_edsa.o
9+
dsa_core-$(CONFIG_NET_DSA_TAG_KSZ) += tag_ksz.o
910
dsa_core-$(CONFIG_NET_DSA_TAG_LAN9303) += tag_lan9303.o
1011
dsa_core-$(CONFIG_NET_DSA_TAG_MTK) += tag_mtk.o
1112
dsa_core-$(CONFIG_NET_DSA_TAG_QCA) += tag_qca.o

net/dsa/dsa.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
4949
#ifdef CONFIG_NET_DSA_TAG_EDSA
5050
[DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
5151
#endif
52+
#ifdef CONFIG_NET_DSA_TAG_KSZ
53+
[DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
54+
#endif
5255
#ifdef CONFIG_NET_DSA_TAG_LAN9303
5356
[DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
5457
#endif

net/dsa/dsa_priv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ extern const struct dsa_device_ops dsa_netdev_ops;
167167
/* tag_edsa.c */
168168
extern const struct dsa_device_ops edsa_netdev_ops;
169169

170+
/* tag_ksz.c */
171+
extern const struct dsa_device_ops ksz_netdev_ops;
172+
170173
/* tag_lan9303.c */
171174
extern const struct dsa_device_ops lan9303_netdev_ops;
172175

net/dsa/tag_ksz.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* net/dsa/tag_ksz.c - Microchip KSZ Switch tag format handling
3+
* Copyright (c) 2017 Microchip Technology
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*/
10+
11+
#include <linux/etherdevice.h>
12+
#include <linux/list.h>
13+
#include <linux/slab.h>
14+
#include <net/dsa.h>
15+
#include "dsa_priv.h"
16+
17+
/* For Ingress (Host -> KSZ), 2 bytes are added before FCS.
18+
* ---------------------------------------------------------------------------
19+
* DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
20+
* ---------------------------------------------------------------------------
21+
* tag0 : Prioritization (not used now)
22+
* tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
23+
*
24+
* For Egress (KSZ -> Host), 1 byte is added before FCS.
25+
* ---------------------------------------------------------------------------
26+
* DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
27+
* ---------------------------------------------------------------------------
28+
* tag0 : zero-based value represents port
29+
* (eg, 0x00=port1, 0x02=port3, 0x06=port7)
30+
*/
31+
32+
#define KSZ_INGRESS_TAG_LEN 2
33+
#define KSZ_EGRESS_TAG_LEN 1
34+
35+
static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev)
36+
{
37+
struct dsa_slave_priv *p = netdev_priv(dev);
38+
struct sk_buff *nskb;
39+
int padlen;
40+
u8 *tag;
41+
42+
padlen = (skb->len >= ETH_ZLEN) ? 0 : ETH_ZLEN - skb->len;
43+
44+
if (skb_tailroom(skb) >= padlen + KSZ_INGRESS_TAG_LEN) {
45+
nskb = skb;
46+
} else {
47+
nskb = alloc_skb(NET_IP_ALIGN + skb->len +
48+
padlen + KSZ_INGRESS_TAG_LEN, GFP_ATOMIC);
49+
if (!nskb) {
50+
kfree_skb(skb);
51+
return NULL;
52+
}
53+
skb_reserve(nskb, NET_IP_ALIGN);
54+
55+
skb_reset_mac_header(nskb);
56+
skb_set_network_header(nskb,
57+
skb_network_header(skb) - skb->head);
58+
skb_set_transport_header(nskb,
59+
skb_transport_header(skb) - skb->head);
60+
skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
61+
kfree_skb(skb);
62+
}
63+
64+
/* skb is freed when it fails */
65+
if (skb_put_padto(nskb, nskb->len + padlen))
66+
return NULL;
67+
68+
tag = skb_put(nskb, KSZ_INGRESS_TAG_LEN);
69+
tag[0] = 0;
70+
tag[1] = 1 << p->dp->index; /* destination port */
71+
72+
return nskb;
73+
}
74+
75+
struct sk_buff *ksz_rcv(struct sk_buff *skb, struct net_device *dev,
76+
struct packet_type *pt, struct net_device *orig_dev)
77+
{
78+
struct dsa_switch_tree *dst = dev->dsa_ptr;
79+
struct dsa_switch *ds;
80+
u8 *tag;
81+
int source_port;
82+
83+
ds = dst->cpu_dp->ds;
84+
85+
tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
86+
87+
source_port = tag[0] & 7;
88+
if (source_port >= ds->num_ports || !ds->ports[source_port].netdev)
89+
return NULL;
90+
91+
pskb_trim_rcsum(skb, skb->len - KSZ_EGRESS_TAG_LEN);
92+
93+
skb->dev = ds->ports[source_port].netdev;
94+
95+
return skb;
96+
}
97+
98+
const struct dsa_device_ops ksz_netdev_ops = {
99+
.xmit = ksz_xmit,
100+
.rcv = ksz_rcv,
101+
};

0 commit comments

Comments
 (0)