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