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