xref: /linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_flower.c (revision 93a3545d812ae7cfe4426374e00a7d8f64ac02e0)
1 /*
2  * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <net/tc_act/tc_mirred.h>
36 #include <net/tc_act/tc_pedit.h>
37 #include <net/tc_act/tc_gact.h>
38 #include <net/tc_act/tc_vlan.h>
39 
40 #include "cxgb4.h"
41 #include "cxgb4_filter.h"
42 #include "cxgb4_tc_flower.h"
43 
44 #define STATS_CHECK_PERIOD (HZ / 2)
45 
46 static struct ch_tc_pedit_fields pedits[] = {
47 	PEDIT_FIELDS(ETH_, DMAC_31_0, 4, dmac, 0),
48 	PEDIT_FIELDS(ETH_, DMAC_47_32, 2, dmac, 4),
49 	PEDIT_FIELDS(ETH_, SMAC_15_0, 2, smac, 0),
50 	PEDIT_FIELDS(ETH_, SMAC_47_16, 4, smac, 2),
51 	PEDIT_FIELDS(IP4_, SRC, 4, nat_fip, 0),
52 	PEDIT_FIELDS(IP4_, DST, 4, nat_lip, 0),
53 	PEDIT_FIELDS(IP6_, SRC_31_0, 4, nat_fip, 0),
54 	PEDIT_FIELDS(IP6_, SRC_63_32, 4, nat_fip, 4),
55 	PEDIT_FIELDS(IP6_, SRC_95_64, 4, nat_fip, 8),
56 	PEDIT_FIELDS(IP6_, SRC_127_96, 4, nat_fip, 12),
57 	PEDIT_FIELDS(IP6_, DST_31_0, 4, nat_lip, 0),
58 	PEDIT_FIELDS(IP6_, DST_63_32, 4, nat_lip, 4),
59 	PEDIT_FIELDS(IP6_, DST_95_64, 4, nat_lip, 8),
60 	PEDIT_FIELDS(IP6_, DST_127_96, 4, nat_lip, 12),
61 };
62 
63 static struct ch_tc_flower_entry *allocate_flower_entry(void)
64 {
65 	struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
66 	if (new)
67 		spin_lock_init(&new->lock);
68 	return new;
69 }
70 
71 /* Must be called with either RTNL or rcu_read_lock */
72 static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
73 						   unsigned long flower_cookie)
74 {
75 	return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
76 				      adap->flower_ht_params);
77 }
78 
79 static void cxgb4_process_flow_match(struct net_device *dev,
80 				     struct flow_rule *rule,
81 				     struct ch_filter_specification *fs)
82 {
83 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
84 		struct flow_match_basic match;
85 		u16 ethtype_key, ethtype_mask;
86 
87 		flow_rule_match_basic(rule, &match);
88 		ethtype_key = ntohs(match.key->n_proto);
89 		ethtype_mask = ntohs(match.mask->n_proto);
90 
91 		if (ethtype_key == ETH_P_ALL) {
92 			ethtype_key = 0;
93 			ethtype_mask = 0;
94 		}
95 
96 		if (ethtype_key == ETH_P_IPV6)
97 			fs->type = 1;
98 
99 		fs->val.ethtype = ethtype_key;
100 		fs->mask.ethtype = ethtype_mask;
101 		fs->val.proto = match.key->ip_proto;
102 		fs->mask.proto = match.mask->ip_proto;
103 	}
104 
105 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
106 		struct flow_match_ipv4_addrs match;
107 
108 		flow_rule_match_ipv4_addrs(rule, &match);
109 		fs->type = 0;
110 		memcpy(&fs->val.lip[0], &match.key->dst, sizeof(match.key->dst));
111 		memcpy(&fs->val.fip[0], &match.key->src, sizeof(match.key->src));
112 		memcpy(&fs->mask.lip[0], &match.mask->dst, sizeof(match.mask->dst));
113 		memcpy(&fs->mask.fip[0], &match.mask->src, sizeof(match.mask->src));
114 
115 		/* also initialize nat_lip/fip to same values */
116 		memcpy(&fs->nat_lip[0], &match.key->dst, sizeof(match.key->dst));
117 		memcpy(&fs->nat_fip[0], &match.key->src, sizeof(match.key->src));
118 	}
119 
120 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
121 		struct flow_match_ipv6_addrs match;
122 
123 		flow_rule_match_ipv6_addrs(rule, &match);
124 		fs->type = 1;
125 		memcpy(&fs->val.lip[0], match.key->dst.s6_addr,
126 		       sizeof(match.key->dst));
127 		memcpy(&fs->val.fip[0], match.key->src.s6_addr,
128 		       sizeof(match.key->src));
129 		memcpy(&fs->mask.lip[0], match.mask->dst.s6_addr,
130 		       sizeof(match.mask->dst));
131 		memcpy(&fs->mask.fip[0], match.mask->src.s6_addr,
132 		       sizeof(match.mask->src));
133 
134 		/* also initialize nat_lip/fip to same values */
135 		memcpy(&fs->nat_lip[0], match.key->dst.s6_addr,
136 		       sizeof(match.key->dst));
137 		memcpy(&fs->nat_fip[0], match.key->src.s6_addr,
138 		       sizeof(match.key->src));
139 	}
140 
141 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
142 		struct flow_match_ports match;
143 
144 		flow_rule_match_ports(rule, &match);
145 		fs->val.lport = be16_to_cpu(match.key->dst);
146 		fs->mask.lport = be16_to_cpu(match.mask->dst);
147 		fs->val.fport = be16_to_cpu(match.key->src);
148 		fs->mask.fport = be16_to_cpu(match.mask->src);
149 
150 		/* also initialize nat_lport/fport to same values */
151 		fs->nat_lport = fs->val.lport;
152 		fs->nat_fport = fs->val.fport;
153 	}
154 
155 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
156 		struct flow_match_ip match;
157 
158 		flow_rule_match_ip(rule, &match);
159 		fs->val.tos = match.key->tos;
160 		fs->mask.tos = match.mask->tos;
161 	}
162 
163 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
164 		struct flow_match_enc_keyid match;
165 
166 		flow_rule_match_enc_keyid(rule, &match);
167 		fs->val.vni = be32_to_cpu(match.key->keyid);
168 		fs->mask.vni = be32_to_cpu(match.mask->keyid);
169 		if (fs->mask.vni) {
170 			fs->val.encap_vld = 1;
171 			fs->mask.encap_vld = 1;
172 		}
173 	}
174 
175 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
176 		struct flow_match_vlan match;
177 		u16 vlan_tci, vlan_tci_mask;
178 
179 		flow_rule_match_vlan(rule, &match);
180 		vlan_tci = match.key->vlan_id | (match.key->vlan_priority <<
181 					       VLAN_PRIO_SHIFT);
182 		vlan_tci_mask = match.mask->vlan_id | (match.mask->vlan_priority <<
183 						     VLAN_PRIO_SHIFT);
184 		fs->val.ivlan = vlan_tci;
185 		fs->mask.ivlan = vlan_tci_mask;
186 
187 		fs->val.ivlan_vld = 1;
188 		fs->mask.ivlan_vld = 1;
189 
190 		/* Chelsio adapters use ivlan_vld bit to match vlan packets
191 		 * as 802.1Q. Also, when vlan tag is present in packets,
192 		 * ethtype match is used then to match on ethtype of inner
193 		 * header ie. the header following the vlan header.
194 		 * So, set the ivlan_vld based on ethtype info supplied by
195 		 * TC for vlan packets if its 802.1Q. And then reset the
196 		 * ethtype value else, hw will try to match the supplied
197 		 * ethtype value with ethtype of inner header.
198 		 */
199 		if (fs->val.ethtype == ETH_P_8021Q) {
200 			fs->val.ethtype = 0;
201 			fs->mask.ethtype = 0;
202 		}
203 	}
204 
205 	/* Match only packets coming from the ingress port where this
206 	 * filter will be created.
207 	 */
208 	fs->val.iport = netdev2pinfo(dev)->port_id;
209 	fs->mask.iport = ~0;
210 }
211 
212 static int cxgb4_validate_flow_match(struct net_device *dev,
213 				     struct flow_rule *rule)
214 {
215 	struct flow_dissector *dissector = rule->match.dissector;
216 	u16 ethtype_mask = 0;
217 	u16 ethtype_key = 0;
218 
219 	if (dissector->used_keys &
220 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
221 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
222 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
223 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
224 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
225 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
226 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
227 	      BIT(FLOW_DISSECTOR_KEY_IP))) {
228 		netdev_warn(dev, "Unsupported key used: 0x%x\n",
229 			    dissector->used_keys);
230 		return -EOPNOTSUPP;
231 	}
232 
233 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
234 		struct flow_match_basic match;
235 
236 		flow_rule_match_basic(rule, &match);
237 		ethtype_key = ntohs(match.key->n_proto);
238 		ethtype_mask = ntohs(match.mask->n_proto);
239 	}
240 
241 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
242 		u16 eth_ip_type = ethtype_key & ethtype_mask;
243 		struct flow_match_ip match;
244 
245 		if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
246 			netdev_err(dev, "IP Key supported only with IPv4/v6");
247 			return -EINVAL;
248 		}
249 
250 		flow_rule_match_ip(rule, &match);
251 		if (match.mask->ttl) {
252 			netdev_warn(dev, "ttl match unsupported for offload");
253 			return -EOPNOTSUPP;
254 		}
255 	}
256 
257 	return 0;
258 }
259 
260 static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
261 			  u8 field)
262 {
263 	u32 set_val = val & ~mask;
264 	u32 offset = 0;
265 	u8 size = 1;
266 	int i;
267 
268 	for (i = 0; i < ARRAY_SIZE(pedits); i++) {
269 		if (pedits[i].field == field) {
270 			offset = pedits[i].offset;
271 			size = pedits[i].size;
272 			break;
273 		}
274 	}
275 	memcpy((u8 *)fs + offset, &set_val, size);
276 }
277 
278 static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
279 				u32 mask, u32 offset, u8 htype)
280 {
281 	switch (htype) {
282 	case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
283 		switch (offset) {
284 		case PEDIT_ETH_DMAC_31_0:
285 			fs->newdmac = 1;
286 			offload_pedit(fs, val, mask, ETH_DMAC_31_0);
287 			break;
288 		case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
289 			if (~mask & PEDIT_ETH_DMAC_MASK)
290 				offload_pedit(fs, val, mask, ETH_DMAC_47_32);
291 			else
292 				offload_pedit(fs, val >> 16, mask >> 16,
293 					      ETH_SMAC_15_0);
294 			break;
295 		case PEDIT_ETH_SMAC_47_16:
296 			fs->newsmac = 1;
297 			offload_pedit(fs, val, mask, ETH_SMAC_47_16);
298 		}
299 		break;
300 	case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
301 		switch (offset) {
302 		case PEDIT_IP4_SRC:
303 			offload_pedit(fs, val, mask, IP4_SRC);
304 			break;
305 		case PEDIT_IP4_DST:
306 			offload_pedit(fs, val, mask, IP4_DST);
307 		}
308 		fs->nat_mode = NAT_MODE_ALL;
309 		break;
310 	case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
311 		switch (offset) {
312 		case PEDIT_IP6_SRC_31_0:
313 			offload_pedit(fs, val, mask, IP6_SRC_31_0);
314 			break;
315 		case PEDIT_IP6_SRC_63_32:
316 			offload_pedit(fs, val, mask, IP6_SRC_63_32);
317 			break;
318 		case PEDIT_IP6_SRC_95_64:
319 			offload_pedit(fs, val, mask, IP6_SRC_95_64);
320 			break;
321 		case PEDIT_IP6_SRC_127_96:
322 			offload_pedit(fs, val, mask, IP6_SRC_127_96);
323 			break;
324 		case PEDIT_IP6_DST_31_0:
325 			offload_pedit(fs, val, mask, IP6_DST_31_0);
326 			break;
327 		case PEDIT_IP6_DST_63_32:
328 			offload_pedit(fs, val, mask, IP6_DST_63_32);
329 			break;
330 		case PEDIT_IP6_DST_95_64:
331 			offload_pedit(fs, val, mask, IP6_DST_95_64);
332 			break;
333 		case PEDIT_IP6_DST_127_96:
334 			offload_pedit(fs, val, mask, IP6_DST_127_96);
335 		}
336 		fs->nat_mode = NAT_MODE_ALL;
337 		break;
338 	case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
339 		switch (offset) {
340 		case PEDIT_TCP_SPORT_DPORT:
341 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
342 				fs->nat_fport = val;
343 			else
344 				fs->nat_lport = val >> 16;
345 		}
346 		fs->nat_mode = NAT_MODE_ALL;
347 		break;
348 	case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
349 		switch (offset) {
350 		case PEDIT_UDP_SPORT_DPORT:
351 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
352 				fs->nat_fport = val;
353 			else
354 				fs->nat_lport = val >> 16;
355 		}
356 		fs->nat_mode = NAT_MODE_ALL;
357 	}
358 }
359 
360 void cxgb4_process_flow_actions(struct net_device *in,
361 				struct flow_action *actions,
362 				struct ch_filter_specification *fs)
363 {
364 	struct flow_action_entry *act;
365 	int i;
366 
367 	flow_action_for_each(i, act, actions) {
368 		switch (act->id) {
369 		case FLOW_ACTION_ACCEPT:
370 			fs->action = FILTER_PASS;
371 			break;
372 		case FLOW_ACTION_DROP:
373 			fs->action = FILTER_DROP;
374 			break;
375 		case FLOW_ACTION_MIRRED:
376 		case FLOW_ACTION_REDIRECT: {
377 			struct net_device *out = act->dev;
378 			struct port_info *pi = netdev_priv(out);
379 
380 			fs->action = FILTER_SWITCH;
381 			fs->eport = pi->port_id;
382 			}
383 			break;
384 		case FLOW_ACTION_VLAN_POP:
385 		case FLOW_ACTION_VLAN_PUSH:
386 		case FLOW_ACTION_VLAN_MANGLE: {
387 			u8 prio = act->vlan.prio;
388 			u16 vid = act->vlan.vid;
389 			u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
390 			switch (act->id) {
391 			case FLOW_ACTION_VLAN_POP:
392 				fs->newvlan |= VLAN_REMOVE;
393 				break;
394 			case FLOW_ACTION_VLAN_PUSH:
395 				fs->newvlan |= VLAN_INSERT;
396 				fs->vlan = vlan_tci;
397 				break;
398 			case FLOW_ACTION_VLAN_MANGLE:
399 				fs->newvlan |= VLAN_REWRITE;
400 				fs->vlan = vlan_tci;
401 				break;
402 			default:
403 				break;
404 			}
405 			}
406 			break;
407 		case FLOW_ACTION_MANGLE: {
408 			u32 mask, val, offset;
409 			u8 htype;
410 
411 			htype = act->mangle.htype;
412 			mask = act->mangle.mask;
413 			val = act->mangle.val;
414 			offset = act->mangle.offset;
415 
416 			process_pedit_field(fs, val, mask, offset, htype);
417 			}
418 			break;
419 		case FLOW_ACTION_QUEUE:
420 			fs->action = FILTER_PASS;
421 			fs->dirsteer = 1;
422 			fs->iq = act->queue.index;
423 			break;
424 		default:
425 			break;
426 		}
427 	}
428 }
429 
430 static bool valid_l4_mask(u32 mask)
431 {
432 	u16 hi, lo;
433 
434 	/* Either the upper 16-bits (SPORT) OR the lower
435 	 * 16-bits (DPORT) can be set, but NOT BOTH.
436 	 */
437 	hi = (mask >> 16) & 0xFFFF;
438 	lo = mask & 0xFFFF;
439 
440 	return hi && lo ? false : true;
441 }
442 
443 static bool valid_pedit_action(struct net_device *dev,
444 			       const struct flow_action_entry *act)
445 {
446 	u32 mask, offset;
447 	u8 htype;
448 
449 	htype = act->mangle.htype;
450 	mask = act->mangle.mask;
451 	offset = act->mangle.offset;
452 
453 	switch (htype) {
454 	case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
455 		switch (offset) {
456 		case PEDIT_ETH_DMAC_31_0:
457 		case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
458 		case PEDIT_ETH_SMAC_47_16:
459 			break;
460 		default:
461 			netdev_err(dev, "%s: Unsupported pedit field\n",
462 				   __func__);
463 			return false;
464 		}
465 		break;
466 	case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
467 		switch (offset) {
468 		case PEDIT_IP4_SRC:
469 		case PEDIT_IP4_DST:
470 			break;
471 		default:
472 			netdev_err(dev, "%s: Unsupported pedit field\n",
473 				   __func__);
474 			return false;
475 		}
476 		break;
477 	case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
478 		switch (offset) {
479 		case PEDIT_IP6_SRC_31_0:
480 		case PEDIT_IP6_SRC_63_32:
481 		case PEDIT_IP6_SRC_95_64:
482 		case PEDIT_IP6_SRC_127_96:
483 		case PEDIT_IP6_DST_31_0:
484 		case PEDIT_IP6_DST_63_32:
485 		case PEDIT_IP6_DST_95_64:
486 		case PEDIT_IP6_DST_127_96:
487 			break;
488 		default:
489 			netdev_err(dev, "%s: Unsupported pedit field\n",
490 				   __func__);
491 			return false;
492 		}
493 		break;
494 	case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
495 		switch (offset) {
496 		case PEDIT_TCP_SPORT_DPORT:
497 			if (!valid_l4_mask(~mask)) {
498 				netdev_err(dev, "%s: Unsupported mask for TCP L4 ports\n",
499 					   __func__);
500 				return false;
501 			}
502 			break;
503 		default:
504 			netdev_err(dev, "%s: Unsupported pedit field\n",
505 				   __func__);
506 			return false;
507 		}
508 		break;
509 	case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
510 		switch (offset) {
511 		case PEDIT_UDP_SPORT_DPORT:
512 			if (!valid_l4_mask(~mask)) {
513 				netdev_err(dev, "%s: Unsupported mask for UDP L4 ports\n",
514 					   __func__);
515 				return false;
516 			}
517 			break;
518 		default:
519 			netdev_err(dev, "%s: Unsupported pedit field\n",
520 				   __func__);
521 			return false;
522 		}
523 		break;
524 	default:
525 		netdev_err(dev, "%s: Unsupported pedit type\n", __func__);
526 		return false;
527 	}
528 	return true;
529 }
530 
531 int cxgb4_validate_flow_actions(struct net_device *dev,
532 				struct flow_action *actions,
533 				struct netlink_ext_ack *extack,
534 				u8 matchall_filter)
535 {
536 	struct flow_action_entry *act;
537 	bool act_redir = false;
538 	bool act_pedit = false;
539 	bool act_vlan = false;
540 	int i;
541 
542 	if (!flow_action_basic_hw_stats_check(actions, extack))
543 		return -EOPNOTSUPP;
544 
545 	flow_action_for_each(i, act, actions) {
546 		switch (act->id) {
547 		case FLOW_ACTION_ACCEPT:
548 		case FLOW_ACTION_DROP:
549 			/* Do nothing */
550 			break;
551 		case FLOW_ACTION_MIRRED:
552 		case FLOW_ACTION_REDIRECT: {
553 			struct adapter *adap = netdev2adap(dev);
554 			struct net_device *n_dev, *target_dev;
555 			bool found = false;
556 			unsigned int i;
557 
558 			if (act->id == FLOW_ACTION_MIRRED &&
559 			    !matchall_filter) {
560 				NL_SET_ERR_MSG_MOD(extack,
561 						   "Egress mirror action is only supported for tc-matchall");
562 				return -EOPNOTSUPP;
563 			}
564 
565 			target_dev = act->dev;
566 			for_each_port(adap, i) {
567 				n_dev = adap->port[i];
568 				if (target_dev == n_dev) {
569 					found = true;
570 					break;
571 				}
572 			}
573 
574 			/* If interface doesn't belong to our hw, then
575 			 * the provided output port is not valid
576 			 */
577 			if (!found) {
578 				netdev_err(dev, "%s: Out port invalid\n",
579 					   __func__);
580 				return -EINVAL;
581 			}
582 			act_redir = true;
583 			}
584 			break;
585 		case FLOW_ACTION_VLAN_POP:
586 		case FLOW_ACTION_VLAN_PUSH:
587 		case FLOW_ACTION_VLAN_MANGLE: {
588 			u16 proto = be16_to_cpu(act->vlan.proto);
589 
590 			switch (act->id) {
591 			case FLOW_ACTION_VLAN_POP:
592 				break;
593 			case FLOW_ACTION_VLAN_PUSH:
594 			case FLOW_ACTION_VLAN_MANGLE:
595 				if (proto != ETH_P_8021Q) {
596 					netdev_err(dev, "%s: Unsupported vlan proto\n",
597 						   __func__);
598 					return -EOPNOTSUPP;
599 				}
600 				break;
601 			default:
602 				netdev_err(dev, "%s: Unsupported vlan action\n",
603 					   __func__);
604 				return -EOPNOTSUPP;
605 			}
606 			act_vlan = true;
607 			}
608 			break;
609 		case FLOW_ACTION_MANGLE: {
610 			bool pedit_valid = valid_pedit_action(dev, act);
611 
612 			if (!pedit_valid)
613 				return -EOPNOTSUPP;
614 			act_pedit = true;
615 			}
616 			break;
617 		case FLOW_ACTION_QUEUE:
618 			/* Do nothing. cxgb4_set_filter will validate */
619 			break;
620 		default:
621 			netdev_err(dev, "%s: Unsupported action\n", __func__);
622 			return -EOPNOTSUPP;
623 		}
624 	}
625 
626 	if ((act_pedit || act_vlan) && !act_redir) {
627 		netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
628 			   __func__);
629 		return -EINVAL;
630 	}
631 
632 	return 0;
633 }
634 
635 static void cxgb4_tc_flower_hash_prio_add(struct adapter *adap, u32 tc_prio)
636 {
637 	spin_lock_bh(&adap->tids.ftid_lock);
638 	if (adap->tids.tc_hash_tids_max_prio < tc_prio)
639 		adap->tids.tc_hash_tids_max_prio = tc_prio;
640 	spin_unlock_bh(&adap->tids.ftid_lock);
641 }
642 
643 static void cxgb4_tc_flower_hash_prio_del(struct adapter *adap, u32 tc_prio)
644 {
645 	struct tid_info *t = &adap->tids;
646 	struct ch_tc_flower_entry *fe;
647 	struct rhashtable_iter iter;
648 	u32 found = 0;
649 
650 	spin_lock_bh(&t->ftid_lock);
651 	/* Bail if the current rule is not the one with the max
652 	 * prio.
653 	 */
654 	if (t->tc_hash_tids_max_prio != tc_prio)
655 		goto out_unlock;
656 
657 	/* Search for the next rule having the same or next lower
658 	 * max prio.
659 	 */
660 	rhashtable_walk_enter(&adap->flower_tbl, &iter);
661 	do {
662 		rhashtable_walk_start(&iter);
663 
664 		fe = rhashtable_walk_next(&iter);
665 		while (!IS_ERR_OR_NULL(fe)) {
666 			if (fe->fs.hash &&
667 			    fe->fs.tc_prio <= t->tc_hash_tids_max_prio) {
668 				t->tc_hash_tids_max_prio = fe->fs.tc_prio;
669 				found++;
670 
671 				/* Bail if we found another rule
672 				 * having the same prio as the
673 				 * current max one.
674 				 */
675 				if (fe->fs.tc_prio == tc_prio)
676 					break;
677 			}
678 
679 			fe = rhashtable_walk_next(&iter);
680 		}
681 
682 		rhashtable_walk_stop(&iter);
683 	} while (fe == ERR_PTR(-EAGAIN));
684 	rhashtable_walk_exit(&iter);
685 
686 	if (!found)
687 		t->tc_hash_tids_max_prio = 0;
688 
689 out_unlock:
690 	spin_unlock_bh(&t->ftid_lock);
691 }
692 
693 int cxgb4_flow_rule_replace(struct net_device *dev, struct flow_rule *rule,
694 			    u32 tc_prio, struct netlink_ext_ack *extack,
695 			    struct ch_filter_specification *fs, u32 *tid)
696 {
697 	struct adapter *adap = netdev2adap(dev);
698 	struct filter_ctx ctx;
699 	u8 inet_family;
700 	int fidx, ret;
701 
702 	if (cxgb4_validate_flow_actions(dev, &rule->action, extack, 0))
703 		return -EOPNOTSUPP;
704 
705 	if (cxgb4_validate_flow_match(dev, rule))
706 		return -EOPNOTSUPP;
707 
708 	cxgb4_process_flow_match(dev, rule, fs);
709 	cxgb4_process_flow_actions(dev, &rule->action, fs);
710 
711 	fs->hash = is_filter_exact_match(adap, fs);
712 	inet_family = fs->type ? PF_INET6 : PF_INET;
713 
714 	/* Get a free filter entry TID, where we can insert this new
715 	 * rule. Only insert rule if its prio doesn't conflict with
716 	 * existing rules.
717 	 */
718 	fidx = cxgb4_get_free_ftid(dev, inet_family, fs->hash,
719 				   tc_prio);
720 	if (fidx < 0) {
721 		NL_SET_ERR_MSG_MOD(extack,
722 				   "No free LETCAM index available");
723 		return -ENOMEM;
724 	}
725 
726 	if (fidx < adap->tids.nhpftids) {
727 		fs->prio = 1;
728 		fs->hash = 0;
729 	}
730 
731 	/* If the rule can be inserted into HASH region, then ignore
732 	 * the index to normal FILTER region.
733 	 */
734 	if (fs->hash)
735 		fidx = 0;
736 
737 	fs->tc_prio = tc_prio;
738 
739 	init_completion(&ctx.completion);
740 	ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
741 	if (ret) {
742 		netdev_err(dev, "%s: filter creation err %d\n",
743 			   __func__, ret);
744 		return ret;
745 	}
746 
747 	/* Wait for reply */
748 	ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
749 	if (!ret)
750 		return -ETIMEDOUT;
751 
752 	/* Check if hw returned error for filter creation */
753 	if (ctx.result)
754 		return ctx.result;
755 
756 	*tid = ctx.tid;
757 
758 	if (fs->hash)
759 		cxgb4_tc_flower_hash_prio_add(adap, tc_prio);
760 
761 	return 0;
762 }
763 
764 int cxgb4_tc_flower_replace(struct net_device *dev,
765 			    struct flow_cls_offload *cls)
766 {
767 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
768 	struct netlink_ext_ack *extack = cls->common.extack;
769 	struct adapter *adap = netdev2adap(dev);
770 	struct ch_tc_flower_entry *ch_flower;
771 	struct ch_filter_specification *fs;
772 	int ret;
773 
774 	ch_flower = allocate_flower_entry();
775 	if (!ch_flower) {
776 		netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
777 		return -ENOMEM;
778 	}
779 
780 	fs = &ch_flower->fs;
781 	fs->hitcnts = 1;
782 	fs->tc_cookie = cls->cookie;
783 
784 	ret = cxgb4_flow_rule_replace(dev, rule, cls->common.prio, extack, fs,
785 				      &ch_flower->filter_id);
786 	if (ret)
787 		goto free_entry;
788 
789 	ch_flower->tc_flower_cookie = cls->cookie;
790 	ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
791 				     adap->flower_ht_params);
792 	if (ret)
793 		goto del_filter;
794 
795 	return 0;
796 
797 del_filter:
798 	if (fs->hash)
799 		cxgb4_tc_flower_hash_prio_del(adap, cls->common.prio);
800 
801 	cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
802 
803 free_entry:
804 	kfree(ch_flower);
805 	return ret;
806 }
807 
808 int cxgb4_flow_rule_destroy(struct net_device *dev, u32 tc_prio,
809 			    struct ch_filter_specification *fs, int tid)
810 {
811 	struct adapter *adap = netdev2adap(dev);
812 	u8 hash;
813 	int ret;
814 
815 	hash = fs->hash;
816 
817 	ret = cxgb4_del_filter(dev, tid, fs);
818 	if (ret)
819 		return ret;
820 
821 	if (hash)
822 		cxgb4_tc_flower_hash_prio_del(adap, tc_prio);
823 
824 	return ret;
825 }
826 
827 int cxgb4_tc_flower_destroy(struct net_device *dev,
828 			    struct flow_cls_offload *cls)
829 {
830 	struct adapter *adap = netdev2adap(dev);
831 	struct ch_tc_flower_entry *ch_flower;
832 	int ret;
833 
834 	ch_flower = ch_flower_lookup(adap, cls->cookie);
835 	if (!ch_flower)
836 		return -ENOENT;
837 
838 	ret = cxgb4_flow_rule_destroy(dev, ch_flower->fs.tc_prio,
839 				      &ch_flower->fs, ch_flower->filter_id);
840 	if (ret)
841 		goto err;
842 
843 	ret = rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
844 				     adap->flower_ht_params);
845 	if (ret) {
846 		netdev_err(dev, "Flow remove from rhashtable failed");
847 		goto err;
848 	}
849 	kfree_rcu(ch_flower, rcu);
850 
851 err:
852 	return ret;
853 }
854 
855 static void ch_flower_stats_handler(struct work_struct *work)
856 {
857 	struct adapter *adap = container_of(work, struct adapter,
858 					    flower_stats_work);
859 	struct ch_tc_flower_entry *flower_entry;
860 	struct ch_tc_flower_stats *ofld_stats;
861 	struct rhashtable_iter iter;
862 	u64 packets;
863 	u64 bytes;
864 	int ret;
865 
866 	rhashtable_walk_enter(&adap->flower_tbl, &iter);
867 	do {
868 		rhashtable_walk_start(&iter);
869 
870 		while ((flower_entry = rhashtable_walk_next(&iter)) &&
871 		       !IS_ERR(flower_entry)) {
872 			ret = cxgb4_get_filter_counters(adap->port[0],
873 							flower_entry->filter_id,
874 							&packets, &bytes,
875 							flower_entry->fs.hash);
876 			if (!ret) {
877 				spin_lock(&flower_entry->lock);
878 				ofld_stats = &flower_entry->stats;
879 
880 				if (ofld_stats->prev_packet_count != packets) {
881 					ofld_stats->prev_packet_count = packets;
882 					ofld_stats->last_used = jiffies;
883 				}
884 				spin_unlock(&flower_entry->lock);
885 			}
886 		}
887 
888 		rhashtable_walk_stop(&iter);
889 
890 	} while (flower_entry == ERR_PTR(-EAGAIN));
891 	rhashtable_walk_exit(&iter);
892 	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
893 }
894 
895 static void ch_flower_stats_cb(struct timer_list *t)
896 {
897 	struct adapter *adap = from_timer(adap, t, flower_stats_timer);
898 
899 	schedule_work(&adap->flower_stats_work);
900 }
901 
902 int cxgb4_tc_flower_stats(struct net_device *dev,
903 			  struct flow_cls_offload *cls)
904 {
905 	struct adapter *adap = netdev2adap(dev);
906 	struct ch_tc_flower_stats *ofld_stats;
907 	struct ch_tc_flower_entry *ch_flower;
908 	u64 packets;
909 	u64 bytes;
910 	int ret;
911 
912 	ch_flower = ch_flower_lookup(adap, cls->cookie);
913 	if (!ch_flower) {
914 		ret = -ENOENT;
915 		goto err;
916 	}
917 
918 	ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
919 					&packets, &bytes,
920 					ch_flower->fs.hash);
921 	if (ret < 0)
922 		goto err;
923 
924 	spin_lock_bh(&ch_flower->lock);
925 	ofld_stats = &ch_flower->stats;
926 	if (ofld_stats->packet_count != packets) {
927 		if (ofld_stats->prev_packet_count != packets)
928 			ofld_stats->last_used = jiffies;
929 		flow_stats_update(&cls->stats, bytes - ofld_stats->byte_count,
930 				  packets - ofld_stats->packet_count, 0,
931 				  ofld_stats->last_used,
932 				  FLOW_ACTION_HW_STATS_IMMEDIATE);
933 
934 		ofld_stats->packet_count = packets;
935 		ofld_stats->byte_count = bytes;
936 		ofld_stats->prev_packet_count = packets;
937 	}
938 	spin_unlock_bh(&ch_flower->lock);
939 	return 0;
940 
941 err:
942 	return ret;
943 }
944 
945 static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
946 	.nelem_hint = 384,
947 	.head_offset = offsetof(struct ch_tc_flower_entry, node),
948 	.key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
949 	.key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
950 	.max_size = 524288,
951 	.min_size = 512,
952 	.automatic_shrinking = true
953 };
954 
955 int cxgb4_init_tc_flower(struct adapter *adap)
956 {
957 	int ret;
958 
959 	if (adap->tc_flower_initialized)
960 		return -EEXIST;
961 
962 	adap->flower_ht_params = cxgb4_tc_flower_ht_params;
963 	ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
964 	if (ret)
965 		return ret;
966 
967 	INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
968 	timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
969 	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
970 	adap->tc_flower_initialized = true;
971 	return 0;
972 }
973 
974 void cxgb4_cleanup_tc_flower(struct adapter *adap)
975 {
976 	if (!adap->tc_flower_initialized)
977 		return;
978 
979 	if (adap->flower_stats_timer.function)
980 		del_timer_sync(&adap->flower_stats_timer);
981 	cancel_work_sync(&adap->flower_stats_work);
982 	rhashtable_destroy(&adap->flower_tbl);
983 	adap->tc_flower_initialized = false;
984 }
985