1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
4 */
5
6 #include <linux/if_ether.h>
7 #include <linux/rhashtable.h>
8 #include <linux/ip.h>
9 #include <linux/ipv6.h>
10 #include <net/flow_offload.h>
11 #include <net/pkt_cls.h>
12 #include <net/dsa.h>
13 #include "mtk_eth_soc.h"
14 #include "mtk_wed.h"
15
16 struct mtk_flow_data {
17 struct ethhdr eth;
18
19 union {
20 struct {
21 __be32 src_addr;
22 __be32 dst_addr;
23 } v4;
24
25 struct {
26 struct in6_addr src_addr;
27 struct in6_addr dst_addr;
28 } v6;
29 };
30
31 __be16 src_port;
32 __be16 dst_port;
33
34 u16 vlan_in;
35
36 struct {
37 struct {
38 u16 id;
39 __be16 proto;
40 } vlans[2];
41 u8 num;
42 } vlan;
43 struct {
44 u16 sid;
45 u8 num;
46 } pppoe;
47 };
48
49 static const struct rhashtable_params mtk_flow_ht_params = {
50 .head_offset = offsetof(struct mtk_flow_entry, node),
51 .key_offset = offsetof(struct mtk_flow_entry, cookie),
52 .key_len = sizeof(unsigned long),
53 .automatic_shrinking = true,
54 };
55
56 static int
mtk_flow_set_ipv4_addr(struct mtk_eth * eth,struct mtk_foe_entry * foe,struct mtk_flow_data * data,bool egress)57 mtk_flow_set_ipv4_addr(struct mtk_eth *eth, struct mtk_foe_entry *foe,
58 struct mtk_flow_data *data, bool egress)
59 {
60 return mtk_foe_entry_set_ipv4_tuple(eth, foe, egress,
61 data->v4.src_addr, data->src_port,
62 data->v4.dst_addr, data->dst_port);
63 }
64
65 static int
mtk_flow_set_ipv6_addr(struct mtk_eth * eth,struct mtk_foe_entry * foe,struct mtk_flow_data * data)66 mtk_flow_set_ipv6_addr(struct mtk_eth *eth, struct mtk_foe_entry *foe,
67 struct mtk_flow_data *data)
68 {
69 return mtk_foe_entry_set_ipv6_tuple(eth, foe,
70 data->v6.src_addr.s6_addr32, data->src_port,
71 data->v6.dst_addr.s6_addr32, data->dst_port);
72 }
73
74 static void
mtk_flow_offload_mangle_eth(const struct flow_action_entry * act,void * eth)75 mtk_flow_offload_mangle_eth(const struct flow_action_entry *act, void *eth)
76 {
77 void *dest = eth + act->mangle.offset;
78 const void *src = &act->mangle.val;
79
80 if (act->mangle.offset > 8)
81 return;
82
83 if (act->mangle.mask == 0xffff) {
84 src += 2;
85 dest += 2;
86 }
87
88 memcpy(dest, src, act->mangle.mask ? 2 : 4);
89 }
90
91 static int
mtk_flow_get_wdma_info(struct net_device * dev,const u8 * addr,struct mtk_wdma_info * info)92 mtk_flow_get_wdma_info(struct net_device *dev, const u8 *addr, struct mtk_wdma_info *info)
93 {
94 struct net_device_path_stack stack;
95 struct net_device_path *path;
96 int err;
97
98 if (!dev)
99 return -ENODEV;
100
101 if (!IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED))
102 return -1;
103
104 err = dev_fill_forward_path(dev, addr, &stack);
105 if (err)
106 return err;
107
108 path = &stack.path[stack.num_paths - 1];
109 if (path->type != DEV_PATH_MTK_WDMA)
110 return -1;
111
112 info->wdma_idx = path->mtk_wdma.wdma_idx;
113 info->queue = path->mtk_wdma.queue;
114 info->bss = path->mtk_wdma.bss;
115 info->wcid = path->mtk_wdma.wcid;
116
117 return 0;
118 }
119
120
121 static int
mtk_flow_mangle_ports(const struct flow_action_entry * act,struct mtk_flow_data * data)122 mtk_flow_mangle_ports(const struct flow_action_entry *act,
123 struct mtk_flow_data *data)
124 {
125 u32 val = ntohl(act->mangle.val);
126
127 switch (act->mangle.offset) {
128 case 0:
129 if (act->mangle.mask == ~htonl(0xffff))
130 data->dst_port = cpu_to_be16(val);
131 else
132 data->src_port = cpu_to_be16(val >> 16);
133 break;
134 case 2:
135 data->dst_port = cpu_to_be16(val);
136 break;
137 default:
138 return -EINVAL;
139 }
140
141 return 0;
142 }
143
144 static int
mtk_flow_mangle_ipv4(const struct flow_action_entry * act,struct mtk_flow_data * data)145 mtk_flow_mangle_ipv4(const struct flow_action_entry *act,
146 struct mtk_flow_data *data)
147 {
148 __be32 *dest;
149
150 switch (act->mangle.offset) {
151 case offsetof(struct iphdr, saddr):
152 dest = &data->v4.src_addr;
153 break;
154 case offsetof(struct iphdr, daddr):
155 dest = &data->v4.dst_addr;
156 break;
157 default:
158 return -EINVAL;
159 }
160
161 memcpy(dest, &act->mangle.val, sizeof(u32));
162
163 return 0;
164 }
165
166 static int
mtk_flow_get_dsa_port(struct net_device ** dev)167 mtk_flow_get_dsa_port(struct net_device **dev)
168 {
169 #if IS_ENABLED(CONFIG_NET_DSA)
170 struct dsa_port *dp;
171
172 dp = dsa_port_from_netdev(*dev);
173 if (IS_ERR(dp))
174 return -ENODEV;
175
176 if (dp->cpu_dp->tag_ops->proto != DSA_TAG_PROTO_MTK)
177 return -ENODEV;
178
179 *dev = dsa_port_to_master(dp);
180
181 return dp->index;
182 #else
183 return -ENODEV;
184 #endif
185 }
186
187 static int
mtk_flow_set_output_device(struct mtk_eth * eth,struct mtk_foe_entry * foe,struct net_device * dev,const u8 * dest_mac,int * wed_index)188 mtk_flow_set_output_device(struct mtk_eth *eth, struct mtk_foe_entry *foe,
189 struct net_device *dev, const u8 *dest_mac,
190 int *wed_index)
191 {
192 struct mtk_wdma_info info = {};
193 int pse_port, dsa_port, queue;
194
195 if (mtk_flow_get_wdma_info(dev, dest_mac, &info) == 0) {
196 mtk_foe_entry_set_wdma(eth, foe, info.wdma_idx, info.queue,
197 info.bss, info.wcid);
198 if (mtk_is_netsys_v2_or_greater(eth)) {
199 switch (info.wdma_idx) {
200 case 0:
201 pse_port = 8;
202 break;
203 case 1:
204 pse_port = 9;
205 break;
206 default:
207 return -EINVAL;
208 }
209 } else {
210 pse_port = 3;
211 }
212 *wed_index = info.wdma_idx;
213 goto out;
214 }
215
216 dsa_port = mtk_flow_get_dsa_port(&dev);
217
218 if (dev == eth->netdev[0])
219 pse_port = PSE_GDM1_PORT;
220 else if (dev == eth->netdev[1])
221 pse_port = PSE_GDM2_PORT;
222 else if (dev == eth->netdev[2])
223 pse_port = PSE_GDM3_PORT;
224 else
225 return -EOPNOTSUPP;
226
227 if (dsa_port >= 0) {
228 mtk_foe_entry_set_dsa(eth, foe, dsa_port);
229 queue = 3 + dsa_port;
230 } else {
231 queue = pse_port - 1;
232 }
233 mtk_foe_entry_set_queue(eth, foe, queue);
234
235 out:
236 mtk_foe_entry_set_pse_port(eth, foe, pse_port);
237
238 return 0;
239 }
240
241 static int
mtk_flow_offload_replace(struct mtk_eth * eth,struct flow_cls_offload * f,int ppe_index)242 mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f,
243 int ppe_index)
244 {
245 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
246 struct flow_action_entry *act;
247 struct mtk_flow_data data = {};
248 struct mtk_foe_entry foe;
249 struct net_device *odev = NULL;
250 struct mtk_flow_entry *entry;
251 int offload_type = 0;
252 int wed_index = -1;
253 u16 addr_type = 0;
254 u8 l4proto = 0;
255 int err = 0;
256 int i;
257
258 if (rhashtable_lookup(ð->flow_table, &f->cookie, mtk_flow_ht_params))
259 return -EEXIST;
260
261 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_META)) {
262 struct flow_match_meta match;
263
264 flow_rule_match_meta(rule, &match);
265 } else {
266 return -EOPNOTSUPP;
267 }
268
269 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
270 struct flow_match_control match;
271
272 flow_rule_match_control(rule, &match);
273 addr_type = match.key->addr_type;
274 } else {
275 return -EOPNOTSUPP;
276 }
277
278 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
279 struct flow_match_basic match;
280
281 flow_rule_match_basic(rule, &match);
282 l4proto = match.key->ip_proto;
283 } else {
284 return -EOPNOTSUPP;
285 }
286
287 switch (addr_type) {
288 case 0:
289 offload_type = MTK_PPE_PKT_TYPE_BRIDGE;
290 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
291 struct flow_match_eth_addrs match;
292
293 flow_rule_match_eth_addrs(rule, &match);
294 memcpy(data.eth.h_dest, match.key->dst, ETH_ALEN);
295 memcpy(data.eth.h_source, match.key->src, ETH_ALEN);
296 } else {
297 return -EOPNOTSUPP;
298 }
299
300 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
301 struct flow_match_vlan match;
302
303 flow_rule_match_vlan(rule, &match);
304
305 if (match.key->vlan_tpid != cpu_to_be16(ETH_P_8021Q))
306 return -EOPNOTSUPP;
307
308 data.vlan_in = match.key->vlan_id;
309 }
310 break;
311 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
312 offload_type = MTK_PPE_PKT_TYPE_IPV4_HNAPT;
313 break;
314 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
315 offload_type = MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T;
316 break;
317 default:
318 return -EOPNOTSUPP;
319 }
320
321 flow_action_for_each(i, act, &rule->action) {
322 switch (act->id) {
323 case FLOW_ACTION_MANGLE:
324 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
325 return -EOPNOTSUPP;
326 if (act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_ETH)
327 mtk_flow_offload_mangle_eth(act, &data.eth);
328 break;
329 case FLOW_ACTION_REDIRECT:
330 odev = act->dev;
331 break;
332 case FLOW_ACTION_CSUM:
333 break;
334 case FLOW_ACTION_VLAN_PUSH:
335 if (data.vlan.num + data.pppoe.num == 2 ||
336 act->vlan.proto != htons(ETH_P_8021Q))
337 return -EOPNOTSUPP;
338
339 data.vlan.vlans[data.vlan.num].id = act->vlan.vid;
340 data.vlan.vlans[data.vlan.num].proto = act->vlan.proto;
341 data.vlan.num++;
342 break;
343 case FLOW_ACTION_VLAN_POP:
344 break;
345 case FLOW_ACTION_PPPOE_PUSH:
346 if (data.pppoe.num == 1 ||
347 data.vlan.num == 2)
348 return -EOPNOTSUPP;
349
350 data.pppoe.sid = act->pppoe.sid;
351 data.pppoe.num++;
352 break;
353 default:
354 return -EOPNOTSUPP;
355 }
356 }
357
358 if (!is_valid_ether_addr(data.eth.h_source) ||
359 !is_valid_ether_addr(data.eth.h_dest))
360 return -EINVAL;
361
362 err = mtk_foe_entry_prepare(eth, &foe, offload_type, l4proto, 0,
363 data.eth.h_source, data.eth.h_dest);
364 if (err)
365 return err;
366
367 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
368 struct flow_match_ports ports;
369
370 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
371 return -EOPNOTSUPP;
372
373 flow_rule_match_ports(rule, &ports);
374 data.src_port = ports.key->src;
375 data.dst_port = ports.key->dst;
376 } else if (offload_type != MTK_PPE_PKT_TYPE_BRIDGE) {
377 return -EOPNOTSUPP;
378 }
379
380 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
381 struct flow_match_ipv4_addrs addrs;
382
383 flow_rule_match_ipv4_addrs(rule, &addrs);
384
385 data.v4.src_addr = addrs.key->src;
386 data.v4.dst_addr = addrs.key->dst;
387
388 mtk_flow_set_ipv4_addr(eth, &foe, &data, false);
389 }
390
391 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
392 struct flow_match_ipv6_addrs addrs;
393
394 flow_rule_match_ipv6_addrs(rule, &addrs);
395
396 data.v6.src_addr = addrs.key->src;
397 data.v6.dst_addr = addrs.key->dst;
398
399 mtk_flow_set_ipv6_addr(eth, &foe, &data);
400 }
401
402 flow_action_for_each(i, act, &rule->action) {
403 if (act->id != FLOW_ACTION_MANGLE)
404 continue;
405
406 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
407 return -EOPNOTSUPP;
408
409 switch (act->mangle.htype) {
410 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
411 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
412 err = mtk_flow_mangle_ports(act, &data);
413 break;
414 case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
415 err = mtk_flow_mangle_ipv4(act, &data);
416 break;
417 case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
418 /* handled earlier */
419 break;
420 default:
421 return -EOPNOTSUPP;
422 }
423
424 if (err)
425 return err;
426 }
427
428 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
429 err = mtk_flow_set_ipv4_addr(eth, &foe, &data, true);
430 if (err)
431 return err;
432 }
433
434 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
435 foe.bridge.vlan = data.vlan_in;
436
437 for (i = 0; i < data.vlan.num; i++)
438 mtk_foe_entry_set_vlan(eth, &foe, data.vlan.vlans[i].id);
439
440 if (data.pppoe.num == 1)
441 mtk_foe_entry_set_pppoe(eth, &foe, data.pppoe.sid);
442
443 err = mtk_flow_set_output_device(eth, &foe, odev, data.eth.h_dest,
444 &wed_index);
445 if (err)
446 return err;
447
448 if (wed_index >= 0 && (err = mtk_wed_flow_add(wed_index)) < 0)
449 return err;
450
451 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
452 if (!entry)
453 return -ENOMEM;
454
455 entry->cookie = f->cookie;
456 memcpy(&entry->data, &foe, sizeof(entry->data));
457 entry->wed_index = wed_index;
458 entry->ppe_index = ppe_index;
459
460 err = mtk_foe_entry_commit(eth->ppe[entry->ppe_index], entry);
461 if (err < 0)
462 goto free;
463
464 err = rhashtable_insert_fast(ð->flow_table, &entry->node,
465 mtk_flow_ht_params);
466 if (err < 0)
467 goto clear;
468
469 return 0;
470
471 clear:
472 mtk_foe_entry_clear(eth->ppe[entry->ppe_index], entry);
473 free:
474 kfree(entry);
475 if (wed_index >= 0)
476 mtk_wed_flow_remove(wed_index);
477 return err;
478 }
479
480 static int
mtk_flow_offload_destroy(struct mtk_eth * eth,struct flow_cls_offload * f)481 mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f)
482 {
483 struct mtk_flow_entry *entry;
484
485 entry = rhashtable_lookup(ð->flow_table, &f->cookie,
486 mtk_flow_ht_params);
487 if (!entry)
488 return -ENOENT;
489
490 mtk_foe_entry_clear(eth->ppe[entry->ppe_index], entry);
491 rhashtable_remove_fast(ð->flow_table, &entry->node,
492 mtk_flow_ht_params);
493 if (entry->wed_index >= 0)
494 mtk_wed_flow_remove(entry->wed_index);
495 kfree(entry);
496
497 return 0;
498 }
499
500 static int
mtk_flow_offload_stats(struct mtk_eth * eth,struct flow_cls_offload * f)501 mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
502 {
503 struct mtk_flow_entry *entry;
504 struct mtk_foe_accounting diff;
505 u32 idle;
506
507 entry = rhashtable_lookup(ð->flow_table, &f->cookie,
508 mtk_flow_ht_params);
509 if (!entry)
510 return -ENOENT;
511
512 idle = mtk_foe_entry_idle_time(eth->ppe[entry->ppe_index], entry);
513 f->stats.lastused = jiffies - idle * HZ;
514
515 if (entry->hash != 0xFFFF &&
516 mtk_foe_entry_get_mib(eth->ppe[entry->ppe_index], entry->hash,
517 &diff)) {
518 f->stats.pkts += diff.packets;
519 f->stats.bytes += diff.bytes;
520 }
521
522 return 0;
523 }
524
525 static DEFINE_MUTEX(mtk_flow_offload_mutex);
526
mtk_flow_offload_cmd(struct mtk_eth * eth,struct flow_cls_offload * cls,int ppe_index)527 int mtk_flow_offload_cmd(struct mtk_eth *eth, struct flow_cls_offload *cls,
528 int ppe_index)
529 {
530 int err;
531
532 mutex_lock(&mtk_flow_offload_mutex);
533 switch (cls->command) {
534 case FLOW_CLS_REPLACE:
535 err = mtk_flow_offload_replace(eth, cls, ppe_index);
536 break;
537 case FLOW_CLS_DESTROY:
538 err = mtk_flow_offload_destroy(eth, cls);
539 break;
540 case FLOW_CLS_STATS:
541 err = mtk_flow_offload_stats(eth, cls);
542 break;
543 default:
544 err = -EOPNOTSUPP;
545 break;
546 }
547 mutex_unlock(&mtk_flow_offload_mutex);
548
549 return err;
550 }
551
552 static int
mtk_eth_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)553 mtk_eth_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
554 {
555 struct flow_cls_offload *cls = type_data;
556 struct net_device *dev = cb_priv;
557 struct mtk_mac *mac;
558 struct mtk_eth *eth;
559
560 mac = netdev_priv(dev);
561 eth = mac->hw;
562
563 if (!tc_can_offload(dev))
564 return -EOPNOTSUPP;
565
566 if (type != TC_SETUP_CLSFLOWER)
567 return -EOPNOTSUPP;
568
569 return mtk_flow_offload_cmd(eth, cls, 0);
570 }
571
572 static int
mtk_eth_setup_tc_block(struct net_device * dev,struct flow_block_offload * f)573 mtk_eth_setup_tc_block(struct net_device *dev, struct flow_block_offload *f)
574 {
575 struct mtk_mac *mac = netdev_priv(dev);
576 struct mtk_eth *eth = mac->hw;
577 static LIST_HEAD(block_cb_list);
578 struct flow_block_cb *block_cb;
579 flow_setup_cb_t *cb;
580
581 if (!eth->soc->offload_version)
582 return -EOPNOTSUPP;
583
584 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
585 return -EOPNOTSUPP;
586
587 cb = mtk_eth_setup_tc_block_cb;
588 f->driver_block_list = &block_cb_list;
589
590 switch (f->command) {
591 case FLOW_BLOCK_BIND:
592 block_cb = flow_block_cb_lookup(f->block, cb, dev);
593 if (block_cb) {
594 flow_block_cb_incref(block_cb);
595 return 0;
596 }
597 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
598 if (IS_ERR(block_cb))
599 return PTR_ERR(block_cb);
600
601 flow_block_cb_incref(block_cb);
602 flow_block_cb_add(block_cb, f);
603 list_add_tail(&block_cb->driver_list, &block_cb_list);
604 return 0;
605 case FLOW_BLOCK_UNBIND:
606 block_cb = flow_block_cb_lookup(f->block, cb, dev);
607 if (!block_cb)
608 return -ENOENT;
609
610 if (!flow_block_cb_decref(block_cb)) {
611 flow_block_cb_remove(block_cb, f);
612 list_del(&block_cb->driver_list);
613 }
614 return 0;
615 default:
616 return -EOPNOTSUPP;
617 }
618 }
619
mtk_eth_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)620 int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
621 void *type_data)
622 {
623 switch (type) {
624 case TC_SETUP_BLOCK:
625 case TC_SETUP_FT:
626 return mtk_eth_setup_tc_block(dev, type_data);
627 default:
628 return -EOPNOTSUPP;
629 }
630 }
631
mtk_eth_offload_init(struct mtk_eth * eth)632 int mtk_eth_offload_init(struct mtk_eth *eth)
633 {
634 return rhashtable_init(ð->flow_table, &mtk_flow_ht_params);
635 }
636