Skip to content

Commit 5ec0588

Browse files
committed
net: dsa: add NXP NETC tag protocol driver
Added NXP NETC tag protocol driver. Signed-off-by: Yangbo Lu <[email protected]>
1 parent 1ed3be9 commit 5ec0588

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

subsys/net/l2/ethernet/dsa/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ zephyr_library_sources(dsa_core.c)
88
zephyr_library_sources(dsa_port.c)
99
zephyr_library_sources(dsa_user.c)
1010
zephyr_library_sources(dsa_tag.c)
11+
zephyr_library_sources_ifdef(CONFIG_DSA_TAG_PROTOCOL_NETC dsa_tag_netc.c)
1112
endif()

subsys/net/l2/ethernet/dsa/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ config DSA_TAG_SIZE
3333
help
3434
Set the DSA tag length in bytes.
3535

36+
config DSA_TAG_PROTOCOL_NETC
37+
bool "Tag protocol - NETC"
38+
default y if $(dt_node_str_prop_equals,$(dt_nodelabel_path,switch_port0),tag-protocol,netc) \
39+
|| $(dt_node_str_prop_equals,$(dt_nodelabel_path,switch_port1),tag-protocol,netc) \
40+
|| $(dt_node_str_prop_equals,$(dt_nodelabel_path,switch_port2),tag-protocol,netc) \
41+
|| $(dt_node_str_prop_equals,$(dt_nodelabel_path,switch_port3),tag-protocol,netc) \
42+
|| $(dt_node_str_prop_equals,$(dt_nodelabel_path,switch_port4),tag-protocol,netc)
43+
help
44+
NXP NETC tag protocol.
45+
3646
module = NET_DSA
3747
module-dep = NET_LOG
3848
module-str = Log level for DSA

subsys/net/l2/ethernet/dsa/dsa_tag.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef ZEPHYR_SUBSYS_DSA_TAG_PRIV_H_
77
#define ZEPHYR_SUBSYS_DSA_TAG_PRIV_H_
88

9+
#include "dsa_tag_netc.h"
10+
911
struct net_if *dsa_tag_recv(struct net_if *iface, struct net_pkt *pkt);
1012
struct net_pkt *dsa_tag_xmit(struct net_if *iface, struct net_pkt *pkt);
1113

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/logging/log.h>
8+
LOG_MODULE_REGISTER(net_dsa_tag_netc, CONFIG_NET_DSA_LOG_LEVEL);
9+
10+
#include <zephyr/net/ethernet.h>
11+
#include <zephyr/net/dsa_core.h>
12+
#include "dsa_tag_netc.h"
13+
#include "fsl_netc_tag.h"
14+
15+
/* tag is inserted after DMAC/SMAC fields */
16+
struct dsa_tag_netc_to_host_header {
17+
uint8_t dmac[NET_ETH_ADDR_LEN];
18+
uint8_t smac[NET_ETH_ADDR_LEN];
19+
netc_swt_tag_host_t tag;
20+
};
21+
22+
struct dsa_tag_netc_to_port_header {
23+
uint8_t dmac[NET_ETH_ADDR_LEN];
24+
uint8_t smac[NET_ETH_ADDR_LEN];
25+
netc_swt_tag_port_no_ts_t tag;
26+
};
27+
28+
struct net_if *dsa_tag_netc_recv(struct net_if *iface, struct net_pkt *pkt)
29+
{
30+
struct ethernet_context *eth_ctx = net_if_l2_data(iface);
31+
uint16_t header_len = sizeof(struct dsa_tag_netc_to_host_header);
32+
struct dsa_tag_netc_to_host_header *header;
33+
struct net_if *iface_dst = iface;
34+
uint8_t *ptr;
35+
36+
if (pkt->frags->len < header_len) {
37+
LOG_ERR("tag len error");
38+
return iface_dst;
39+
}
40+
41+
/* redirect to user port */
42+
header = (struct dsa_tag_netc_to_host_header *)pkt->frags->data;
43+
iface_dst = eth_ctx->dsa_switch_ctx->iface_user[header->tag.comTag.port];
44+
45+
/* drop tag */
46+
ptr = net_buf_pull(pkt->frags, sizeof(netc_swt_tag_host_t));
47+
for (int i = 0; i < (NET_ETH_ADDR_LEN * 2); i++) {
48+
ptr[i] = *(uint8_t *)((uintptr_t)header + i);
49+
}
50+
51+
return iface_dst;
52+
}
53+
54+
struct net_pkt *dsa_tag_netc_xmit(struct net_if *iface, struct net_pkt *pkt)
55+
{
56+
const struct device *dev = net_if_get_device(iface);
57+
struct dsa_port_config *cfg = (struct dsa_port_config *)dev->config;
58+
struct dsa_tag_netc_to_port_header *header;
59+
struct net_buf *buf;
60+
61+
size_t buf_len = pkt->frags->len + sizeof(netc_swt_tag_port_no_ts_t);
62+
63+
/* allocate new net_buf */
64+
buf = net_buf_alloc_len(net_buf_pool_get(pkt->buffer->pool_id), buf_len, K_NO_WAIT);
65+
if (!buf) {
66+
LOG_ERR("cannot allocate new data buffer");
67+
return NULL;
68+
}
69+
70+
/* init buf */
71+
buf->len = buf_len;
72+
memset(buf->data, 0, buf->len);
73+
74+
/* fill header */
75+
header = (struct dsa_tag_netc_to_port_header *)buf->data;
76+
memcpy(header, pkt->frags->data, (NET_ETH_ADDR_LEN * 2));
77+
header->tag.comTag.tpid = NETC_SWITCH_DEFAULT_ETHER_TYPE;
78+
header->tag.comTag.subType = kNETC_TagToPortNoTs;
79+
header->tag.comTag.type = kNETC_TagToPort;
80+
header->tag.comTag.swtId = 1,
81+
header->tag.comTag.port = cfg->port_idx;
82+
83+
/* copy the rest data */
84+
memcpy((void *)((uintptr_t)buf->data + sizeof(*header)),
85+
(void *)((uintptr_t)pkt->frags->data + (2 * NET_ETH_ADDR_LEN)),
86+
pkt->frags->len - (2 * NET_ETH_ADDR_LEN));
87+
88+
/* insert the new frag */
89+
net_buf_frag_insert(pkt->frags, buf);
90+
91+
/* drop the old frag */
92+
pkt->frags = buf;
93+
net_pkt_cursor_init(pkt);
94+
95+
return pkt;
96+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef ZEPHYR_SUBSYS_DSA_TAG_NETC_H_
7+
#define ZEPHYR_SUBSYS_DSA_TAG_NETC_H_
8+
9+
#ifdef CONFIG_DSA_TAG_PROTOCOL_NETC
10+
struct net_if *dsa_tag_netc_recv(struct net_if *iface, struct net_pkt *pkt);
11+
struct net_pkt *dsa_tag_netc_xmit(struct net_if *iface, struct net_pkt *pkt);
12+
#else
13+
static inline struct net_if *dsa_tag_netc_recv(struct net_if *iface, struct net_pkt *pkt)
14+
{
15+
return iface;
16+
}
17+
18+
static inline struct net_pkt *dsa_tag_netc_xmit(struct net_if *iface, struct net_pkt *pkt)
19+
{
20+
return pkt;
21+
}
22+
#endif /* CONFIG_DSA_TAG_PROTOCOL_NETC */
23+
24+
#endif /* ZEPHYR_SUBSYS_DSA_TAG_NETC_H_ */

0 commit comments

Comments
 (0)