| 1 | |
| 2 | |
| 3 | * Copyright 2025-2026 NXP
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/dsa/tag_netc.h>
|
| 7 |
|
| 8 | #include "tag.h"
|
| 9 |
|
| 10 | #define NETC_NAME "nxp_netc"
|
| 11 |
|
| 12 |
|
| 13 | #define NETC_TAG_FORWARD 0
|
| 14 |
|
| 15 |
|
| 16 | #define NETC_TAG_TO_PORT 1
|
| 17 |
|
| 18 | #define NETC_TAG_TP_SUBTYPE0 0
|
| 19 |
|
| 20 |
|
| 21 | #define NETC_TAG_TO_HOST 2
|
| 22 |
|
| 23 | #define NETC_TAG_TH_SUBTYPE0 0
|
| 24 |
|
| 25 | #define NETC_TAG_TH_SUBTYPE1 1
|
| 26 |
|
| 27 | #define NETC_TAG_TH_SUBTYPE2 2
|
| 28 |
|
| 29 |
|
| 30 | #define NETC_TAG_FORWARD_LEN 6
|
| 31 | #define NETC_TAG_TP_SUBTYPE0_LEN 6
|
| 32 | #define NETC_TAG_TH_SUBTYPE0_LEN 6
|
| 33 | #define NETC_TAG_TH_SUBTYPE1_LEN 14
|
| 34 | #define NETC_TAG_TH_SUBTYPE2_LEN 14
|
| 35 | #define NETC_TAG_CMN_LEN 5
|
| 36 |
|
| 37 | #define NETC_TAG_SUBTYPE GENMASK(3, 0)
|
| 38 | #define NETC_TAG_TYPE GENMASK(7, 4)
|
| 39 | #define NETC_TAG_QV BIT(0)
|
| 40 | #define NETC_TAG_IPV GENMASK(4, 2)
|
| 41 | #define NETC_TAG_SWITCH GENMASK(2, 0)
|
| 42 | #define NETC_TAG_PORT GENMASK(7, 3)
|
| 43 |
|
| 44 | struct netc_tag_cmn {
|
| 45 | __be16 tpid;
|
| 46 | u8 type;
|
| 47 | u8 qos;
|
| 48 | u8 switch_port;
|
| 49 | } __packed;
|
| 50 |
|
| 51 | static void netc_fill_common_tag(struct netc_tag_cmn *tag, u8 type,
|
| 52 | u8 subtype, u8 sw_id, u8 port, u8 ipv)
|
| 53 | {
|
| 54 | tag->tpid = htons(ETH_P_NXP_NETC);
|
| 55 | tag->type = FIELD_PREP(NETC_TAG_TYPE, type) |
|
| 56 | FIELD_PREP(NETC_TAG_SUBTYPE, subtype);
|
| 57 | tag->qos = NETC_TAG_QV | FIELD_PREP(NETC_TAG_IPV, ipv);
|
| 58 | tag->switch_port = FIELD_PREP(NETC_TAG_SWITCH, sw_id) |
|
| 59 | FIELD_PREP(NETC_TAG_PORT, port);
|
| 60 | }
|
| 61 |
|
| 62 | static void *netc_fill_common_tp_tag(struct sk_buff *skb,
|
| 63 | struct net_device *ndev,
|
| 64 | u8 subtype, int tag_len)
|
| 65 | {
|
| 66 | struct dsa_port *dp = dsa_user_to_port(ndev);
|
| 67 | u16 queue = skb_get_queue_mapping(skb);
|
| 68 | s8 ipv = netdev_txq_to_tc(ndev, queue);
|
| 69 | void *tag;
|
| 70 |
|
| 71 | if (unlikely(ipv < 0))
|
| 72 | ipv = 0;
|
| 73 |
|
| 74 | skb_push(skb, tag_len);
|
| 75 | dsa_alloc_etype_header(skb, tag_len);
|
| 76 |
|
| 77 | tag = dsa_etype_header_pos_tx(skb);
|
| 78 | memset(tag + NETC_TAG_CMN_LEN, 0, tag_len - NETC_TAG_CMN_LEN);
|
| 79 | |
| 80 | * is used to specify the switch ID (thus the hardware switch ID and
|
| 81 | * the software switch ID are consistent), its range is 1 ~ 7. The
|
| 82 | * NETC switch driver will check this value, and if it is invalid,
|
| 83 | * the switch driver will fail the probe.
|
| 84 | * In addition, according to the nxp,netc-switch.yaml doc, the port
|
| 85 | * index will not be greater than 0xf.
|
| 86 |
|
| 87 | netc_fill_common_tag(tag, NETC_TAG_TO_PORT, subtype,
|
| 88 | dp->ds->index, dp->index, ipv);
|
| 89 |
|
| 90 | return tag;
|
| 91 | }
|
| 92 |
|
| 93 | static void netc_fill_tp_tag_subtype0(struct sk_buff *skb,
|
| 94 | struct net_device *ndev)
|
| 95 | {
|
| 96 | netc_fill_common_tp_tag(skb, ndev, NETC_TAG_TP_SUBTYPE0,
|
| 97 | NETC_TAG_TP_SUBTYPE0_LEN);
|
| 98 | }
|
| 99 |
|
| 100 |
|
| 101 | static struct sk_buff *netc_xmit(struct sk_buff *skb,
|
| 102 | struct net_device *ndev)
|
| 103 | {
|
| 104 | netc_fill_tp_tag_subtype0(skb, ndev);
|
| 105 |
|
| 106 | return skb;
|
| 107 | }
|
| 108 |
|
| 109 | static int netc_get_rx_tag_len(int type, int subtype)
|
| 110 | {
|
| 111 | |
| 112 | * NETC_TAG_TO_PORT is a TX switch tag that does not exist in RX.
|
| 113 |
|
| 114 | if (type == NETC_TAG_TO_HOST) {
|
| 115 | if (subtype == NETC_TAG_TH_SUBTYPE1)
|
| 116 | return NETC_TAG_TH_SUBTYPE1_LEN;
|
| 117 | else if (subtype == NETC_TAG_TH_SUBTYPE2)
|
| 118 | return NETC_TAG_TH_SUBTYPE2_LEN;
|
| 119 | else
|
| 120 | return NETC_TAG_TH_SUBTYPE0_LEN;
|
| 121 | }
|
| 122 |
|
| 123 | return NETC_TAG_FORWARD_LEN;
|
| 124 | }
|
| 125 |
|
| 126 | static struct sk_buff *netc_rcv(struct sk_buff *skb,
|
| 127 | struct net_device *ndev)
|
| 128 | {
|
| 129 | struct netc_tag_cmn *tag_cmn;
|
| 130 | int tag_len, sw_id, port;
|
| 131 | int type, subtype;
|
| 132 |
|
| 133 | if (unlikely(!pskb_may_pull(skb, NETC_TAG_MAX_LEN)))
|
| 134 | goto err_free_skb;
|
| 135 |
|
| 136 | tag_cmn = dsa_etype_header_pos_rx(skb);
|
| 137 | if (ntohs(tag_cmn->tpid) != ETH_P_NXP_NETC) {
|
| 138 | dev_warn_ratelimited(&ndev->dev, "Unknown TPID 0x%04x\n",
|
| 139 | ntohs(tag_cmn->tpid));
|
| 140 | goto err_free_skb;
|
| 141 | }
|
| 142 |
|
| 143 | if (tag_cmn->qos & NETC_TAG_QV)
|
| 144 | skb->priority = FIELD_GET(NETC_TAG_IPV, tag_cmn->qos);
|
| 145 |
|
| 146 | sw_id = FIELD_GET(NETC_TAG_SWITCH, tag_cmn->switch_port);
|
| 147 |
|
| 148 | if (!sw_id) {
|
| 149 | dev_warn_ratelimited(&ndev->dev,
|
| 150 | "VEPA switch ID is not supported yet\n");
|
| 151 | goto err_free_skb;
|
| 152 | }
|
| 153 |
|
| 154 | port = FIELD_GET(NETC_TAG_PORT, tag_cmn->switch_port);
|
| 155 | skb->dev = dsa_conduit_find_user(ndev, sw_id, port);
|
| 156 | if (!skb->dev)
|
| 157 | goto err_free_skb;
|
| 158 |
|
| 159 | type = FIELD_GET(NETC_TAG_TYPE, tag_cmn->type);
|
| 160 | subtype = FIELD_GET(NETC_TAG_SUBTYPE, tag_cmn->type);
|
| 161 | if (type == NETC_TAG_FORWARD) {
|
| 162 | dsa_default_offload_fwd_mark(skb);
|
| 163 | } else if (type == NETC_TAG_TO_HOST) {
|
| 164 |
|
| 165 | if (subtype != NETC_TAG_TH_SUBTYPE0)
|
| 166 | goto err_free_skb;
|
| 167 | } else {
|
| 168 | dev_warn_ratelimited(&ndev->dev,
|
| 169 | "Unexpected tag type %d\n", type);
|
| 170 | goto err_free_skb;
|
| 171 | }
|
| 172 |
|
| 173 |
|
| 174 | tag_len = netc_get_rx_tag_len(type, subtype);
|
| 175 | skb_pull_rcsum(skb, tag_len);
|
| 176 | dsa_strip_etype_header(skb, tag_len);
|
| 177 |
|
| 178 | return skb;
|
| 179 |
|
| 180 | err_free_skb:
|
| 181 | kfree_skb(skb);
|
| 182 | return NULL;
|
| 183 | }
|
| 184 |
|
| 185 | static void netc_flow_dissect(const struct sk_buff *skb, __be16 *proto,
|
| 186 | int *offset)
|
| 187 | {
|
| 188 | struct netc_tag_cmn *tag_cmn = (struct netc_tag_cmn *)(skb->data - 2);
|
| 189 | int subtype = FIELD_GET(NETC_TAG_SUBTYPE, tag_cmn->type);
|
| 190 | int type = FIELD_GET(NETC_TAG_TYPE, tag_cmn->type);
|
| 191 | int tag_len = netc_get_rx_tag_len(type, subtype);
|
| 192 |
|
| 193 | |
| 194 | * and the frame is received by the ENETC driver. From the hardware
|
| 195 | * perspective, the receive buffer of RX BD is at least 128 bytes,
|
| 196 | * so the switch tag header is guaranteed to be in the linear region
|
| 197 | * of the skb.
|
| 198 |
|
| 199 | *offset = tag_len;
|
| 200 | *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
|
| 201 | }
|
| 202 |
|
| 203 | static const struct dsa_device_ops netc_netdev_ops = {
|
| 204 | .name = NETC_NAME,
|
| 205 | .proto = DSA_TAG_PROTO_NETC,
|
| 206 | .xmit = netc_xmit,
|
| 207 | .rcv = netc_rcv,
|
| 208 | .needed_headroom = NETC_TAG_MAX_LEN,
|
| 209 | .flow_dissect = netc_flow_dissect,
|
| 210 | };
|
| 211 |
|
| 212 | MODULE_DESCRIPTION("DSA tag driver for NXP NETC switch family");
|
| 213 | MODULE_LICENSE("GPL");
|
| 214 |
|
| 215 | MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_NETC, NETC_NAME);
|
| 216 | module_dsa_tag_driver(netc_netdev_ops);
|