xref: /linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_flower.c (revision 6ce2b689932ba8288ceef9a82c1caf029b0b23f9)
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 const struct cxgb4_natmode_config cxgb4_natmode_config_array[] = {
64 	/* Default supported NAT modes */
65 	{
66 		.chip = CHELSIO_T5,
67 		.flags = CXGB4_ACTION_NATMODE_NONE,
68 		.natmode = NAT_MODE_NONE,
69 	},
70 	{
71 		.chip = CHELSIO_T5,
72 		.flags = CXGB4_ACTION_NATMODE_DIP,
73 		.natmode = NAT_MODE_DIP,
74 	},
75 	{
76 		.chip = CHELSIO_T5,
77 		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT,
78 		.natmode = NAT_MODE_DIP_DP,
79 	},
80 	{
81 		.chip = CHELSIO_T5,
82 		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT |
83 			 CXGB4_ACTION_NATMODE_SIP,
84 		.natmode = NAT_MODE_DIP_DP_SIP,
85 	},
86 	{
87 		.chip = CHELSIO_T5,
88 		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_DPORT |
89 			 CXGB4_ACTION_NATMODE_SPORT,
90 		.natmode = NAT_MODE_DIP_DP_SP,
91 	},
92 	{
93 		.chip = CHELSIO_T5,
94 		.flags = CXGB4_ACTION_NATMODE_SIP | CXGB4_ACTION_NATMODE_SPORT,
95 		.natmode = NAT_MODE_SIP_SP,
96 	},
97 	{
98 		.chip = CHELSIO_T5,
99 		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP |
100 			 CXGB4_ACTION_NATMODE_SPORT,
101 		.natmode = NAT_MODE_DIP_SIP_SP,
102 	},
103 	{
104 		.chip = CHELSIO_T5,
105 		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP |
106 			 CXGB4_ACTION_NATMODE_DPORT |
107 			 CXGB4_ACTION_NATMODE_SPORT,
108 		.natmode = NAT_MODE_ALL,
109 	},
110 	/* T6+ can ignore L4 ports when they're disabled. */
111 	{
112 		.chip = CHELSIO_T6,
113 		.flags = CXGB4_ACTION_NATMODE_SIP,
114 		.natmode = NAT_MODE_SIP_SP,
115 	},
116 	{
117 		.chip = CHELSIO_T6,
118 		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SPORT,
119 		.natmode = NAT_MODE_DIP_DP_SP,
120 	},
121 	{
122 		.chip = CHELSIO_T6,
123 		.flags = CXGB4_ACTION_NATMODE_DIP | CXGB4_ACTION_NATMODE_SIP,
124 		.natmode = NAT_MODE_ALL,
125 	},
126 };
127 
128 static void cxgb4_action_natmode_tweak(struct ch_filter_specification *fs,
129 				       u8 natmode_flags)
130 {
131 	u8 i = 0;
132 
133 	/* Translate the enabled NAT 4-tuple fields to one of the
134 	 * hardware supported NAT mode configurations. This ensures
135 	 * that we pick a valid combination, where the disabled fields
136 	 * do not get overwritten to 0.
137 	 */
138 	for (i = 0; i < ARRAY_SIZE(cxgb4_natmode_config_array); i++) {
139 		if (cxgb4_natmode_config_array[i].flags == natmode_flags) {
140 			fs->nat_mode = cxgb4_natmode_config_array[i].natmode;
141 			return;
142 		}
143 	}
144 }
145 
146 static struct ch_tc_flower_entry *allocate_flower_entry(void)
147 {
148 	struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
149 	if (new)
150 		spin_lock_init(&new->lock);
151 	return new;
152 }
153 
154 /* Must be called with either RTNL or rcu_read_lock */
155 static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
156 						   unsigned long flower_cookie)
157 {
158 	return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
159 				      adap->flower_ht_params);
160 }
161 
162 static void cxgb4_process_flow_match(struct net_device *dev,
163 				     struct flow_rule *rule,
164 				     struct ch_filter_specification *fs)
165 {
166 	u16 addr_type = 0;
167 
168 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
169 		struct flow_match_control match;
170 
171 		flow_rule_match_control(rule, &match);
172 		addr_type = match.key->addr_type;
173 	} else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
174 		addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
175 	} else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
176 		addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
177 	}
178 
179 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
180 		struct flow_match_basic match;
181 		u16 ethtype_key, ethtype_mask;
182 
183 		flow_rule_match_basic(rule, &match);
184 		ethtype_key = ntohs(match.key->n_proto);
185 		ethtype_mask = ntohs(match.mask->n_proto);
186 
187 		if (ethtype_key == ETH_P_ALL) {
188 			ethtype_key = 0;
189 			ethtype_mask = 0;
190 		}
191 
192 		if (ethtype_key == ETH_P_IPV6)
193 			fs->type = 1;
194 
195 		fs->val.ethtype = ethtype_key;
196 		fs->mask.ethtype = ethtype_mask;
197 		fs->val.proto = match.key->ip_proto;
198 		fs->mask.proto = match.mask->ip_proto;
199 	}
200 
201 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
202 		struct flow_match_ipv4_addrs match;
203 
204 		flow_rule_match_ipv4_addrs(rule, &match);
205 		fs->type = 0;
206 		memcpy(&fs->val.lip[0], &match.key->dst, sizeof(match.key->dst));
207 		memcpy(&fs->val.fip[0], &match.key->src, sizeof(match.key->src));
208 		memcpy(&fs->mask.lip[0], &match.mask->dst, sizeof(match.mask->dst));
209 		memcpy(&fs->mask.fip[0], &match.mask->src, sizeof(match.mask->src));
210 
211 		/* also initialize nat_lip/fip to same values */
212 		memcpy(&fs->nat_lip[0], &match.key->dst, sizeof(match.key->dst));
213 		memcpy(&fs->nat_fip[0], &match.key->src, sizeof(match.key->src));
214 	}
215 
216 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
217 		struct flow_match_ipv6_addrs match;
218 
219 		flow_rule_match_ipv6_addrs(rule, &match);
220 		fs->type = 1;
221 		memcpy(&fs->val.lip[0], match.key->dst.s6_addr,
222 		       sizeof(match.key->dst));
223 		memcpy(&fs->val.fip[0], match.key->src.s6_addr,
224 		       sizeof(match.key->src));
225 		memcpy(&fs->mask.lip[0], match.mask->dst.s6_addr,
226 		       sizeof(match.mask->dst));
227 		memcpy(&fs->mask.fip[0], match.mask->src.s6_addr,
228 		       sizeof(match.mask->src));
229 
230 		/* also initialize nat_lip/fip to same values */
231 		memcpy(&fs->nat_lip[0], match.key->dst.s6_addr,
232 		       sizeof(match.key->dst));
233 		memcpy(&fs->nat_fip[0], match.key->src.s6_addr,
234 		       sizeof(match.key->src));
235 	}
236 
237 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
238 		struct flow_match_ports match;
239 
240 		flow_rule_match_ports(rule, &match);
241 		fs->val.lport = be16_to_cpu(match.key->dst);
242 		fs->mask.lport = be16_to_cpu(match.mask->dst);
243 		fs->val.fport = be16_to_cpu(match.key->src);
244 		fs->mask.fport = be16_to_cpu(match.mask->src);
245 
246 		/* also initialize nat_lport/fport to same values */
247 		fs->nat_lport = fs->val.lport;
248 		fs->nat_fport = fs->val.fport;
249 	}
250 
251 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
252 		struct flow_match_ip match;
253 
254 		flow_rule_match_ip(rule, &match);
255 		fs->val.tos = match.key->tos;
256 		fs->mask.tos = match.mask->tos;
257 	}
258 
259 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
260 		struct flow_match_enc_keyid match;
261 
262 		flow_rule_match_enc_keyid(rule, &match);
263 		fs->val.vni = be32_to_cpu(match.key->keyid);
264 		fs->mask.vni = be32_to_cpu(match.mask->keyid);
265 		if (fs->mask.vni) {
266 			fs->val.encap_vld = 1;
267 			fs->mask.encap_vld = 1;
268 		}
269 	}
270 
271 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
272 		struct flow_match_vlan match;
273 		u16 vlan_tci, vlan_tci_mask;
274 
275 		flow_rule_match_vlan(rule, &match);
276 		vlan_tci = match.key->vlan_id | (match.key->vlan_priority <<
277 					       VLAN_PRIO_SHIFT);
278 		vlan_tci_mask = match.mask->vlan_id | (match.mask->vlan_priority <<
279 						     VLAN_PRIO_SHIFT);
280 		fs->val.ivlan = vlan_tci;
281 		fs->mask.ivlan = vlan_tci_mask;
282 
283 		fs->val.ivlan_vld = 1;
284 		fs->mask.ivlan_vld = 1;
285 
286 		/* Chelsio adapters use ivlan_vld bit to match vlan packets
287 		 * as 802.1Q. Also, when vlan tag is present in packets,
288 		 * ethtype match is used then to match on ethtype of inner
289 		 * header ie. the header following the vlan header.
290 		 * So, set the ivlan_vld based on ethtype info supplied by
291 		 * TC for vlan packets if its 802.1Q. And then reset the
292 		 * ethtype value else, hw will try to match the supplied
293 		 * ethtype value with ethtype of inner header.
294 		 */
295 		if (fs->val.ethtype == ETH_P_8021Q) {
296 			fs->val.ethtype = 0;
297 			fs->mask.ethtype = 0;
298 		}
299 	}
300 
301 	/* Match only packets coming from the ingress port where this
302 	 * filter will be created.
303 	 */
304 	fs->val.iport = netdev2pinfo(dev)->port_id;
305 	fs->mask.iport = ~0;
306 }
307 
308 static int cxgb4_validate_flow_match(struct netlink_ext_ack *extack,
309 				     struct flow_rule *rule)
310 {
311 	struct flow_dissector *dissector = rule->match.dissector;
312 	u16 ethtype_mask = 0;
313 	u16 ethtype_key = 0;
314 
315 	if (dissector->used_keys &
316 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
317 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
318 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
319 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
320 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
321 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
322 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
323 	      BIT_ULL(FLOW_DISSECTOR_KEY_IP))) {
324 		NL_SET_ERR_MSG_FMT_MOD(extack,
325 				       "Unsupported key used: 0x%llx",
326 				       dissector->used_keys);
327 		return -EOPNOTSUPP;
328 	}
329 
330 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
331 		struct flow_match_basic match;
332 
333 		flow_rule_match_basic(rule, &match);
334 		ethtype_key = ntohs(match.key->n_proto);
335 		ethtype_mask = ntohs(match.mask->n_proto);
336 	}
337 
338 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
339 		u16 eth_ip_type = ethtype_key & ethtype_mask;
340 		struct flow_match_ip match;
341 
342 		if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
343 			NL_SET_ERR_MSG_MOD(extack,
344 					   "IP Key supported only with IPv4/v6");
345 			return -EINVAL;
346 		}
347 
348 		flow_rule_match_ip(rule, &match);
349 		if (match.mask->ttl) {
350 			NL_SET_ERR_MSG_MOD(extack,
351 					   "ttl match unsupported for offload");
352 			return -EOPNOTSUPP;
353 		}
354 	}
355 
356 	return 0;
357 }
358 
359 static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
360 			  u8 field)
361 {
362 	u32 set_val = val & ~mask;
363 	u32 offset = 0;
364 	u8 size = 1;
365 	int i;
366 
367 	for (i = 0; i < ARRAY_SIZE(pedits); i++) {
368 		if (pedits[i].field == field) {
369 			offset = pedits[i].offset;
370 			size = pedits[i].size;
371 			break;
372 		}
373 	}
374 	memcpy((u8 *)fs + offset, &set_val, size);
375 }
376 
377 static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
378 				u32 mask, u32 offset, u8 htype,
379 				u8 *natmode_flags)
380 {
381 	switch (htype) {
382 	case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
383 		switch (offset) {
384 		case PEDIT_ETH_DMAC_31_0:
385 			fs->newdmac = 1;
386 			offload_pedit(fs, val, mask, ETH_DMAC_31_0);
387 			break;
388 		case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
389 			if (~mask & PEDIT_ETH_DMAC_MASK)
390 				offload_pedit(fs, val, mask, ETH_DMAC_47_32);
391 			else
392 				offload_pedit(fs, val >> 16, mask >> 16,
393 					      ETH_SMAC_15_0);
394 			break;
395 		case PEDIT_ETH_SMAC_47_16:
396 			fs->newsmac = 1;
397 			offload_pedit(fs, val, mask, ETH_SMAC_47_16);
398 		}
399 		break;
400 	case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
401 		switch (offset) {
402 		case PEDIT_IP4_SRC:
403 			offload_pedit(fs, val, mask, IP4_SRC);
404 			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
405 			break;
406 		case PEDIT_IP4_DST:
407 			offload_pedit(fs, val, mask, IP4_DST);
408 			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
409 		}
410 		break;
411 	case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
412 		switch (offset) {
413 		case PEDIT_IP6_SRC_31_0:
414 			offload_pedit(fs, val, mask, IP6_SRC_31_0);
415 			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
416 			break;
417 		case PEDIT_IP6_SRC_63_32:
418 			offload_pedit(fs, val, mask, IP6_SRC_63_32);
419 			*natmode_flags |=  CXGB4_ACTION_NATMODE_SIP;
420 			break;
421 		case PEDIT_IP6_SRC_95_64:
422 			offload_pedit(fs, val, mask, IP6_SRC_95_64);
423 			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
424 			break;
425 		case PEDIT_IP6_SRC_127_96:
426 			offload_pedit(fs, val, mask, IP6_SRC_127_96);
427 			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
428 			break;
429 		case PEDIT_IP6_DST_31_0:
430 			offload_pedit(fs, val, mask, IP6_DST_31_0);
431 			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
432 			break;
433 		case PEDIT_IP6_DST_63_32:
434 			offload_pedit(fs, val, mask, IP6_DST_63_32);
435 			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
436 			break;
437 		case PEDIT_IP6_DST_95_64:
438 			offload_pedit(fs, val, mask, IP6_DST_95_64);
439 			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
440 			break;
441 		case PEDIT_IP6_DST_127_96:
442 			offload_pedit(fs, val, mask, IP6_DST_127_96);
443 			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
444 		}
445 		break;
446 	case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
447 		switch (offset) {
448 		case PEDIT_TCP_SPORT_DPORT:
449 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK) {
450 				fs->nat_fport = val;
451 				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
452 			} else {
453 				fs->nat_lport = val >> 16;
454 				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
455 			}
456 		}
457 		break;
458 	case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
459 		switch (offset) {
460 		case PEDIT_UDP_SPORT_DPORT:
461 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK) {
462 				fs->nat_fport = val;
463 				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
464 			} else {
465 				fs->nat_lport = val >> 16;
466 				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
467 			}
468 		}
469 		break;
470 	}
471 }
472 
473 static int cxgb4_action_natmode_validate(struct adapter *adap, u8 natmode_flags,
474 					 struct netlink_ext_ack *extack)
475 {
476 	u8 i = 0;
477 
478 	/* Extract the NAT mode to enable based on what 4-tuple fields
479 	 * are enabled to be overwritten. This ensures that the
480 	 * disabled fields don't get overwritten to 0.
481 	 */
482 	for (i = 0; i < ARRAY_SIZE(cxgb4_natmode_config_array); i++) {
483 		const struct cxgb4_natmode_config *c;
484 
485 		c = &cxgb4_natmode_config_array[i];
486 		if (CHELSIO_CHIP_VERSION(adap->params.chip) >= c->chip &&
487 		    natmode_flags == c->flags)
488 			return 0;
489 	}
490 	NL_SET_ERR_MSG_MOD(extack, "Unsupported NAT mode 4-tuple combination");
491 	return -EOPNOTSUPP;
492 }
493 
494 void cxgb4_process_flow_actions(struct net_device *in,
495 				struct flow_action *actions,
496 				struct ch_filter_specification *fs)
497 {
498 	struct flow_action_entry *act;
499 	u8 natmode_flags = 0;
500 	int i;
501 
502 	flow_action_for_each(i, act, actions) {
503 		switch (act->id) {
504 		case FLOW_ACTION_ACCEPT:
505 			fs->action = FILTER_PASS;
506 			break;
507 		case FLOW_ACTION_DROP:
508 			fs->action = FILTER_DROP;
509 			break;
510 		case FLOW_ACTION_MIRRED:
511 		case FLOW_ACTION_REDIRECT: {
512 			struct net_device *out = act->dev;
513 			struct port_info *pi = netdev_priv(out);
514 
515 			fs->action = FILTER_SWITCH;
516 			fs->eport = pi->port_id;
517 			}
518 			break;
519 		case FLOW_ACTION_VLAN_POP:
520 		case FLOW_ACTION_VLAN_PUSH:
521 		case FLOW_ACTION_VLAN_MANGLE: {
522 			u8 prio = act->vlan.prio;
523 			u16 vid = act->vlan.vid;
524 			u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
525 			switch (act->id) {
526 			case FLOW_ACTION_VLAN_POP:
527 				fs->newvlan |= VLAN_REMOVE;
528 				break;
529 			case FLOW_ACTION_VLAN_PUSH:
530 				fs->newvlan |= VLAN_INSERT;
531 				fs->vlan = vlan_tci;
532 				break;
533 			case FLOW_ACTION_VLAN_MANGLE:
534 				fs->newvlan |= VLAN_REWRITE;
535 				fs->vlan = vlan_tci;
536 				break;
537 			default:
538 				break;
539 			}
540 			}
541 			break;
542 		case FLOW_ACTION_MANGLE: {
543 			u32 mask, val, offset;
544 			u8 htype;
545 
546 			htype = act->mangle.htype;
547 			mask = act->mangle.mask;
548 			val = act->mangle.val;
549 			offset = act->mangle.offset;
550 
551 			process_pedit_field(fs, val, mask, offset, htype,
552 					    &natmode_flags);
553 			}
554 			break;
555 		case FLOW_ACTION_QUEUE:
556 			fs->action = FILTER_PASS;
557 			fs->dirsteer = 1;
558 			fs->iq = act->queue.index;
559 			break;
560 		default:
561 			break;
562 		}
563 	}
564 	if (natmode_flags)
565 		cxgb4_action_natmode_tweak(fs, natmode_flags);
566 
567 }
568 
569 static bool valid_l4_mask(u32 mask)
570 {
571 	u16 hi, lo;
572 
573 	/* Either the upper 16-bits (SPORT) OR the lower
574 	 * 16-bits (DPORT) can be set, but NOT BOTH.
575 	 */
576 	hi = (mask >> 16) & 0xFFFF;
577 	lo = mask & 0xFFFF;
578 
579 	return hi && lo ? false : true;
580 }
581 
582 static bool valid_pedit_action(struct netlink_ext_ack *extack,
583 			       const struct flow_action_entry *act,
584 			       u8 *natmode_flags)
585 {
586 	u32 mask, offset;
587 	u8 htype;
588 
589 	htype = act->mangle.htype;
590 	mask = act->mangle.mask;
591 	offset = act->mangle.offset;
592 
593 	switch (htype) {
594 	case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
595 		switch (offset) {
596 		case PEDIT_ETH_DMAC_31_0:
597 		case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
598 		case PEDIT_ETH_SMAC_47_16:
599 			break;
600 		default:
601 			NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit field");
602 			return false;
603 		}
604 		break;
605 	case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
606 		switch (offset) {
607 		case PEDIT_IP4_SRC:
608 			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
609 			break;
610 		case PEDIT_IP4_DST:
611 			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
612 			break;
613 		default:
614 			NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit field");
615 			return false;
616 		}
617 		break;
618 	case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
619 		switch (offset) {
620 		case PEDIT_IP6_SRC_31_0:
621 		case PEDIT_IP6_SRC_63_32:
622 		case PEDIT_IP6_SRC_95_64:
623 		case PEDIT_IP6_SRC_127_96:
624 			*natmode_flags |= CXGB4_ACTION_NATMODE_SIP;
625 			break;
626 		case PEDIT_IP6_DST_31_0:
627 		case PEDIT_IP6_DST_63_32:
628 		case PEDIT_IP6_DST_95_64:
629 		case PEDIT_IP6_DST_127_96:
630 			*natmode_flags |= CXGB4_ACTION_NATMODE_DIP;
631 			break;
632 		default:
633 			NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit field");
634 			return false;
635 		}
636 		break;
637 	case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
638 		switch (offset) {
639 		case PEDIT_TCP_SPORT_DPORT:
640 			if (!valid_l4_mask(~mask)) {
641 				NL_SET_ERR_MSG_MOD(extack,
642 						   "Unsupported mask for TCP L4 ports");
643 				return false;
644 			}
645 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
646 				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
647 			else
648 				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
649 			break;
650 		default:
651 			NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit field");
652 			return false;
653 		}
654 		break;
655 	case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
656 		switch (offset) {
657 		case PEDIT_UDP_SPORT_DPORT:
658 			if (!valid_l4_mask(~mask)) {
659 				NL_SET_ERR_MSG_MOD(extack,
660 						   "Unsupported mask for UDP L4 ports");
661 				return false;
662 			}
663 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
664 				*natmode_flags |= CXGB4_ACTION_NATMODE_SPORT;
665 			else
666 				*natmode_flags |= CXGB4_ACTION_NATMODE_DPORT;
667 			break;
668 		default:
669 			NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit field");
670 			return false;
671 		}
672 		break;
673 	default:
674 		NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit type");
675 		return false;
676 	}
677 	return true;
678 }
679 
680 int cxgb4_validate_flow_actions(struct net_device *dev,
681 				struct flow_action *actions,
682 				struct netlink_ext_ack *extack,
683 				u8 matchall_filter)
684 {
685 	struct adapter *adap = netdev2adap(dev);
686 	struct flow_action_entry *act;
687 	bool act_redir = false;
688 	bool act_pedit = false;
689 	bool act_vlan = false;
690 	u8 natmode_flags = 0;
691 	int i;
692 
693 	if (!flow_action_basic_hw_stats_check(actions, extack))
694 		return -EOPNOTSUPP;
695 
696 	flow_action_for_each(i, act, actions) {
697 		switch (act->id) {
698 		case FLOW_ACTION_ACCEPT:
699 		case FLOW_ACTION_DROP:
700 			/* Do nothing */
701 			break;
702 		case FLOW_ACTION_MIRRED:
703 		case FLOW_ACTION_REDIRECT: {
704 			struct net_device *n_dev, *target_dev;
705 			bool found = false;
706 			unsigned int i;
707 
708 			if (act->id == FLOW_ACTION_MIRRED &&
709 			    !matchall_filter) {
710 				NL_SET_ERR_MSG_MOD(extack,
711 						   "Egress mirror action is only supported for tc-matchall");
712 				return -EOPNOTSUPP;
713 			}
714 
715 			target_dev = act->dev;
716 			for_each_port(adap, i) {
717 				n_dev = adap->port[i];
718 				if (target_dev == n_dev) {
719 					found = true;
720 					break;
721 				}
722 			}
723 
724 			/* If interface doesn't belong to our hw, then
725 			 * the provided output port is not valid
726 			 */
727 			if (!found) {
728 				NL_SET_ERR_MSG_MOD(extack, "Out port invalid");
729 				return -EINVAL;
730 			}
731 			act_redir = true;
732 			}
733 			break;
734 		case FLOW_ACTION_VLAN_POP:
735 		case FLOW_ACTION_VLAN_PUSH:
736 		case FLOW_ACTION_VLAN_MANGLE: {
737 			u16 proto = be16_to_cpu(act->vlan.proto);
738 
739 			switch (act->id) {
740 			case FLOW_ACTION_VLAN_POP:
741 				break;
742 			case FLOW_ACTION_VLAN_PUSH:
743 			case FLOW_ACTION_VLAN_MANGLE:
744 				if (proto != ETH_P_8021Q) {
745 					NL_SET_ERR_MSG_MOD(extack,
746 							   "Unsupported vlan proto");
747 					return -EOPNOTSUPP;
748 				}
749 				break;
750 			default:
751 				NL_SET_ERR_MSG_MOD(extack,
752 						   "Unsupported vlan action");
753 				return -EOPNOTSUPP;
754 			}
755 			act_vlan = true;
756 			}
757 			break;
758 		case FLOW_ACTION_MANGLE: {
759 			bool pedit_valid = valid_pedit_action(extack, act,
760 							      &natmode_flags);
761 
762 			if (!pedit_valid)
763 				return -EOPNOTSUPP;
764 			act_pedit = true;
765 			}
766 			break;
767 		case FLOW_ACTION_QUEUE:
768 			/* Do nothing. cxgb4_set_filter will validate */
769 			break;
770 		default:
771 			NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
772 			return -EOPNOTSUPP;
773 		}
774 	}
775 
776 	if ((act_pedit || act_vlan) && !act_redir) {
777 		NL_SET_ERR_MSG_MOD(extack,
778 				   "pedit/vlan rewrite invalid without egress redirect");
779 		return -EINVAL;
780 	}
781 
782 	if (act_pedit) {
783 		int ret;
784 
785 		ret = cxgb4_action_natmode_validate(adap, natmode_flags,
786 						    extack);
787 		if (ret)
788 			return ret;
789 	}
790 
791 	return 0;
792 }
793 
794 static void cxgb4_tc_flower_hash_prio_add(struct adapter *adap, u32 tc_prio)
795 {
796 	spin_lock_bh(&adap->tids.ftid_lock);
797 	if (adap->tids.tc_hash_tids_max_prio < tc_prio)
798 		adap->tids.tc_hash_tids_max_prio = tc_prio;
799 	spin_unlock_bh(&adap->tids.ftid_lock);
800 }
801 
802 static void cxgb4_tc_flower_hash_prio_del(struct adapter *adap, u32 tc_prio)
803 {
804 	struct tid_info *t = &adap->tids;
805 	struct ch_tc_flower_entry *fe;
806 	struct rhashtable_iter iter;
807 	u32 found = 0;
808 
809 	spin_lock_bh(&t->ftid_lock);
810 	/* Bail if the current rule is not the one with the max
811 	 * prio.
812 	 */
813 	if (t->tc_hash_tids_max_prio != tc_prio)
814 		goto out_unlock;
815 
816 	/* Search for the next rule having the same or next lower
817 	 * max prio.
818 	 */
819 	rhashtable_walk_enter(&adap->flower_tbl, &iter);
820 	do {
821 		rhashtable_walk_start(&iter);
822 
823 		fe = rhashtable_walk_next(&iter);
824 		while (!IS_ERR_OR_NULL(fe)) {
825 			if (fe->fs.hash &&
826 			    fe->fs.tc_prio <= t->tc_hash_tids_max_prio) {
827 				t->tc_hash_tids_max_prio = fe->fs.tc_prio;
828 				found++;
829 
830 				/* Bail if we found another rule
831 				 * having the same prio as the
832 				 * current max one.
833 				 */
834 				if (fe->fs.tc_prio == tc_prio)
835 					break;
836 			}
837 
838 			fe = rhashtable_walk_next(&iter);
839 		}
840 
841 		rhashtable_walk_stop(&iter);
842 	} while (fe == ERR_PTR(-EAGAIN));
843 	rhashtable_walk_exit(&iter);
844 
845 	if (!found)
846 		t->tc_hash_tids_max_prio = 0;
847 
848 out_unlock:
849 	spin_unlock_bh(&t->ftid_lock);
850 }
851 
852 int cxgb4_flow_rule_replace(struct net_device *dev, struct flow_rule *rule,
853 			    u32 tc_prio, struct netlink_ext_ack *extack,
854 			    struct ch_filter_specification *fs, u32 *tid)
855 {
856 	struct adapter *adap = netdev2adap(dev);
857 	struct filter_ctx ctx;
858 	u8 inet_family;
859 	int fidx, ret;
860 
861 	if (cxgb4_validate_flow_actions(dev, &rule->action, extack, 0))
862 		return -EOPNOTSUPP;
863 
864 	if (cxgb4_validate_flow_match(extack, rule))
865 		return -EOPNOTSUPP;
866 
867 	cxgb4_process_flow_match(dev, rule, fs);
868 	cxgb4_process_flow_actions(dev, &rule->action, fs);
869 
870 	fs->hash = is_filter_exact_match(adap, fs);
871 	inet_family = fs->type ? PF_INET6 : PF_INET;
872 
873 	/* Get a free filter entry TID, where we can insert this new
874 	 * rule. Only insert rule if its prio doesn't conflict with
875 	 * existing rules.
876 	 */
877 	fidx = cxgb4_get_free_ftid(dev, inet_family, fs->hash,
878 				   tc_prio);
879 	if (fidx < 0) {
880 		NL_SET_ERR_MSG_MOD(extack,
881 				   "No free LETCAM index available");
882 		return -ENOMEM;
883 	}
884 
885 	if (fidx < adap->tids.nhpftids) {
886 		fs->prio = 1;
887 		fs->hash = 0;
888 	}
889 
890 	/* If the rule can be inserted into HASH region, then ignore
891 	 * the index to normal FILTER region.
892 	 */
893 	if (fs->hash)
894 		fidx = 0;
895 
896 	fs->tc_prio = tc_prio;
897 
898 	init_completion(&ctx.completion);
899 	ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
900 	if (ret) {
901 		NL_SET_ERR_MSG_FMT_MOD(extack, "filter creation err %d", ret);
902 		return ret;
903 	}
904 
905 	/* Wait for reply */
906 	ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
907 	if (!ret)
908 		return -ETIMEDOUT;
909 
910 	/* Check if hw returned error for filter creation */
911 	if (ctx.result)
912 		return ctx.result;
913 
914 	*tid = ctx.tid;
915 
916 	if (fs->hash)
917 		cxgb4_tc_flower_hash_prio_add(adap, tc_prio);
918 
919 	return 0;
920 }
921 
922 int cxgb4_tc_flower_replace(struct net_device *dev,
923 			    struct flow_cls_offload *cls)
924 {
925 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
926 	struct netlink_ext_ack *extack = cls->common.extack;
927 	struct adapter *adap = netdev2adap(dev);
928 	struct ch_tc_flower_entry *ch_flower;
929 	struct ch_filter_specification *fs;
930 	int ret;
931 
932 	ch_flower = allocate_flower_entry();
933 	if (!ch_flower) {
934 		netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
935 		return -ENOMEM;
936 	}
937 
938 	fs = &ch_flower->fs;
939 	fs->hitcnts = 1;
940 	fs->tc_cookie = cls->cookie;
941 
942 	ret = cxgb4_flow_rule_replace(dev, rule, cls->common.prio, extack, fs,
943 				      &ch_flower->filter_id);
944 	if (ret)
945 		goto free_entry;
946 
947 	ch_flower->tc_flower_cookie = cls->cookie;
948 	ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
949 				     adap->flower_ht_params);
950 	if (ret)
951 		goto del_filter;
952 
953 	return 0;
954 
955 del_filter:
956 	if (fs->hash)
957 		cxgb4_tc_flower_hash_prio_del(adap, cls->common.prio);
958 
959 	cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
960 
961 free_entry:
962 	kfree(ch_flower);
963 	return ret;
964 }
965 
966 int cxgb4_flow_rule_destroy(struct net_device *dev, u32 tc_prio,
967 			    struct ch_filter_specification *fs, int tid)
968 {
969 	struct adapter *adap = netdev2adap(dev);
970 	u8 hash;
971 	int ret;
972 
973 	hash = fs->hash;
974 
975 	ret = cxgb4_del_filter(dev, tid, fs);
976 	if (ret)
977 		return ret;
978 
979 	if (hash)
980 		cxgb4_tc_flower_hash_prio_del(adap, tc_prio);
981 
982 	return ret;
983 }
984 
985 int cxgb4_tc_flower_destroy(struct net_device *dev,
986 			    struct flow_cls_offload *cls)
987 {
988 	struct adapter *adap = netdev2adap(dev);
989 	struct ch_tc_flower_entry *ch_flower;
990 	int ret;
991 
992 	ch_flower = ch_flower_lookup(adap, cls->cookie);
993 	if (!ch_flower)
994 		return -ENOENT;
995 
996 	rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
997 			       adap->flower_ht_params);
998 
999 	ret = cxgb4_flow_rule_destroy(dev, ch_flower->fs.tc_prio,
1000 				      &ch_flower->fs, ch_flower->filter_id);
1001 	if (ret)
1002 		netdev_err(dev, "Flow rule destroy failed for tid: %u, ret: %d",
1003 			   ch_flower->filter_id, ret);
1004 
1005 	kfree_rcu(ch_flower, rcu);
1006 	return ret;
1007 }
1008 
1009 static void ch_flower_stats_handler(struct work_struct *work)
1010 {
1011 	struct adapter *adap = container_of(work, struct adapter,
1012 					    flower_stats_work);
1013 	struct ch_tc_flower_entry *flower_entry;
1014 	struct ch_tc_flower_stats *ofld_stats;
1015 	struct rhashtable_iter iter;
1016 	u64 packets;
1017 	u64 bytes;
1018 	int ret;
1019 
1020 	rhashtable_walk_enter(&adap->flower_tbl, &iter);
1021 	do {
1022 		rhashtable_walk_start(&iter);
1023 
1024 		while ((flower_entry = rhashtable_walk_next(&iter)) &&
1025 		       !IS_ERR(flower_entry)) {
1026 			ret = cxgb4_get_filter_counters(adap->port[0],
1027 							flower_entry->filter_id,
1028 							&packets, &bytes,
1029 							flower_entry->fs.hash);
1030 			if (!ret) {
1031 				spin_lock(&flower_entry->lock);
1032 				ofld_stats = &flower_entry->stats;
1033 
1034 				if (ofld_stats->prev_packet_count != packets) {
1035 					ofld_stats->prev_packet_count = packets;
1036 					ofld_stats->last_used = jiffies;
1037 				}
1038 				spin_unlock(&flower_entry->lock);
1039 			}
1040 		}
1041 
1042 		rhashtable_walk_stop(&iter);
1043 
1044 	} while (flower_entry == ERR_PTR(-EAGAIN));
1045 	rhashtable_walk_exit(&iter);
1046 	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
1047 }
1048 
1049 static void ch_flower_stats_cb(struct timer_list *t)
1050 {
1051 	struct adapter *adap = from_timer(adap, t, flower_stats_timer);
1052 
1053 	schedule_work(&adap->flower_stats_work);
1054 }
1055 
1056 int cxgb4_tc_flower_stats(struct net_device *dev,
1057 			  struct flow_cls_offload *cls)
1058 {
1059 	struct adapter *adap = netdev2adap(dev);
1060 	struct ch_tc_flower_stats *ofld_stats;
1061 	struct ch_tc_flower_entry *ch_flower;
1062 	u64 packets;
1063 	u64 bytes;
1064 	int ret;
1065 
1066 	ch_flower = ch_flower_lookup(adap, cls->cookie);
1067 	if (!ch_flower) {
1068 		ret = -ENOENT;
1069 		goto err;
1070 	}
1071 
1072 	ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
1073 					&packets, &bytes,
1074 					ch_flower->fs.hash);
1075 	if (ret < 0)
1076 		goto err;
1077 
1078 	spin_lock_bh(&ch_flower->lock);
1079 	ofld_stats = &ch_flower->stats;
1080 	if (ofld_stats->packet_count != packets) {
1081 		if (ofld_stats->prev_packet_count != packets)
1082 			ofld_stats->last_used = jiffies;
1083 		flow_stats_update(&cls->stats, bytes - ofld_stats->byte_count,
1084 				  packets - ofld_stats->packet_count, 0,
1085 				  ofld_stats->last_used,
1086 				  FLOW_ACTION_HW_STATS_IMMEDIATE);
1087 
1088 		ofld_stats->packet_count = packets;
1089 		ofld_stats->byte_count = bytes;
1090 		ofld_stats->prev_packet_count = packets;
1091 	}
1092 	spin_unlock_bh(&ch_flower->lock);
1093 	return 0;
1094 
1095 err:
1096 	return ret;
1097 }
1098 
1099 static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
1100 	.nelem_hint = 384,
1101 	.head_offset = offsetof(struct ch_tc_flower_entry, node),
1102 	.key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
1103 	.key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
1104 	.max_size = 524288,
1105 	.min_size = 512,
1106 	.automatic_shrinking = true
1107 };
1108 
1109 int cxgb4_init_tc_flower(struct adapter *adap)
1110 {
1111 	int ret;
1112 
1113 	if (adap->tc_flower_initialized)
1114 		return -EEXIST;
1115 
1116 	adap->flower_ht_params = cxgb4_tc_flower_ht_params;
1117 	ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
1118 	if (ret)
1119 		return ret;
1120 
1121 	INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
1122 	timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
1123 	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
1124 	adap->tc_flower_initialized = true;
1125 	return 0;
1126 }
1127 
1128 void cxgb4_cleanup_tc_flower(struct adapter *adap)
1129 {
1130 	if (!adap->tc_flower_initialized)
1131 		return;
1132 
1133 	if (adap->flower_stats_timer.function)
1134 		timer_shutdown_sync(&adap->flower_stats_timer);
1135 	cancel_work_sync(&adap->flower_stats_work);
1136 	rhashtable_destroy(&adap->flower_tbl);
1137 	adap->tc_flower_initialized = false;
1138 }
1139