1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 NXP Semiconductors
4 #include <soc/mscc/ocelot.h>
5 #include <linux/packing.h>
8 /* The CPU injection header and the CPU extraction header can have 3 types of
9 * prefixes: long, short and no prefix. The format of the header itself is the
10 * same in all 3 cases.
12 * Extraction with long prefix:
14 * +-------------------+-------------------+------+------+------------+-------+
15 * | ff:ff:ff:ff:ff:ff | ff:ff:ff:ff:ff:ff | 8880 | 000a | extraction | frame |
16 * | | | | | header | |
17 * +-------------------+-------------------+------+------+------------+-------+
18 * 48 bits 48 bits 16 bits 16 bits 128 bits
20 * Extraction with short prefix:
22 * +------+------+------------+-------+
23 * | 8880 | 000a | extraction | frame |
25 * +------+------+------------+-------+
26 * 16 bits 16 bits 128 bits
28 * Extraction with no prefix:
30 * +------------+-------+
31 * | extraction | frame |
33 * +------------+-------+
37 * Injection with long prefix:
39 * +-------------------+-------------------+------+------+------------+-------+
40 * | any dmac | any smac | 8880 | 000a | injection | frame |
41 * | | | | | header | |
42 * +-------------------+-------------------+------+------+------------+-------+
43 * 48 bits 48 bits 16 bits 16 bits 128 bits
45 * Injection with short prefix:
47 * +------+------+------------+-------+
48 * | 8880 | 000a | injection | frame |
50 * +------+------+------------+-------+
51 * 16 bits 16 bits 128 bits
53 * Injection with no prefix:
55 * +------------+-------+
56 * | injection | frame |
58 * +------------+-------+
61 * The injection header looks like this (network byte order, bit 127
62 * is part of lowest address byte in memory, bit 0 is part of highest
65 * +------+------+------+------+------+------+------+------+
66 * 127:120 |BYPASS| MASQ | MASQ_PORT |REW_OP|REW_OP|
67 * +------+------+------+------+------+------+------+------+
69 * +------+------+------+------+------+------+------+------+
71 * +------+------+------+------+------+------+------+------+
73 * +------+------+------+------+------+------+------+------+
75 * +------+------+------+------+------+------+------+------+
77 * +------+------+------+------+------+------+------+------+
79 * +------+------+------+------+------+------+------+------+
80 * 71: 64 | RSV | DEST |
81 * +------+------+------+------+------+------+------+------+
83 * +------+------+------+------+------+------+------+------+
85 * +------+------+------+------+------+------+------+------+
86 * 47: 40 | RSV | SRC_PORT | RSV |TFRM_TIMER|
87 * +------+------+------+------+------+------+------+------+
88 * 39: 32 | TFRM_TIMER | RSV |
89 * +------+------+------+------+------+------+------+------+
90 * 31: 24 | RSV | DP | POP_CNT | CPUQ |
91 * +------+------+------+------+------+------+------+------+
92 * 23: 16 | CPUQ | QOS_CLASS |TAG_TYPE|
93 * +------+------+------+------+------+------+------+------+
94 * 15: 8 | PCP | DEI | VID |
95 * +------+------+------+------+------+------+------+------+
97 * +------+------+------+------+------+------+------+------+
99 * And the extraction header looks like this:
101 * +------+------+------+------+------+------+------+------+
102 * 127:120 | RSV | REW_OP |
103 * +------+------+------+------+------+------+------+------+
104 * 119:112 | REW_OP | REW_VAL |
105 * +------+------+------+------+------+------+------+------+
106 * 111:104 | REW_VAL |
107 * +------+------+------+------+------+------+------+------+
108 * 103: 96 | REW_VAL |
109 * +------+------+------+------+------+------+------+------+
111 * +------+------+------+------+------+------+------+------+
112 * 87: 80 | REW_VAL | LLEN |
113 * +------+------+------+------+------+------+------+------+
114 * 79: 72 | LLEN | WLEN |
115 * +------+------+------+------+------+------+------+------+
116 * 71: 64 | WLEN | RSV |
117 * +------+------+------+------+------+------+------+------+
119 * +------+------+------+------+------+------+------+------+
121 * +------+------+------+------+------+------+------+------+
122 * 47: 40 | RSV | SRC_PORT | ACL_ID |
123 * +------+------+------+------+------+------+------+------+
124 * 39: 32 | ACL_ID | RSV | SFLOW_ID |
125 * +------+------+------+------+------+------+------+------+
126 * 31: 24 |ACL_HIT| DP | LRN_FLAGS | CPUQ |
127 * +------+------+------+------+------+------+------+------+
128 * 23: 16 | CPUQ | QOS_CLASS |TAG_TYPE|
129 * +------+------+------+------+------+------+------+------+
130 * 15: 8 | PCP | DEI | VID |
131 * +------+------+------+------+------+------+------+------+
133 * +------+------+------+------+------+------+------+------+
136 static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
137 struct net_device *netdev)
139 struct dsa_port *dp = dsa_slave_to_port(netdev);
140 struct dsa_switch *ds = dp->ds;
141 struct ocelot *ocelot = ds->priv;
142 struct ocelot_port *ocelot_port;
143 u64 qos_class, rew_op;
146 if (unlikely(skb_cow_head(skb, OCELOT_TAG_LEN) < 0)) {
147 netdev_err(netdev, "Cannot make room for tag.\n");
151 ocelot_port = ocelot->ports[dp->index];
153 injection = skb_push(skb, OCELOT_TAG_LEN);
155 memcpy(injection, ocelot_port->xmit_template, OCELOT_TAG_LEN);
156 /* Fix up the fields which are not statically determined
159 qos_class = skb->priority;
160 packing(injection, &qos_class, 19, 17, OCELOT_TAG_LEN, PACK, 0);
162 if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
163 struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
165 rew_op = ocelot_port->ptp_cmd;
166 /* Retrieve timestamp ID populated inside skb->cb[0] of the
167 * clone by ocelot_port_add_txtstamp_skb
169 if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
170 rew_op |= clone->cb[0] << 3;
172 packing(injection, &rew_op, 125, 117, OCELOT_TAG_LEN, PACK, 0);
178 static struct sk_buff *ocelot_rcv(struct sk_buff *skb,
179 struct net_device *netdev,
180 struct packet_type *pt)
182 u64 src_port, qos_class;
183 u8 *start = skb->data;
186 /* Revert skb->data by the amount consumed by the DSA master,
187 * so it points to the beginning of the frame.
189 skb_push(skb, ETH_HLEN);
190 /* We don't care about the long prefix, it is just for easy entrance
191 * into the DSA master's RX filter. Discard it now by moving it into
194 skb_pull(skb, OCELOT_LONG_PREFIX_LEN);
195 /* And skb->data now points to the extraction frame header.
196 * Keep a pointer to it.
198 extraction = skb->data;
199 /* Now the EFH is part of the headroom as well */
200 skb_pull(skb, OCELOT_TAG_LEN);
201 /* Reset the pointer to the real MAC header */
202 skb_reset_mac_header(skb);
203 skb_reset_mac_len(skb);
204 /* And move skb->data to the correct location again */
205 skb_pull(skb, ETH_HLEN);
207 /* Remove from inet csum the extraction header */
208 skb_postpull_rcsum(skb, start, OCELOT_LONG_PREFIX_LEN + OCELOT_TAG_LEN);
210 packing(extraction, &src_port, 46, 43, OCELOT_TAG_LEN, UNPACK, 0);
211 packing(extraction, &qos_class, 19, 17, OCELOT_TAG_LEN, UNPACK, 0);
213 skb->dev = dsa_master_find_slave(netdev, 0, src_port);
215 /* The switch will reflect back some frames sent through
216 * sockets opened on the bare DSA master. These will come back
217 * with src_port equal to the index of the CPU port, for which
218 * there is no slave registered. So don't print any error
219 * message here (ignore and drop those frames).
223 skb->offload_fwd_mark = 1;
224 skb->priority = qos_class;
229 static const struct dsa_device_ops ocelot_netdev_ops = {
231 .proto = DSA_TAG_PROTO_OCELOT,
234 .overhead = OCELOT_TAG_LEN + OCELOT_LONG_PREFIX_LEN,
237 MODULE_LICENSE("GPL v2");
238 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_OCELOT);
240 module_dsa_tag_driver(ocelot_netdev_ops);