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 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 	PEDIT_FIELDS(TCP_, SPORT, 2, nat_fport, 0),
62 	PEDIT_FIELDS(TCP_, DPORT, 2, nat_lport, 0),
63 	PEDIT_FIELDS(UDP_, SPORT, 2, nat_fport, 0),
64 	PEDIT_FIELDS(UDP_, DPORT, 2, nat_lport, 0),
65 };
66 
67 static struct ch_tc_flower_entry *allocate_flower_entry(void)
68 {
69 	struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
70 	spin_lock_init(&new->lock);
71 	return new;
72 }
73 
74 /* Must be called with either RTNL or rcu_read_lock */
75 static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
76 						   unsigned long flower_cookie)
77 {
78 	return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
79 				      adap->flower_ht_params);
80 }
81 
82 static void cxgb4_process_flow_match(struct net_device *dev,
83 				     struct tc_cls_flower_offload *cls,
84 				     struct ch_filter_specification *fs)
85 {
86 	u16 addr_type = 0;
87 
88 	if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
89 		struct flow_dissector_key_control *key =
90 			skb_flow_dissector_target(cls->dissector,
91 						  FLOW_DISSECTOR_KEY_CONTROL,
92 						  cls->key);
93 
94 		addr_type = key->addr_type;
95 	}
96 
97 	if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
98 		struct flow_dissector_key_basic *key =
99 			skb_flow_dissector_target(cls->dissector,
100 						  FLOW_DISSECTOR_KEY_BASIC,
101 						  cls->key);
102 		struct flow_dissector_key_basic *mask =
103 			skb_flow_dissector_target(cls->dissector,
104 						  FLOW_DISSECTOR_KEY_BASIC,
105 						  cls->mask);
106 		u16 ethtype_key = ntohs(key->n_proto);
107 		u16 ethtype_mask = ntohs(mask->n_proto);
108 
109 		if (ethtype_key == ETH_P_ALL) {
110 			ethtype_key = 0;
111 			ethtype_mask = 0;
112 		}
113 
114 		fs->val.ethtype = ethtype_key;
115 		fs->mask.ethtype = ethtype_mask;
116 		fs->val.proto = key->ip_proto;
117 		fs->mask.proto = mask->ip_proto;
118 	}
119 
120 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
121 		struct flow_dissector_key_ipv4_addrs *key =
122 			skb_flow_dissector_target(cls->dissector,
123 						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
124 						  cls->key);
125 		struct flow_dissector_key_ipv4_addrs *mask =
126 			skb_flow_dissector_target(cls->dissector,
127 						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
128 						  cls->mask);
129 		fs->type = 0;
130 		memcpy(&fs->val.lip[0], &key->dst, sizeof(key->dst));
131 		memcpy(&fs->val.fip[0], &key->src, sizeof(key->src));
132 		memcpy(&fs->mask.lip[0], &mask->dst, sizeof(mask->dst));
133 		memcpy(&fs->mask.fip[0], &mask->src, sizeof(mask->src));
134 
135 		/* also initialize nat_lip/fip to same values */
136 		memcpy(&fs->nat_lip[0], &key->dst, sizeof(key->dst));
137 		memcpy(&fs->nat_fip[0], &key->src, sizeof(key->src));
138 
139 	}
140 
141 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
142 		struct flow_dissector_key_ipv6_addrs *key =
143 			skb_flow_dissector_target(cls->dissector,
144 						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
145 						  cls->key);
146 		struct flow_dissector_key_ipv6_addrs *mask =
147 			skb_flow_dissector_target(cls->dissector,
148 						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
149 						  cls->mask);
150 
151 		fs->type = 1;
152 		memcpy(&fs->val.lip[0], key->dst.s6_addr, sizeof(key->dst));
153 		memcpy(&fs->val.fip[0], key->src.s6_addr, sizeof(key->src));
154 		memcpy(&fs->mask.lip[0], mask->dst.s6_addr, sizeof(mask->dst));
155 		memcpy(&fs->mask.fip[0], mask->src.s6_addr, sizeof(mask->src));
156 
157 		/* also initialize nat_lip/fip to same values */
158 		memcpy(&fs->nat_lip[0], key->dst.s6_addr, sizeof(key->dst));
159 		memcpy(&fs->nat_fip[0], key->src.s6_addr, sizeof(key->src));
160 	}
161 
162 	if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
163 		struct flow_dissector_key_ports *key, *mask;
164 
165 		key = skb_flow_dissector_target(cls->dissector,
166 						FLOW_DISSECTOR_KEY_PORTS,
167 						cls->key);
168 		mask = skb_flow_dissector_target(cls->dissector,
169 						 FLOW_DISSECTOR_KEY_PORTS,
170 						 cls->mask);
171 		fs->val.lport = cpu_to_be16(key->dst);
172 		fs->mask.lport = cpu_to_be16(mask->dst);
173 		fs->val.fport = cpu_to_be16(key->src);
174 		fs->mask.fport = cpu_to_be16(mask->src);
175 
176 		/* also initialize nat_lport/fport to same values */
177 		fs->nat_lport = cpu_to_be16(key->dst);
178 		fs->nat_fport = cpu_to_be16(key->src);
179 	}
180 
181 	if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
182 		struct flow_dissector_key_ip *key, *mask;
183 
184 		key = skb_flow_dissector_target(cls->dissector,
185 						FLOW_DISSECTOR_KEY_IP,
186 						cls->key);
187 		mask = skb_flow_dissector_target(cls->dissector,
188 						 FLOW_DISSECTOR_KEY_IP,
189 						 cls->mask);
190 		fs->val.tos = key->tos;
191 		fs->mask.tos = mask->tos;
192 	}
193 
194 	if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
195 		struct flow_dissector_key_vlan *key, *mask;
196 		u16 vlan_tci, vlan_tci_mask;
197 
198 		key = skb_flow_dissector_target(cls->dissector,
199 						FLOW_DISSECTOR_KEY_VLAN,
200 						cls->key);
201 		mask = skb_flow_dissector_target(cls->dissector,
202 						 FLOW_DISSECTOR_KEY_VLAN,
203 						 cls->mask);
204 		vlan_tci = key->vlan_id | (key->vlan_priority <<
205 					   VLAN_PRIO_SHIFT);
206 		vlan_tci_mask = mask->vlan_id | (mask->vlan_priority <<
207 						 VLAN_PRIO_SHIFT);
208 		fs->val.ivlan = cpu_to_be16(vlan_tci);
209 		fs->mask.ivlan = cpu_to_be16(vlan_tci_mask);
210 
211 		/* Chelsio adapters use ivlan_vld bit to match vlan packets
212 		 * as 802.1Q. Also, when vlan tag is present in packets,
213 		 * ethtype match is used then to match on ethtype of inner
214 		 * header ie. the header following the vlan header.
215 		 * So, set the ivlan_vld based on ethtype info supplied by
216 		 * TC for vlan packets if its 802.1Q. And then reset the
217 		 * ethtype value else, hw will try to match the supplied
218 		 * ethtype value with ethtype of inner header.
219 		 */
220 		if (fs->val.ethtype == ETH_P_8021Q) {
221 			fs->val.ivlan_vld = 1;
222 			fs->mask.ivlan_vld = 1;
223 			fs->val.ethtype = 0;
224 			fs->mask.ethtype = 0;
225 		}
226 	}
227 
228 	/* Match only packets coming from the ingress port where this
229 	 * filter will be created.
230 	 */
231 	fs->val.iport = netdev2pinfo(dev)->port_id;
232 	fs->mask.iport = ~0;
233 }
234 
235 static int cxgb4_validate_flow_match(struct net_device *dev,
236 				     struct tc_cls_flower_offload *cls)
237 {
238 	u16 ethtype_mask = 0;
239 	u16 ethtype_key = 0;
240 
241 	if (cls->dissector->used_keys &
242 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
243 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
244 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
245 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
246 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
247 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
248 	      BIT(FLOW_DISSECTOR_KEY_IP))) {
249 		netdev_warn(dev, "Unsupported key used: 0x%x\n",
250 			    cls->dissector->used_keys);
251 		return -EOPNOTSUPP;
252 	}
253 
254 	if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
255 		struct flow_dissector_key_basic *key =
256 			skb_flow_dissector_target(cls->dissector,
257 						  FLOW_DISSECTOR_KEY_BASIC,
258 						  cls->key);
259 		struct flow_dissector_key_basic *mask =
260 			skb_flow_dissector_target(cls->dissector,
261 						  FLOW_DISSECTOR_KEY_BASIC,
262 						  cls->mask);
263 		ethtype_key = ntohs(key->n_proto);
264 		ethtype_mask = ntohs(mask->n_proto);
265 	}
266 
267 	if (dissector_uses_key(cls->dissector, FLOW_DISSECTOR_KEY_IP)) {
268 		u16 eth_ip_type = ethtype_key & ethtype_mask;
269 		struct flow_dissector_key_ip *mask;
270 
271 		if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
272 			netdev_err(dev, "IP Key supported only with IPv4/v6");
273 			return -EINVAL;
274 		}
275 
276 		mask = skb_flow_dissector_target(cls->dissector,
277 						 FLOW_DISSECTOR_KEY_IP,
278 						 cls->mask);
279 		if (mask->ttl) {
280 			netdev_warn(dev, "ttl match unsupported for offload");
281 			return -EOPNOTSUPP;
282 		}
283 	}
284 
285 	return 0;
286 }
287 
288 static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
289 			  u8 field)
290 {
291 	u32 set_val = val & ~mask;
292 	u32 offset = 0;
293 	u8 size = 1;
294 	int i;
295 
296 	for (i = 0; i < ARRAY_SIZE(pedits); i++) {
297 		if (pedits[i].field == field) {
298 			offset = pedits[i].offset;
299 			size = pedits[i].size;
300 			break;
301 		}
302 	}
303 	memcpy((u8 *)fs + offset, &set_val, size);
304 }
305 
306 static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
307 				u32 mask, u32 offset, u8 htype)
308 {
309 	switch (htype) {
310 	case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
311 		switch (offset) {
312 		case PEDIT_ETH_DMAC_31_0:
313 			fs->newdmac = 1;
314 			offload_pedit(fs, val, mask, ETH_DMAC_31_0);
315 			break;
316 		case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
317 			if (~mask & PEDIT_ETH_DMAC_MASK)
318 				offload_pedit(fs, val, mask, ETH_DMAC_47_32);
319 			else
320 				offload_pedit(fs, val >> 16, mask >> 16,
321 					      ETH_SMAC_15_0);
322 			break;
323 		case PEDIT_ETH_SMAC_47_16:
324 			fs->newsmac = 1;
325 			offload_pedit(fs, val, mask, ETH_SMAC_47_16);
326 		}
327 		break;
328 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
329 		switch (offset) {
330 		case PEDIT_IP4_SRC:
331 			offload_pedit(fs, val, mask, IP4_SRC);
332 			break;
333 		case PEDIT_IP4_DST:
334 			offload_pedit(fs, val, mask, IP4_DST);
335 		}
336 		fs->nat_mode = NAT_MODE_ALL;
337 		break;
338 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
339 		switch (offset) {
340 		case PEDIT_IP6_SRC_31_0:
341 			offload_pedit(fs, val, mask, IP6_SRC_31_0);
342 			break;
343 		case PEDIT_IP6_SRC_63_32:
344 			offload_pedit(fs, val, mask, IP6_SRC_63_32);
345 			break;
346 		case PEDIT_IP6_SRC_95_64:
347 			offload_pedit(fs, val, mask, IP6_SRC_95_64);
348 			break;
349 		case PEDIT_IP6_SRC_127_96:
350 			offload_pedit(fs, val, mask, IP6_SRC_127_96);
351 			break;
352 		case PEDIT_IP6_DST_31_0:
353 			offload_pedit(fs, val, mask, IP6_DST_31_0);
354 			break;
355 		case PEDIT_IP6_DST_63_32:
356 			offload_pedit(fs, val, mask, IP6_DST_63_32);
357 			break;
358 		case PEDIT_IP6_DST_95_64:
359 			offload_pedit(fs, val, mask, IP6_DST_95_64);
360 			break;
361 		case PEDIT_IP6_DST_127_96:
362 			offload_pedit(fs, val, mask, IP6_DST_127_96);
363 		}
364 		fs->nat_mode = NAT_MODE_ALL;
365 		break;
366 	case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
367 		switch (offset) {
368 		case PEDIT_TCP_SPORT_DPORT:
369 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
370 				offload_pedit(fs, cpu_to_be32(val) >> 16,
371 					      cpu_to_be32(mask) >> 16,
372 					      TCP_SPORT);
373 			else
374 				offload_pedit(fs, cpu_to_be32(val),
375 					      cpu_to_be32(mask), TCP_DPORT);
376 		}
377 		fs->nat_mode = NAT_MODE_ALL;
378 		break;
379 	case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
380 		switch (offset) {
381 		case PEDIT_UDP_SPORT_DPORT:
382 			if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
383 				offload_pedit(fs, cpu_to_be32(val) >> 16,
384 					      cpu_to_be32(mask) >> 16,
385 					      UDP_SPORT);
386 			else
387 				offload_pedit(fs, cpu_to_be32(val),
388 					      cpu_to_be32(mask), UDP_DPORT);
389 		}
390 		fs->nat_mode = NAT_MODE_ALL;
391 	}
392 }
393 
394 static void cxgb4_process_flow_actions(struct net_device *in,
395 				       struct tc_cls_flower_offload *cls,
396 				       struct ch_filter_specification *fs)
397 {
398 	const struct tc_action *a;
399 	LIST_HEAD(actions);
400 
401 	tcf_exts_to_list(cls->exts, &actions);
402 	list_for_each_entry(a, &actions, list) {
403 		if (is_tcf_gact_ok(a)) {
404 			fs->action = FILTER_PASS;
405 		} else if (is_tcf_gact_shot(a)) {
406 			fs->action = FILTER_DROP;
407 		} else if (is_tcf_mirred_egress_redirect(a)) {
408 			int ifindex = tcf_mirred_ifindex(a);
409 			struct net_device *out = __dev_get_by_index(dev_net(in),
410 								    ifindex);
411 			struct port_info *pi = netdev_priv(out);
412 
413 			fs->action = FILTER_SWITCH;
414 			fs->eport = pi->port_id;
415 		} else if (is_tcf_vlan(a)) {
416 			u32 vlan_action = tcf_vlan_action(a);
417 			u8 prio = tcf_vlan_push_prio(a);
418 			u16 vid = tcf_vlan_push_vid(a);
419 			u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
420 
421 			switch (vlan_action) {
422 			case TCA_VLAN_ACT_POP:
423 				fs->newvlan |= VLAN_REMOVE;
424 				break;
425 			case TCA_VLAN_ACT_PUSH:
426 				fs->newvlan |= VLAN_INSERT;
427 				fs->vlan = vlan_tci;
428 				break;
429 			case TCA_VLAN_ACT_MODIFY:
430 				fs->newvlan |= VLAN_REWRITE;
431 				fs->vlan = vlan_tci;
432 				break;
433 			default:
434 				break;
435 			}
436 		} else if (is_tcf_pedit(a)) {
437 			u32 mask, val, offset;
438 			int nkeys, i;
439 			u8 htype;
440 
441 			nkeys = tcf_pedit_nkeys(a);
442 			for (i = 0; i < nkeys; i++) {
443 				htype = tcf_pedit_htype(a, i);
444 				mask = tcf_pedit_mask(a, i);
445 				val = tcf_pedit_val(a, i);
446 				offset = tcf_pedit_offset(a, i);
447 
448 				process_pedit_field(fs, val, mask, offset,
449 						    htype);
450 			}
451 		}
452 	}
453 }
454 
455 static bool valid_l4_mask(u32 mask)
456 {
457 	u16 hi, lo;
458 
459 	/* Either the upper 16-bits (SPORT) OR the lower
460 	 * 16-bits (DPORT) can be set, but NOT BOTH.
461 	 */
462 	hi = (mask >> 16) & 0xFFFF;
463 	lo = mask & 0xFFFF;
464 
465 	return hi && lo ? false : true;
466 }
467 
468 static bool valid_pedit_action(struct net_device *dev,
469 			       const struct tc_action *a)
470 {
471 	u32 mask, offset;
472 	u8 cmd, htype;
473 	int nkeys, i;
474 
475 	nkeys = tcf_pedit_nkeys(a);
476 	for (i = 0; i < nkeys; i++) {
477 		htype = tcf_pedit_htype(a, i);
478 		cmd = tcf_pedit_cmd(a, i);
479 		mask = tcf_pedit_mask(a, i);
480 		offset = tcf_pedit_offset(a, i);
481 
482 		if (cmd != TCA_PEDIT_KEY_EX_CMD_SET) {
483 			netdev_err(dev, "%s: Unsupported pedit cmd\n",
484 				   __func__);
485 			return false;
486 		}
487 
488 		switch (htype) {
489 		case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
490 			switch (offset) {
491 			case PEDIT_ETH_DMAC_31_0:
492 			case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
493 			case PEDIT_ETH_SMAC_47_16:
494 				break;
495 			default:
496 				netdev_err(dev, "%s: Unsupported pedit field\n",
497 					   __func__);
498 				return false;
499 			}
500 			break;
501 		case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
502 			switch (offset) {
503 			case PEDIT_IP4_SRC:
504 			case PEDIT_IP4_DST:
505 				break;
506 			default:
507 				netdev_err(dev, "%s: Unsupported pedit field\n",
508 					   __func__);
509 				return false;
510 			}
511 			break;
512 		case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
513 			switch (offset) {
514 			case PEDIT_IP6_SRC_31_0:
515 			case PEDIT_IP6_SRC_63_32:
516 			case PEDIT_IP6_SRC_95_64:
517 			case PEDIT_IP6_SRC_127_96:
518 			case PEDIT_IP6_DST_31_0:
519 			case PEDIT_IP6_DST_63_32:
520 			case PEDIT_IP6_DST_95_64:
521 			case PEDIT_IP6_DST_127_96:
522 				break;
523 			default:
524 				netdev_err(dev, "%s: Unsupported pedit field\n",
525 					   __func__);
526 				return false;
527 			}
528 			break;
529 		case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
530 			switch (offset) {
531 			case PEDIT_TCP_SPORT_DPORT:
532 				if (!valid_l4_mask(~mask)) {
533 					netdev_err(dev, "%s: Unsupported mask for TCP L4 ports\n",
534 						   __func__);
535 					return false;
536 				}
537 				break;
538 			default:
539 				netdev_err(dev, "%s: Unsupported pedit field\n",
540 					   __func__);
541 				return false;
542 			}
543 			break;
544 		case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
545 			switch (offset) {
546 			case PEDIT_UDP_SPORT_DPORT:
547 				if (!valid_l4_mask(~mask)) {
548 					netdev_err(dev, "%s: Unsupported mask for UDP L4 ports\n",
549 						   __func__);
550 					return false;
551 				}
552 				break;
553 			default:
554 				netdev_err(dev, "%s: Unsupported pedit field\n",
555 					   __func__);
556 				return false;
557 			}
558 			break;
559 		default:
560 			netdev_err(dev, "%s: Unsupported pedit type\n",
561 				   __func__);
562 			return false;
563 		}
564 	}
565 	return true;
566 }
567 
568 static int cxgb4_validate_flow_actions(struct net_device *dev,
569 				       struct tc_cls_flower_offload *cls)
570 {
571 	const struct tc_action *a;
572 	bool act_redir = false;
573 	bool act_pedit = false;
574 	bool act_vlan = false;
575 	LIST_HEAD(actions);
576 
577 	tcf_exts_to_list(cls->exts, &actions);
578 	list_for_each_entry(a, &actions, list) {
579 		if (is_tcf_gact_ok(a)) {
580 			/* Do nothing */
581 		} else if (is_tcf_gact_shot(a)) {
582 			/* Do nothing */
583 		} else if (is_tcf_mirred_egress_redirect(a)) {
584 			struct adapter *adap = netdev2adap(dev);
585 			struct net_device *n_dev;
586 			unsigned int i, ifindex;
587 			bool found = false;
588 
589 			ifindex = tcf_mirred_ifindex(a);
590 			for_each_port(adap, i) {
591 				n_dev = adap->port[i];
592 				if (ifindex == n_dev->ifindex) {
593 					found = true;
594 					break;
595 				}
596 			}
597 
598 			/* If interface doesn't belong to our hw, then
599 			 * the provided output port is not valid
600 			 */
601 			if (!found) {
602 				netdev_err(dev, "%s: Out port invalid\n",
603 					   __func__);
604 				return -EINVAL;
605 			}
606 			act_redir = true;
607 		} else if (is_tcf_vlan(a)) {
608 			u16 proto = be16_to_cpu(tcf_vlan_push_proto(a));
609 			u32 vlan_action = tcf_vlan_action(a);
610 
611 			switch (vlan_action) {
612 			case TCA_VLAN_ACT_POP:
613 				break;
614 			case TCA_VLAN_ACT_PUSH:
615 			case TCA_VLAN_ACT_MODIFY:
616 				if (proto != ETH_P_8021Q) {
617 					netdev_err(dev, "%s: Unsupported vlan proto\n",
618 						   __func__);
619 					return -EOPNOTSUPP;
620 				}
621 				break;
622 			default:
623 				netdev_err(dev, "%s: Unsupported vlan action\n",
624 					   __func__);
625 				return -EOPNOTSUPP;
626 			}
627 			act_vlan = true;
628 		} else if (is_tcf_pedit(a)) {
629 			bool pedit_valid = valid_pedit_action(dev, a);
630 
631 			if (!pedit_valid)
632 				return -EOPNOTSUPP;
633 			act_pedit = true;
634 		} else {
635 			netdev_err(dev, "%s: Unsupported action\n", __func__);
636 			return -EOPNOTSUPP;
637 		}
638 	}
639 
640 	if ((act_pedit || act_vlan) && !act_redir) {
641 		netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
642 			   __func__);
643 		return -EINVAL;
644 	}
645 
646 	return 0;
647 }
648 
649 int cxgb4_tc_flower_replace(struct net_device *dev,
650 			    struct tc_cls_flower_offload *cls)
651 {
652 	struct adapter *adap = netdev2adap(dev);
653 	struct ch_tc_flower_entry *ch_flower;
654 	struct ch_filter_specification *fs;
655 	struct filter_ctx ctx;
656 	int fidx;
657 	int ret;
658 
659 	if (cxgb4_validate_flow_actions(dev, cls))
660 		return -EOPNOTSUPP;
661 
662 	if (cxgb4_validate_flow_match(dev, cls))
663 		return -EOPNOTSUPP;
664 
665 	ch_flower = allocate_flower_entry();
666 	if (!ch_flower) {
667 		netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
668 		return -ENOMEM;
669 	}
670 
671 	fs = &ch_flower->fs;
672 	fs->hitcnts = 1;
673 	cxgb4_process_flow_match(dev, cls, fs);
674 	cxgb4_process_flow_actions(dev, cls, fs);
675 
676 	fs->hash = is_filter_exact_match(adap, fs);
677 	if (fs->hash) {
678 		fidx = 0;
679 	} else {
680 		fidx = cxgb4_get_free_ftid(dev, fs->type ? PF_INET6 : PF_INET);
681 		if (fidx < 0) {
682 			netdev_err(dev, "%s: No fidx for offload.\n", __func__);
683 			ret = -ENOMEM;
684 			goto free_entry;
685 		}
686 	}
687 
688 	init_completion(&ctx.completion);
689 	ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
690 	if (ret) {
691 		netdev_err(dev, "%s: filter creation err %d\n",
692 			   __func__, ret);
693 		goto free_entry;
694 	}
695 
696 	/* Wait for reply */
697 	ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
698 	if (!ret) {
699 		ret = -ETIMEDOUT;
700 		goto free_entry;
701 	}
702 
703 	ret = ctx.result;
704 	/* Check if hw returned error for filter creation */
705 	if (ret) {
706 		netdev_err(dev, "%s: filter creation err %d\n",
707 			   __func__, ret);
708 		goto free_entry;
709 	}
710 
711 	ch_flower->tc_flower_cookie = cls->cookie;
712 	ch_flower->filter_id = ctx.tid;
713 	ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
714 				     adap->flower_ht_params);
715 	if (ret)
716 		goto del_filter;
717 
718 	return 0;
719 
720 del_filter:
721 	cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
722 
723 free_entry:
724 	kfree(ch_flower);
725 	return ret;
726 }
727 
728 int cxgb4_tc_flower_destroy(struct net_device *dev,
729 			    struct tc_cls_flower_offload *cls)
730 {
731 	struct adapter *adap = netdev2adap(dev);
732 	struct ch_tc_flower_entry *ch_flower;
733 	int ret;
734 
735 	ch_flower = ch_flower_lookup(adap, cls->cookie);
736 	if (!ch_flower)
737 		return -ENOENT;
738 
739 	ret = cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
740 	if (ret)
741 		goto err;
742 
743 	ret = rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
744 				     adap->flower_ht_params);
745 	if (ret) {
746 		netdev_err(dev, "Flow remove from rhashtable failed");
747 		goto err;
748 	}
749 	kfree_rcu(ch_flower, rcu);
750 
751 err:
752 	return ret;
753 }
754 
755 static void ch_flower_stats_handler(struct work_struct *work)
756 {
757 	struct adapter *adap = container_of(work, struct adapter,
758 					    flower_stats_work);
759 	struct ch_tc_flower_entry *flower_entry;
760 	struct ch_tc_flower_stats *ofld_stats;
761 	struct rhashtable_iter iter;
762 	u64 packets;
763 	u64 bytes;
764 	int ret;
765 
766 	rhashtable_walk_enter(&adap->flower_tbl, &iter);
767 	do {
768 		flower_entry = ERR_PTR(rhashtable_walk_start(&iter));
769 		if (IS_ERR(flower_entry))
770 			goto walk_stop;
771 
772 		while ((flower_entry = rhashtable_walk_next(&iter)) &&
773 		       !IS_ERR(flower_entry)) {
774 			ret = cxgb4_get_filter_counters(adap->port[0],
775 							flower_entry->filter_id,
776 							&packets, &bytes,
777 							flower_entry->fs.hash);
778 			if (!ret) {
779 				spin_lock(&flower_entry->lock);
780 				ofld_stats = &flower_entry->stats;
781 
782 				if (ofld_stats->prev_packet_count != packets) {
783 					ofld_stats->prev_packet_count = packets;
784 					ofld_stats->last_used = jiffies;
785 				}
786 				spin_unlock(&flower_entry->lock);
787 			}
788 		}
789 walk_stop:
790 		rhashtable_walk_stop(&iter);
791 	} while (flower_entry == ERR_PTR(-EAGAIN));
792 	rhashtable_walk_exit(&iter);
793 	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
794 }
795 
796 static void ch_flower_stats_cb(struct timer_list *t)
797 {
798 	struct adapter *adap = from_timer(adap, t, flower_stats_timer);
799 
800 	schedule_work(&adap->flower_stats_work);
801 }
802 
803 int cxgb4_tc_flower_stats(struct net_device *dev,
804 			  struct tc_cls_flower_offload *cls)
805 {
806 	struct adapter *adap = netdev2adap(dev);
807 	struct ch_tc_flower_stats *ofld_stats;
808 	struct ch_tc_flower_entry *ch_flower;
809 	u64 packets;
810 	u64 bytes;
811 	int ret;
812 
813 	ch_flower = ch_flower_lookup(adap, cls->cookie);
814 	if (!ch_flower) {
815 		ret = -ENOENT;
816 		goto err;
817 	}
818 
819 	ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
820 					&packets, &bytes,
821 					ch_flower->fs.hash);
822 	if (ret < 0)
823 		goto err;
824 
825 	spin_lock_bh(&ch_flower->lock);
826 	ofld_stats = &ch_flower->stats;
827 	if (ofld_stats->packet_count != packets) {
828 		if (ofld_stats->prev_packet_count != packets)
829 			ofld_stats->last_used = jiffies;
830 		tcf_exts_stats_update(cls->exts, bytes - ofld_stats->byte_count,
831 				      packets - ofld_stats->packet_count,
832 				      ofld_stats->last_used);
833 
834 		ofld_stats->packet_count = packets;
835 		ofld_stats->byte_count = bytes;
836 		ofld_stats->prev_packet_count = packets;
837 	}
838 	spin_unlock_bh(&ch_flower->lock);
839 	return 0;
840 
841 err:
842 	return ret;
843 }
844 
845 static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
846 	.nelem_hint = 384,
847 	.head_offset = offsetof(struct ch_tc_flower_entry, node),
848 	.key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
849 	.key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
850 	.max_size = 524288,
851 	.min_size = 512,
852 	.automatic_shrinking = true
853 };
854 
855 int cxgb4_init_tc_flower(struct adapter *adap)
856 {
857 	int ret;
858 
859 	adap->flower_ht_params = cxgb4_tc_flower_ht_params;
860 	ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
861 	if (ret)
862 		return ret;
863 
864 	INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
865 	timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
866 	mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
867 	return 0;
868 }
869 
870 void cxgb4_cleanup_tc_flower(struct adapter *adap)
871 {
872 	if (adap->flower_stats_timer.function)
873 		del_timer_sync(&adap->flower_stats_timer);
874 	cancel_work_sync(&adap->flower_stats_work);
875 	rhashtable_destroy(&adap->flower_tbl);
876 }
877