[PATCH net-next v2 3/5] net: dsa: tag_ks8995: Add the KS8995 tag handling
From: Linus Walleij <linusw@kernel.org>
Date: 2026-05-22 13:21:53
Also in:
netdev
Subsystem:
networking [dsa], networking [general], the rest · Maintainers:
Andrew Lunn, Vladimir Oltean, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds
The KS8995 100Mbit switch can do proper DSA per-port tagging with the proper set-up. This adds the code to handle ingress and egress KS8995 tags. The tag is a modified 0x8100 ethertype tag where a bit in the last nybble is set for each target port. Signed-off-by: Linus Walleij <linusw@kernel.org> --- include/net/dsa.h | 2 + net/dsa/Kconfig | 6 +++ net/dsa/Makefile | 1 + net/dsa/tag_ks8995.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+)
diff --git a/include/net/dsa.h b/include/net/dsa.h
index 4cc67469cf2e..91e31e293ff3 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h@@ -58,6 +58,7 @@ struct tc_action; #define DSA_TAG_PROTO_YT921X_VALUE 30 #define DSA_TAG_PROTO_MXL_GSW1XX_VALUE 31 #define DSA_TAG_PROTO_MXL862_VALUE 32 +#define DSA_TAG_PROTO_KS8995_VALUE 33 enum dsa_tag_protocol { DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
@@ -93,6 +94,7 @@ enum dsa_tag_protocol { DSA_TAG_PROTO_YT921X = DSA_TAG_PROTO_YT921X_VALUE, DSA_TAG_PROTO_MXL_GSW1XX = DSA_TAG_PROTO_MXL_GSW1XX_VALUE, DSA_TAG_PROTO_MXL862 = DSA_TAG_PROTO_MXL862_VALUE, + DSA_TAG_PROTO_KS8995 = DSA_TAG_PROTO_KS8995_VALUE, }; struct dsa_switch;
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index 5ed8c704636d..11fd72891759 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig@@ -119,6 +119,12 @@ config NET_DSA_TAG_MXL_GSW1XX Say Y or M if you want to enable support for tagging frames for MaxLinear GSW1xx switches. +config NET_DSA_TAG_KS8995 + tristate "Tag driver for Micrel KS8995 switch" + help + Say Y if you want to enable support for tagging frames for the + Micrel KS8995 switch. + config NET_DSA_TAG_KSZ tristate "Tag driver for Microchip 8795/937x/9477/9893 families of switches" help
diff --git a/net/dsa/Makefile b/net/dsa/Makefile
index bf7247759a64..8ff313f1d329 100644
--- a/net/dsa/Makefile
+++ b/net/dsa/Makefile@@ -25,6 +25,7 @@ obj-$(CONFIG_NET_DSA_TAG_BRCM_COMMON) += tag_brcm.o obj-$(CONFIG_NET_DSA_TAG_DSA_COMMON) += tag_dsa.o obj-$(CONFIG_NET_DSA_TAG_GSWIP) += tag_gswip.o obj-$(CONFIG_NET_DSA_TAG_HELLCREEK) += tag_hellcreek.o +obj-$(CONFIG_NET_DSA_TAG_KS8995) += tag_ks8995.o obj-$(CONFIG_NET_DSA_TAG_KSZ) += tag_ksz.o obj-$(CONFIG_NET_DSA_TAG_LAN9303) += tag_lan9303.o obj-$(CONFIG_NET_DSA_TAG_MTK) += tag_mtk.o
diff --git a/net/dsa/tag_ks8995.c b/net/dsa/tag_ks8995.c
new file mode 100644
index 000000000000..b3ffbd5e6393
--- /dev/null
+++ b/net/dsa/tag_ks8995.c@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2026 Linus Walleij <linusw@kernel.org> + */ +#include <linux/etherdevice.h> +#include <linux/log2.h> +#include <linux/list.h> +#include <linux/slab.h> + +#include "tag.h" + +/* The Micrel KS8995XA / Microchip KSZ8995XA Special Tag Packet ID (STPID) + * pushes its tag in a modified VLAN (802.1Q) tag. + * ----------------------------------------------------------- + * | MAC DA | MAC SA | 2 bytes tag | 2 bytes TCI | EtherType | + * ----------------------------------------------------------- + * The tag is: 0x8100 |= BIT(port), ports 0,1,2,3 + */ + +#define KS8995_NAME "ks8995" + +#define KS8995M_STPID_STD GENMASK(15, 4) +#define KS8995M_STPID_PORTMASK GENMASK(3, 0) +#define KS8995M_STPID(portmask) htons(ETH_P_8021Q | FIELD_PREP(KS8995M_STPID_PORTMASK, portmask)) + +static struct sk_buff *ks8995_xmit(struct sk_buff *skb, struct net_device *dev) +{ + struct vlan_ethhdr *hdr = vlan_eth_hdr(skb); + bool have_hwaccel_tag = false; + u16 tci = 0, portmask; + + /* Prepare the special KS8995 tags */ + portmask = dsa_xmit_port_mask(skb, dev); + + if (skb_vlan_tag_present(skb) && skb->vlan_proto == htons(ETH_P_8021Q)) { + tci = skb_vlan_tag_get(skb); + __vlan_hwaccel_clear_tag(skb); + have_hwaccel_tag = true; + } + + if (have_hwaccel_tag || hdr->h_vlan_proto != htons(ETH_P_8021Q)) { + skb = vlan_insert_tag(skb, KS8995M_STPID(portmask), tci); + if (!skb) + return NULL; + hdr = vlan_eth_hdr(skb); + netdev_dbg(dev, "%s: inserted VLAN TAG %04x TCI %04x\n", + __func__, hdr->h_vlan_proto, hdr->h_vlan_TCI); + } else { + /* VLAN tag already exists in skb head, modify it in place */ + hdr = vlan_eth_hdr(skb); + hdr->h_vlan_proto = KS8995M_STPID(portmask); + netdev_dbg(dev, "%s: modified VLAN TAG %04x\n", + __func__, hdr->h_vlan_proto); + } + + return skb; +} + +static struct sk_buff *ks8995_rcv(struct sk_buff *skb, struct net_device *dev) +{ + int portmask; + u16 etype; + + /* We are expecting all received packets to have a mangled VLAN + * TPID, so drop anything else. Because of the non-standard TPID, + * don't even bother looking for a tag in the hwaccel area. + * + * We have to inspect the ethertype directly because skb->protocol + * will contain garbage. + */ + etype = ntohs(*(__be16 *)dsa_etype_header_pos_rx(skb)); + if ((etype & KS8995M_STPID_STD) != ETH_P_8021Q) { + netdev_info(dev, "%s: dropped ethertype 0x%04x\n", + __func__, etype); + return NULL; + } + netdev_dbg(dev, "%s: received ethertype %04x\n", + __func__, etype); + + /* Move the custom DSA+VLAN tag into the hwaccel area and strip + * it from the skb head + */ + skb = skb_vlan_untag(skb); + if (!skb) { + netdev_err(dev, "%s: unable to untag skb\n", __func__); + return NULL; + } + + portmask = FIELD_GET(KS8995M_STPID_PORTMASK, etype); + netdev_dbg(dev, "%s: etype %04x portmask %04x (%d)\n", + __func__, etype, portmask, ilog2(portmask)); + skb->dev = dsa_conduit_find_user(dev, 0, ilog2(portmask)); + if (!skb->dev) + return NULL; + + /* Preserve the VLAN tag if it contains a non-zero VID which is not + * identical to 0x001, or PCP, and restore its TPID to the standard + * value. + * + * If this is just an ordinary inbound package the datasheet claims + * it will "replace null VID with ingress port VID", which means + * VID set to 1: 0x8101 0001 for port 0 or 0x8102 0001 for port 1. + * So in the DSA driver we will set the default port VID to 0 so + * we can properly detect non-VLAN frames. + */ + if (!skb->vlan_tci) { + netdev_dbg(dev, "%s: clear VLAN tag from frame\n", __func__); + __vlan_hwaccel_clear_tag(skb); + } else { + skb->vlan_proto = htons(ETH_P_8021Q); + netdev_dbg(dev, "%s: vlan_tci = 0x%04x VLAN frame\n", + __func__, skb->vlan_tci); + } + + dsa_default_offload_fwd_mark(skb); + + return skb; +} + +static const struct dsa_device_ops ks8995_netdev_ops = { + .name = KS8995_NAME, + .proto = DSA_TAG_PROTO_KS8995, + .xmit = ks8995_xmit, + .rcv = ks8995_rcv, + .needed_headroom = VLAN_HLEN, +}; + +MODULE_DESCRIPTION("DSA tag driver for Micrel KS8995 family of switches"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KS8995, KS8995_NAME); + +module_dsa_tag_driver(ks8995_netdev_ops);
--
2.54.0