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, CONFIG_NET_DSA_LOG_LEVEL);
9 
10 #include <zephyr/net/ethernet.h>
11 #include <zephyr/net/dsa_core.h>
12 #include "dsa_tag.h"
13 
dsa_tag_recv(struct net_if * iface,struct net_pkt * pkt)14 struct net_if *dsa_tag_recv(struct net_if *iface, struct net_pkt *pkt)
15 {
16 	const struct ethernet_context *eth_ctx = net_if_l2_data(iface);
17 	const struct dsa_switch_context *dsa_switch_ctx = eth_ctx->dsa_switch_ctx;
18 
19 	/* For no tag type, use NULL recv to let host ethernet driver handle. */
20 	if (dsa_switch_ctx->dapi->recv == NULL) {
21 		return iface;
22 	}
23 
24 	return dsa_switch_ctx->dapi->recv(iface, pkt);
25 }
26 
dsa_tag_xmit(struct net_if * iface,struct net_pkt * pkt)27 struct net_pkt *dsa_tag_xmit(struct net_if *iface, struct net_pkt *pkt)
28 {
29 	const struct device *dev = net_if_get_device(iface);
30 	const struct dsa_switch_context *dsa_switch_ctx = dev->data;
31 
32 	/* For no tag type, use NULL xmit to let host ethernet driver handle. */
33 	if (dsa_switch_ctx->dapi->xmit == NULL) {
34 		/* Here utilize iface field to store origin user port iface.
35 		 * Host ethernet driver needs to handle then.
36 		 */
37 		pkt->iface = iface;
38 		return pkt;
39 	}
40 
41 	return dsa_switch_ctx->dapi->xmit(iface, pkt);
42 }
43 
dsa_tag_setup(const struct device * dev_cpu)44 void dsa_tag_setup(const struct device *dev_cpu)
45 {
46 	const struct dsa_port_config *cfg = dev_cpu->config;
47 	struct dsa_switch_context *dsa_switch_ctx = dev_cpu->data;
48 
49 	switch (cfg->tag_proto) {
50 #ifdef CONFIG_DSA_TAG_PROTOCOL_NETC
51 	case DSA_TAG_PROTO_NETC:
52 		dsa_switch_ctx->dapi->recv = dsa_tag_netc_recv;
53 		dsa_switch_ctx->dapi->xmit = dsa_tag_netc_xmit;
54 		break;
55 #endif
56 	case DSA_TAG_PROTO_NOTAG:
57 		dsa_switch_ctx->dapi->recv = NULL;
58 		dsa_switch_ctx->dapi->xmit = NULL;
59 		break;
60 	default:
61 		LOG_ERR("DSA tag protocol %d not supported", cfg->tag_proto);
62 	}
63 }
64